Feign匹配动态URL

原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11811932.html

Project Directory

Maven Dependency

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <modelVersion>4.0.0</modelVersion>
 6 
 7     <groupId>org.fool.feign</groupId>
 8     <artifactId>hello-feign</artifactId>
 9     <version>1.0-SNAPSHOT</version>
10 
11     <parent>
12         <groupId>org.springframework.boot</groupId>
13         <artifactId>spring-boot-starter-parent</artifactId>
14         <version>2.2.0.RELEASE</version>
15     </parent>
16 
17     <dependencies>
18         <dependency>
19             <groupId>org.springframework.boot</groupId>
20             <artifactId>spring-boot-starter-web</artifactId>
21             <exclusions>
22                 <exclusion>
23                     <groupId>org.springframework.boot</groupId>
24                     <artifactId>spring-boot-starter-tomcat</artifactId>
25                 </exclusion>
26             </exclusions>
27         </dependency>
28 
29         <dependency>
30             <groupId>org.springframework.boot</groupId>
31             <artifactId>spring-boot-starter-jetty</artifactId>
32             <exclusions>
33                 <exclusion>
34                     <groupId>org.eclipse.jetty.websocket</groupId>
35                     <artifactId>websocket-server</artifactId>
36                 </exclusion>
37                 <exclusion>
38                     <groupId>org.eclipse.jetty.websocket</groupId>
39                     <artifactId>javax-websocket-server-impl</artifactId>
40                 </exclusion>
41             </exclusions>
42         </dependency>
43 
44         <dependency>
45             <groupId>org.projectlombok</groupId>
46             <artifactId>lombok</artifactId>
47         </dependency>
48 
49         <dependency>
50             <groupId>org.springframework.cloud</groupId>
51             <artifactId>spring-cloud-starter-openfeign</artifactId>
52             <version>2.1.3.RELEASE</version>
53         </dependency>
54 
55         <dependency>
56             <groupId>com.google.code.gson</groupId>
57             <artifactId>gson</artifactId>
58         </dependency>
59 
60         <dependency>
61             <groupId>org.springframework.boot</groupId>
62             <artifactId>spring-boot-starter-test</artifactId>
63             <scope>test</scope>
64         </dependency>
65     </dependencies>
66 
67     <build>
68         <plugins>
69             <plugin>
70                 <groupId>org.springframework.boot</groupId>
71                 <artifactId>spring-boot-maven-plugin</artifactId>
72             </plugin>
73         </plugins>
74     </build>
75 </project>
View Code

application.properties

1 server.port=8188
2 
3 feign.hystrix.enabled=true
4 hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=10000
5 hystrix.threadpool.default.coreSize=10

Source Code

Application.java

 1 package org.fool.feign;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.cloud.openfeign.EnableFeignClients;
 6 
 7 @SpringBootApplication
 8 @EnableFeignClients
 9 public class Application {
10     public static void main(String[] args) {
11         SpringApplication.run(Application.class, args);
12     }
13 }

Note: 使用Feign需要加上 @EnableFeignClients 注解

AbstractFallbackFactory.java

 1 package org.fool.feign.client;
 2 
 3 import feign.hystrix.FallbackFactory;
 4 import org.fool.feign.common.LogDomain;
 5 import org.fool.feign.common.ResultCode;
 6 import org.fool.feign.contract.response.ApplicationResponse;
 7 import org.fool.feign.logger.ApplicationLogger;
 8 import org.fool.feign.logger.ApplicationLoggerFactory;
 9 import org.fool.feign.util.JsonUtils;
10 
11 public abstract class AbstractFallbackFactory<T> implements FallbackFactory<T> {
12     protected final ApplicationLogger LOGGER = ApplicationLoggerFactory.getLogger(getClass());
13     private static final String DEFAULT_MESSAGE = "isolation by hystrix";
14 
15     String handleExceptions(Throwable cause, String message) {
16         LOGGER.error(LogDomain.CLIENT, message, cause);
17 
18         ApplicationResponse response = ApplicationResponse.builder()
19                 .resultCode(ResultCode.INTERNAL_ERROR)
20                 .message(ResultCode.INTERNAL_ERROR + ":" + DEFAULT_MESSAGE)
21                 .build();
22 
23         return JsonUtils.objectToJson(response);
24     }
25 }

OrderClient.java

 1 package org.fool.feign.client;
 2 
 3 import org.fool.feign.config.FeignConfiguration;
 4 import org.fool.feign.contract.request.DemoRequest;
 5 import org.springframework.cloud.openfeign.FeignClient;
 6 import org.springframework.stereotype.Component;
 7 import org.springframework.web.bind.annotation.PostMapping;
 8 import org.springframework.web.bind.annotation.RequestBody;
 9 
10 import java.net.URI;
11 
12 @FeignClient(name = "ORDER-SERVICE", url = "url-placeholder",
13         fallbackFactory = OrderClient.OrderClientFallbackFactory.class, configuration = FeignConfiguration.class)
14 public interface OrderClient {
15 
16     @PostMapping(value = "/demo")
17     String queryOrder(URI uri, @RequestBody DemoRequest orderRequest);
18 
19     @Component
20     class OrderClientFallbackFactory extends AbstractFallbackFactory<OrderClient> {
21         @Override
22         public OrderClient create(Throwable throwable) {
23             return new OrderClient() {
24                 @Override
25                 public String queryOrder(URI uri, DemoRequest orderRequest) {
26                     String message = "failed to query_order with " + orderRequest + " url: " + uri.toString();
27                     return handleExceptions(throwable, message);
28                 }
29             };
30         }
31     }
32 }

Note:

通常第一个红框输入的是远程服务的URL,第二个红框则没有URI uri 参数,这里为了实现远程调用模版接口相同,请求URL不同,采用如下图所示实现

ApplicationTest.java

 1 package org.fool.feign.test;
 2 
 3 import org.fool.feign.Application;
 4 import org.fool.feign.client.OrderClient;
 5 import org.fool.feign.contract.request.DemoRequest;
 6 import org.junit.Test;
 7 import org.junit.runner.RunWith;
 8 import org.springframework.beans.factory.annotation.Autowired;
 9 import org.springframework.boot.test.context.SpringBootTest;
10 import org.springframework.test.context.junit4.SpringRunner;
11 
12 import java.net.URI;
13 
14 @RunWith(SpringRunner.class)
15 @SpringBootTest(classes = Application.class)
16 public class ApplicationTest {
17 
18     @Autowired
19     private OrderClient orderClient;
20 
21     @Test
22     public void test() throws Exception {
23         String url = "http://localhost:8188/order/v1/resource";
24 
25         DemoRequest request = new DemoRequest();
26         request.setOrderTime("2019-11-11");
27         request.setBizTag("order");
28 
29         String result = orderClient.queryOrder(new URI(url), request);
30         System.out.println(result);
31     }
32 }

Note: 实现动态调用如下图红框所示,动态传如一个URL

Console Output

猜你喜欢

转载自www.cnblogs.com/agilestyle/p/11811932.html