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

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

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

server:
  ##服务端口
  port: 3333
spring:
  application:
    ##服务名称
    name:consul
cloud:
  consul:
    port: 8500
    host: localhost
    discovery:
      ##服务地址直接为ip地址
      hostname: 192.168.1.1xx

 

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://consul/index",String.class);
}

 

posted @ 2020-01-15 17:39  fishness  阅读(1252)  评论(0)    收藏  举报