zookeeper+openfeign远程调用
Posted isTrueLoveColour
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了zookeeper+openfeign远程调用相关的知识,希望对你有一定的参考价值。
首先确保zookeeper集群可用
所需依赖为:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
服务1配置文件
server:
port: 9010
spring:
application:
name: zkServer1
cloud:
zookeeper:
connect-string: 192.168.158.158:2181,192.168.158.159:2181,192.168.158.160:2181
服务2配置文件
server:
port: 9020
spring:
application:
name: zkServer2
cloud:
zookeeper:
connect-string: 192.168.158.158:2181,192.168.158.159:2181,192.168.158.160:2181
服务启动类
@SpringBootApplication
//开启openfeign
@EnableFeignClients
//向zookeeper注册
@EnableDiscoveryClient
public class ZookeeperApplication
public static void main(String[] args)
SpringApplication.run(ZookeeperApplication.class, args);
服务1代码
@RestController
@RequestMapping("/zk1")
public class ZkController
@Autowired
private ZkFeignService zkFeignService;
@GetMapping("/zk1return")
public String zk1return()
return "服务2调用1成功";
@GetMapping("/test")
public String test()
return zkFeignService.test();
@Component
@FeignClient(value = "zkServer2")
public interface ZkFeignService
@GetMapping("/zk2/zk2return")
public String test();
服务2代码
@RestController
@RequestMapping("/zk2")
public class ZkController
@Autowired
private ZkFeignService zkFeignService;
@GetMapping("/test")
public String test()
return zkFeignService.test();
@GetMapping("/zk2return")
public String zk1return()
return "服务1调用2成功";
@Component
@FeignClient(value = "zkServer1")
public interface ZkFeignService
@GetMapping("/zk1/zk1return")
public String test();
然后两个服务就可以通过指定接口相互访问
以上是关于zookeeper+openfeign远程调用的主要内容,如果未能解决你的问题,请参考以下文章
SpringCloud系列——openfeign远程服务调用实战