在学习中总会遇到很多问题,本例子可做参考用。
在微服务中的ribbon用于访问其它微服务中的节点,而hystrix用于在访问其它微服务的节点失败时所返回的信息,当访问的那个微服务挂掉了,或超时了,就会走@HystrixCommand(fallbackMethod = "fallbackIndex")中所指定的方法来反馈给用户,也能防止级联效应或雪崩效应。
在学习的过程中需要用到的jar:
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.cloud
spring-cloud-starter-netflix-hystrix
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-starter-netflix-turbine
org.springframework.cloud
spring-cloud-starter-netflix-hystrix-dashboard
首先是在启动类上配置必要的信息和需要用到的标签:
@SpringBootApplication
@EnableDiscoveryClient
@RibbonClient(name="eureka-ribbon-hystrix") //使用ribbon用的标签,name参数用于指定要访问的那个项目名称
@ComponentScan("com.自己包名.controller")
@EnableCircuitBreaker //在hystrix中需要的标签
@EnableHystrix //开启hystrix
@EnableTurbine //使用集群管理
@EnableHystrixDashboard //使用Dashboard来查看各个微服务的使用状况
public class SpringCloudRibbonHytrixApplication {
//ribbon的配置内容
@Bean
// 负载功能
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(SpringCloudRibbonHytrixApplication.class, args);
}
//用于配置/actuator/hystrix.stream打印信息的访问路径。
@Bean
public ServletRegistrationBean getServlet(){
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/actuator/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
}
然后在controller中写的访问点:
@Controller
public class EurekaController {
Logger logger= LoggerFactory.getLogger(getClass());
//ribbon中需要的对象。
@Autowired
RestTemplate restTemplate;
@RequestMapping("/index")
@ResponseBody
@HystrixCommand(fallbackMethod = "fallbackIndex") //当访问失败时,自动访问fallbackIndex方法。
public Object index() {
logger.info("我是ribbon-hystrix种的服务正在被访问中……");
// 填写第一个参数为其它Eureka服务器的项目名,也可以自己指定项目名,第二个是返回类型
return restTemplate.getForObject("http://eureka-ribbon/index", String.class);
}
public Object fallbackIndex() {
return "index主界面访问失败了!";
}
}
然后是配置文件中的内容,本人采用properties和yml两中格式来共同配置本项目:
spring.application.name=ribbon-hystrix-turbine
server.port=8063
eureka.client.service-url.defaultZone= http://localhost:80/eureka/
#hystrix的超时时间
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=5000
#配置burbine要集群的项目名称和方式,这是默认的所有集群。
turbine:
appConfig: ribbon-hystrix-turbine,RIBBON-HYSTRIX
clusterNameExpression: "'default'"
最后运行后的效果:
注册在eureka服务中的客户端
用端口为8060和8063的微服务访问端口为8091的/index节点,会得到:
打印什么自己可随便弄。
然后访问http://localhost:8063/turbine.stream节点会得到如下信息:
需要注意的时,如果在没有先访问/index的情况下,可能没有如下信息,只有一个ping单词。
在启动类中,还配置了单个查看信息的功能http://localhost:8063/actuator/hystrix.stream节点,访问它效果和上面相同,但是数据不同:
当然有了上面的数据后,我看起来很不方便,在启动类中还配置了dashboard用于查看的工具,在使用的时候这个工具比较的灵活,首先可以把单个的/actuator/hystrix.stream查看节点放上来,也可以吧/turbine.stream的节点放上来,访问路径http://localhost:8063/hystrix可看到下面效果:
在地址中输入http://localhost:8063/turbine.stream或http://localhost:8063/actuator/hystrix.stream都行。
输入完成后下面两项不用管:
直接点击下方的
得到的效果如下:
前面说了输入http://localhost:8063/turbine.stream或http://localhost:8063/actuator/hystrix.stream都行,但是效果不同,前者如果监控的多个集群,所有信息都能看到,如果是后者,只能看到某一个项目的信息内容。
最后由于本8060和8063端口项目访问的是8091端口项目中节点,如果将8091端口项目关闭将得到如下效果:
如果查看本博客的童鞋有其它疑问,可以查看spring cloud官方文档中的说明,本博客只做参考例子而已。