当前位置:网站首页>Openfeign uses

Openfeign uses

2022-06-25 11:30:00 sermonlizhi

One 、OpenFeign

1.1 Basic use

OpenFeign yes Spring Cloud stay Feign Based on the support of Spring MVC Annotations , Columns such as @RequestMapping etc. ,OpenFeign Of @FeignClient Can be parsed SpringMVC Of @RequestMapping Interface under annotation , And through the way of dynamic proxy to produce the implementation class , Load balancing in the implementation class and calling other services .

stay shop-common Modular pom Add... To the file OpenFeign Dependency package

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

establish OpenFeign Interface , Add... To the interface @FeignClient annotation , adopt value Property specifies the name of the service that provides the service , This name is registered to Nacos Service name in ,path Property to set the root path of interface access , In the interface method, you can use @RequestMapping Annotation to specify the access path , The root path and the specified access path are assembled to be the real access path

If you need to transfer parameters , Can be like Controller The receiving parameters are the same , adopt @RequestParm、@PathVariable And so on

@Component
@FeignClient(value = "shop-order",path = "order")
public interface UserToOrderFeignClient {
    

    /** *  Order details  * * @date 2022/2/8 * @param uid * @return java.util.List<com.lizhi.entity.Order> */
    @GetMapping("details")
    List<Order> details(@RequestParam("uid") Integer uid);

}

stay shop-order In service , To write Controller class , At the same time, add... On the startup class @EnableFeignClients annotation , Indicates permission to pass OpenFeign To access the interface

@RestController
@RequestMapping
public class OrderController {
    

    @Resource
    OrderService orderService;

    @GetMapping("details")
    List<Order> details(@RequestParam("uid") Integer uid){
    
        return orderService.details(uid);
    }

}

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class OrderApplication {
    

    public static void main(String[] args) {
    
        SpringApplication.run(OrderApplication.class,args);
    }

}

To configure shop-order Root path of service access

spring:
  mvc:
    servlet:
      path: /order

stay shop-user Module Injection UserToOrderFeignClient Proxy object of , Then you can directly call UserToOrderFeignClient Of details() Method to call shop-order Provided interface

@Resource
UserToOrderFeignClient userToOrderFeignClient;

@GetMapping("test")
List<Order> test(){
    
    return userToOrderFeignClient.details(1);
}

1.2 Client parameter configuration

@FeignClient Annotated configuration Property to configure client parameters , The default configuration class is FeignClientsConfiguration, It provides EncoderDecoderLoggerContractRetryerFeignClientConfigurer Etc

We can configure these parameters through a custom configuration class , And then in @FeignClient Configuration in comments configuration attribute

@Configuration
public class FeignConfiguration {
    

    @Bean
    public Contract feignContract(){
    ...}

    @Bean
    public Encoder encoder(){
    ...}
    ……
}
@Component
@FeignClient(value = "shop-order",path = "order",fallback = OrderFeignClientFallBack.class,configuration = FeignConfiguration.class)
public interface UserToOrderFeignClient {
    

    @GetMapping("details")
    List<Order> details(@RequestParam("uid") Integer uid);

}

In addition to configuring classes , You can also configure through the configuration file ,feign.client.config It's a Map, So you need to customize a KEY, The following example puts the service name shop-user As KEY, Attributes correspond to FeignClientProperties The inner class of FeignClientConfiguration

feign:
  sentinel:
    enabled: true
  client:
    config:
      shop-user:
        connectTimeout:
        loggerLevel:
        readTimeout: 5000
        oggerLevel: full
        encoder: com.lizhi.config.Encoder
        decoder: com.lizhi.config.Decoder
        contract: com.lizhi.config.Contract

1.3 Circuit breaker configuration

SpringCloud Circuit breaker support fallBack The concept of , With Sentinel For example , demonstration OpenFeign in fallBack Use

First add... To the configuration file feign.sentinel.enabled=true Parameters ,Spring The configured metadata describes this parameter in detail

{
    
    "name": "feign.sentinel.enabled",
    "type": "java.lang.Boolean",
    "description": "If true, an OpenFeign client will be wrapped with a Sentinel circuit breaker.",
    "defaultValue": "false"
}

We create a OrderFeignClientFallBack class , Let it be UserToOrderFeignClient This FeignClient The interface of , And define this class as a Bean

@Component
public class OrderFeignClientFallBack implements UserToOrderFeignClient {
    

    @Override
    public List<Order> details(Integer uid) {
    
        throw new NoFallbackAvailableException("service degrade",new RuntimeException());
    }
}

And then in FeignClient Interface @FeignClient In the annotations , Appoint fallBack The corresponding processing class

@Component
@FeignClient(value = "shop-order",path = "order",fallback = OrderFeignClientFallBack.class)
public interface UserToOrderFeignClient {
    

    @GetMapping("details")
    List<Order> details(@RequestParam("uid") Integer uid);

}

Reintegration Sentinel after , It can be done to /order/detail Interface setting fusing rules , If the interface call triggers the fusing rule , Will call fallBack Methods of the corresponding interface in the class

原网站

版权声明
本文为[sermonlizhi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202200537567549.html