前言
先发句牢骚,最近太TM忙了,一直没时间静下心来继续写微服务架构!EMMMMMM。。。。。
经过前文的讲解,我们已经实现了微服务的注册与发现。启动各个微服务时,Eureka Client会把自己的网络信息注册到Eureka Server上。世界似乎更加美好了。
但是,这里面仍存在一些问题,比如负载均衡。一般来说,在生产环境中,各个微服务都会部署多个实例。那么服务消费者要如何将请求分摊到多个服务提供者实例上呢?
Ribbon简介
提到负载均衡,很多人肯定第一反应就想到了nginx(关于nginx,如果有不熟悉的,可以看下我的nginx模块下面的几篇随笔,写的不好,希望能帮到你)。我们今天主要说Ribbon来做负载均衡。
好了,那么nginx和Ribbon有啥区别呢?这么说吧:nginx适用于服务端实现负载均衡,如tomcat;Ribbon适用于在微服务中RPC远程调用中实现本地服务负载均衡,如Dubbo,springcloud中都是采用本地负载均衡。
什么是Ribbon?
Ribbon是Netflix发布的负载均衡器,是基于HTTP和TCP客户端的负载均衡器。为Ribbon配置服务提供者地址列表后,Ribbon就可基于某种负载均衡算法,自动地帮助服务消费者去请求。Ribbon默认为我们提供了很多的负载均衡算法,例如:轮询,随机等。当然,我们也可为Ribbon实现自定义的负载均衡算法。
在Spring cloud中,当Ribbon与Eureka配合使用时,Ribbon可自动从Eureka Server获取服务提供者地址列表,并基于负载均衡算法,请求其中一个服务提供者实例。
消费者整合Ribbon
1.复制项目microservice-consumer-movie,将ArtifactId修改为microservice-consumer-movie-ribbon.
2.为项目引入Ribbon的依赖,Ribbon的依赖如下:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
但是呢,前文中我们已经添加了依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
这个依赖包含了spring-cloud-starter-netflix-ribbon,因此无需再次引入。
3.为RestTemplate添加@LoadBalanced注解。
@Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); }
@LoadBalanced就一个注解,就可为RestTemplate整合Ribbon,使其具备负载均衡的能力。
4.对Controller代码进行修改
@RestController public class MovieController { private static final Logger LOGGER = LoggerFactory.getLogger(MovieController.class); @Autowired private RestTemplate restTemplate; @Autowired private LoadBalancerClient loadBalancerClient; @GetMapping("/user/{id}") public User findById(@PathVariable Long id) { return restTemplate.getForObject("http://microservice-provider-user/" + id, User.class); } @GetMapping("log-user-instance") public void logUserInstance() { ServiceInstance serviceInstance = loadBalancerClient.choose("microservice-provider-user"); LOGGER.info("{}:{}:{}", serviceInstance.getServiceId(), serviceInstance.getHost(), serviceInstance.getPort()); } }
由代码可知,我们将请求的地址改成了http://microservice-provider-user/.microservice-provider-user是用户微服务的虚拟主机名,当Ribbon和Eureka配合使用时,会自动将虚拟主机名映射成微服务的网络地址。在新增的logUserInstance()方法中可使用LoadBalanceClient的API更加直观地获取当前选择的用户微服务节点。
测试
1.启动microservice-discovery-eureka。
2.启动2个或更多microservice-provider-user实例。
3.启动microservice-consumer-movie-ribbon。
4.访问http://localhost:8761(eureka的端口)
两个user,一个movie都注册进来了。
5.多次访问http://localhost:8010/user/1(movie的端口)
结果如下:
{"id":1,"username":"account1","name":"张三","age":20,"balance":98.23}
同时,两个微服务实例的控制台都打印日志:
Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=? 2019-04-10 19:45:11.740 TRACE 13180 --- [nio-8081-exec-1] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [BIGINT] - [1] 2019-04-10 19:45:11.742 TRACE 13180 --- [nio-8081-exec-1] o.h.type.descriptor.sql.BasicExtractor : extracted value ([age2_0_0_] : [INTEGER]) - [20] 2019-04-10 19:45:11.742 TRACE 13180 --- [nio-8081-exec-1] o.h.type.descriptor.sql.BasicExtractor : extracted value ([balance3_0_0_] : [NUMERIC]) - [98.23] 2019-04-10 19:45:11.742 TRACE 13180 --- [nio-8081-exec-1] o.h.type.descriptor.sql.BasicExtractor : extracted value ([name4_0_0_] : [VARCHAR]) - [张三] 2019-04-10 19:45:11.742 TRACE 13180 --- [nio-8081-exec-1] o.h.type.descriptor.sql.BasicExtractor : extracted value ([username5_0_0_] : [VARCHAR]) - [account1] 2019-04-10 19:46:03.875 INFO 13180 --- [trap-executor-0] c.n.d.s.r.aws.ConfigClusterResolver : Resolving eureka endpoints via configuration
6.多次访问http://localhost:8010/log-user-instance,控制台会打印如下日志:
2019-04-10 19:48:55.151 INFO 12048 --- [nio-8082-exec-4] c.i.cloud.controller.MovieController : microservice-provider-user:192.168.1.33:8084 2019-04-10 19:48:55.333 INFO 12048 --- [nio-8082-exec-6] c.i.cloud.controller.MovieController : microservice-provider-user:192.168.1.33:8081 2019-04-10 19:48:55.506 INFO 12048 --- [nio-8082-exec-8] c.i.cloud.controller.MovieController : microservice-provider-user:192.168.1.33:8084 2019-04-10 19:48:55.674 INFO 12048 --- [io-8082-exec-10] c.i.cloud.controller.MovieController : microservice-provider-user:192.168.1.33:8081 2019-04-10 19:48:55.850 INFO 12048 --- [nio-8082-exec-2] c.i.cloud.controller.MovieController : microservice-provider-user:192.168.1.33:8084 2019-04-10 19:48:56.057 INFO 12048 --- [nio-8082-exec-4] c.i.cloud.controller.MovieController : microservice-provider-user:192.168.1.33:8081 2019-04-10 19:48:56.248 INFO 12048 --- [nio-8082-exec-6] c.i.cloud.controller.MovieController : microservice-provider-user:192.168.1.33:8084 2019-04-10 19:48:56.793 INFO 12048 --- [nio-8082-exec-8] c.i.cloud.controller.MovieController : microservice-provider-user:192.168.1.33:8081 2019-04-10 19:48:56.958 INFO 12048 --- [io-8082-exec-10] c.i.cloud.controller.MovieController : microservice-provider-user:192.168.1.33:8084 2019-04-10 19:48:57.111 INFO 12048 --- [nio-8082-exec-2] c.i.cloud.controller.MovieController : microservice-provider-user:192.168.1.33:8081 2019-04-10 19:48:57.274 INFO 12048 --- [nio-8082-exec-4] c.i.cloud.controller.MovieController : microservice-provider-user:192.168.1.33:8084 2019-04-10 19:48:57.459 INFO 12048 --- [nio-8082-exec-6] c.i.cloud.controller.MovieController : microservice-provider-user:192.168.1.33:8081 2019-04-10 19:48:57.616 INFO 12048 --- [nio-8082-exec-8] c.i.cloud.controller.MovieController : microservice-provider-user:192.168.1.33:8084 2019-04-10 19:48:57.786 INFO 12048 --- [io-8082-exec-10] c.i.cloud.controller.MovieController : microservice-provider-user:192.168.1.33:8081
我的两个user端口是8081和8084,可以看到,此时请求会均匀分布到两个用户微服务节点上,说明已经成功实现了负载均衡。
代码下载地址:https://gitee.com/fengyuduke/my_open_resources/blob/master/ribbon.zip