SpringCloud 2020.0.4 系列之 Feign

1. 概述

老话说的好:任何问题都有不止一种的解决方法,当前的问题没有解决,只是还没有发现解决方法,而并不是无解。

 

言归正传,之前我们聊了 SpringCloud 的服务治理组件 Eureka,今天我们来聊聊服务间的通信组件 Feign。

闲话不多说,直接上代码。

 

2. my-eureka-client 工程中增加Service方法

2.1 概述

在 my-eureka-client 工程中,创建 Service 方法,然后新增一个 my-feign 服务使用 Feign 组件远程调用这个方法。

关于 my-eureka-client 的搭建,可参见我的上一篇文章《SpringCloud 2020.0.4 系列之Eureka》(https://www.cnblogs.com/w84422/p/15449221.html)。

 

2.2 增加接口 EurekaClientService

@RequestMapping("/api")
public interface EurekaClientService {

    @PostMapping("/hello")
    String hello(@RequestParam(required = false) String name,
                  @RequestParam(required = false) Integer age);
}

注意:由于此 Service 方法,我们需要提供给其他服务远程调用,因此需要使用 @RequestMapping 指定他的请求资源路径。

 

2.3 增加实现类 EurekaClientServiceImpl 

@RestController
public class EurekaClientServiceImpl implements EurekaClientService{

    @Value("${server.port}")
    private String port;

    @Override
    public String hello(String name, Integer age) {

        System.out.println("-------------- my-eureka-client service---------------");

        System.out.println(name);
        System.out.println(age);
        System.out.println(port);

        System.out.println("-------------- my-eureka-client service---------------");

        return port;
    }
}

注意:由于此 Service 方法,我们需要提供给其他服务远程调用,因此这里不能再用 @Service 注解了,而要使用 @RestController 注解,将其作为 Rest 接口暴露出去。

 

3. my-feign 工程的搭建

3.1 主要依赖

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 健康检查 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

 

3.2 主要配置及 Feign 超时策略配置

spring:
  application:
    name: my-feign
server:
  port: 37000
eureka:
  client:
    service-url:
      defaultZone: http://zhuifengren1:35000/eureka/,http://zhuifengren2:35001/eureka/    # Eureka Server的地址
    healthcheck:
      enabled: true    # 开启健康检查, 依赖于 spring-boot-starter-actuator
  instance:
    lease-renewal-interval-in-seconds: 5      # 发出续约指令的间隔,默认30秒
    lease-expiration-duration-in-seconds: 30  # 租期到期时间,默认90秒


# feign 配置
feign:
  client:
    config:
      # 全局配置
      default:
        connectTimeout: 1000   # 连接超时时间,单位ms
        readTimeout: 3000      # 获取Response响应超时时间,单位ms

      # 针对 my-eureka-client 的 feign 配置,优先级高于全局配置
      my-eureka-client:
        connectTimeout: 300   # 连接超时时间,单位ms
        readTimeout: 2000     # 获取Response响应超时时间,单位ms

 

3.3 Feign 重试策略配置

增加 FeignConfigure 类

@Configuration
public class FeignConfigure {

    @Bean
    public Retryer feignRetryer(){

        // 第一个参数:重试的时间间隔,单位毫秒,每次会增加1.5倍,但会小于等于最大间隔
        // 第二个参数:发起当前请求的最大时间间隔,单位毫秒
        // 第三个参数:重试次数,包括第一次调用,每次重试可能会请求不同服务节点
        return new Retryer.Default(100, 1000, 3);
    }
}

 

3.4 增加 Feign 接口 

@FeignClient("my-eureka-client")  // value 为被调用服务的名称
@RequestMapping("/api")
public interface EurekaClientService {

    @PostMapping("/hello")
    String hello(@RequestParam(required = false) String name,
                  @RequestParam(required = false) Integer age);
}

此接口可以由调用方根据实际情况编写。也可由被调用方将Service接口层剥离为单独的模块提供,调用方依赖即可。

 

3.5 将 Feign 接口类注入到调用方的 Controller 类或 Service 类中

    @Autowired
    private EurekaClientService eurekaClientService;

    public String hello(String name, Integer age) {
        System.out.println("------------ my-feign service ------------");
        return eurekaClientService.hello(name, age);
    }

 

3.6 启动类增加 @EnableFeignClients 注解

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class MyFeignApplication {

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

 

3.7 启动服务

先启动 Eureka Server,再启动 my-eureka-client,最后启动 my-feign。

 

4. 综述

今天聊了一下 Feign 的相关知识,希望可以对大家的工作有所帮助。

欢迎帮忙点赞、评论、转发、加关注 :)

关注追风人聊Java,每天更新Java干货。

 

5. 个人公众号

追风人聊Java,欢迎大家关注

posted @ 2021-10-26 14:11  追风人聊Java  阅读(533)  评论(0编辑  收藏  举报