ribbon实现步骤
- 一:创建eureka,两个producter服务(不做介绍),一个ribbon服务
- 二:ribbon服务
- pom
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
<version>1.4.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
- application.properties配置
server.port= 9010
spring.application.name= server-ribbon
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:9000/eureka
- 新建collection
@RestController
public class TestController {
@Autowired
private LoadBalancerClient loadBalancerClient;
@RequestMapping("/test")
public String test() throws ExecutionException, InterruptedException {
//server-producer为 producer服务的 spring.application.name=server-producer
//两个服务的名称一致,端口不一致,(本demo分别为:9101和9101)
ServiceInstance serviceInstance = loadBalancerClient.choose("server-producer");
//负载均衡算法默认是轮询,轮询取得服务
URI uri = URI.create(String.format("http://%s:%s", serviceInstance.getHost(), serviceInstance.getPort()));
return uri.toString();
}
}
- 在原有基础上进行添加,(本例中两个producter服务,接口路径,服务名均相同,除端口,test接口返回值不同,其余相同)
- application.properties配置添加
server.port= 9010
spring.application.name= server-ribbon
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:9000/eureka
//为特定的服务配置负载均衡策略(server-producer服务名)
server-producer.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule
- 在ribbon服务启动类添加,和@EnableDiscoveryClient注解
@SpringBootApplication
@EnableDiscoveryClient
public class RibbonApplication {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(RibbonApplication.class, args);
}
}
- 在testCollection添加test1方法
@Autowired
private LoadBalancerClient loadBalancerClient;
//注入 RestTemplate
@Autowired
private RestTemplate restTemplate;
@GetMapping("/test1")
public String test1() {
ServiceInstance serviceInstance = loadBalancerClient.choose("server-producer");
//http://这里是服务名(server-producer)/这里collection对应路径(test)
String s = this.restTemplate.getForObject("http://server-producer/test", String.class);
return s+serviceInstance.getPort();
}
- 启动测试
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c8f9d8f01f688aec1f6f6a94aa033bbe.jpeg)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d99b68d5bc98f662f0cb1d0ea03f0e5d.jpeg)
至此简单的轮询调用完成
结合 - 断路器使用
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.4.5.RELEASE</version>
</dependency>
- 启动类添加@EnableHystrix注解
- 修改testCollection
//fallbackMethod = "getServiceError" getServiceError为调用方法
@HystrixCommand(fallbackMethod = "getServiceError")
@GetMapping("/test1")
public String test1() {
ServiceInstance serviceInstance = loadBalancerClient.choose("server-producer");
String s = this.restTemplate.getForObject("http://server-producer/test", String.class);
return s+serviceInstance.getPort();
}
public String getServiceError() {
return "出现错误!!!!;调用getServiceError()";
}