- Spring Cloud Alibaba之服务容错组件 - Se
- Spring Cloud Alibaba之服务容错组件 - Se
- Spring Cloud Alibaba之服务容错组件 - Se
- Spring Cloud Alibaba之服务容错组件 - Se
- Spring Cloud Alibaba之服务容错组件 - Se
- Spring Cloud Alibaba之服务容错组件 - Se
- Spring Cloud Alibaba之服务容错组件 - Se
- Spring Cloud Alibaba之服务容错组件 - Se
- Spring Cloud Alibaba之服务容错组件 - Se
- Spring Cloud Alibaba 微服务商城系统
Spring Cloud Alibaba Sentinel 除了对 RestTemplate 做了支持,同样对于 Feign 也做了支持,如果我们要从 Hystrix 切换到 Sentinel 是非常方便的,下面介绍下如何使用 Feign 支持以及实现原理。
Sentinel 集成 Feign使用
第一步: 新建Spring Boot web应用 alibaba-sentinel-feign
在pom.xml,加入 openfeign starter 依赖使 sentinel starter 中的自动化配置类生效:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
第二步: 需要在配置文件 application.properties 中开启 sentinel 对 feign 的支持
#打开 sentinel 对 feign 的支持
feign.sentinel.enabled=true
第三步: 编写启动类和FeignClient测试接口
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class SentinelFeignApplication {
public static void main(String[] args) {
SpringApplication.run(SentinelFeignApplication.class, args);
}
}
在启动类上添加 @EnableFeignClients
注解开启对Feign支持,同时不要忘了@EnableDiscoveryClient
@FeignClient(name = "user-service", fallback = UserFeignClientFallback.class)
public interface UserFeignClient {
@GetMapping("/user/{id}")
public String getUser(@PathVariable("id") Long id);
}
@Component
public class UserFeignClientFallback implements UserFeignClient {
@Override
public String getUser(Long id) {
return "fallback";
}
}
你可以将这个 Client 对应的 user-service 停掉,然后就可以看到输出的内容是 "fallback"
FallbackFactory
使用 fallback 方式是无法获取异常信息的,如果想要获取异常信息,可以使用 fallbackFactory参数
@Slf4j
@Component
public class UserFeignClientFallbackFactory implements FallbackFactory {
@Override
public String create(Throwable cause) {
return new UserFeignClient (Long id) {
@Override
public String getUser(Long id) {
log.warn("远程调用被限流/降级了", cause);
return "fallback";
}
}
}
}
网友评论