Feign是一个声明式的Web Service客户端,它使编写Web Service客户端变得容易。Spring Cloud为Feign客户端添加了Spring MVC的注解支持,Feign在整合了Ribbon后可以提供负载均衡功能。
spring.application.name=open-feign
server.port=50006
eureka.client.fetch-registry=true
eureka.client.register-with-eureka=false
eureka.client.service-url.defaultZone=http://eureka01:50001/eureka/,http://eureka02:50002//eureka/
@SpringBootApplication
@EnableDiscoveryClient //开启客户端发现
@EnableFeignClients //开启Feign支持
public class OpenFeignApplication {
public static void main(String[] args) {
SpringApplication.run(OpenFeignApplication.class, args);
}
}
@FeignClient(name = "provider")
public interface FeignClients {
@GetMapping("/hello")
public String hello();
}
@RestController
public class HelloController {
@Autowired
FeignClients feignClients;
@GetMapping("/hello")
public String index(){
return feignClients.hello();
}
}
访问http://localhost:50006/hello
经验: