注册中心为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);
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM