您现在的位置是:首页 > 学无止境 > Spring Cloud

spring cloud alibaba(5)基础-熔断器防止雪崩sentinel

贾继涛 2019-03-11 1125人围观

相关文章

 spring cloud alibaba(1)基础注册中心nacos

 spring cloud alibaba(2)基础-服务提供者

 spring cloud alibaba(3)基础-创建消费者

 spring cloud alibaba(4)基础-创建消费者(feign)

 spring cloud alibaba(5)基础-熔断器防止雪崩sentinel

 spring cloud alibaba(6)基础-Sentinel 控制台搭建

 spring cloud alibaba(7)基础-Spring Cloud Gateway网关

 spring cloud alibaba(8)基础-geteway路由全局过滤

 spring cloud alibaba(9)基础-nacos云配置服务-nacos-config

 spring cloud alibaba(10)基础-Nacos Config多环境配置



Feign 中使用 Sentinel

1.pom中增加

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>


2.service

package com.jjt.hello.spring.cloud.alibaba.nacos.consumer.feign.service;
import com.jjt.hello.spring.cloud.alibaba.nacos.consumer.feign.fallback.HelloServiceFallback;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@FeignClient(value = "nacos-provider",fallback = HelloServiceFallback.class)
public interface HelloService {
    @GetMapping("/h/{msg}")
    public String hello(@PathVariable("msg") String msg);
}


3.fallback


package com.jjt.hello.spring.cloud.alibaba.nacos.consumer.feign.fallback;
import com.jjt.hello.spring.cloud.alibaba.nacos.consumer.feign.service.HelloService;
import org.springframework.stereotype.Component;
@Component
public class HelloServiceFallback implements HelloService {
    @Override
    public String hello(String msg) {
        return "请检查网络连接是否错误";
    }
}


4. application.yml


spring:
  application:
    name: nacos-consumer-feign
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.102.130:8848
server:
  port: 9092
management:
  endpoints:
    web:
      exposure:
        include: "*"
feign:
  sentinel:
    enabled: true


5.测试停止服务提供者就能看到效果

源码地址

 https://gitee.com/jiajitao/Spring-Cloud-Ali


您的支持