spring cloud: 二、服务的消费(ribbon和feign两种方式)

左手的ㄟ右手 2024-02-18 18:31 41阅读 0赞

上一篇文章总结了在分布式服务系统中,服务是如何在服务中心注册与发现的,这篇文章主要总结一下注册的服务之间是怎么调用的。传统的一个项目当中,通常会有系统管理、用户管理、角色管理、人员管理等等这些模块,在分布式的系统当中,以往的同一个项目里的每一个功能模块可以相应独立拆分成一个项目也就是一个服务,然后这些服务在服务中心注册,服务与服务之间不直接调用,而通过服务中心来调用。

Spring cloud 服务间调用一般有两种方式,第一种:ribbon+restTemplate第二种:fegin

第一种ribbon+restTemplate:

Ribbon是一个负载均衡客户端,可以很好的控制http和tcp的一些行为。Feign默认集成了ribbon。

新建一个maven项目:zhangsanService,作为服务消费者,去调用上一篇文章注册到服务中心的helloService.

Pom.xml:

<?xml version=”1.0” encoding=”UTF-8”?>


4.0.0

com.gaox.zhangsanService
zhangsanService
0.0.1-SNAPSHOT
jar

zhangsanService
Demo project for Spring Boot


org.springframework.boot
spring-boot-starter-parent
1.5.10.RELEASE




UTF-8
UTF-8
1.8
Edgware.SR2




org.springframework.boot
spring-boot-starter-web


org.springframework.cloud
spring-cloud-starter-netflix-eureka-client


org.springframework.cloud
spring-cloud-starter-netflix-ribbon



org.springframework.boot
spring-boot-starter-test
test






org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import







org.springframework.boot
spring-boot-maven-plugin






spring-milestones
Spring Milestones
https://repo.spring.io/milestone

false





Application.properties:

  1. server.port=8764
  2. spring.application.name=zhangsanService
  3. eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

ZhangsanServiceApplication.java

  1. package com.gaox.zhangsanService;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.cloud.client.loadbalancer.LoadBalanced;
  6. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RestController;
  10. import org.springframework.web.client.RestTemplate;
  11. @SpringBootApplication
  12. @EnableEurekaClient
  13. @RestController
  14. public class ZhangsanServiceApplication {
  15. public static void main(String[] args) {
  16. SpringApplication.run(ZhangsanServiceApplication.class, args);
  17. }
  18. @Bean
  19. @LoadBalanced
  20. public RestTemplate restTemplate(){
  21. return new RestTemplate();
  22. }
  23. @Autowired
  24. private RestTemplate restTemplate;
  25. @RequestMapping("/hello")
  26. public String hello(String name){
  27. String result=restTemplate.getForObject("http://HELLOSERVICE/hello?name="+name,String.class);
  28. return result;
  29. }
  30. }

依次启动serviceCenter,helloService:8762,helloService:8763,zhangsanService,在浏览器上多次访问http://localhost:8764/hello?name=zhangsan,浏览器交替显示:

你好,zhangsan。我是helloService,端口是8762

你好,zhangsan。我是helloService,端口是8763

这说明当我通地调用restTemplate.getForObject(http://HELLOSERVICE/hello?name=+zhangsan,String.class)方法时,已经作了负载均衡,访问了不同端口的服务实例。

这时项目的架构就是

一个服务注册中心:serviceCenter,端口8761

在服务注册中心注册的两个helloService实例,端口分别是8762和8763

在服务注册中心注册的另一个实例:zhangsanService,端口8764

当zhangsanService通过restTemplate调用helloService的hello接口时,因为用ribbon进行了负载均衡,所以才会出现轮流的调用8762和8763两个端口的helloService实例。

第二种方法feign:

新建一个lisiService,通过feign的方法去调用上一篇文章在服务注册中心注册的helloService。

Feign是一个声明式的伪http客户端,它使得写http客户端变得更简单,使用fegin,只需要创建一个接口,并注解,它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解。Feign支持可插拔的编码器和解码器。Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果。

Pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.gaox.lisiService</groupId>
  6. <artifactId>lisiService</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <name>lisiService</name>
  10. <description>Demo project for Spring Boot</description>
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>1.5.10.RELEASE</version>
  15. <relativePath/> <!-- lookup parent from repository -->
  16. </parent>
  17. <properties>
  18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  20. <java.version>1.8</java.version>
  21. <spring-cloud.version>Edgware.SR2</spring-cloud.version>
  22. </properties>
  23. <dependencies>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-web</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.cloud</groupId>
  30. <artifactId>spring-cloud-starter-eureka</artifactId>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework.cloud</groupId>
  34. <artifactId>spring-cloud-starter-feign</artifactId>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.springframework.boot</groupId>
  38. <artifactId>spring-boot-starter-test</artifactId>
  39. <scope>test</scope>
  40. </dependency>
  41. </dependencies>
  42. <dependencyManagement>
  43. <dependencies>
  44. <dependency>
  45. <groupId>org.springframework.cloud</groupId>
  46. <artifactId>spring-cloud-dependencies</artifactId>
  47. <version>${spring-cloud.version}</version>
  48. <type>pom</type>
  49. <scope>import</scope>
  50. </dependency>
  51. </dependencies>
  52. </dependencyManagement>
  53. <build>
  54. <plugins>
  55. <plugin>
  56. <groupId>org.springframework.boot</groupId>
  57. <artifactId>spring-boot-maven-plugin</artifactId>
  58. </plugin>
  59. </plugins>
  60. </build>
  61. </project>

Application.properties

  1. server.port=8765
  2. spring.application.name=lisiService
  3. eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

LisiServiceApplication.java

  1. package com.gaox.lisiService;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  6. import org.springframework.cloud.netflix.feign.EnableFeignClients;
  7. import org.springframework.cloud.netflix.feign.FeignClient;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RequestMethod;
  10. import org.springframework.web.bind.annotation.RequestParam;
  11. import org.springframework.web.bind.annotation.RestController;
  12. @SpringBootApplication
  13. @EnableEurekaClient
  14. @EnableFeignClients
  15. @RestController
  16. public class LisiServiceApplication {
  17. public static void main(String[] args) {
  18. SpringApplication.run(LisiServiceApplication.class, args);
  19. }
  20. @FeignClient("helloService")
  21. public interface HelloService {
  22. @RequestMapping(value = "/hello", method = RequestMethod.GET)
  23. public String hello(@RequestParam("name") String name);
  24. }
  25. @Autowired
  26. private HelloService helloService;
  27. @RequestMapping("/hello")
  28. public String hello(@RequestParam("name")String name){
  29. return helloService.hello(name);
  30. }
  31. }

依次启动serviceCenter,helloService:8762,helloService:8763,lisiService,多次访问http://localhost:8765/hello?name=lisi,浏览器会交替显示

你好,lisi。我是helloService,端口是8762

你好,lisi。我是helloService,端口是8763

这说明feign确实默认集成了ribbon,实现了负载均衡。

发表评论

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

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

相关阅读