From 11f4f91655c2454d25563fe8cf5c94ca52096927 Mon Sep 17 00:00:00 2001 From: woniu <1003271829@qq.com> Date: Mon, 23 Jan 2023 19:31:49 +0800 Subject: [PATCH] =?UTF-8?q?feign=E4=B8=87=E8=83=BD=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- feiginclientdemo/.idea/.gitignore | 8 ++ feiginclientdemo/.idea/compiler.xml | 19 +++ feiginclientdemo/.idea/encodings.xml | 6 + feiginclientdemo/.idea/jarRepositories.xml | 25 ++++ feiginclientdemo/.idea/misc.xml | 14 ++ feiginclientdemo/.idea/uiDesigner.xml | 124 ++++++++++++++++++ feiginclientdemo/feiginclientdemo.iml | 2 + feiginclientdemo/pom.xml | 81 ++++++++++++ .../java/com/woniu/demo/MainApplication.java | 13 ++ .../main/java/com/woniu/demo/entity/R.java | 12 ++ .../com/woniu/demo/feign/DynamicClient.java | 22 ++++ .../demo/feign/DynamicFeignClientFactory.java | 19 +++ .../woniu/demo/feignclient/ProductClient.java | 19 +++ .../woniu/demo/feignclient/UserClient.java | 14 ++ .../com/woniu/demo/inter/DynamicService.java | 16 +++ .../main/java/com/woniu/demo/test/DemoC.java | 15 +++ .../src/main/resources/application.yml | 6 + 17 files changed, 415 insertions(+) create mode 100644 feiginclientdemo/.idea/.gitignore create mode 100644 feiginclientdemo/.idea/compiler.xml create mode 100644 feiginclientdemo/.idea/encodings.xml create mode 100644 feiginclientdemo/.idea/jarRepositories.xml create mode 100644 feiginclientdemo/.idea/misc.xml create mode 100644 feiginclientdemo/.idea/uiDesigner.xml create mode 100644 feiginclientdemo/feiginclientdemo.iml create mode 100644 feiginclientdemo/pom.xml create mode 100644 feiginclientdemo/src/main/java/com/woniu/demo/MainApplication.java create mode 100644 feiginclientdemo/src/main/java/com/woniu/demo/entity/R.java create mode 100644 feiginclientdemo/src/main/java/com/woniu/demo/feign/DynamicClient.java create mode 100644 feiginclientdemo/src/main/java/com/woniu/demo/feign/DynamicFeignClientFactory.java create mode 100644 feiginclientdemo/src/main/java/com/woniu/demo/feignclient/ProductClient.java create mode 100644 feiginclientdemo/src/main/java/com/woniu/demo/feignclient/UserClient.java create mode 100644 feiginclientdemo/src/main/java/com/woniu/demo/inter/DynamicService.java create mode 100644 feiginclientdemo/src/main/java/com/woniu/demo/test/DemoC.java create mode 100644 feiginclientdemo/src/main/resources/application.yml diff --git a/feiginclientdemo/.idea/.gitignore b/feiginclientdemo/.idea/.gitignore new file mode 100644 index 0000000..634e5de --- /dev/null +++ b/feiginclientdemo/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/../../../:\feiginclientdemo\.idea/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/feiginclientdemo/.idea/compiler.xml b/feiginclientdemo/.idea/compiler.xml new file mode 100644 index 0000000..26650e0 --- /dev/null +++ b/feiginclientdemo/.idea/compiler.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/feiginclientdemo/.idea/encodings.xml b/feiginclientdemo/.idea/encodings.xml new file mode 100644 index 0000000..63e9001 --- /dev/null +++ b/feiginclientdemo/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/feiginclientdemo/.idea/jarRepositories.xml b/feiginclientdemo/.idea/jarRepositories.xml new file mode 100644 index 0000000..40a2eee --- /dev/null +++ b/feiginclientdemo/.idea/jarRepositories.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/feiginclientdemo/.idea/misc.xml b/feiginclientdemo/.idea/misc.xml new file mode 100644 index 0000000..4b661a5 --- /dev/null +++ b/feiginclientdemo/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/feiginclientdemo/.idea/uiDesigner.xml b/feiginclientdemo/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/feiginclientdemo/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/feiginclientdemo/feiginclientdemo.iml b/feiginclientdemo/feiginclientdemo.iml new file mode 100644 index 0000000..78b2cc5 --- /dev/null +++ b/feiginclientdemo/feiginclientdemo.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/feiginclientdemo/pom.xml b/feiginclientdemo/pom.xml new file mode 100644 index 0000000..0bd5e29 --- /dev/null +++ b/feiginclientdemo/pom.xml @@ -0,0 +1,81 @@ + + + 4.0.0 + + com.woniu.demo + feiginclientdemo + 1.0-SNAPSHOT + + + 8 + 8 + + + + org.springframework.boot + spring-boot-starter-parent + 2.2.6.RELEASE + + + + + + + + com.alibaba + druid-spring-boot-starter + 1.1.13 + + + org.springframework.boot + spring-boot-starter-web + + + + cn.hutool + hutool-all + 5.8.10 + + + + org.projectlombok + lombok + 1.18.24 + + + + + + org.springframework.boot + spring-boot-starter-test + test + + + junit + junit + 4.13.1 + test + + + + + + org.springframework.cloud + spring-cloud-starter-openfeign + 3.1.3 + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + \ No newline at end of file diff --git a/feiginclientdemo/src/main/java/com/woniu/demo/MainApplication.java b/feiginclientdemo/src/main/java/com/woniu/demo/MainApplication.java new file mode 100644 index 0000000..30c8e1f --- /dev/null +++ b/feiginclientdemo/src/main/java/com/woniu/demo/MainApplication.java @@ -0,0 +1,13 @@ +package com.woniu.demo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.openfeign.EnableFeignClients; + +@SpringBootApplication +@EnableFeignClients +public class MainApplication { + public static void main(String[] args) { + SpringApplication.run(MainApplication.class, args); + } +} diff --git a/feiginclientdemo/src/main/java/com/woniu/demo/entity/R.java b/feiginclientdemo/src/main/java/com/woniu/demo/entity/R.java new file mode 100644 index 0000000..cdbd29a --- /dev/null +++ b/feiginclientdemo/src/main/java/com/woniu/demo/entity/R.java @@ -0,0 +1,12 @@ +package com.woniu.demo.entity; + +import lombok.Data; + +@Data +public class R { + + private Integer code; + private String message; + private T data; + +} diff --git a/feiginclientdemo/src/main/java/com/woniu/demo/feign/DynamicClient.java b/feiginclientdemo/src/main/java/com/woniu/demo/feign/DynamicClient.java new file mode 100644 index 0000000..f29b34a --- /dev/null +++ b/feiginclientdemo/src/main/java/com/woniu/demo/feign/DynamicClient.java @@ -0,0 +1,22 @@ +package com.woniu.demo.feign; + +import com.woniu.demo.inter.DynamicService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class DynamicClient { + + @Autowired + private DynamicFeignClientFactory dynamicFeignClientFactory; + + public Object executePostApi(String feignName, String url, Object params) { + DynamicService dynamicService = dynamicFeignClientFactory.getFeignClient(DynamicService.class, feignName); + return dynamicService.executePostApi(url, params); + } + + public Object executeGetApi(String feignName, String url, Object params) { + DynamicService dynamicService = dynamicFeignClientFactory.getFeignClient(DynamicService.class, feignName); + return dynamicService.executeGetApi(url, params); + } +} \ No newline at end of file diff --git a/feiginclientdemo/src/main/java/com/woniu/demo/feign/DynamicFeignClientFactory.java b/feiginclientdemo/src/main/java/com/woniu/demo/feign/DynamicFeignClientFactory.java new file mode 100644 index 0000000..7d237d1 --- /dev/null +++ b/feiginclientdemo/src/main/java/com/woniu/demo/feign/DynamicFeignClientFactory.java @@ -0,0 +1,19 @@ +package com.woniu.demo.feign; + +import org.springframework.cloud.openfeign.FeignClientBuilder; +import org.springframework.context.ApplicationContext; +import org.springframework.stereotype.Component; + +@Component +public class DynamicFeignClientFactory { + + private FeignClientBuilder feignClientBuilder; + + public DynamicFeignClientFactory(ApplicationContext appContext) { + this.feignClientBuilder = new FeignClientBuilder(appContext); + } + + public T getFeignClient(final Class type, String serviceId) { + return this.feignClientBuilder.forType(type, serviceId).build(); + } +} diff --git a/feiginclientdemo/src/main/java/com/woniu/demo/feignclient/ProductClient.java b/feiginclientdemo/src/main/java/com/woniu/demo/feignclient/ProductClient.java new file mode 100644 index 0000000..6556766 --- /dev/null +++ b/feiginclientdemo/src/main/java/com/woniu/demo/feignclient/ProductClient.java @@ -0,0 +1,19 @@ +package com.woniu.demo.feignclient; + +import com.woniu.demo.entity.R; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.GetMapping; + +/** + * 手写一个动态Feign, + * 实现一个“万能”接口调用 + * + */ +@FeignClient(name = "product") +public interface ProductClient { + @GetMapping("/product/test1") + R test1(String test1); + + @GetMapping("/product/test2") + R test2(String test2); +} diff --git a/feiginclientdemo/src/main/java/com/woniu/demo/feignclient/UserClient.java b/feiginclientdemo/src/main/java/com/woniu/demo/feignclient/UserClient.java new file mode 100644 index 0000000..5b171da --- /dev/null +++ b/feiginclientdemo/src/main/java/com/woniu/demo/feignclient/UserClient.java @@ -0,0 +1,14 @@ +package com.woniu.demo.feignclient; + +import com.woniu.demo.entity.R; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.GetMapping; + +@FeignClient(name = "user") +public interface UserClient { + @GetMapping("/user/test1") + R test1(String test1); + + @GetMapping("/user/test2") + R test2(String test2); +} diff --git a/feiginclientdemo/src/main/java/com/woniu/demo/inter/DynamicService.java b/feiginclientdemo/src/main/java/com/woniu/demo/inter/DynamicService.java new file mode 100644 index 0000000..6fca197 --- /dev/null +++ b/feiginclientdemo/src/main/java/com/woniu/demo/inter/DynamicService.java @@ -0,0 +1,16 @@ +package com.woniu.demo.inter; + +import org.springframework.cloud.openfeign.SpringQueryMap; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; + +public interface DynamicService { + @PostMapping("{url}") + Object executePostApi(@PathVariable("url") String url, @RequestBody Object params); + + @GetMapping("{url}") + Object executeGetApi(@PathVariable("url") String url, @SpringQueryMap Object params); + +} diff --git a/feiginclientdemo/src/main/java/com/woniu/demo/test/DemoC.java b/feiginclientdemo/src/main/java/com/woniu/demo/test/DemoC.java new file mode 100644 index 0000000..7bcbe65 --- /dev/null +++ b/feiginclientdemo/src/main/java/com/woniu/demo/test/DemoC.java @@ -0,0 +1,15 @@ +package com.woniu.demo.test; + +import cn.hutool.extra.spring.SpringUtil; +import com.woniu.demo.feign.DynamicClient; + +import java.util.HashMap; + + +public class DemoC { + public static void main(String[] args) { + DynamicClient dynamicClient = SpringUtil.getBean(DynamicClient.class); + Object user = dynamicClient.executePostApi("user", "/system/test", new HashMap<>()); + + } +} diff --git a/feiginclientdemo/src/main/resources/application.yml b/feiginclientdemo/src/main/resources/application.yml new file mode 100644 index 0000000..6e32bd5 --- /dev/null +++ b/feiginclientdemo/src/main/resources/application.yml @@ -0,0 +1,6 @@ +server: + port: 8080 + +spring: + application: + name: feiginclientdemo \ No newline at end of file