Feign与Hystrix组合使用
1.引入jar 包
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
2.application.yml开启 feign对Hystrix的支持
feign: hystrix: enabled: true
3.主启动类
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableHystrix
public class OrderHystrixMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderHystrixMain80.class, args);
}
}
4.使用feign调用其他服务
@FeignClient(value = "CLOUD-PAYMENT-HYSTRIX-SERVICE") //value 填写需要调用的微服务名称
public interface OrderService {
@GetMapping("/payment/hystrix/ok/{id}")
String paymentInfo_OK(@PathVariable("id") Integer id);
@GetMapping("/payment/hystrix/timeout/{id}")
String paymentInfo_timeOut(@PathVariable("id") Integer id);
}
Feign与Sentinel组合
1.引入jar包
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> <!-- <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency>--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency>
2.application.yml 配置
feign: #开启feign对sentinel的支持 sentinel: enabled: true server: port: 8401 spring: application: name: cloudalibaba-sentinel-service cloud: nacos: discovery: server-addr: localhost:8848 #Nacos 注冊配置中心地址 sentinel: #sentinel dashboard 地址 transport: dashboard: localhost:8080 port: 8719 # 默認8719端口,假如被占用会自动从8719依次+1 扫描,直到找到空闲端口 logging.level.com.alibaba.nacos.client.naming: WARN #指定日志级别 management: endpoints: web: exposure: include: "*"
3.主启动类配置
添加注解 @EnableFeignClients
@SpringBootApplication
@EnableDiscoveeryClient //开启Nacos
@EnableFeignClients //开启Feign
public class OrderHystrixMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderHystrixMain80.class, args);
}
}
4.使用feign调用其他服务
@FeignClient(value = "CLOUD-PAYMENT-HYSTRIX-SERVICE") //value 填写需要调用的微服务名称
public interface FeignService {
@GetMapping("/payment/hystrix/ok/{id}")
String paymentInfo_OK(@PathVariable("id") Integer id);
@GetMapping("/payment/hystrix/timeout/{id}")
String paymentInfo_timeOut(@PathVariable("id") Integer id);
}
5, 配置服务降级方法
添加 Fallback 属性,参数为当前接口的实现类: @FeignClient(value="CLOUD-PAYMENT-HYSTRIX-SERVICE", fallback = Fallbacck.class)
@Service
public class FeignServiceImpl implements FeignService {
@Override
String paymentInfo_Ok(Integer id); {
return "Feign FallbackHandler:" + id;
}
@Override
String paymentInfo_timeOut(Integer id); {
return "Feign FallbackHandler:" + id;
}
}
当调用某个方法出错时会调用实现类中对应的方法实现降级处理。