栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringCloudAlibaba:Sentinel-熔断和fegin调用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringCloudAlibaba:Sentinel-熔断和fegin调用

SpringCloudAlibaba:Sentinel-熔断和fegin调用 一、环境准备 父模块依赖

        1.8
        UTF-8
        UTF-8
        2.3.7.RELEASE
        Hoxton.SR9
        2.1.0.RELEASE
    
    
        
            
                org.springframework.boot
                spring-boot-dependencies
                ${spring-boot.version}
                pom
                import
            
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
            
            
                com.alibaba.cloud
                spring-cloud-alibaba-dependencies
                ${spring-cloud-alibaba.version}
                pom
                import
            
        
    
nacos-provider pom文件

        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

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文件

        
        
            com.alibaba.csp
            sentinel-datasource-nacos
            1.8.2
        
        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-sentinel
            2.2.1.RELEASE
        
        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
            2.1.0.RELEASE
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    
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";

    
    @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";

    
    @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文件

        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        
        
        
            com.alibaba.csp
            sentinel-datasource-nacos
        
        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-sentinel
        
        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
            org.springframework.boot
            spring-boot-starter-test
        
    
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。期待后续解决 - -!!!,修改后重启解决
Hoxton.SR1
8.测试

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/356873.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号