新建一个Springboot项目hystrix_ribbon_server1。
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--eureka ribbon-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<!--hystrix-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!--springcloud依赖管理-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<spring-cloud.version>Finchley.SR2</spring-cloud.version>
server:
port: 8017
spring:
application:
name: hystrix-ribbon-server
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
4.5步骤和前文的Ribbon一样。
package com.java4all.hystrix_ribbon_server1;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@EnableHystrix
@EnableEurekaClient
@SpringBootApplication
public class HystrixRibbonServer1Application {
/**
*
* @Bean : 注入一个名为restTemplate的bean
* @LoadBalanced :表明这个 restTemplate 开启了负载均衡的功能
*
* */
@Bean
@LoadBalanced
RestTemplate restTemplate(){return new RestTemplate();}
public static void main(String[] args) {
SpringApplication.run(HystrixRibbonServer1Application.class, args);
}
}
写一套简单的业务代码,然后调用前面创建的company-server服务提供的接口。
@RestController
@RequestMapping(value = "hrs")
public class HrsController {
@Autowired
private HrsService hrsService;
@GetMapping(value = "getCompany")
public String getCompany(String id){
String company = hrsService.getCompany(id);
System.out.println(company);
return company;
}
}
public interface RibbonService {
String getCompany(String id);
}
在serviceImpl实现类中,我们引入前面的RestTemplate模板类,来跨服务调用,并开启了负载均衡功能。
在getCompany方法上添加@HystrixCommand注解。该注解对该方法创建了熔断器的功能,并指定了fallbackMethod熔断方法erroCompany,熔断方法直接返回了一个字符串
@Service
public class HrsServiceImpl implements HrsService{
//启动类中注入了此模板,并且开启了负载均衡功能
@Autowired
RestTemplate restTemplate;
//给方法添加熔断器的功能,并指定熔断方法
@HystrixCommand(fallbackMethod = "erroCompany")
@Override
public String getCompany(String id) {
//程序名替代服务地址,ribbon会根据服务名自动选择服务实例
String company = restTemplate
.getForObject("http://company-server/company/get?id=" + id, String.class);
return company;
}
/**熔断方法*/
public String erroCompany(String id){
return "服务出错,返回默认企业:"+id;
}
}
此时,启动项目,访问http://localhost:8017/hrs/getCompany?id=234,我们这个项目的接口会去调用前面启动的企业服务,由于企业服务启动了两台:8011,8014,那么,通过ribbon负载均衡后,会分发到两台服务上。如下:
当我们把8011挂掉后,此时,由于eureka更新服务列表默认时间为30s,这之间,如果请求落在8011上,那么,就会调用熔断方法,返回指定的结果,如下: