注册中心为zookeeper版,springcloud整合Ribbon负载均衡

1.创建两个demo(我使用的是springboot)项目,分别整合了zookeeper,端口分别2222,3333

2.yml配置(两个分别配置注册到zookeeper)

server:
##服务端口
port: 3333
spring:
application:
##服务名称
name:zk
cloud:
zookeeper:
##注册到zookeeper地址
connect-string: 127.0.0.1:2181

3.启动类加入注解

@EnableDiscoveryClient

4.即可使用rpc远程调用测试一下

 1.启动类加入RestTemplate(RestTemplate 是从 Spring3.0 开始支持的一个 HTTP 请求工具)

@Bean
@LoadBalanced //开启别名方式 获取注册信息 本地实现rpc远程调用
RestTemplate restTemplate(){
return new RestTemplate();
}

 2.

@Autowired
private RestTemplate restTemplate;
@RequestMapping(value = "rpc")
public String rpc(){
return restTemplate.getForObject("http://zk/index",String.class);
}

猜你喜欢

转载自www.cnblogs.com/fishness/p/12197913.html