1.nacos安装
请参考 : docker 安装 nacos 1.X
2.项目父工程
请参考 : SpringCloud H系列 alibaba 2.1.1 (一) nacos1.X 注册中心 服务端
3.项目结构
4.创建Feign 服务调用端
pom.xml
<parent>
<groupId>com.alibaba</groupId>
<artifactId>spring-cloud-hoxton</artifactId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>alibaba-sentinel-feign-server</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
SentinelFeignServerApplication.java 服务提供端启动类
/**
* @author Lion Li
*/
@EnableDiscoveryClient // 启动Feign客户端
@SpringBootApplication
public class SentinelFeignServerApplication {
public static void main(String[] args) {
SpringApplication.run(SentinelFeignServerApplication.class, args);
}
}
TestController.java 接口提供
/**
* @author Lion Li
*/
@RestController
public class TestController {
@PostMapping("/test")
public String test(@RequestParam("name") String name) {
return "服务提供端::返回值 => " + name;
}
}
application.yml 配置文件
spring:
application:
name: alibaba-sentinel-feign-server
cloud:
nacos:
discovery:
server-addr: 192.168.101.11:8848
server:
port: 8000
启动服务提供端 查看nacos
注册成功
5.创建Feign 服务消费端
pom.xml
<parent>
<groupId>com.alibaba</groupId>
<artifactId>spring-cloud-hoxton</artifactId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>alibaba-sentinel-feign-client</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-transport-simple-http</artifactId>
</dependency>
</dependencies>
SentinelFeignClientApplication.java 服务消费端启动类
/**
* @author Lion Li
*/
@EnableFeignClients // 启动Feign客户端
@SpringCloudApplication
public class SentinelFeignClientApplication {
public static void main(String[] args) {
SpringApplication.run(SentinelFeignClientApplication.class, args);
}
}
TestService.java Feign远程接口调用
/**
* @author Lion Li
*/
@FeignClient(name = "alibaba-sentinel-feign-server",
fallback = TestServiceFallback.class)
public interface TestService {
/**
* 调用服务提供端的接口.
*/
@PostMapping("/test")
String test(@RequestParam("name") String name);
}
TestServiceFallback.java 接口降级响应
/**
* @author Lion Li
* sentinel 降级处理
*/
@Component
public class TestServiceFallback implements TestService {
@Override
public String test(String name) {
return "服务消费端::降级";
}
}
TestController.java web接口
/**
* @author Lion Li
*/
@RestController
public class TestController {
@Autowired
private TestService testService;
@GetMapping("/test")
public String test(@RequestParam("name") String name) {
return testService.test(name);
}
}
application.yml 配置文件
spring:
application:
name: alibaba-sentinel-feign-client
cloud:
nacos:
discovery:
server-addr: 192.168.101.11:8848
# sentinel 控制台地址
sentinel:
transport:
dashboard: 127.0.0.1:8080
# Feign开启sentinel服务
feign:
sentinel:
enabled: true
server:
port: 8002
启动服务消费端 查看nacos
启动成功
6.测试 功能
访问 服务消费端接口 http://localhost:8002/test?name=Lion Li
响应正常
7.测试 限流熔断
进入sentinel-dashboard 控制台 (这里注意 Sentinel是懒加载 需要请求一次后 控制台方才有应用显示)
进入簇点链路 找到服务接口 设置流控规则
设置 流控方式为 QPS 阀值设置为1 (查询大于1则降级)
快速访问两次 http://localhost:8002/test?name=Lion Li
第一次成功
第二次降级
查看实时监控 通过1拒绝1 流控降级成功
项目已上传到gitee
地址: spring-cloud-alibaba-H-demo
如果帮到您了,请帮忙点个star
来源:CSDN
作者:Lion Li
链接:https://blog.csdn.net/weixin_40461281/article/details/104106746