微服务调用框架-Feign

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;
    }
}

 当调用某个方法出错时会调用实现类中对应的方法实现降级处理。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值