微服务之SpringCloud服务调用

港控/mmm° 2023-10-04 20:26 154阅读 0赞

SpringCloud调用服务有两种方式,一种是Ribbon+RestTemplate, 另外一种是Feign。

Ribbon是一个基于HTTP和TCP客户端的负载均衡器,其实feign也使用了ribbon, 只要使用@FeignClient时,ribbon就会自动使用。

Ribbon介绍

Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端负载均衡的工具。

Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法和服务调用。Ribbon客户端组件提供一新列完善的配置项如连接超时,重试等。简单的说,就是在配置文件中列出Load Balancer(简称LB)后面所有的机器,Ribbon会自动的帮助你基于某种规则(如简单轮询,随机连接等)去连接这些机器。我们很容易使用Ribbon实现自定义的负载均衡算法。

Ribbon提供了与服务器集群通信的软件负载平衡器。负载平衡器提供以下基本功能:

  • 向通信客户端提供单个服务器的公共DNS名称或lP。

  • 根据特定逻辑在服务器列表中循环。

某些负载平衡器还可以提供高级功能,如

  • 通过将cient和服务器划分为多个区域(如数据中心中的机架)来建立它们之间的亲和力,并支持同一区域中的服务器以减少延迟

  • 保持服务器的统计信息,避免服务器出现高延迟或频繁故障。

  • 保持区域统计数据,避免可能处于停机状态的区域

使用高级功能需要使用Ribbon中提供的一个客户端,因为它与负载平衡器集成,并为负载平衡器统计信息提供输入。

LB负载均衡(Load Balance)是什么

简单的说就是将用户的请求平摊的分配到多个服务上,从而达到系统的HA(高可用)。常见的负载均衡有软件Nginx,LVS,硬件F5等。

Ribbon本地负载均衡客户端VS Nginx服务端负载均衡区别

Nginx是服务器负载均衡,客户端所有请求都会交给nginx,然后由nginx实现转发请求。即负载均衡是由服务端实现的。

Ribbon本地负载均衡,在调用微服务接口时候,会在注册中心上获取注册信息服务列表之后缓存到VM本地,从而在本地实现RPC远程服务调用技术。

Ribbon中的关键组件

83018fd6fffae5a5f330238287db424e.png

  • ServerList:可以响应客户端的特定服务的服务器列表。

  • ServerListFilter:可以动态获得的具有所需特征的候选服务器列表的过滤器。

  • ServerListUpdater:用于执行动态服务器列表更新。

  • Rule:负载均衡策略,用于确定从服务器列表返回哪个服务器。

  • Ping:客户端用于快速检查服务器当时是否处于活动状态。

  • LoadBalancer:负载均衡器,负责负载均衡调度的管理

Ribbon工作步骤

第—步先选择EurekaServer ,它优先选择在同一个区域内负载较少的server。

第二步再根据用户指定的策略,在从server取到的服务注册列表中选择一个地址。其中Ribbon提供了多种策略:比如轮询、随机和根据响应时间加权。

Ribbon+RestTemplate实现负载均衡

默认情况Ribbon是使用轮询的负载方式的,因此这块我们只需要在消费者服务端进行负载均衡构建即可,实现对服务提供端的轮询调用。由于服务是构建在Eureka服务上的,因此项目的基本服务提供模块以及eureka服务不作详细说明,需要具体了解的可参考前一章微服务之服务注册与发现

7f31b33353bfd013df0e3f9d339258b5.png

这里我们主要对消费者端80端口项目进行构建,首先准备好项目中的所需的Ribbon依赖。其他需要依赖不做详细展示

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-netflix-ribbon</artifactId>
  4. <version>2.2.1.RELEASE</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.netflix.ribbon</groupId>
  8. <artifactId>ribbon-loadbalancer</artifactId>
  9. <version>2.3.0</version>
  10. </dependency>

在spring-cloud-starter-netflix-eureka-client的2.2.1.RELEASE版本中其实已经配备了Ribbon依赖,但是笔者这块的eureka依赖是3.1.3版本的,没有集成Ribbon依赖,因此需要额外导入。

另外我们还需要配置yml中的eureka服务配置,保证80服务能注册到eureka中。

  1. server:
  2. port: 80
  3. spring:
  4. application:
  5. name: order-consumer-service
  6. #客户端注册进eureka
  7. eureka:
  8. client:
  9. register-with-eureka: true#将自己注册进eureka服务
  10. fetch-registry: true#是否从eureka服务抓取已有的注册信息 默认true
  11. service-url:
  12. #defaultZone: http://localhost:7001/eureka 单机情况
  13. defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka #集群状态下

然后就可以对项目的基本结构进行构建了,在Ribbon+RestTemplate实现负载的时候需要注意的是,默认情况下的RestTemplate是不具备负载均衡的能力的,需要我们手动添加@LoadBanced注解配置实现。

  1. packagecom.yy.myconfig;
  2. importorg.springframework.cloud.client.loadbalancer.LoadBalanced;
  3. importorg.springframework.context.annotation.Bean;
  4. importorg.springframework.context.annotation.Configuration;
  5. importorg.springframework.web.client.RestTemplate;
  6. /**
  7. * @author young
  8. * @date 2022/12/16 15:49
  9. * @description: 配置类
  10. */
  11. @Configuration
  12. publicclassApplicationContextConfig {
  13. /**
  14. * 配置restTemplate
  15. */
  16. @Bean
  17. //给restTemplate提供负载均衡效果,用于服务建立集群的时候
  18. @LoadBalanced
  19. publicRestTemplategetRestTemplate() {
  20. returnnewRestTemplate();
  21. }
  22. }

最后构建controller层的方法测试即可。

  1. packagecom.yy.controller;
  2. importcom.yy.myconfig.LoadBalancer;
  3. importcom.yy.entity.Payment;
  4. importcom.yy.utils.Result;
  5. importlombok.extern.slf4j.Slf4j;
  6. importorg.springframework.cloud.client.ServiceInstance;
  7. importorg.springframework.cloud.client.discovery.DiscoveryClient;
  8. importorg.springframework.web.bind.annotation.GetMapping;
  9. importorg.springframework.web.bind.annotation.PathVariable;
  10. importorg.springframework.web.bind.annotation.RequestMapping;
  11. importorg.springframework.web.bind.annotation.RestController;
  12. importorg.springframework.web.client.RestTemplate;
  13. importjavax.annotation.Resource;
  14. importjava.net.URI;
  15. importjava.util.List;
  16. /**
  17. * @author young
  18. * @date 2022/12/16 15:46
  19. * @description: 模拟客户端接口
  20. */
  21. @RestController
  22. @Slf4j
  23. @RequestMapping("order")
  24. publicclassOrderController {
  25. /**
  26. * 用应用名代替
  27. */
  28. publicstaticfinalStringPAYMENT_URL="http://CLOUD-PAYMENT-SERVICE";
  29. @Resource
  30. privateRestTemplaterestTemplate;
  31. @GetMapping("/add")
  32. publicResult<Payment>create(Paymentpayment){
  33. returnrestTemplate.postForObject(PAYMENT_URL+"/payment/add",payment,Result.class);
  34. }
  35. @GetMapping("/get/{id}")
  36. publicResult<Payment>create(@PathVariableIntegerid){
  37. returnrestTemplate.getForObject(PAYMENT_URL+"/payment/"+id,Result.class);
  38. }

415a32a0a0409705e22190e3a38e2a71.png

729ffdbdfa17feeda173b0d071e65d53.png

源码探究

那为何加上@LoadBanced注解就能实现负载均衡效果呢?

通过源码我们可以发现这是一个标记注解:

  1. @Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD})
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Documented
  4. @Inherited
  5. @Qualifier
  6. public @interface LoadBalanced {
  7. }

通过注释可以知道@LoadBalanced注解是用来给RestTemplate做标记,方便我们对RestTemplate添加一个LoadBalancerClient,以实现客户端负载均衡。

根据spring boot的自动配置原理,可以知道同包下的LoadBalancerAutoConfiguration,应该是实现客户端负载均衡器的自动化配置类。代码如下:

  1. //
  2. // Source code recreated from a .class file by IntelliJ IDEA
  3. // (powered by FernFlower decompiler)
  4. //
  5. @Configuration(
  6. proxyBeanMethods = false
  7. )
  8. @ConditionalOnClass({RestTemplate.class})
  9. @ConditionalOnBean({LoadBalancerClient.class})
  10. @EnableConfigurationProperties({LoadBalancerClientsProperties.class})
  11. public class LoadBalancerAutoConfiguration {
  12. @LoadBalanced
  13. @Autowired(
  14. required = false
  15. )
  16. private List<RestTemplate> restTemplates = Collections.emptyList();
  17. @Autowired(
  18. required = false
  19. )
  20. private List<LoadBalancerRequestTransformer> transformers = Collections.emptyList();
  21. public LoadBalancerAutoConfiguration() {
  22. }
  23. @Bean
  24. public SmartInitializingSingleton loadBalancedRestTemplateInitializerDeprecated(final ObjectProvider<List<RestTemplateCustomizer>> restTemplateCustomizers) {
  25. return () -> {
  26. restTemplateCustomizers.ifAvailable((customizers) -> {
  27. Iterator var2 = this.restTemplates.iterator();
  28. while(var2.hasNext()) {
  29. RestTemplate restTemplate = (RestTemplate)var2.next();
  30. Iterator var4 = customizers.iterator();
  31. while(var4.hasNext()) {
  32. RestTemplateCustomizer customizer = (RestTemplateCustomizer)var4.next();
  33. customizer.customize(restTemplate);
  34. }
  35. }
  36. });
  37. };
  38. }
  39. @Bean
  40. @ConditionalOnMissingBean
  41. public LoadBalancerRequestFactory loadBalancerRequestFactory(LoadBalancerClient loadBalancerClient) {
  42. return new LoadBalancerRequestFactory(loadBalancerClient, this.transformers);
  43. }
  44. @Configuration(
  45. proxyBeanMethods = false
  46. )
  47. @ConditionalOnClass({RetryTemplate.class})
  48. @ConditionalOnBean({ReactiveLoadBalancer.Factory.class})
  49. @ConditionalOnProperty(
  50. value = {"spring.cloud.loadbalancer.retry.enabled"},
  51. matchIfMissing = true
  52. )
  53. public static class RetryInterceptorAutoConfiguration {
  54. public RetryInterceptorAutoConfiguration() {
  55. }
  56. @Bean
  57. @ConditionalOnMissingBean
  58. public RetryLoadBalancerInterceptor loadBalancerInterceptor(LoadBalancerClient loadBalancerClient, LoadBalancerRequestFactory requestFactory, LoadBalancedRetryFactory loadBalancedRetryFactory, ReactiveLoadBalancer.Factory<ServiceInstance> loadBalancerFactory) {
  59. return new RetryLoadBalancerInterceptor(loadBalancerClient, requestFactory, loadBalancedRetryFactory, loadBalancerFactory);
  60. }
  61. @Bean
  62. @ConditionalOnMissingBean
  63. public RestTemplateCustomizer restTemplateCustomizer(final RetryLoadBalancerInterceptor loadBalancerInterceptor) {
  64. return (restTemplate) -> {
  65. List<ClientHttpRequestInterceptor> list = new ArrayList(restTemplate.getInterceptors());
  66. list.add(loadBalancerInterceptor);
  67. restTemplate.setInterceptors(list);
  68. };
  69. }
  70. }
  71. @Configuration(
  72. proxyBeanMethods = false
  73. )
  74. @ConditionalOnClass({RetryTemplate.class})
  75. public static class RetryAutoConfiguration {
  76. public RetryAutoConfiguration() {
  77. }
  78. @Bean
  79. @ConditionalOnMissingBean
  80. public LoadBalancedRetryFactory loadBalancedRetryFactory() {
  81. return new LoadBalancedRetryFactory() {
  82. };
  83. }
  84. }
  85. private static class RetryMissingOrDisabledCondition extends AnyNestedCondition {
  86. RetryMissingOrDisabledCondition() {
  87. super(ConfigurationPhase.REGISTER_BEAN);
  88. }
  89. @ConditionalOnProperty(
  90. value = {"spring.cloud.loadbalancer.retry.enabled"},
  91. havingValue = "false"
  92. )
  93. static class RetryDisabled {
  94. RetryDisabled() {
  95. }
  96. }
  97. @ConditionalOnMissingClass({"org.springframework.retry.support.RetryTemplate"})
  98. static class RetryTemplateMissing {
  99. RetryTemplateMissing() {
  100. }
  101. }
  102. }
  103. @Configuration(
  104. proxyBeanMethods = false
  105. )
  106. @Conditional({RetryMissingOrDisabledCondition.class})
  107. static class LoadBalancerInterceptorConfig {
  108. LoadBalancerInterceptorConfig() {
  109. }
  110. @Bean
  111. public LoadBalancerInterceptor loadBalancerInterceptor(LoadBalancerClient loadBalancerClient, LoadBalancerRequestFactory requestFactory) {
  112. return new LoadBalancerInterceptor(loadBalancerClient, requestFactory);
  113. }
  114. @Bean
  115. @ConditionalOnMissingBean
  116. public RestTemplateCustomizer restTemplateCustomizer(final LoadBalancerInterceptor loadBalancerInterceptor) {
  117. return (restTemplate) -> {
  118. List<ClientHttpRequestInterceptor> list = new ArrayList(restTemplate.getInterceptors());
  119. list.add(loadBalancerInterceptor);
  120. restTemplate.setInterceptors(list);
  121. };
  122. }
  123. }
  124. }

代码层面可以看出,这个类作用主要是使用RestTemplateCustomizer对所有标注了@LoadBalanced的RestTemplate Bean添加了一个LoadBalancerInterceptor拦截器,而这个拦截器的作用就是对请求的URI进行转换获取到具体应该请求哪个服务实例。 那再看看添加的拦截器LoadBalancerInterceptor的代码,如下:

  1. public class LoadBalancerInterceptor implements ClientHttpRequestInterceptor {
  2. private LoadBalancerClient loadBalancer;
  3. private LoadBalancerRequestFactory requestFactory;
  4. public LoadBalancerInterceptor(LoadBalancerClient loadBalancer, LoadBalancerRequestFactory requestFactory) {
  5. this.loadBalancer = loadBalancer;
  6. this.requestFactory = requestFactory;
  7. }
  8. public LoadBalancerInterceptor(LoadBalancerClient loadBalancer) {
  9. this(loadBalancer, new LoadBalancerRequestFactory(loadBalancer));
  10. }
  11. public ClientHttpResponse intercept(final HttpRequest request, final byte[] body, final ClientHttpRequestExecution execution) throws IOException {
  12. URI originalUri = request.getURI();
  13. String serviceName = originalUri.getHost();
  14. Assert.state(serviceName != null, "Request URI does not contain a valid hostname: " + originalUri);
  15. return (ClientHttpResponse)this.loadBalancer.execute(serviceName, this.requestFactory.createRequest(request, body, execution));
  16. }
  17. }

从代码可以看出 LoadBalancerInterceptor 拦截了请求后,通过LoadBalancerClient执行具体的请求发送。

而LoadBancerClient作为一个接口,其中包含四个方法:

  1. public interface LoadBalancerClient extends ServiceInstanceChooser {
  2. <T> T execute(String serviceId, LoadBalancerRequest<T> request) throws IOException;
  3. <T> T execute(String serviceId, ServiceInstance serviceInstance, LoadBalancerRequest<T> request) throws IOException;
  4. URI reconstructURI(ServiceInstance instance, URI original);
  5. }

其中主要的三个方法可以了解一下:

  • ServiceInstance choose(String serviceId):根据传入的服务id,从负载均衡器中为指定的服务选择一个服务实例。

  • T execute(String serviceId, LoadBalancerRequest request):根据传入的服务id,指定的负载均衡器中的服务实例执行请求。

  • T execute(String serviceId, ServiceInstance serviceInstance, LoadBalancerRequest request):根据传入的服务实例,执行请求。

这个接口的唯一实现类是RibbonLoadBalancerClient,他的核心代码在于:

  1. ……
  2. public ServiceInstance choose(String serviceId) {
  3. return this.choose(serviceId, (Object)null);
  4. }
  5. public ServiceInstance choose(String serviceId, Object hint) {
  6. Server server = this.getServer(this.getLoadBalancer(serviceId), hint);
  7. return server == null ? null : new RibbonServer(serviceId, server, this.isSecure(server, serviceId), this.serverIntrospector(serviceId).getMetadata(server));
  8. }
  9. public <T> T execute(String serviceId, LoadBalancerRequest<T> request) throws IOException {
  10. return this.execute(serviceId, (LoadBalancerRequest)request, (Object)null);
  11. }
  12. public <T> T execute(String serviceId, LoadBalancerRequest<T> request, Object hint) throws IOException {
  13. ILoadBalancer loadBalancer = this.getLoadBalancer(serviceId);
  14. Server server = this.getServer(loadBalancer, hint);
  15. if (server == null) {
  16. throw new IllegalStateException("No instances available for " + serviceId);
  17. } else {
  18. RibbonServer ribbonServer = new RibbonServer(serviceId, server, this.isSecure(server, serviceId), this.serverIntrospector(serviceId).getMetadata(server));
  19. return this.execute(serviceId, (ServiceInstance)ribbonServer, (LoadBalancerRequest)request);
  20. }
  21. }
  22. protected Server getServer(String serviceId) {
  23. return this.getServer(this.getLoadBalancer(serviceId), (Object)null);
  24. }
  25. protected Server getServer(ILoadBalancer loadBalancer) {
  26. return this.getServer(loadBalancer, (Object)null);
  27. }
  28. protected Server getServer(ILoadBalancer loadBalancer, Object hint) {
  29. return loadBalancer == null ? null : loadBalancer.chooseServer(hint != null ? hint : "default");
  30. }
  31. protected ILoadBalancer getLoadBalancer(String serviceId) {
  32. return this.clientFactory.getLoadBalancer(serviceId);
  33. }
  34. ……

从代码上可以看出实际负载均衡的是通过 ILoadBalancer 来实现。

  1. public interface ILoadBalancer {
  2. void addServers(List<Server> var1);
  3. Server chooseServer(Object var1);
  4. void markServerDown(Server var1);
  5. /** @deprecated */
  6. @Deprecated
  7. List<Server> getServerList(boolean var1);
  8. List<Server> getReachableServers();
  9. List<Server> getAllServers();
  10. }
  • addServers:向负载均衡器中添加一个服务实例集合。

  • chooseServer:跟据key,从负载均衡器获取服务实例。

  • markServerDown:用来标记某个服务实例下线。

  • getReachableServers:获取可用的服务实例集合。

  • getAllServers():获取所有服务实例集合,包括下线的服务实例。

这个接口与实现类的关系图如下:

e8238adf6da1ac4eecd5fa48396efdc3.png

  • NoOpLoadBalancer:啥都不做

  • BaseLoadBalancer:

一个负载均衡器的基本实现,其中有一个任意列表,可以将服务器设置为服务器池。可以设置一个ping来确定服务器的活力。

在内部,该类维护一个“all”服务器列表,以及一个“up”服务器列表,并根据调用者的要求使用它们。

  • DynamicServerListLoadBalancer:

通过动态的获取服务器的候选列表的负载平衡器。

可以通过筛选标准来传递服务器列表,以过滤不符合所需条件的服务器。

  • ZoneAwareLoadBalancer:

用于测量区域条件的关键指标是平均活动请求,它根据每个rest客户机和每个区域聚合。这是区域内未完成的请求总数除以可用目标实例的数量(不包括断路器跳闸实例)。当在坏区上缓慢发生超时时,此度量非常有效。

该负载均衡器将计算并检查所有可用区域的区域状态。如果任何区域的平均活动请求已达到配置的阈值,则该区域将从活动服务器列表中删除。如果超过一个区域达到阈值,则将删除每个服务器上活动请求最多的区域。一旦去掉最坏的区域,将在其余区域中选择一个区域,其概率与其实例数成正比。服务器将使用给定的规则从所选区域返回。对于每个请求,将重复上述步骤。也就是说,每个与区域相关的负载平衡决策都是实时做出的,最新的统计数据可以帮助进行选择。那么在整合Ribbon的时候Spring Cloud默认采用了哪个具体实现呢?我们通过RibbonClientConfiguration配置类,可以知道在整合时默认采用了ZoneAwareLoadBalancer来实现负载均衡器。

  1. @Bean
  2. @ConditionalOnMissingBean
  3. public ILoadBalancer ribbonLoadBalancer(IClientConfig config, ServerList<Server> serverList, ServerListFilter<Server> serverListFilter, IRule rule, IPing ping, ServerListUpdater serverListUpdater) {
  4. return (ILoadBalancer)(this.propertiesFactory
  5. .isSet(ILoadBalancer.class, this.name) ? (ILoadBalancer)this.propertiesFactory
  6. .get(ILoadBalancer.class, config, this.name) : new ZoneAwareLoadBalancer(config, rule, ping, serverList, serverListFilter, serverListUpdater));
  7. }

从这段代码 ,也可以看出,负载均衡器所需的主要配置项是IClientConfig, ServerList, ServerListFilter, IRule, IPing, ServerListUpdater。其中重点的是IRule为LoadBalancer定义“负载均衡策略”的接口

它的实现类关系图如下:

b36f24f124cd7167909a9b4835819a1b.png

  • BestAvailableRule:选择具有最低并发请求的服务器。

  • ClientConfigEnabledRoundRobinRule:轮询。

  • RandomRule:随机选择一个服务器。

  • RoundRobinRule:轮询选择服务器。

  • RetryRule:具备重试机制的轮询。

  • WeightedResponseTimeRule:根据使用平均响应时间去分配一个weight(权重) ,weight越低,被选择的可能性就越低。

  • ZoneAvoidanceRule:根据区域和可用性筛选,再轮询选择服务器

然后我们可以看看默认的轮询负载均衡是如何实现的

  1. public class RoundRobinRule extends AbstractLoadBalancerRule {
  2. private AtomicInteger nextServerCyclicCounter;
  3. private static final boolean AVAILABLE_ONLY_SERVERS = true;
  4. private static final boolean ALL_SERVERS = false;
  5. private static Logger log = LoggerFactory.getLogger(RoundRobinRule.class);
  6. //无参的初始化了一个原子操作类,为保证线程安全
  7. public RoundRobinRule() {
  8. this.nextServerCyclicCounter = new AtomicInteger(0);
  9. }
  10. //有参构造类设置了加载的服务器集群LoadBalancer
  11. public RoundRobinRule(ILoadBalancer lb) {
  12. this();
  13. this.setLoadBalancer(lb);
  14. }
  15. //通过实现的choose方法来选择要指定的服务器
  16. public Server choose(ILoadBalancer lb, Object key) {
  17. if (lb == null) {
  18. log.warn("no load balancer");
  19. return null;
  20. } else {
  21. //将上次选择的服务器置空,重新根据规则选择
  22. Server server = null;
  23. //请求次数统计
  24. int count = 0;
  25. while(true) {
  26. if (server == null && count++ < 10) {
  27. //获取可达(健康)的服务器集合
  28. List<Server> reachableServers = lb.getReachableServers();
  29. List<Server> allServers = lb.getAllServers();
  30. //获取up状态的服务器数
  31. int upCount = reachableServers.size();
  32. //服务集群总数
  33. int serverCount = allServers.size();
  34. //如果没有up状态的服务器或者没有服务器,直接退出选择
  35. if (upCount != 0 && serverCount != 0) {
  36. //增加并获得,返回要选择的服务器下标
  37. int nextServerIndex = this.incrementAndGetModulo(serverCount);
  38. //通过下标选择服务器
  39. server = (Server)allServers.get(nextServerIndex);
  40. if (server == null) {
  41. Thread.yield();
  42. } else {
  43. if (server.isAlive() && server.isReadyToServe()) {
  44. return server;
  45. }
  46. server = null;
  47. }
  48. continue;
  49. }
  50. log.warn("No up servers available from load balancer: " + lb);
  51. return null;
  52. }
  53. if (count >= 10) {
  54. log.warn("No available alive servers after 10 tries from load balancer: " + lb);
  55. }
  56. //返回选择的服务器
  57. return server;
  58. }
  59. }
  60. }
  61. private int incrementAndGetModulo(int modulo) {
  62. int current;
  63. int next;
  64. do {
  65. //这个是我们的atmic原子类。得到默认的值0
  66. current = this.nextServerCyclicCounter.get();
  67. //这个是服务的个数。这个next的值如果这一样取,其实就是和serverlist的下标对应,并且,每次加1.
  68. next = (current + 1) % modulo;
  69. }
  70. while(!this.nextServerCyclicCounter.compareAndSet(current, next));//因为是多线程,我们要保证安全,所以用了循环的CAS自旋锁
  71. return next;
  72. }
  73. public Server choose(Object key) {
  74. return this.choose(this.getLoadBalancer(), key);
  75. }
  76. public void initWithNiwsConfig(IClientConfig clientConfig) {
  77. }
  78. }

因此轮询算法实际就是实际调用服务器的下标 = rest接口第几次的请求数 % 服务器集群总数量,注意:每次重新启动服务后rest接口从1开始计数,这样我们就得到了实际调用服务器的下标。现在我们有两台服务器分别为:server-1(端口号:8001,下标:0),server-2(端口号:8002,下标:1)当请求数为1时:1%2=1 对应的下标为1,则获得的服务地址为ip:8002当请求数为2时:2%2=0对应的下标为0,则获得的服务地址为ip:8001

这也就是为什么我们请求时,先反馈的是8002的服务,然后是8001.

手写一个简单的轮询算法

首先创建一个类似的LoadBancer接口,里面包含一个从服务集群获取一个服务实例的方法。

  1. public interface LoadBalancer {
  2. ServiceInstance instance(List<ServiceInstance> serviceInstances);
  3. }

然后通过实现类去实现轮询算法的实现过程。

  1. /**
  2. * @author young
  3. * @date 2022/12/23 20:37
  4. * @description: 自定义一个轮询算法
  5. */
  6. @Component
  7. publicclassMyRandomRuleimplementsLoadBalancer{
  8. privatefinalAtomicIntegeratomicInteger=newAtomicInteger(0);
  9. publicfinalintgetAndIncrement(){
  10. intcurrent;
  11. intnext;
  12. do {
  13. current=this.atomicInteger.get();
  14. next=current>=Integer.MAX_VALUE?0:current+1;
  15. //自旋锁设计
  16. }while (!this.atomicInteger.compareAndSet(current,next));
  17. System.out.println("访问次数为:"+next+"次");
  18. returnnext;
  19. }
  20. //负载均衡算法: rest接口第几次请求数%服务器集群总数量=实际调用服务器位置下标m,每次服务重启动后rest接口
  21. @Override
  22. publicServiceInstanceinstance(List<ServiceInstance>serviceInstances) {
  23. intindex=getAndIncrement()%serviceInstances.size();
  24. returnserviceInstances.get(index);
  25. }
  26. }

另外需要注意的是,需要将之前配置的restTamplate上的@LoadBanced注解注释掉,不然我们自己写的就浪费了。

最后通过一个controller方法去测试。

  1. /**
  2. * @author young
  3. * @date 2022/12/16 15:46
  4. * @description: 模拟客户端接口
  5. */
  6. @RestController
  7. @Slf4j
  8. @RequestMapping("order")
  9. publicclassOrderController {
  10. @Resource
  11. privateRestTemplaterestTemplate;
  12. @Resource
  13. privateLoadBalancerloadBalancer;
  14. @Resource
  15. privateDiscoveryClientdiscoveryClient;
  16. @GetMapping("/myTest/lb")
  17. publicStringgetMyLB(){
  18. List<ServiceInstance>instances=discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
  19. if (instances==null||instances.size()<=0){
  20. returnnull;
  21. }
  22. ServiceInstanceinstance=loadBalancer.instance(instances);
  23. returnrestTemplate.getForObject(instance.getUri()+"/client/lb",String.class);
  24. }
  25. }

测试结果:

932acc98ed93901822e544f2b4282825.png

219702c8690c16a74cad6026c757b118.png

成功实现模拟的轮询负载效果。

更换负载均衡算法

虽然Ribbon默认的负载均衡算法为轮询算法,但是我们也可以更具自己的业务需求去进行切换。上面也说到了,ribbon其实有7个负载规则。方式很简单:

首先在启动类所在父级目录外不同的地方创建一个新的文件夹,以存放我们自定义的负载规则。因为官方文档明确给出了警告:这个自定义配置类不能放在@ComponentScan所扫描的当前包下以及子包下,否则我们自定义的这个配置类就会被所有的Ribbon客户端所共享,达不到特殊化定制的目的了。

d982ea0b0595b9852c9b471394298fed.png

然后编写配置类去更换我们的负载规则,并使其注入到spring配置。

  1. /**
  2. * @author young
  3. * @date 2022/12/23 19:41
  4. * @description: 自定义Ribbon负载均衡规则,父包不能在启动类所在包或子包下!
  5. */
  6. @Configuration
  7. publicclassMySelfRule {
  8. @Bean
  9. publicIRulemyRule(){
  10. //随机负载均衡模式
  11. returnnewRandomRule();
  12. }
  13. }

最后在启动类上添加@RibbonClient指明指定的微服务更换负载规则配置。

  1. @SpringBootApplication
  2. @EnableEurekaClient
  3. @RibbonClient(name="CLOUD-PAYMENT-SERVICE",configuration=MySelfRule.class) //指定更换负载均衡模式
  4. publicclassOrderApplication {
  5. publicstaticvoidmain(String[] args) {
  6. SpringApplication.run(OrderApplication.class,args);
  7. }
  8. }

这里由于我们配置的随机负载规则,因此完成后启动测试调试接口就会随机去调用服务端提供的服务接口了。

Feign介绍

Feign是一个声明式的Web服务客户端。这使得Web服务客户端的写入更加方便 要使用Feign创建一个界面并对其进行注释。它具有可插入注释支持,包括Feign注释和JAX-RS注释。Feign还支持可插拔编码器和解码器。Spring Cloud增加了对Spring MVC注释的支持,并使用Spring Web中默认使用的HttpMessageConverters。Spring Cloud集成Ribbon和Eureka以在使用Feign时提供负载均衡的http客户端。

Spring Cloud Netflix默认为feign(BeanType beanName:ClassName)提供以下bean:

  • Decoder feignDecoder:ResponseEntityDecoder(其中包含SpringDecoder)

  • Encoder feignEncoder:SpringEncoder

  • Logger feignLogger:Slf4jLogger

  • Contract feignContract:SpringMvcContract

  • Feign.Builder feignBuilder:HystrixFeign.Builder

  • Client feignClient:如果Ribbon启用,则为LoadBalancerFeignClient,否则将使用默认的feign客户端。

可以通过将feign.okhttp.enabled或feign.httpclient.enabled设置为true,并将它们放在类路径上来使用OkHttpClient和ApacheHttpClient feign客户端。

Spring Cloud Netflix 默认情况下提供以下bean,但是仍然从应用程序上下文中查找这些类型的bean以创建假客户机:

  • Logger.Level

  • Retryer

  • ErrorDecoder

  • Request.Options

  • Collection

  • SetterFactory

因此如果需要配置指定的日志记录,需要我们自己配置并注入。Feign日志记录为每个创建的Feign客户端创建一个记录器。默认情况下,记录器的名称是用于创建Feign客户端的接口的全类名称。Feign日志记录仅响应DEBUG级别。

这里以Logger.Level为例

  1. /**
  2. * @author young
  3. * @date 2022/12/24 18:09
  4. * @description: 配置Feign日志
  5. */
  6. @Configuration
  7. public class FeignConfig {
  8. @Bean
  9. Logger.Level feignLogger(){
  10. return Logger.Level.FULL;
  11. }
  12. }
  • NONE:默认,不显示任何日志;

  • BASIC: 仅记录请求方法、URL、响应状态码及执行时间;

  • HEADERS:除了BASIC中定义的信息之外,还有请求头和响应头信息;

  • FULL:除了HEADERS中定义的信息之外,还有请求的正文和响应数据。

配置完成后在yml文件中即可指定对应服务接口的调用情况日志打印。

  1. #配置feign日志
  2. logging:
  3. level:
  4. com.yy.service.TestFeignService: debug

Feign实现服务调用

同样的套路,创建新的springboot模板后添加相关的feign依赖,这里我们添加OpenFeign。

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter</artifactId>
  8. </dependency>
  9. <dependency>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter-web</artifactId>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.springframework.cloud</groupId>
  15. <artifactId>spring-cloud-starter-openfeign</artifactId>
  16. </dependency>
  17. <dependency>
  18. <groupId>com.yy</groupId>
  19. <artifactId>cloud-common</artifactId>
  20. <version>${myproject.version}</version>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-actuator</artifactId>
  25. </dependency>

OpenFeign是Spring Cloud在Feign的基础上支持了SpringMVC的注解,如@RequesMapping等等。OpenFeign的@FeignClient可以解析SpringMVc的@RequestMapping注解下的接口,并通过动态代理的方式产生实现类,实现类中做负载均衡并调用其他服务。

  • OpenFeign的设计宗旨式简化Java Http客户端的开发。Feign在restTemplate的基础上做了进一步的封装,由其来帮助我们定义和实现依赖服务接口的定义。在OpenFeign的协助下,我们只需创建一个接口并使用注解的方式进行配置(类似于Dao接口上面的Mapper注解)即可完成对服务提供方的接口绑定,大大简化了Spring cloud Ribbon的开发,自动封装服务调用客户端的开发量。

  • OpenFeign集成了Ribbon,利用ribbon维护了服务列表,并且通过ribbon实现了客户端的负载均衡。与ribbon不同的是,通过OpenFeign只需要定义服务绑定接口且以申明式的方法,优雅而简单的实现了服务调用

然后编写yml配置。

  1. server:
  2. port: 80
  3. eureka:
  4. client:
  5. register-with-eureka: true
  6. service-url:
  7. defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka #集群状态下
  8. fetch-registry: true
  9. spring:
  10. application:
  11. name: OpenFeign-service-order-80
  12. #配置feign日志
  13. logging:
  14. level:
  15. com.yy.service.TestFeignService: debug

既然是面向接口编程,因此我们使用Feign的时候不用配置restTamplate去调用服务,而是创建另外的消费者服务接口,并且通过@FeignClient指明服务提供端的服务名。

  1. @FeignClient(value="CLOUD-PAYMENT-SERVICE")
  2. @Component
  3. publicinterfaceTestFeignService {
  4. @GetMapping("/payment/{id}")
  5. Result<Payment>getOne(@PathVariable("id")Integerid);
  6. @GetMapping("/payment/timeout")
  7. Stringtimeout();
  8. }

这里接口中的方法对应的服务提供端的controller层的服务方法。

  1. /**
  2. * 通过主键查询单条数据
  3. *
  4. * @param id 主键
  5. * @return 单条数据
  6. */
  7. @GetMapping("{id}")
  8. publicResult<Payment>selectOne(@PathVariableSerializableid) {
  9. returnResult.ok(this.paymentService.getById(id)).message("调用的eureka服务端口号为"+port);
  10. }
  11. @GetMapping("/timeout")
  12. publicStringtimeout(){
  13. try{
  14. TimeUnit.SECONDS.sleep(3);
  15. }catch (InterruptedExceptione){
  16. e.printStackTrace();
  17. }
  18. returnport;
  19. }

最后我们就可以编写消费者请求接口去测试调用服务结果了。

  1. @RestController
  2. @Slf4j
  3. @RequestMapping("/feign")
  4. public class OrderFeinController {
  5. @Resource
  6. private TestFeignService testFeignService;
  7. @GetMapping("/getService/{id}")
  8. public Result<Payment> getOne(@PathVariable("id")Integer id){
  9. return testFeignService.getOne(id);
  10. }
  11. @GetMapping("/timeout")
  12. public String feignTimeout(){
  13. return testFeignService.timeout();
  14. }
  15. }

db9ace9b37a98f3690b3b56eca9b2f66.png

并且日志增强后的服务执行日志也成功打印出来了

  1. 2023-01-06 20:39:18.708 INFO 3860 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient : Application version is -1: false
  2. 2023-01-06 20:39:18.708 INFO 3860 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient : Getting all instance registry info from the eureka server
  3. 2023-01-06 20:39:18.729 INFO 3860 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient : The response status is 200
  4. 2023-01-06 20:39:48.979 DEBUG 3860 --- [p-nio-80-exec-9] com.yy.service.TestFeignService : [TestFeignService#getOne] ---> GET http://CLOUD-PAYMENT-SERVICE/payment/1 HTTP/1.1
  5. 2023-01-06 20:39:48.979 DEBUG 3860 --- [p-nio-80-exec-9] com.yy.service.TestFeignService : [TestFeignService#getOne] ---> END HTTP (0-byte body)
  6. 2023-01-06 20:39:49.289 DEBUG 3860 --- [p-nio-80-exec-9] com.yy.service.TestFeignService : [TestFeignService#getOne] <--- HTTP/1.1 200 (309ms)
  7. 2023-01-06 20:39:49.290 DEBUG 3860 --- [p-nio-80-exec-9] com.yy.service.TestFeignService : [TestFeignService#getOne] connection: keep-alive
  8. 2023-01-06 20:39:49.290 DEBUG 3860 --- [p-nio-80-exec-9] com.yy.service.TestFeignService : [TestFeignService#getOne] content-type: application/json
  9. 2023-01-06 20:39:49.290 DEBUG 3860 --- [p-nio-80-exec-9] com.yy.service.TestFeignService : [TestFeignService#getOne] date: Fri, 06 Jan 2023 12:39:49 GMT
  10. 2023-01-06 20:39:49.290 DEBUG 3860 --- [p-nio-80-exec-9] com.yy.service.TestFeignService : [TestFeignService#getOne] keep-alive: timeout=60
  11. 2023-01-06 20:39:49.290 DEBUG 3860 --- [p-nio-80-exec-9] com.yy.service.TestFeignService : [TestFeignService#getOne] transfer-encoding: chunked
  12. 2023-01-06 20:39:49.290 DEBUG 3860 --- [p-nio-80-exec-9] com.yy.service.TestFeignService : [TestFeignService#getOne]
  13. 2023-01-06 20:39:49.300 DEBUG 3860 --- [p-nio-80-exec-9] com.yy.service.TestFeignService : [TestFeignService#getOne] {"code":200,"message":"调用的eureka服务端口号为8001","data":{"id":1,"serial":"001"},"type":"success"}
  14. 2023-01-06 20:39:49.300 DEBUG 3860 --- [p-nio-80-exec-9] com.yy.service.TestFeignService : [TestFeignService#getOne] <--- END HTTP (110-byte body)

发表评论

表情:
评论列表 (有 0 条评论,154人围观)

还没有评论,来说两句吧...

相关阅读