当前位置:   article > 正文

SpringCloud集成zookeeper&Feign API_feignclient调用zookeeper中的api

feignclient调用zookeeper中的api

SpringBoot 2.0增加了zookeeper作为注册中心,使用Feign API 的方式演示使用zookeeper作为注册中心,spring-boot-zookeeper 同样内置实现了服务发现,负载均衡等

下面是使用示例

  1. @EnableFeignClients(clients = HelloApi.class)
  2. @EnableDiscoveryClient
  3. @SpringBootApplication
  4. public class SystemApplication {
  5. public static void main(String[] args) {
  6. SpringApplication.run(SystemApplication.class, args);
  7. }
  8. }

zookeeper配置

  1. #zookeeper注册中心配置
  2. #Connection string to the Zookeeper cluster
  3. spring.cloud.zookeeper.connectString=localhost:2181
  4. #Is Zookeeper enabled
  5. spring.cloud.zookeeper.enabled=true

定义Feign API

  1. @FeignClient(value = "spring-boot-zookeeper-demo", path = "api/hello")
  2. public interface HelloApi {
  3. @RequestMapping(value = "/{name}/getUniqueId")
  4. public String getUniqueId(@PathVariable("name") String name);
  5. @RequestMapping(value = "/getValue")
  6. public String getValue(@RequestParam(value = "id") int id);
  7. }

服务提供者

  1. /**
  2. * 服务端-API
  3. */
  4. @RestController
  5. @RequestMapping("/api/hello")
  6. public class HelloServerController implements HelloApi {
  7. @RequestMapping(value = "/{name}/getUniqueId")
  8. public String getUniqueId(@PathVariable("name") String name) {
  9. System.out.println("SERVER--->>>>"+name);
  10. return "hello world->"+name+" "+new Random().nextDouble();
  11. }
  12. @RequestMapping(value = "/getValue")
  13. public String getValue(@RequestParam(value = "id") int id) {
  14. return "ok:"+new Random().nextDouble();
  15. }
  16. }

服务消费者示例

  1. /**
  2. * 客户端测试
  3. */
  4. @RestController()
  5. public class HelloClientController {
  6. @Autowired
  7. HelloApi helloApi;
  8. @RequestMapping(value="/test/feign")
  9. public Object test(){
  10. return helloApi.getUniqueId("zhangsan");
  11. }
  12. }

详细了解可以下载源码:  https://github.com/kevinLuan/spring-boot-zookeeper-demo.git

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/53965
推荐阅读
相关标签
  

闽ICP备14008679号