SpringCloud Sentinel 结合OpenFeign的使用介绍

概述:sentinel 结合 feign 的使用方式与feign单独使用非常类似。其他需要配置的主要为在配置文件中配置feign激活sentinel。其他使用方式基本一致。sentinel 整合feign无法区分降级与异常处理逻辑,都公用一个fallback降级处理方式。

项目使用文件:

  1. 父工程pom.xml
  2. 子模块pom.xml
  3. 配置文件
  4. 启动类
  5. feignClient类
  6. feignClient失败处理类
  7. controller

一、父工程pom.xml (pom.xml)




  4.0.0

  com.xiaohui.springCloud
  SpringCloud
  1.0-SNAPSHOT
  pom

  
    org.springframework.boot
    spring-boot-starter-parent
    2.2.2.RELEASE
  

  
    product_service
    order_service
    cloud-api-common
    eureka_server
    eureka_server2
    cloud-import-test
    consul_product_service
    consul_order_service
    openfeign_order_service
      hystrix_order_service_ipconn
      hystrix_order_service_rest
      hystrix-turbine
      sentinel_order_service_rest
    sentinel_order_service_feign
  

  
  
    UTF-8
    1.8
    1.8
    4.12
    1.2.17
    1.16.18
    5.1.47
    1.1.16
    1.3.0
  

  
  
    
      
        org.springframework.cloud
        spring-cloud-dependencies
        Hoxton.SR1
        pom
        import
      

      
      
        com.alibaba.cloud
        spring-cloud-alibaba-dependencies
        2.1.0.RELEASE
        pom
        import
      

      
        com.alibaba
        druid
        ${druid.version}
      
      
        org.mybatis.spring.boot
        mybatis-spring-boot-starter
        ${mybatis.spring.boot.version}
      

      
        mysql
        mysql-connector-java
        ${mysql.version}
      

      
        junit
        junit
        ${junit.version}
      

      
        org.projectlombok
        lombok
        ${lombok.version}
        true
      

    
  

  
    
      
      
        org.springframework.boot
        spring-boot-maven-plugin
        
          true
          true
        
      
    
  

二、子模块pom.xml(SpringCloud\sentinel_order_service_feign\pom.xml)



    
        com.xiaohui.springCloud
        SpringCloud
        1.0-SNAPSHOT
    
    4.0.0

    sentinel_order_service_feign

    
        
            com.xiaohui.springCloud
            cloud-api-common
            ${project.version}
        

        
        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        

        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            org.springframework.retry
            spring-retry
        

        
        
            org.springframework.boot
            spring-boot-starter-web
        
        

        
            org.projectlombok
            lombok
            true
        

        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-sentinel
        
    

三、子工程配置文件

server:
  port: 9001

spring:
  application:
    name: openfeign-order-service
  cloud:
    sentinel:
      transport:
        dashboard: 127.0.0.1:8080
      eager: true #立即加载接口

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://eureka1.com:9000/eureka/
  instance:
    prefer-ip-address: true #使用ip进行注册
    instance-id: ${spring.cloud.client.ip-address}:${server.port} #向注册中心注册服务ID
    lease-renewal-interval-in-seconds: 5 #发送心跳间隔时间 秒
    lease-expiration-duration-in-seconds: 10 # 服务续约时间 10秒内没有发送心跳(宕机)


#Feign 日志输出配置
feign:
  client:
    config:
      CLOUD-PAYMENT-SERVICE:
        loggerLevel: FULL
        connectTimeout: 5000
        readTimeout: 5000
  sentinel:
    enabled: true #feign激活sentinel
logging:
  level:
    com.xiaohui.springcloud.service.ProductFeignClient: debug

四、启动类(src/main/java/com/xiaohui/springcloud/SFOrderApplication.java)

package com.xiaohui.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
public class SFOrderApplication {
    public static void main(String[] args) {
        SpringApplication.run(SFOrderApplication.class,args);
    }
}

五、feignClient类(src/main/java/com/xiaohui/springcloud/service/ProductFeignClient.java)

package com.xiaohui.springcloud.service;

import com.xiaohui.springcloud.entities.CommonResult;
import com.xiaohui.springcloud.service.fallback.ProductFeignClientFallBack;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@FeignClient(name="CLOUD-PAYMENT-SERVICE",fallback = ProductFeignClientFallBack.class)
public interface ProductFeignClient {

    @RequestMapping(value="/payment/get/{id}",method = RequestMethod.GET)
    CommonResult getPayment(@PathVariable("id") Long id);

    @GetMapping("/payment/get/timeout")
    CommonResult getPaymentTimeout();
}

六、feignClient失败处理类(src/main/java/com/xiaohui/springcloud/service/fallback/ProductFeignClientFallBack.java)

package com.xiaohui.springcloud.service.fallback;

import com.xiaohui.springcloud.entities.CommonResult;
import com.xiaohui.springcloud.service.ProductFeignClient;
import org.springframework.stereotype.Component;

@Component
public class ProductFeignClientFallBack implements ProductFeignClient {

    @Override
    public CommonResult getPayment(Long id) {
        return new CommonResult(500, "OpenFeign--容错---处理",null );
    }

    @Override
    public CommonResult getPaymentTimeout() {
        return null;
    }
}

七、controller(src/main/java/com/xiaohui/springcloud/controller/OrderController.java)

package com.xiaohui.springcloud.controller;

import com.xiaohui.springcloud.entities.CommonResult;
import com.xiaohui.springcloud.entities.Payment;
import com.xiaohui.springcloud.service.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class OrderController {

    @Resource
    private ProductFeignClient productFeignClient;

    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult getPayment(@PathVariable("id") Long id){
        return productFeignClient.getPayment(id);
    }

    @GetMapping("/consumer/payment/get/timeout")
    public CommonResult getPaymentTimeout(){
        return productFeignClient.getPaymentTimeout();
    }
}

 

 

你可能感兴趣的:(SpringCloud,sentinel)