SpringCloudAlibaba:Sentinel-熔断和fegin调用

x33g5p2x  于2021-10-29 转载在 Spring  
字(10.6k)|赞(0)|评价(0)|浏览(414)

一、环境准备

父模块依赖

<properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
        <spring-cloud.version>Hoxton.SR9</spring-cloud.version>
        <spring-cloud-alibaba.version>2.1.0.RELEASE</spring-cloud-alibaba.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--spring cloud alibaba 2.1.0.RELEASE-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

nacos-provider

pom文件
<dependencies>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

application.yml

server:
  port: 8001

spring:
  application:
    name: provider
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.31.100:8848

#暴露监控
management:
  endpoints:
    web:
      exposure:
        include: '*'
启动类
@SpringBootApplication
@EnableDiscoveryClient
public class NacosProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(NacosProviderApplication.class, args);
    }

}
controller
@RestController
@RequestMapping("/provider")
public class ProviderController {
    @Value("${server.port}")
    private Integer port;
    @GetMapping("/hello")
    public String hello(){
        return "Hello nacos , server port is "+port;
    }
}
测试

http://localhost:8001/provider/hello

nacos-provider-salve

nacos-provider-salve模块跟nacos-provider模块一样就端口不一样

http://localhost:8002/provider/hello

nacos-consumer

pom文件
<dependencies>
        <!--sentinel nacos-->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</artifactId>
            <version>1.8.2</version>
        </dependency>
        <!--sentinel-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
            <version>2.2.1.RELEASE</version>
        </dependency>
        <!--spring cloud alibaba-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>
        <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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
application.yml
server:
  port: 8003
spring:
  application:
    name: customer
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.31.100:8848
    sentinel:
      transport:
        #配置sentinel地址,端口
        dashboard: 192.168.31.100:8859   #这里是我linux地址
        #客户端IP(sentinel dashboard进行实时监控的主机ip地址)
        # 默认端口8719端口假如被占用会自动从8719开始依次+1扫描,直到找到未被占用的端口
        port: 8719
        client-ip: 172.100.20.220   #这里是我windows地址
controller
@RestController
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    private RestTemplate restTemplate;

    private final String SERVER_URL="http://provider";

    @GetMapping(value = "/hello")
    @SentinelResource(value = "hello")
    public String hello(){
        return restTemplate.getForObject(SERVER_URL+"/provider/hello", String.class);
    }
}
RestTemplateConfig
@Configuration
public class RestTemplateConfig {
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}
启动类
@SpringBootApplication
@EnableDiscoveryClient
public class NacosConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(NacosConsumerApplication.class, args);
    }

}
测试

测试接口

http://localhost:8003/customer/hello

查看nacos

查看sentinel

二、fallback

1.修改CustomerController

@RestController
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    private RestTemplate restTemplate;

    private final String SERVER_URL="http://provider";

    @GetMapping(value = "/hello/{id}")
    @SentinelResource(value = "hello",fallback = "fallbackHandler")
    public String hello(@PathVariable("id") String id){
        if (id.equals("1")){
            throw new RuntimeException("id不能为1");
        }
        return restTemplate.getForObject(SERVER_URL+"/provider/hello", String.class);
    }

    public String fallbackHandler(@PathVariable("id") String id ,Throwable e){
        return "CustomerController invoke fallbackHandler";
    }
}

2.测试

http://localhost:8003/customer/hello/1

http://localhost:8003/customer/hello/2

3.注意

fallback:是进行降级处理

三、blockHandler

1.修改CustomerController

@RestController
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private RestTemplate restTemplate;

    private final String SERVER_URL="http://provider";

    /** * blockHandler:仅负责sentinel违规 */
    @GetMapping(value = "/hello/{id}")
    @SentinelResource(value = "hello",blockHandler = "helloBlockHandler")
    public String hello(@PathVariable("id") String id){
        if (id.equals("1")){
            throw new RuntimeException("id不能为1");
        }
        return restTemplate.getForObject(SERVER_URL+"/provider/hello", String.class);
    }
    public String fallbackHandler(@PathVariable("id") String id ,Throwable e){
        return "CustomerController invoke fallbackHandler";
    }
    public String helloBlockHandler(String id, BlockException e){
        return "CustomerController invoke blockHandler";
    }
}

2.sentinel配置

3.测试

#访问1时直接报错,java没有对应fallback处理
http://localhost:8003/customer/hello/1
#当频繁访问,出现限流响应
http://localhost:8003/customer/hello/2

4.注意

blockHandler:仅负责sentinel违规

四、fallback、blockHandler同时配置

1.修改controller

@RestController
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private RestTemplate restTemplate;

    private final String SERVER_URL="http://provider";

    /** * blockHandler:仅负责sentinel违规 */
    @GetMapping(value = "/hello/{id}")
    @SentinelResource(value = "hello",blockHandler = "helloBlockHandler",fallback = "fallbackHandler")
    public String hello(@PathVariable("id") String id){
        System.out.println(id);
        if (id.equals("1")){
            throw new RuntimeException("id不能为1");
        }
        return restTemplate.getForObject(SERVER_URL+"/provider/hello", String.class);
    }
    public String fallbackHandler(@PathVariable("id") String id ,Throwable e){
        return "CustomerController invoke fallbackHandler";
    }
    public String helloBlockHandler(String id,BlockException e){
        return "CustomerController invoke blockHandler";
    }
}

2.测试

#java异常fallback->fallbackHandler
http://localhost:8003/customer/hello/1
#限流控制->helloBlockHandler
http://localhost:8003/customer/hello/2

五、OpenFegin

1.pom文件

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!--sentinel nacos-->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</artifactId>
        </dependency>
        <!--sentinel-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <!--spring cloud alibaba-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
    </dependencies>

2.application.yml

server:
  port: 9005
spring:
  application:
    name: customer-fegin
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.31.100:8848
    sentinel:
      transport:
        #配置sentinel地址,端口
        dashboard: 192.168.31.100:8859
        port: 8719
        #客户端IP
        client-ip: 172.100.20.220
#开启sentinel 对fegin的支持
feign:
  sentinel:
    enabled: true

3.启动类

@EnableFeignClients
@SpringBootApplication
@EnableDiscoveryClient
public class OpenfeginApplication {

    public static void main(String[] args) {
        SpringApplication.run(OpenfeginApplication.class, args);
    }

}

4.降级类

@Service
public class ProviderServiceFallback implements ProviderService {
    @Override
    public String hello() {
        return "ProviderServiceFallback invoke hello";
    }
}

5.Fegin服务类

@FeignClient(value = "provider",path = "/provider",fallback = ProviderServiceFallback.class)
@Service
public interface ProviderService {
    @GetMapping("/hello")
    String hello();
}

6.controller

@RestController
@RequestMapping("/fegin")
public class FeginController {

    @Autowired
    private ProviderService providerService;

    @GetMapping("/hello")
    @SentinelResource(value = "hello")
    public String hello(){
        return providerService.hello();
    }
}

7.启动主程序

#报错
com.alibaba.cloud.sentinel.feign.SentinelContractHolder.parseAndValidateMetadata(Ljava/lang/Class;)
#解决办法,修改父pomspringcloud版本为Hoxton.SR1。- -,BUG。期待后续解决 - -!!!,修改后重启解决
<spring-cloud.version>Hoxton.SR1</spring-cloud.version>

8.测试

http://localhost:9005/fegin/hello/

相关文章