动态创建FeignClient

之前在项目里面都是指定一个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

comments powered by Disqus