集成Feign简化调用

open-feign
DerekYRC 3 years ago
parent cbd9092c4e
commit 41b51974db

@ -34,12 +34,6 @@
<artifactId>mini-spring-cloud-load-balancer</artifactId> <artifactId>mini-spring-cloud-load-balancer</artifactId>
</dependency> </dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-core</artifactId>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.cloud</groupId> <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId> <artifactId>spring-cloud-starter-feign</artifactId>

@ -54,7 +54,6 @@
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
<scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>

@ -0,0 +1,65 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>mini-spring-cloud</artifactId>
<groupId>com.github</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mini-spring-cloud-openfeign</artifactId>
<dependencies>
<dependency>
<groupId>com.github</groupId>
<artifactId>mini-spring-cloud-load-balancer</artifactId>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>

@ -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 {
}

@ -0,0 +1,17 @@
package com.github.cloud.openfeign;
/**
* Feign
*
* @author derek()
* @date 2022/4/7
*/
public @interface FeignClient {
/**
*
*
* @return
*/
String value();
}

@ -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;
/**
* FeignFactoryBean
*
* @author derek()
* @date 2022/4/7
*/
public class FeignClientFactoryBean implements FactoryBean<Object>, 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;
}
}

@ -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;
/**
* beanFeign
*
* @author derek()
* @date 2022/4/7
*/
public class FeignClientsRegistrar implements ImportBeanDefinitionRegistrar {
/**
* beanFeign
*
* @param importingClassMetadata
* @param registry
*/
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
//为FeignClient注解修饰的接口生成代理bean即Feign客户端并注册到bean容器
String packageName = ClassUtils.getPackageName(importingClassMetadata.getClassName());
//扫描所有被FeignClient注解修饰的接口
Set<Class<?>> 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);
}
}
}

@ -22,6 +22,7 @@
<module>mini-spring-cloud-examples/mini-spring-cloud-provider-example</module> <module>mini-spring-cloud-examples/mini-spring-cloud-provider-example</module>
<module>mini-spring-cloud-examples/mini-spring-cloud-consumer-examples</module> <module>mini-spring-cloud-examples/mini-spring-cloud-consumer-examples</module>
<module>mini-spring-cloud-load-balancer</module> <module>mini-spring-cloud-load-balancer</module>
<module>mini-spring-cloud-openfeign</module>
</modules> </modules>
<properties> <properties>
@ -29,6 +30,7 @@
<fastjson.version>1.2.79</fastjson.version> <fastjson.version>1.2.79</fastjson.version>
<hutool.version>5.7.21</hutool.version> <hutool.version>5.7.21</hutool.version>
<ribbon.version>2.3.0</ribbon.version> <ribbon.version>2.3.0</ribbon.version>
<feign.version>11.8</feign.version>
</properties> </properties>
<dependencyManagement> <dependencyManagement>
@ -90,6 +92,12 @@
<artifactId>ribbon-core</artifactId> <artifactId>ribbon-core</artifactId>
<version>${ribbon.version}</version> <version>${ribbon.version}</version>
</dependency> </dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-core</artifactId>
<version>${feign.version}</version>
</dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>

Loading…
Cancel
Save