Feign服务调用
在服务的消费方使用Feign远程调用提供方的服务
pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
使用接口调用
在消费方创建调用提供方的接口
@FeignClient("nacos-provider")
public interface ProviderFeignClient {
@RequestMapping(value = "/provider/{string}", method = RequestMethod.GET)
public String echo(@PathVariable String string);
}
controller
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients("cn.zysheep")
public class CosumerApplication {
public static void main(String[] args) {
SpringApplication.run(CosumerApplication.class, args);
}
@RestController
@RefreshScope //支持Nacos的动态刷新功能
public class TestController{
@Autowired
private ProviderFeignClient providerFeignClient;
@GetMapping("/testFeignClient")
public String testFeignClient(){
//测试nacos config
String testFeignClient = providerFeignClient.echo("TestFeignClient");
return testFeignClient;
}
}
}