开启sentinel feign支持并添加demo

pull/137/head
koltZhang 2024-09-04 09:40:18 +08:00
parent b209fc0bb2
commit 7b59766c17
7 changed files with 106 additions and 1 deletions

View File

@ -0,0 +1,23 @@
package cn.iocoder.yudao.module.infra.api.demo;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.infra.api.demo.fallback.SentinelFeigenDemoFallback;
import cn.iocoder.yudao.module.infra.enums.ApiConstants;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
/**
* sentinel Feign demo
*/
@FeignClient(name = ApiConstants.NAME, fallback = SentinelFeigenDemoFallback.class)
public interface SentinelFeigenDemoApi {
String PREFIX = ApiConstants.PREFIX + "/provider/sentinel";
@GetMapping(PREFIX +"/test/{message}")
public CommonResult<String> providerSentinelTest(@PathVariable("message") String message);
}

View File

@ -0,0 +1,15 @@
package cn.iocoder.yudao.module.infra.api.demo.fallback;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.infra.api.demo.SentinelFeigenDemoApi;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
@Component
public class SentinelFeigenDemoFallback implements SentinelFeigenDemoApi {
@Override
public CommonResult<String> providerSentinelTest(String message) {
return CommonResult.error(HttpStatus.TOO_MANY_REQUESTS.value(), "对方服务不可用,开始服务降级处理");
}
}

View File

@ -0,0 +1,21 @@
package cn.iocoder.yudao.module.infra.api.demo;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
/**
* RESTful API Feign
*/
@RestController
@Validated
public class SentinelFeigenApiImpl implements SentinelFeigenDemoApi {
@Override
public CommonResult<String> providerSentinelTest(String message) {
return success("OK");
}
}

View File

@ -179,3 +179,9 @@ yudao:
- infra_data_source_config
debug: false
--- #################### 开启feign对sentinel的支持 ####################
feign:
sentinel:
enabled: true

View File

@ -0,0 +1,28 @@
package cn.iocoder.yudao.module.system.controller.admin.demo;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.infra.api.demo.SentinelFeigenDemoApi;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
@Tag(name = "管理后台 - Sentinel Feigen Demo")
@RestController()
@RequestMapping("/system/sentinel/demo")
public class SentinelFeigenDemoController {
@Resource
private SentinelFeigenDemoApi sentinelFeigenDemoApiService;
@GetMapping("/provider/{message}")
@PermitAll
public CommonResult<String> providerSentinelTest(@PathVariable("message") String message) {
return sentinelFeigenDemoApiService.providerSentinelTest(message);
}
}

View File

@ -1,12 +1,17 @@
package cn.iocoder.yudao.module.system.framework.rpc.config;
import cn.iocoder.yudao.module.infra.api.config.ConfigApi;
import cn.iocoder.yudao.module.infra.api.demo.SentinelFeigenDemoApi;
import cn.iocoder.yudao.module.infra.api.demo.fallback.SentinelFeigenDemoFallback;
import cn.iocoder.yudao.module.infra.api.file.FileApi;
import cn.iocoder.yudao.module.infra.api.websocket.WebSocketSenderApi;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClientsConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration(proxyBeanMethods = false)
@EnableFeignClients(clients = {FileApi.class, WebSocketSenderApi.class, ConfigApi.class})
@EnableFeignClients(clients = {FileApi.class, WebSocketSenderApi.class, ConfigApi.class, SentinelFeigenDemoApi.class})
@Import({SentinelFeigenDemoFallback.class})
public class RpcConfiguration {
}

View File

@ -174,6 +174,7 @@ yudao:
- /rpc-api/system/tenant/valid # 防止递归。避免调用 /rpc-api/system/tenant/valid 接口时,又去触发 /rpc-api/system/tenant/valid 去校验
- /rpc-api/system/tenant/id-list # 获得租户列表的时候,无需传递租户编号
- /rpc-api/system/oauth2/token/check # 访问令牌校验时,无需传递租户编号;主要解决上传文件的场景,前端不会传递 tenant-id
- /admin-api/system/sentinel/demo/provider/* # 示例演示,演示 Sentinel 熔断降级
ignore-tables:
- system_tenant
- system_tenant_package
@ -205,3 +206,9 @@ yudao:
end-code: 9999 # 这里配置 9999 的原因是,测试方便。
debug: false
--- #################### 开启feign对sentinel的支持 ####################
feign:
sentinel:
enabled: true