@Spring Cloud | NO.2 - 服务的消费者 [Ribbon & Feign]

本文继@Spring Cloud | NO.1 – 服务的注册与发现 [Eureka],上一篇已经开启了一个服务注册中心并已注册服务提供者的服务到注册中心,本章主要讲如何调用注册中心的服务。

在SpringCloud微服务架构中,服务与服务之间的通信基于
HTTP RESTFUL

服务的消费者-Ribbon

Ribbon是一个客户端负载均衡器,它可以很好地控制HTTP和TCP客户端的行为。Ribbon中的中心概念是指定客户端的概念。每个负载平衡器是组合的组合的一部分,它们一起工作以根据需要联系远程服务器,并且集合具有您将其作为应用程序开发人员(例如使用@FeignClient注释)的名称。

——官方直译

项目集成

1. pom.xml引入Maven依赖

<parent>
    <!-- spring boot -->
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <!-- netflix-eureka-client-->
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
        <!-- netflix-ribbon -->
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <!-- spring cloud -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Finchley RC1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

注意:
Ribbon引入的Maven依赖中
artifactId有变动,
SpringBoot2.0之前的版本为
spring-cloud-starter-ribbon

2. 添加注解支持

@SpringBootApplication
@EnableDiscoveryClient
public class RibbonServiceApplication {

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

    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

通过
@EnableDiscoveryClient向服务中心注册

通过
@Bean注入一个
RestTemplate Bean,并通过
@LoadBalanced注解表明这个
restRemplate开启负载均衡的功能

3. application.yml配置

spring:
  application:
    name: service-ribbon
server:
  port: 8764
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

将名称为
service-ribbon的服务注册到注册中心,端口为
8764

4. 服务调用

@Service
public class HelloService {

    @Autowired
    RestTemplate restTemplate;

    public String hiService(String name) {
        return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);
    }

}

与正常的
RestTemplate调用服务不同的是将请求
服务的url替换成了具体的
服务名称,也就是注册在注册中心的服务名称,如果相同的服务名称在注册中心存在不同端口的注册,那么在这里会做客户端的负载均衡。

至此Ribbon的配置完毕。

此时,我们写一个Controller来展示服务调用的结果。

@RestController
public class HelloControler {

    @Autowired
    HelloService helloService;
    
    @RequestMapping(value = "/hi")
    public String hi(@RequestParam String name){
        return helloService.hiService(name);
    }
}

4. 运行概览图

启动一个服务注册中心,两个服务提供者,一个服务消费者(Ribbon)

《@Spring Cloud | NO.2 - 服务的消费者 [Ribbon & Feign]》

多次访问 http://127.0.0.1:8764/hi?name…,浏览器交替显示:

hi forezp,i am from port:8762

hi forezp,i am from port:8763

服务的消费者-Feign

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

——官方直译

项目集成

1. pom.xml引入Maven依赖

<parent>
    <!-- spring boot -->
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <!-- netflix-eureka-client-->
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <!-- spring cloud -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Finchley RC1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

注意:
Feign引入的Maven依赖中
artifactId有变动,
SpringBoot2.0之前的版本为
spring-cloud-starter-feign

2. 添加注解支持

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class FeignServiceApplication {

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

通过
@EnableDiscoveryClient向服务中心注册

通过
@EnableFeignClients注解开启Feign的功能

3. application.yml配置

spring:
  application:
    name: service-feign
server:
  port: 8765

cage:
  feign-client:
    base-packages: ltd.ueic,ltd.ueic.cages

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

将名称为
service-feign的服务注册到注册中心,端口为
8765

4. 服务调用

定义一个接口来调用服务

@FeignClient(value = "service-hi")
public interface SchedualServiceHi {
    @RequestMapping(value = "/hi",method = RequestMethod.GET)
    String sayHiFromClientOne(@RequestParam(value = "name") String name);
}

通过
@FeignClient制定调用的服务名称

通过在接口上声明
@RequestMapping指明调用服务的接口地址

至此Feign的配置完毕。

此时,我们写一个Controller来展示服务调用的结果。

@RestController
public class HiController {

    @Autowired
    SchedualServiceHi schedualServiceHi;
    
    @RequestMapping(value = "/hi",method = RequestMethod.GET)
    public String sayHi(@RequestParam String name){
        return schedualServiceHi.sayHiFromClientOne(name);
    }
}

4. 运行概览图

启动一个服务注册中心,两个服务提供者,一个服务消费者(Ribbon),一个服务消费者(Feign)

《@Spring Cloud | NO.2 - 服务的消费者 [Ribbon & Feign]》

多次访问 http://localhost:8765/hi?name…,浏览器交替显示:

hi forezp,i am from port:8762

hi forezp,i am from port:8763

附加说明

  1. 本文参考资料
    原文作者:看不见的未来
    原文地址: https://segmentfault.com/a/1190000014827953
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞