title: ‘SpringCloudAlibaba-3-nacos服务注册与发现整合feign:webflux调用’
date: 2020-01-26 22:36:11
tags:
SpringCloudAlibaba
1.Feign
Feign是从Netflix中分离出来的轻量级项目,能够在类接口上添加注解,成为一个RES API客户端。
2.OpenFegin
Spring Cloud在netflix feign的基础上扩展了支持Spring MVC注解,并通过自动配置为Spring Boot应用程序提供快速集成。
3.使用Fegin
1.复制nacos-discovery-consumer项目,修改maven坐标及其在父工程里面添加module即可。
2.在nacos-discovery-consumer-fegin引入fegin依赖
|
|
3.开启feign
|
|
4.nacos-discovery-consumer-fegin调用nacos-discovery-provider
a.创建feign的cient端口,并且其是nacos-discovery-provider的调用接口
接口:HelloFeignService
|
|
@RestController
public class TestController {
@Autowired
private HelloFeignService helloFeignService;
@GetMapping(“/test”)
public String test(String name){
return helloFeignService.hello(name);
}
}
@EnableDiscoveryClient
@SpringBootApplication
public class NacosDiscoveryConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(NacosDiscoveryConsumerApplication.class, args);
}
@Bean
@LoadBalanced
public WebClient.Builder webClientBuilder(){
return WebClient.builder();
}
}
@RestController
public class TestController {
@Autowired
private WebClient.Builder webClientBuilder;
@GetMapping(“/test”)
public Mono
return webClientBuilder.build()
.get()
.uri(“http://nacos-discovery-provider/hello?name=“ + name)
.retrieve()
.bodyToMono(String.class);
}
}
```