当前位置: 代码迷 >> 综合 >> [SpringCould篇]之服务消费方式Ribbon+RestTemplate
  详细解决方案

[SpringCould篇]之服务消费方式Ribbon+RestTemplate

热度:65   发布时间:2023-12-03 19:38:57.0

1.前言

1.1调用服务的方式

Spring-Cloud调用服务有两种方式

  • Ribbon+RestTemplate
  • Feign

Ribbon是一个基于HTTP和TCP客户端的负载均衡器,feign默认集成了ribbon。
本文主介绍Ribbon方式消费者
相关依赖版本

  • spring-boot - 2.1.4.RELEASE
  • spring-cloud - Greenwich.SR1

2.Maven依赖

  <parent><artifactId>spring-could-example</artifactId><groupId>com.example</groupId><version>1.0.0</version></parent><modelVersion>4.0.0</modelVersion><artifactId>server-consum-ribbon</artifactId><description>服务消费者 以Ribbon 方式调用</description><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-ribbon</artifactId></dependency></dependencies>

3.配置文件

server:port: 8703#应用名称
spring:application:name: server-consum-ribbon# 注册中心配置
eureka:client:serviceUrl:# 配置服务注册中心集群时,此处可以配置多个地址(通过逗号隔开)defaultZone: http://127.0.0.1:7001/eureka/

4.初始化Rest模板与启动类

/*** Ribbon 方式 服务消费者-实例** @author 程序员小强*/
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class RibbonApplication {
    public static void main(String[] args) {
    SpringApplication.run(RibbonApplication.class, args);}/*** 初始化 Rest模板 bean*/@Bean@LoadBalancedRestTemplate restTemplate() {
    return new RestTemplate();}}

5.Service-Api接口

创建测试类HelloService,通过restTemplate来消费生成者服务的“/hello”接口,
可以直接用服务名,替代了具体的url地址,Ribbon会根据服务名来选择具体的服务实例,根据服务实例在请求的时 候会用具体的url替换掉服务名。

@Service
public class HelloServiceApi {
    @Autowiredprivate RestTemplate restTemplate;public Map<String, Object> hello(String consumerApplicationName) {
    return restTemplate.getForObject("http://server-provider/hello?consumerApplicationName=" + consumerApplicationName, Map.class);}
}

注 : restTemplate.getForObject 中参数http://server-provider/hello就是请求地址。

6.Controller测试

在controller中用调用HelloService.hello方法

/*** 服务消费者-测试 controller** @author 程序员小强*/
@RestController
public class TestController {
    private static final Logger log = LoggerFactory.getLogger(TestController.class);@Value("${spring.application.name}")private String applicationName;@Autowiredprivate HelloServiceApi helloService;@RequestMapping("/hello")public Map<String, Object> hello() {
    log.info(" [ Ribbon 消费者 {} ] >> hello method start ", applicationName);Map<String, Object> resultMap = helloService.hello(applicationName);log.info(" [ Ribbon 消费者 {} ] >> hello method end , resultMap: {}", applicationName, resultMap);return resultMap;}
}

7.测试负载均衡

为了方便测试负载均衡,启动了两个生产者服务,端口分别是8701、9701
![image.png](https://img-blog.csdnimg.cn/img_convert/84e227af951e744ffc146b0bacaadbcf.png#align=left&display=inline&height=219&margin=[object Object]&name=image.png&originHeight=438&originWidth=2544&size=321206&status=done&style=none&width=1272)
架构图
在这里插入图片描述

请求测试在这里插入图片描述
由上图可以看到,连续两次请求,分别路由到了不同的端口服务上。
这说明负载均衡生效了。

  • 当server-consum-ribbon 通过restTemplate调用server-provider的hello接口时.因为用ribbon进行了负载均衡,会轮流的调用service-hi:8762和8763 两个端口的hi接口;

7.源码地址

传送门

关注程序员小强公众号更多编程趣事,知识心得与您分享
在这里插入图片描述

  相关解决方案