Spring Cloud学习笔记(四)负载均衡客户端Ribbon

在Spring Cloud的体系中,实现服务调用的负载均衡有两种方式,一种是让你的RestTemplate有用负载均衡的功能,也就是ribbon + RestTemplate的方式;另外就是使用feign客户端。

ribbon目前进入维护期,官方推荐的是:BlockingLoadBalancerClient ,但是网上看了某大牛的分析(https://my.oschina.net/giegie/blog/3095354),还是推荐消停用ribbon先

In order to use a load-balanced RestTemplate, you need to have a load-balancer implementation in your classpath. The recommended implementation is BlockingLoadBalancerClient - add org.springframework.cloud:spring-cloud-loadbalancer in order to use it. The RibbonLoadBalancerClient also can be used, but it’s now under maintenance and we do not recommend adding it to new projects.

RestTemplate通过注解配置可以直接拥有负载均衡的功能。

RestTemplate can be automatically configured to use a Load-balancer client under the hood. To create a load-balanced RestTemplate, create a RestTemplate@Bean and use the @LoadBalanced qualifier

先基于之前的工程改造代码,变成这个样子

其中service-consumer是服务调用方,service-provider-a和service-provider-b是服务提供方。其中两个service-provider的配置文件相同,只有端口不同,服务名称也叫做一个,如下图:

server:
  port: 9001 #另外一个为9002
spring:
  application:
    name: service-provider #应用名称,会显示在eureka server中

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8100/eureka/ #eureka server地址

接下来编写服务提供方的接口从而对外提供服务。代码如下,输出服务提供方端口号。

package org.dothwinds.serviceprovider.controller;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ServiceProviderController {
    @Value("${server.port}")
    private int port;

    @GetMapping("/service")
    public String sayHello(){
        return "service from port:" + port;
    }
}

启动eureka server和两个服务提供方,查看服务注册的情况。

没有问题,接下来我们编写一下service-consumer的代码。启动类,注入RestTemplate,加上对应的注解。

package org.dothwinds.serviceconsumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableEurekaClient
public class SpringCloudStudyServiceConsumerApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringCloudStudyServiceConsumerApplication.class, args);
	}

	//注入restTemplate
	@Bean
	@LoadBalanced
	public RestTemplate getRestTemplate(){
		return new RestTemplate();
	}

}

编写Controller,调用服务测试,代码如下:

package org.dothwinds.serviceconsumer.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class ServiceConsumerController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/testLoadBalance")
    public String serviceCousumer(){
       return restTemplate.getForObject("http://SERVICE-PROVIDER/service", String.class);
    }
}

启动Consumer服务测试:

重复刷新页面,可以看到9001和9002交替显示出来,说明consumer的调用负载均衡生效了,每次请求到了不同的service provider上。
 

此时的架构流程,如下图:

参考资料:https://cloud.spring.io/spring-cloud-static/Greenwich.SR5/single/spring-cloud.html

代码地址:https://gitee.com/dothwinds/Spring-Cloud-Study/tree/master/spring-cloud-study-ribbon

发布了18 篇原创文章 · 获赞 33 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Dothwinds/article/details/105044642