SpringCloud的负载均衡组件用的是Ribbon,这个东西就是服务消费者。它只是一个配置用的中转器,放在 Zuul 和 Eureka Client 之间用来转发的,它里面写了好多规则,用来指定负载的规则。
首先,Zuul:
application.yml
1eureka: 2 client: 3 serviceUrl: 4 defaultZone: http://localhost:7087/eureka/ 5server: 6 port: 7089 7spring: 8 application: 9 name: service-zuul 10zuul: 11 host: 12 maxTotalConnections: 10000 13 maxPerRouteConnections: 60000 14 routes: 15 api-a: 16 path: /dbmeta/** 17 serviceId: service-ribbon 18 api-b: 19 path: /kylin/** 20 serviceId: service-kylin 21 22ribbon: 23 eureka: 24 enabled: false 25 26service-ribbon: 27 ribbon: 28 listOfServers: http://localhost:7088,http://localhost:7091 29 ConnectTimeout: 10000 30 ReadTimeout: 30000 31 MaxTotalHttpConnections: 10000 32 MaxConnectionsPerHost: 60000
Application
1package com.shinho; 2 3 4import org.springframework.boot.SpringApplication; 5import org.springframework.boot.autoconfigure.SpringBootApplication; 6import org.springframework.boot.builder.SpringApplicationBuilder; 7import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 8import org.springframework.cloud.netflix.zuul.EnableZuulProxy; 9 10@EnableZuulProxy 11@SpringBootApplication 12public class DbzuulApplication { 13 14 public static void main(String[] args) { 15 SpringApplication.run(DbzuulApplication.class,args); 16 } 17}
pom.xml
1<?xml version="1.0" encoding="UTF-8"?> 2<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 6 <groupId>com.shinho</groupId> 7 <artifactId>dbzuul</artifactId> 8 <version>0.0.1</version> 9 <packaging>jar</packaging> 10 11 <name>dbzuul</name> 12 <description>Demo project for Spring Boot</description> 13 14 <parent> 15 <groupId>org.springframework.boot</groupId> 16 <artifactId>spring-boot-starter-parent</artifactId> 17 <version>2.0.0.RELEASE</version> 18 <relativePath/> <!-- lookup parent from repository --> 19 </parent> 20 21 <properties> 22 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 23 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 24 <java.version>1.8</java.version> 25 <spring-cloud.version>Finchley.M9</spring-cloud.version> 26 </properties> 27 28 <dependencies> 29 <dependency> 30 <groupId>org.springframework.boot</groupId> 31 <artifactId>spring-boot-starter-web</artifactId> 32 </dependency> 33 <dependency> 34 <groupId>org.springframework.cloud</groupId> 35 <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> 36 </dependency> 37 <dependency> 38 <groupId>org.springframework.cloud</groupId> 39 <artifactId>spring-cloud-starter-netflix-zuul</artifactId> 40 </dependency> 41 42 <dependency> 43 <groupId>org.springframework.boot</groupId> 44 <artifactId>spring-boot-starter-test</artifactId> 45 <scope>test</scope> 46 </dependency> 47 <dependency> 48 <groupId>org.springframework</groupId> 49 <artifactId>spring-expression</artifactId> 50 <version>4.3.14.RELEASE</version> 51 </dependency> 52 </dependencies> 53 54 <dependencyManagement> 55 <dependencies> 56 <dependency> 57 <groupId>org.springframework.cloud</groupId> 58 <artifactId>spring-cloud-dependencies</artifactId> 59 <version>${spring-cloud.version}</version> 60 <type>pom</type> 61 <scope>import</scope> 62 </dependency> 63 </dependencies> 64 </dependencyManagement> 65 66 <build> 67 <plugins> 68 <plugin> 69 <groupId>org.springframework.boot</groupId> 70 <artifactId>spring-boot-maven-plugin</artifactId> 71 </plugin> 72 </plugins> 73 </build> 74 75 <repositories> 76 <repository> 77 <id>spring-snapshots</id> 78 <name>Spring Snapshots</name> 79 <url>https://repo.spring.io/snapshot</url> 80 <snapshots> 81 <enabled>true</enabled> 82 </snapshots> 83 </repository> 84 <repository> 85 <id>spring-milestones</id> 86 <name>Spring Milestones</name> 87 <url>https://repo.spring.io/milestone</url> 88 <snapshots> 89 <enabled>false</enabled> 90 </snapshots> 91 </repository> 92 </repositories> 93 94 <pluginRepositories> 95 <pluginRepository> 96 <id>spring-snapshots</id> 97 <name>Spring Snapshots</name> 98 <url>https://repo.spring.io/snapshot</url> 99 <snapshots> 100 <enabled>true</enabled> 101 </snapshots> 102 </pluginRepository> 103 <pluginRepository> 104 <id>spring-milestones</id> 105 <name>Spring Milestones</name> 106 <url>https://repo.spring.io/milestone</url> 107 <snapshots> 108 <enabled>false</enabled> 109 </snapshots> 110 </pluginRepository> 111 </pluginRepositories> 112 113 114</project>
然后,Ribbon客户端
application.yml
1eureka: 2 client: 3 serviceUrl: 4 defaultZone: http://localhost:7087/eureka 5server: 6 port: 7090 7 8spring: 9 application: 10 name: service-ribbon
Config类
1package com.config; 2 3import org.springframework.cloud.netflix.ribbon.ZonePreferenceServerListFilter; 4import org.springframework.context.annotation.Bean; 5import org.springframework.context.annotation.Configuration; 6 7import com.netflix.client.config.IClientConfig; 8import com.netflix.loadbalancer.BestAvailableRule; 9import com.netflix.loadbalancer.IPing; 10import com.netflix.loadbalancer.IRule; 11import com.netflix.loadbalancer.PingUrl; 12import com.netflix.loadbalancer.Server; 13import com.netflix.loadbalancer.ServerList; 14import com.netflix.loadbalancer.ServerListSubsetFilter; 15import com.shinho.DbribbonApplication; 16 17@Configuration 18public class MyConfig { 19 @Bean 20 public IRule ribbonRule() { 21 return new BestAvailableRule(); 22 } 23 24 @Bean 25 public IPing ribbonPing() { 26 return new PingUrl(); 27 } 28 29 @Bean 30 public ServerList<Server> ribbonServerList(IClientConfig config) { 31 return new DbribbonApplication.BazServiceList(config); 32 } 33 34 @Bean 35 public ServerListSubsetFilter serverListFilter() { 36 ServerListSubsetFilter filter = new ServerListSubsetFilter(); 37 return filter; 38 } 39}
Application
1package com.shinho; 2 3import org.springframework.boot.SpringApplication; 4import org.springframework.boot.autoconfigure.SpringBootApplication; 5import org.springframework.cloud.netflix.ribbon.RibbonClient; 6 7import com.config.MyConfig; 8import com.netflix.client.config.IClientConfig; 9import com.netflix.loadbalancer.ConfigurationBasedServerList; 10 11@SpringBootApplication 12@RibbonClient(name = "service-ribbon", configuration = MyConfig.class) 13public class DbribbonApplication { 14 15 public static void main(String[] args) { 16 SpringApplication.run(DbribbonApplication.class, args); 17 } 18 19 public static class BazServiceList extends ConfigurationBasedServerList { 20 public BazServiceList(IClientConfig config) { 21 super.initWithNiwsConfig(config); 22 } 23 } 24}
pom.xml
1<?xml version="1.0" encoding="UTF-8"?> 2<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 6 <groupId>com.shinho</groupId> 7 <artifactId>dbribbon</artifactId> 8 <version>0.0.1</version> 9 <packaging>jar</packaging> 10 11 <name>dbribbon</name> 12 <description>Demo project for Spring Boot</description> 13 14 <parent> 15 <groupId>org.springframework.boot</groupId> 16 <artifactId>spring-boot-starter-parent</artifactId> 17 <version>2.0.0.RELEASE</version> 18 <relativePath/> <!-- lookup parent from repository --> 19 </parent> 20 21 <properties> 22 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 23 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 24 <java.version>1.8</java.version> 25 <spring-cloud.version>Finchley.M9</spring-cloud.version> 26 </properties> 27 28 <dependencies> 29 <dependency> 30 <groupId>org.springframework.boot</groupId> 31 <artifactId>spring-boot-starter-web</artifactId> 32 </dependency> 33 <dependency> 34 <groupId>org.springframework.cloud</groupId> 35 <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> 36 </dependency> 37 38 <dependency> 39 <groupId>org.springframework.boot</groupId> 40 <artifactId>spring-boot-starter-test</artifactId> 41 <scope>test</scope> 42 </dependency> 43 <dependency> 44 <groupId>org.springframework</groupId> 45 <artifactId>spring-expression</artifactId> 46 <version>4.3.14.RELEASE</version> 47 </dependency> 48 </dependencies> 49 50 <dependencyManagement> 51 <dependencies> 52 <dependency> 53 <groupId>org.springframework.cloud</groupId> 54 <artifactId>spring-cloud-dependencies</artifactId> 55 <version>${spring-cloud.version}</version> 56 <type>pom</type> 57 <scope>import</scope> 58 </dependency> 59 </dependencies> 60 </dependencyManagement> 61 62 <build> 63 <plugins> 64 <plugin> 65 <groupId>org.springframework.boot</groupId> 66 <artifactId>spring-boot-maven-plugin</artifactId> 67 </plugin> 68 </plugins> 69 </build> 70 71 <repositories> 72 <repository> 73 <id>spring-snapshots</id> 74 <name>Spring Snapshots</name> 75 <url>https://repo.spring.io/snapshot</url> 76 <snapshots> 77 <enabled>true</enabled> 78 </snapshots> 79 </repository> 80 <repository> 81 <id>spring-milestones</id> 82 <name>Spring Milestones</name> 83 <url>https://repo.spring.io/milestone</url> 84 <snapshots> 85 <enabled>false</enabled> 86 </snapshots> 87 </repository> 88 </repositories> 89 90 <pluginRepositories> 91 <pluginRepository> 92 <id>spring-snapshots</id> 93 <name>Spring Snapshots</name> 94 <url>https://repo.spring.io/snapshot</url> 95 <snapshots> 96 <enabled>true</enabled> 97 </snapshots> 98 </pluginRepository> 99 <pluginRepository> 100 <id>spring-milestones</id> 101 <name>Spring Milestones</name> 102 <url>https://repo.spring.io/milestone</url> 103 <snapshots> 104 <enabled>false</enabled> 105 </snapshots> 106 </pluginRepository> 107 </pluginRepositories> 108 109 110</project>
客户端就和其它的一样了,注意客户端的Service-id不要和Ribbon的重复,Ribbon的Service-id是要单独的哦。