SpringCloud的负载均衡组件用的是Ribbon,这个东西就是服务消费者。它只是一个配置用的中转器,放在 Zuul 和 Eureka Client 之间用来转发的,它里面写了好多规则,用来指定负载的规则。
首先,Zuul:
application.yml
eureka: client: serviceUrl: defaultZone: http://localhost:7087/eureka/server: port: 7089spring: application: name: service-zuulzuul: host: maxTotalConnections: 10000 maxPerRouteConnections: 60000 routes: api-a: path: /dbmeta/** serviceId: service-ribbon api-b: path: /kylin/** serviceId: service-kylinribbon: eureka: enabled: falseservice-ribbon: ribbon: listOfServers: http://localhost:7088,http://localhost:7091 ConnectTimeout: 10000 ReadTimeout: 30000 MaxTotalHttpConnections: 10000 MaxConnectionsPerHost: 60000
Application
package com.shinho;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.builder.SpringApplicationBuilder;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.cloud.netflix.zuul.EnableZuulProxy;@EnableZuulProxy@SpringBootApplicationpublic class DbzuulApplication { public static void main(String[] args) { SpringApplication.run(DbzuulApplication.class,args); }}
pom.xml
4.0.0 com.shinho dbzuul 0.0.1 jar dbzuul Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 2.0.0.RELEASE UTF-8 UTF-8 1.8 Finchley.M9 org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-netflix-eureka-server org.springframework.cloud spring-cloud-starter-netflix-zuul org.springframework.boot spring-boot-starter-test test org.springframework spring-expression 4.3.14.RELEASE org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin spring-snapshots Spring Snapshots https://repo.spring.io/snapshot true spring-milestones Spring Milestones https://repo.spring.io/milestone false spring-snapshots Spring Snapshots https://repo.spring.io/snapshot true spring-milestones Spring Milestones https://repo.spring.io/milestone false
然后,Ribbon客户端
application.yml
eureka: client: serviceUrl: defaultZone: http://localhost:7087/eurekaserver: port: 7090 spring: application: name: service-ribbon
Config类
package com.config;import org.springframework.cloud.netflix.ribbon.ZonePreferenceServerListFilter;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import com.netflix.client.config.IClientConfig;import com.netflix.loadbalancer.BestAvailableRule;import com.netflix.loadbalancer.IPing;import com.netflix.loadbalancer.IRule;import com.netflix.loadbalancer.PingUrl;import com.netflix.loadbalancer.Server;import com.netflix.loadbalancer.ServerList;import com.netflix.loadbalancer.ServerListSubsetFilter;import com.shinho.DbribbonApplication;@Configurationpublic class MyConfig { @Bean public IRule ribbonRule() { return new BestAvailableRule(); } @Bean public IPing ribbonPing() { return new PingUrl(); } @Bean public ServerListribbonServerList(IClientConfig config) { return new DbribbonApplication.BazServiceList(config); } @Bean public ServerListSubsetFilter serverListFilter() { ServerListSubsetFilter filter = new ServerListSubsetFilter(); return filter; }}
Application
package com.shinho;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.ribbon.RibbonClient;import com.config.MyConfig;import com.netflix.client.config.IClientConfig;import com.netflix.loadbalancer.ConfigurationBasedServerList;@SpringBootApplication@RibbonClient(name = "service-ribbon", configuration = MyConfig.class)public class DbribbonApplication { public static void main(String[] args) { SpringApplication.run(DbribbonApplication.class, args); } public static class BazServiceList extends ConfigurationBasedServerList { public BazServiceList(IClientConfig config) { super.initWithNiwsConfig(config); } }}
pom.xml
4.0.0 com.shinho dbribbon 0.0.1 jar dbribbon Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 2.0.0.RELEASE UTF-8 UTF-8 1.8 Finchley.M9 org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-netflix-eureka-server org.springframework.boot spring-boot-starter-test test org.springframework spring-expression 4.3.14.RELEASE org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin spring-snapshots Spring Snapshots https://repo.spring.io/snapshot true spring-milestones Spring Milestones https://repo.spring.io/milestone false spring-snapshots Spring Snapshots https://repo.spring.io/snapshot true spring-milestones Spring Milestones https://repo.spring.io/milestone false
客户端就和其它的一样了,注意客户端的Service-id不要和Ribbon的重复,Ribbon的Service-id是要单独的哦。