前言:微服务架构,不可避免的存在单个微服务有多个实例,那么客户端如何将请求分摊到多个微服务的实例上呢?这里我们就需要使用负载均衡了
一、ribbon简介
ribbon是netflix发布的负载均衡器,它有助于控制http和tcp客户端的行为。为ribbon配置服务提供者地址列表后,ribbon就可基于某种负载均衡算法,自动地帮助服务消费者去请求。ribbon默认为我们提供了很多的负载均衡算法,例如:轮询,随机等,也可自定义;
ribbon的github: https://github.com/netflix/ribbon
而在springcloud中使用ribbon和eureka时,ribbon会自动从eurekaserver中获取服务提供者地址列表,并基于负载均衡算法。
二、ribbon实战
1、创建eurekaserver,eurekaclient1,eurekaclient2,之前已经说过了eureka的使用,这里直接上代码:
eurekaserver:
serverapplication.java
1
2
3
4
5
6
7
8
9
|
@springbootapplication @enableeurekaserver public class serverapplication { public static void main(string[] args) { springapplication.run(serverapplication. class ,args); } } |
pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
<?xml version= "1.0" encoding= "utf-8" ?> <project xmlns= "http://maven.apache.org/pom/4.0.0" xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation= "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelversion> 4.0 . 0 </modelversion> <groupid>com.cn</groupid> <artifactid>eureka-ribbon-server</artifactid> <version> 1.0 -snapshot</version> <properties> <project.build.sourceencoding>utf- 8 </project.build.sourceencoding> <java.version> 1.8 </java.version> </properties> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version> 1.5 . 13 .release</version> </parent> <dependencies> <dependency> <groupid>org.springframework.</groupid> <artifactid></artifactid> </dependency> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-eureka-server</artifactid> </dependency> </dependencies> <dependencymanagement> <dependencies> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-dependencies</artifactid> <version>edgware.sr3</version> <type>pom</type> <scope> import </scope> </dependency> </dependencies> </dependencymanagement> <!-- 添加spring-boot的maven插件 --> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> </project> |
application.properties
1
2
3
4
5
6
7
8
|
server.port= 8761 #注意:这两个配置eureka默认为 true ,要改成 false ,否则会报错,connot connect server #表示是否将自己注册在eurekaserver上 eureka.client.register-with-eureka= false #表示是否从eurekaserver获取注册信息 eureka.client.fetch-registry= false eureka.client.service-url.defaultzone=http: //localhost:8761/eureka/ |
eurekaclient1:
pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
<?xml version= "1.0" encoding= "utf-8" ?> <project xmlns= "http://maven.apache.org/pom/4.0.0" xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation= "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelversion> 4.0 . 0 </modelversion> <groupid>com.cn</groupid> <artifactid>eureka-ribbon-client</artifactid> <version> 1.0 -snapshot</version> <properties> <project.build.sourceencoding>utf- 8 </project.build.sourceencoding> <java.version> 1.8 </java.version> </properties> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version> 1.5 . 13 .release</version> </parent> <dependencies> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-ribbon</artifactid> </dependency> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-eureka</artifactid> </dependency> </dependencies> <dependencymanagement> <dependencies> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-dependencies</artifactid> <version>edgware.sr3</version> <type>pom</type> <scope> import </scope> </dependency> </dependencies> </dependencymanagement> <!-- 添加spring-boot的maven插件 --> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> </project> |
1
2
3
|
server.port= 8762 spring.application.name=client- 8762 eureka.client.service-url.defaultzone=http: //localhost:8761/eureka/ |
而在启动类中加入resttemplate远程调用实例到容器中,并且 添加loadbalanced注解,使resttemplate具备负载均衡的能力 :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
@springbootapplication @enablediscoveryclient public class clientapplication { public static void main(string[] args) { springapplication.run(clientapplication. class , args); } /** * @description: 加入@loadbalanced注解,就可以为resttemplate加入负载均衡的能力 * @param: * @return: * @author: * @date: 2018/6/15 */ @bean @loadbalanced public resttemplate getresttemplate() { return new resttemplate(); } } |
创建controller,注入resttemplate、loadbalancerclient实例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
package com.cn.controller; import org.springframework.beans.factory.annotation.autowired; import org.springframework.cloud.client.serviceinstance; import org.springframework.cloud.client.loadbalancer.loadbalancerclient; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.getmapping; import org.springframework.web.bind.annotation.responsebody; import org.springframework.web.client.resttemplate; /** * @program: springcloud-example * @description: * @author: * @create: 2018-06-15 15:55 **/ @controller public class ribboncontroller { @autowired private loadbalancerclient loadbalancerclient; @autowired private resttemplate resttemplate; @getmapping ( "/loadinstance" ) @responsebody public string loadinstance() { serviceinstance choose = this .loadbalancerclient.choose( "client-87" ); system.out.println(choose.getserviceid()+ ":" +choose.gethost()+ ":" +choose.getport()); return choose.getserviceid() + ":" + choose.gethost() + ":" + choose.getport(); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package com.cn; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.cloud.client.discovery.enablediscoveryclient; /** * @program: springcloud-example * @description: * @author: 535504 * @create: 2018-06-15 16:05 **/ @springbootapplication @enablediscoveryclient public class clientapplication { public static void main(string[] args) { springapplication.run(clientapplication. class , args); } } |
eurekaclient2:
pom.xml与eurekaclient1中一致
application.xml:
1
2
3
|
server.port= 8763 spring.application.name=client- 87 eureka.client.service-url.defaultzone=http: //localhost:8761/eureka |
clientcontroller.java:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
package com.cn.contorller; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.getmapping; import org.springframework.web.bind.annotation.responsebody; /** * @program: springcloud-example * @description: * @author: * @create: 2018-06-15 16:12 **/ @controller public class clientcontroller { @getmapping ( "/getuser" ) @responsebody public string getuser() { system.out.println( "获取用户成功" ); return "获取用户成功" ; } } |
2、启动顺序:
①、依次启动eurekaserver =》 eurekaclient1 =》 eurekaclient2 ;
②、然后将eurekaclient2中的application.properties的server.port=8763改为server.port=8764,再次启动该项目;
③、打开eurekaserver的配置页面(http://localhost:8761/),如下:
④、我们在地址栏输入http://localhost:8762/loadinstance,多刷新几次,会发现每次调用的端口实例都不同,如下图:
⑤、我们在看控制台,如图:
至此,ribbon已经入门,是不是很简单,但是这只是最简单的应用,九牛一毛尔...学无止境乎!
示例代码:https://gitee.com/lfalex/springcloud-example
原文链接:http://www.cnblogs.com/lfalex0831/p/9188214.html