前提
服务注册中心Nacos+服务提供者
Sentinel
会搭建SpringBoot(2.x)+web
服务消费者:OpenFeign使用
代码下载:https://github.com/cbeann/share/tree/master/nacos-sentinel-openfeign
在SpringBoot(2.2.2)+web的基础之上
添加pom依赖
<!-- SpringCloud alibaba nacos --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>2.1.0.RELEASE</version> </dependency> <!-- sentinel --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> <version>2.1.0.RELEASE</version> </dependency> <!--<!– openfeign –>--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>2.2.1.RELEASE</version> </dependency>
修改application.yml文件
server: port: 80 spring: application: name: sentinel-ribbon-openFeign cloud: nacos: discovery: #Nacos注册中心地址 server-addr: 39.105.30.146:8848 sentinel: transport: #dashboard地址 dashboard: localhost:8080 port: 8719 #默认端口,如果被占用则从8719依次+1扫描 ###sentinel可以监控到此服务 management: endpoints: web: exposure: include: "*" ###激活Sentinel对openFeign的支持 feign: sentinel: enabled: true
修改主启动类
@EnableDiscoveryClient @EnableFeignClients
编写service层(☆☆☆☆☆),虽然是一个接口,但是可以被注入到controller中
@FeignClient(value = "provider",//服务名称 fallback = HelloFallbackService.class)//回调方法 public interface HelloService { @GetMapping(value = "/provider/get/{id}") String PaymentSQL(@PathVariable("id") Long id); }
@Component public class HelloFallbackService implements HelloService { @Override public String PaymentSQL(Long id) { return "------fallback"; } }
编写控制层
@RestController public class HelloController { @Autowired private HelloService helloService; @GetMapping(value = "/consumer/get/{id}") public String paymentSQL(@PathVariable("id") Long id) { return helloService.PaymentSQL(id); } }
OpenFeign的超时控制
如下面代码所示,当请求/consumer/payment/feign/timeout 时,调用的微服务是sleep三秒后在返回结果,但是Feign规定一秒请求不到结果就报错,因为我们需要修改配置让其支持更久的响应时间。
@RestController public class OrderFeignController { @Autowired private PaymentFeignService paymentFeignService; @GetMapping(value = "/consumer/payment/feign/timeout") public String paymentFeignTimeout(){ String s = paymentFeignService.paymentFeignTimeout(); return s; } }
修改办法
修改application.yml配置文件
#设置feign 客户端超时时间(openFeign默认支持ribbon) ribbon: #指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间 ReadTimeout: 5000 #指的是建立连接后从服务器读取到可用资源所用的时间 ConnectTimeout: 5000
OpenFeign的日志增强
添加OpenFeign的日志类
import feign.Logger; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @author CBeann * @create 2020-04-08 23:25 */ @Configuration public class FeignConfig { @Bean Logger.Level feignLoggerLevel(){ return Logger.Level.FULL; } }
修改yml文件
logging: level: #feign日志以什么级别监控哪个接口 #feign日志以debug级别监控com.imooc.springcloud.service.PaymentFeignService com.imooc.springcloud.service.PaymentFeignService: debug
总结
OpenFeign和原来的Feign用法差不多,就是添加的依赖不一样
原来是
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency>
现在是
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>