springboot的ribbon通信
2022-06-25 11:36:12 3 举报
登录查看完整内容
springboot的ribbon通信
作者其他创作
大纲/内容
server: port: 8761eureka: instance: hostname: localhost client: register-with-eureka: false # 本服务是否在注册中心注册为一个服务 fetch-registry: false # 从其他注册中心拉取服务信息 (不拉取) service-url: # 注册地址信息 defaultZone: http://localhost:8761/eureka
生产者
producer
提供一个web服务的接口访问
yml配置
server: port: 8081spring: application: name: producer-serviceeureka: client: service-url: defaultZone: http://localhost:8761/eureka
消费者
使用Ribbon通信依赖spring-cloud-starter-netflix-ribbon
SpringCloud
在启动类添加注解声名这是Eureka的启动中心@EnableEurekaClient @EnableDiscoveryClient //开启注册中心客户端服务
consumer
server: port: 8083spring: application: name: consumer-serviceeureka: client: service-url: defaultZone: http://localhost:8761/eureka
public String fetchInfo(String name){// String uri = \"http://localhost:8081/hello?name=\"+name; String uri = \"http://producer-service/hello?name=\
@Bean @LoadBalanced public RestTemplate restTemplate(){ return new RestTemplate();// return new RestTemplateBuilder().build(); }@Bean //配置负载均衡策略 public IRule myrule(){ return new RoundRobinRule() ;//轮询负载均衡此略 }}
首先配置注册erueka
在启动类添加注解声名这是Eureka的启动中心@EnableEurekaServer //开启服务注册中心
注册中心
Eureka
导入eureka依赖spring-cloud-starter-netflix-eureka-client
收藏
0 条评论
回复 删除
下一页