之前在项目里面都是指定一个interface做FeignClient,带上FeignClient的注解,这样子每一个微服务都需要有个interface。在做中台项目的时候需要通过配置化来创建FeignClient,就想到怎么手动去创建FeignClient。一番操作猛如虎后,写个memo以防自己以后忘记了。
创建eureka服务
在 https://start.spring.io/
中勾选Eureka Server,下载下来后:
代码:Application类上加注解 @EnableEurekaServer
配置文件:
server.port=8761 spring.application.name=eureka-server eureka.instance.hostname=localhost eureka.client.register-with-eureka=false eureka.client.fetch-registry=false eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
启动即可,访问 http://localhost:8761
可以看到eureka的页面
创建服务端
在 https://start.spring.io/
中勾选Eureka Discovery Client,下载下来后:
代码:Application类上加注解:
@EnableDiscoveryClient //启用服务发现客户端 @SpringBootApplication @EnableFeignClients //启用feignClient
pom.xml文件如果没有以下两项的话需要加上:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
自己写个Controller:
@RestController @RequestMapping("/test") public class TestController { @PostMapping("/test") public boolean test(@RequestBody Test test) { return true; } }
配置文件:
spring.application.name=feign-server server.port=8001 eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka
启动后查看 eureka,看到feign-server被注册上了。
创建消费端
在 https://start.spring.io/
中勾选Eureka Discovery Client,下载下来后:
pom.xml文件如果没有以下两项的话需要加上:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
创建一个feignClient但是不用注解FeignClient:
public interface PersonFeignClient {
@RequestMapping(value = "/test/test", method = RequestMethod.POST)
boolean personTest(@RequestBody Test test);
}
动态创建FeignClient的类:
@Component @Import(FeignClientsConfiguration.class) public class PersonTest { //自定义的FeignClient PersonFeignClient personFeignClient; //Feign 原生构造器 Feign.Builder builder; //创建构造器 public PersonTest(Decoder decoder, Encoder encoder, Client client, Contract contract) { this.builder = Feign.builder() .client(client) .encoder(encoder) .decoder(decoder) .contract(contract); } public void test() { String serviceId="feign-server"; Test test = new Test(); test.setTest(false); //注意 构建的url 必须添加 http:// this.personFeignClient = this.builder.target(PersonFeignClient.class, "http://" + serviceId); //使用FeignClient请求服务 boolean result = this.personFeignClient.personTest(test); System.out.println(result); } }
使用的地方:
@RestController @RequestMapping("/test") public class TestController { @Autowired PersonTest personTest; ... @GetMapping("/test3") public boolean test3() { personTest.test(); return true; } }
配置文件:
spring.application.name=feign-client server.port=8002 eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
启动后,请求http://localhost:8001/test/test3 即可访问。
参考: https://www.cnblogs.com/yunxi520/articles/12037168.html
http://blog.didispace.com/spring-cloud-starter-dalston-2-3/
https://cloud.tencent.com/developer/article/1483067
原文
http://yizhanggou.top/dong-tai-chuang-jian-feignclient/
本站部分文章源于互联网,本着传播知识、有益学习和研究的目的进行的转载,为网友免费提供。如有著作权人或出版方提出异议,本站将立即删除。如果您对文章转载有任何疑问请告之我们,以便我们及时纠正。PS:推荐一个微信公众号: askHarries 或者qq群:474807195,里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

转载请注明原文出处:Harries Blog™ » 动态创建FeignClient