0
点赞
收藏
分享

微信扫一扫

Sentinel 服务熔断 OpenFeign

玉新行者 2022-08-30 阅读 85

Feign组件一般是消费侧

1.POM中增加openfeign引入

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud-nacos</artifactId>
<groupId>com.ckfuture.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>cloudalibaba-consumer-nacos-order84</artifactId>

<dependencies>
<!--SpringCloud alibaba nacos-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--SpringCloud alibaba sentinel-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!--SpringCloud openfeign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--web+actuator-->
<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>
</dependencies>

</project>

Sentinel 服务熔断 OpenFeign_xml

 

 

2.YML中激活Sentinel对Feign的支持

server:
port: 84
spring:
application:
name: nacos-order-consumer
cloud:
nacos:
discovery:
server-addr: localhost:8848
sentinel:
transport:
#配置Sentinel dashboard地址
dashboard: localhost:8080
port: 8719

#消费者将要去访问的微服务名称(注册成功进nacos的微服务提供者)
service-url:
nacos-user-service: http://nacos-payment-provider
#激活Sentinel 对Feign的支持
feign:
sentinel:
enabled: true

Sentinel 服务熔断 OpenFeign_ide_02

 

3.主启动类激活@EnableFeignClients

package com.ckfuture.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class OrderNacosMain84 {
public static void main(String[] args) {
SpringApplication.run(OrderNacosMain84.class,args);
}
}

Sentinel 服务熔断 OpenFeign_xml_03

 

 4.新建带@FeignClient注解的业务接口

Feign就是接口+注解

接口中方法调用地址要和生产者中的调用地址一样。参考Feign调用方式

 

package com.ckfuture.springcloud.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
* @author ckfuture
*/
@FeignClient(value = "nacos-payment-provider")
public interface PaymentService {

@GetMapping(value = "/payment/nacos/{id}")
public String getPayment(@PathVariable("id") Integer id);

}

 

Sentinel 服务熔断 OpenFeign_spring_04

 

 5.创建Feign兜底类

创建名为“PaymentFallbackService”的类,实现PaymentService接口方法

package com.ckfuture.springcloud.service;

import org.springframework.stereotype.Component;

/**
* Feign
* 兜底类
*/
@Component
public class PaymentFallbackService implements PaymentService{
@Override
public String getPayment(Integer id) {
return "服务降级返回,---PaymentFallbackService";
}
}

6.接口中添加fallback(兜底)方法

package com.ckfuture.springcloud.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
* @author ckfuture
*/
@FeignClient(value = "nacos-payment-provider",fallback = PaymentFallbackService.class)
public interface PaymentService {

@GetMapping(value = "/payment/nacos/{id}")
public String getPayment(@PathVariable("id") Integer id);
}

Sentinel 服务熔断 OpenFeign_xml_05

7.添加controller方法

//=============OpenFeign============
@Resource
private PaymentService paymentService;

@GetMapping(value = "/consumer/payment/nacos/{id}")
public String getPayment(@PathVariable("id") Integer id){
return paymentService.getPayment(id);
}

8.测试

启动9001生产者微服务,在Nacos中查看。

Sentinel 服务熔断 OpenFeign_spring_06

 

 启动84端口的消费者,在Nacos中查看

Sentinel 服务熔断 OpenFeign_xml_07

 

 

Sentinel 服务熔断 OpenFeign_xml_08

 

 此时故意关闭9001端口微服务,再次请求查看效果。

Sentinel 服务熔断 OpenFeign_xml_09

 



举报

相关推荐

0 条评论