企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# 添加依赖 ```xml <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> ``` 完整版 ```xml <?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>cloudframe</artifactId> <groupId>com.gosuncn</groupId> <version>1.0</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>sys-provider</artifactId> <dependencies> <dependency> <groupId>com.gosuncn</groupId> <artifactId>common</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </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-oauth2</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>com/gosuncn/dao/*.xml</include> </includes> </resource> </resources> </build> </project> ``` # 修改配置文件 ```yaml spring: cloud: sentinel: transport: dashboard: 127.0.0.1:8080 ``` 完整版 ```yaml server: port: 8003 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql:///provider?serverTimezone=UTC username: root password: root application: name: provider cloud: nacos: discovery: server-addr: 127.0.0.1:8848 sentinel: transport: dashboard: 127.0.0.1:8080 mybatis: mapper-locations: classpath*:/com/gosuncn/dao/*.xml cloudframe: public-key: public.cert ``` # 创建ProviderControllerBlockException ```java package com.gosuncn.controller; public class ProviderControllerBlockException { public static CommonResult findAll(BlockException exception) { return new CommonResult<>(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), "ProviderControllerBlockException"); } public CommonResult createProvider(BlockException exception) { return new CommonResult<>(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), "ProviderControllerBlockException"); } } ``` # 创建ProviderControllerThrowable ```java package com.gosuncn.controller; public class ProviderControllerThrowable { public static CommonResult findAll(Throwable exception) { return new CommonResult<>(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), "ProviderControllerThrowable"); } public CommonResult createProvider(Throwable exception) { return new CommonResult<>(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), "ProviderControllerThrowable"); } } ``` # 修改ProviderController ```java package com.gosuncn.controller; @RestController public class ProviderController { @Autowired private ProviderService providerService; @GetMapping("/") @SentinelResource(value = "findAll", fallbackClass = ProviderControllerThrowable.class, fallback = "findAll", blockHandlerClass = ProviderControllerBlockException.class, blockHandler = "findAll") public CommonResult findAll() { return new CommonResult<>(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), providerService.findAll().orElse(null)); } @GetMapping("/provider") @SentinelResource(value = "createProvider", fallbackClass = ProviderControllerThrowable.class, fallback = "createProvider", blockHandlerClass = ProviderControllerBlockException.class, blockHandler = "createProvider") public CommonResult createProvider() { Provider provider = new Provider(); provider.setName("Hi Girl."); return new CommonResult<>(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), providerService.createProvider(provider)); } } ``` # 消费者与提供者步骤相同 ##### 若需要支持feign需要修改application.properties ```properties feign.sentinel.enabled=true ```