【笔记】SpringCloud(3)之Spring Cloud Feign (声明式服务调用)

前言

整合了Spring Cloud Ribbon 、Spring Cloud Hystrix,提供了声明式的web服务客户端定义方式;

使用

  1. 创建Spring Boot 项目,引入spring-cloud-starter-feign、spring-cloud-starter-eureka-server;
  2. 应用主类添加@@EnableFeignClients 开启Feign;
  3. Service接口定义使用@FeignClient注解指定服务名、绑定服务;
  4. Controller 中@Autowried注入实例;
  5. 同Ribbon 中的服务消费者一样,指定服务注册中心,定义自己的服务名和端口号;

Service接口

@FeignClient(name = "hello-service",fallback = HelloServiceFallback.class) //通过注解绑定服务名
public interface HelloService {

    @RequestMapping("/hello") //该服务提供的REST接口
    String hello();

    @RequestMapping(value = "/hello1",method = RequestMethod.GET)
    String hello(@RequestParam("name") String name);

    @RequestMapping(value = "/hello2",method = RequestMethod.GET)
    User hello(@RequestHeader("name") String name,@RequestHeader("id") String id);

    @RequestMapping(value = "/hello3",method = RequestMethod.POST)
    String hello(@RequestBody User user);


}

实现类服务降级

//针对Helloervice 的服务降级
@Component
public class HelloServiceFallback  implements HelloService {
//方法
}

Controller控制层

@RestController
public class ConsumerController {
    @Autowired HelloService helloService;
//....
}

参数绑定

使用@RequestBody、@RequestParam、@RequestHeader等指定参数名称的注解,它们的@RequestMapping 的value 不能缺少;

继承特性(抽取公有复用)

  1. 为了能够复用DTO和接口定义,创建基本的Maven工程,命名为hello-service-api;
  2. 需要用到SpringMvc的注解 加入spring-boot-starter-web依赖
  3. 创建公用的bean实体类和Service接口(接口需要添加@RequestMapping),接口中定义相关方法和注解;
  4. 重构服务提供者
    1. 本项目依赖添加到其他项目pom.xml中(这里添加到Spring-Hello项目中);
    2. 服务提供者项目中创建RefactoeHelloController类实现hello-service-api中的Service接口
  5. 重构服务消费者
    1. 将hello-service-api依赖添加到Feign项目中
    2. 创建RefactorHelloService接口,并继承hello-service-api的接口,添加@FeignClient注解绑定服务
    3. Controller中注入RefactorHelloService接口使用

抽取公有

@RequestMapping("/refactor")
public interface HelloService {
    @RequestMapping(value = "/hello4",method = RequestMethod.GET)
    String hello(@RequestParam("name") String name);
.....
}

重构服务提供者

@RestController
public class RefactoeHelloController implements HelloService {
    @Override
    public String hello(String name) {
        return "hello "+ name;
    }
...
}

重构服务消费者

//RefactorHelloService  
@FeignClient(value = "HELLO-SERVICE")
public interface RefactorHelloService  extends com.space.service.HelloService {
}


@RestController
public class ConsumerController {
    @Autowired RefactorHelloService refactorHelloService;
....
}

Ribbon配置

  1. Spring Cloud Feign得客户端负载均衡是通过Spring Cloud Ribbon 实现的;
  2. 全局配置 ribbon. = < value>
  3. 指定服务配置 .ribbon.key = < value>

重试机制

  1. 服务名.ribbon.MaxAutoRetries = 1 重试策略,失败一次后更换访问实例;
  2. 服务名.ribbon.MaxAutoRetriesNextServes = 2 更换访问实例得次数
  3. 区别Ribbon超时和Hystrix超时

Hystrix配置

  1. feign.hystrix.enabled=true 设置hystrix可用
  2. 通过全局配置可进行配置
  3. 针对某个服务单独配置一个关闭Hystrix的A类,进行关闭Hystrix,在@FeignClient中的configuations配置A.class
  4. 指令命令配置 hystrix.command. Feign客户端的方法名

服务降级配置

  1. 为Feign客户端定义接口写一个具体的实现类,加上@Component,重写方法的实现逻辑,实现服务降级;
  2. @FeignClient 中的fallback指定上述的实现类;

其他配置

  1. 请求压缩、响应压缩
  2. 日志配置
#设置接口指定日志级别
logging.level.com.feign.demo.service.HelloService = debug
//应用主类添加bean
 @Bean
  Logger.Level feignLoggerLevel(){
      return Logger.Level.FULL;
  }

在这里插入图片描述

参考书籍《Spring Cloud 微服务实战》

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值