Sentinel控制台(可选)
搭建Sentinel的控制台服务sentinel-dashboard
- 下载sentinel-dashboard.jar包
- 启动sentinel控制台
进入下载好的sentinel-dashboard-1.8.9.jar所在的目录,地址栏输入cmd
输入如下命令
# -Dserver.port=8080 指定sentinel-dashboard服务启动的端口
# -Dcsp.sentinel.dashboard.server=localhost:8080 向Sentinel接入端指定控制台的地址
# -Dproject.name=sentinel-dashboard 向 Sentinel 指定应用名称为sentinel-dashboard
java -Dserver.port=8088 -Dcsp.sentinel.dashboard.server=localhost:8088 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.8.9.jar
- 访问sentinel控制台
默认的用户和密码为:sentinel sentinel
登录成功
服务消费者改造
- 在服务消费者server-consumer的build.gradle文件中,引入sentinel的starter包
implementation "com.alibaba.cloud:spring-cloud-starter-alibaba-sentinel" // sentinel
- 給消费者server-consumer在nacos上的的配置文件server-consumer.yaml中新增sentinel配置和feign相关的配置
server:
port: 8082
spring:
cloud:
sentinel: # sentinel相关的配置
transport:
dashboard: localhost:8080 # Sentinel控制台地址
port: 8719 # 客户端监控端口
eager: true # 提前加载,避免首次调用才初始化
enabled: true # 启用Sentinel
# feign相关的配置
feign:
client:
config:
default: # 默认配置
connectTimeout: 5000 # 连接超时(毫秒)
readTimeout: 6000 # 读取超时(毫秒)
loggerLevel: full # 日志级别:NONE, BASIC, HEADERS, FULL
server-provider: # 针对服务提供者的配置(会覆盖默认)
connectTimeout: 3000
readTimeout: 4000
# 启用sentinel对feign的支持
sentinel:
enabled: true
# 暴露端点用于监控
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always
- 修改服务消费者server-consumer的feign客户端HelloClient.java
package com.chaoup.consumer.feign.clients;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(name = "server-provider", fallback = HelloClientFallback.class)
public interface HelloClient {
@GetMapping("/hellos/say")
String hello(@RequestParam("name") String name);
}
- 新增feign客户端的实现类HelloClientFallBack.java
package com.chaoup.consumer.feign.clients;
import org.springframework.stereotype.Component;
@Component
public class HelloClientFallback implements HelloClient{
@Override
public String hello(String name) {
return String.format("hello %s,我是服务提供者的备胎", name);
}
}
- 修改服务提供者,故意使其响应时间超过配置的4000毫秒
package com.chaoup.modelA.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.TimeUnit;
@RestController
@RequestMapping("/hellos")
public class Hello {
@GetMapping("/say")
String hello(@RequestParam("name") String name) {
try {
// 睡眠5秒,已经超过了在nacos上给server-provider配置读取超时间4秒
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return String.format("hello %s,我是服务提供者", name);
}
}
- 重启服务生产者和服务消费者,访问:http://localhost:8082/tests/test1?name=dj
返回的是HelloClientFallback类中的内容
- 修改服务消费者server-provider的sentinel配置,将对server-provider配置的读取超时时间由4秒调整为6秒
feign:
client:
config:
default: # 默认配置
connectTimeout: 5000 # 连接超时(毫秒)
readTimeout: 6000 # 读取超时(毫秒)
loggerLevel: full # 日志级别:NONE, BASIC, HEADERS, FULL
service-provider: # 针对服务提供者的配置(会覆盖默认)
connectTimeout: 3000
readTimeout: 6000 # 由原来的4000修改为6000
- 重启服务消费者server-consumer后,再次访问:http://localhost:8082/tests/test1?name=dj
服务提供者server-provider处理时间没有超过,故返回正常业务结果