From 41b51974dbbea6041163726f8ae24610e8b49af4 Mon Sep 17 00:00:00 2001 From: DerekYRC <15521077528@163.com> Date: Thu, 7 Apr 2022 23:03:36 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9B=86=E6=88=90Feign=E7=AE=80=E5=8C=96?= =?UTF-8?q?=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pom.xml | 6 -- mini-spring-cloud-load-balancer/pom.xml | 1 - mini-spring-cloud-openfeign/pom.xml | 65 +++++++++++++++++++ .../cloud/openfeign/EnableFeignClients.java | 18 +++++ .../github/cloud/openfeign/FeignClient.java | 17 +++++ .../openfeign/FeignClientFactoryBean.java | 52 +++++++++++++++ .../openfeign/FeignClientsRegistrar.java | 45 +++++++++++++ pom.xml | 8 +++ 8 files changed, 205 insertions(+), 7 deletions(-) create mode 100644 mini-spring-cloud-openfeign/pom.xml create mode 100644 mini-spring-cloud-openfeign/src/main/java/com/github/cloud/openfeign/EnableFeignClients.java create mode 100644 mini-spring-cloud-openfeign/src/main/java/com/github/cloud/openfeign/FeignClient.java create mode 100644 mini-spring-cloud-openfeign/src/main/java/com/github/cloud/openfeign/FeignClientFactoryBean.java create mode 100644 mini-spring-cloud-openfeign/src/main/java/com/github/cloud/openfeign/FeignClientsRegistrar.java diff --git a/mini-spring-cloud-examples/mini-spring-cloud-consumer-examples/pom.xml b/mini-spring-cloud-examples/mini-spring-cloud-consumer-examples/pom.xml index a9c5f04..a545464 100644 --- a/mini-spring-cloud-examples/mini-spring-cloud-consumer-examples/pom.xml +++ b/mini-spring-cloud-examples/mini-spring-cloud-consumer-examples/pom.xml @@ -34,12 +34,6 @@ mini-spring-cloud-load-balancer - - io.github.openfeign - feign-core - test - - org.springframework.cloud spring-cloud-starter-feign diff --git a/mini-spring-cloud-load-balancer/pom.xml b/mini-spring-cloud-load-balancer/pom.xml index 9a963e0..82a27f2 100644 --- a/mini-spring-cloud-load-balancer/pom.xml +++ b/mini-spring-cloud-load-balancer/pom.xml @@ -54,7 +54,6 @@ org.springframework.boot spring-boot-starter-web - test diff --git a/mini-spring-cloud-openfeign/pom.xml b/mini-spring-cloud-openfeign/pom.xml new file mode 100644 index 0000000..99a980a --- /dev/null +++ b/mini-spring-cloud-openfeign/pom.xml @@ -0,0 +1,65 @@ + + + + mini-spring-cloud + com.github + 1.0.0-SNAPSHOT + + 4.0.0 + + mini-spring-cloud-openfeign + + + + com.github + mini-spring-cloud-load-balancer + + + + io.github.openfeign + feign-core + + + + org.springframework.boot + spring-boot-configuration-processor + true + + + + org.springframework.boot + spring-boot + true + + + + org.springframework.boot + spring-boot-autoconfigure + true + + + + org.springframework.boot + spring-boot-starter + true + + + + org.springframework.cloud + spring-cloud-commons + + + + org.springframework.cloud + spring-cloud-context + + + + org.springframework.boot + spring-boot-starter-web + + + + \ No newline at end of file diff --git a/mini-spring-cloud-openfeign/src/main/java/com/github/cloud/openfeign/EnableFeignClients.java b/mini-spring-cloud-openfeign/src/main/java/com/github/cloud/openfeign/EnableFeignClients.java new file mode 100644 index 0000000..3c51eb7 --- /dev/null +++ b/mini-spring-cloud-openfeign/src/main/java/com/github/cloud/openfeign/EnableFeignClients.java @@ -0,0 +1,18 @@ +package com.github.cloud.openfeign; + +import org.springframework.context.annotation.Import; + +import java.lang.annotation.*; + +/** + * 启用Feign + * + * @author derek(易仁川) + * @date 2022/4/7 + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +@Documented +@Import(FeignClientsRegistrar.class) +public @interface EnableFeignClients { +} diff --git a/mini-spring-cloud-openfeign/src/main/java/com/github/cloud/openfeign/FeignClient.java b/mini-spring-cloud-openfeign/src/main/java/com/github/cloud/openfeign/FeignClient.java new file mode 100644 index 0000000..ffdaa7b --- /dev/null +++ b/mini-spring-cloud-openfeign/src/main/java/com/github/cloud/openfeign/FeignClient.java @@ -0,0 +1,17 @@ +package com.github.cloud.openfeign; + +/** + * Feign客户端注解 + * + * @author derek(易仁川) + * @date 2022/4/7 + */ +public @interface FeignClient { + + /** + * 服务提供者应用名称 + * + * @return + */ + String value(); +} diff --git a/mini-spring-cloud-openfeign/src/main/java/com/github/cloud/openfeign/FeignClientFactoryBean.java b/mini-spring-cloud-openfeign/src/main/java/com/github/cloud/openfeign/FeignClientFactoryBean.java new file mode 100644 index 0000000..2141931 --- /dev/null +++ b/mini-spring-cloud-openfeign/src/main/java/com/github/cloud/openfeign/FeignClientFactoryBean.java @@ -0,0 +1,52 @@ +package com.github.cloud.openfeign; + +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.FactoryBean; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; + +/** + * 生成Feign客户端的FactoryBean + * + * @author derek(易仁川) + * @date 2022/4/7 + */ +public class FeignClientFactoryBean implements FactoryBean, ApplicationContextAware { + + private String contextId; + + private Class type; + + private ApplicationContext applicationContext; + + @Override + public Object getObject() throws Exception { + return null; + } + + @Override + public Class getObjectType() { + return this.type; + } + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + this.applicationContext = applicationContext; + } + + public String getContextId() { + return contextId; + } + + public void setContextId(String contextId) { + this.contextId = contextId; + } + + public Class getType() { + return type; + } + + public void setType(Class type) { + this.type = type; + } +} diff --git a/mini-spring-cloud-openfeign/src/main/java/com/github/cloud/openfeign/FeignClientsRegistrar.java b/mini-spring-cloud-openfeign/src/main/java/com/github/cloud/openfeign/FeignClientsRegistrar.java new file mode 100644 index 0000000..082b777 --- /dev/null +++ b/mini-spring-cloud-openfeign/src/main/java/com/github/cloud/openfeign/FeignClientsRegistrar.java @@ -0,0 +1,45 @@ +package com.github.cloud.openfeign; + +import cn.hutool.core.util.ClassUtil; +import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.beans.factory.support.GenericBeanDefinition; +import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; +import org.springframework.core.type.AnnotationMetadata; +import org.springframework.util.ClassUtils; + +import java.util.Set; + +/** + * 往bean容器中注册Feign客户端 + * + * @author derek(易仁川) + * @date 2022/4/7 + */ +public class FeignClientsRegistrar implements ImportBeanDefinitionRegistrar { + + /** + * 往bean容器中注册Feign客户端 + * + * @param importingClassMetadata + * @param registry + */ + @Override + public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { + //为FeignClient注解修饰的接口生成代理bean即Feign客户端,并注册到bean容器 + String packageName = ClassUtils.getPackageName(importingClassMetadata.getClassName()); + //扫描所有被FeignClient注解修饰的接口 + Set> classes = ClassUtil.scanPackageByAnnotation(packageName, FeignClient.class); + for (Class clazz : classes) { + GenericBeanDefinition beanDefinition = new GenericBeanDefinition(); + //使用FeignClientFactoryBean生成Feign客户端 + beanDefinition.setBeanClass(FeignClientFactoryBean.class); + String clientName = clazz.getAnnotation(FeignClient.class).value(); + beanDefinition.getPropertyValues().addPropertyValue("contextId", clientName); + beanDefinition.getPropertyValues().addPropertyValue("type", clazz); + + //将Feign客户端注册进bean容器 + String beanName = clazz.getName(); + registry.registerBeanDefinition(beanName, beanDefinition); + } + } +} diff --git a/pom.xml b/pom.xml index fb339f5..eb28a04 100644 --- a/pom.xml +++ b/pom.xml @@ -22,6 +22,7 @@ mini-spring-cloud-examples/mini-spring-cloud-provider-example mini-spring-cloud-examples/mini-spring-cloud-consumer-examples mini-spring-cloud-load-balancer + mini-spring-cloud-openfeign @@ -29,6 +30,7 @@ 1.2.79 5.7.21 2.3.0 + 11.8 @@ -90,6 +92,12 @@ ribbon-core ${ribbon.version} + + + io.github.openfeign + feign-core + ${feign.version} +