使用IntelliJ IDEA在Spring Cloud的Ribbon项目中使用Hystrix熔断器

Eureka注册中心:《使用IntelliJ IDEA创建Spring Cloud服务注册中心

服务提供者创建:《使用IntelliJ IDEA创建Spring Cloud的Eureka Client

Ribbon实现负载均衡:《使用IntelliJ IDEA创建Ribbon项目实现负载均衡

集成Feign的项目:《使用IntelliJ IDEA创建集成Feign的项目简化服务调用的网络连接
在微服务架构里,一个系统会有很多的服务。若服务A要调用服务B,服务B要调用服务C,即A—>B—>C。此时,如果其中的服务C挂掉,无法正常访问了,导致服务B调用服务C的时候,都会卡住几秒钟,然后抛出—个超时异常。如果系统处于高并发的场景下,存在大量访问请求,线程都会卡在请求服务C这块,会导致服务B可用线程逐渐被耗尽,服务B不可用挂掉,进而可能导致服务A的业务不可用。以此类推,就可能产生微服务架构中的服务雪崩问题。本文将讲述Ribbon项目中使用Hystrix熔断器来解决该问题。

创建Ribbon项目

File---new---module---Spring Assistant

点击next

点击next

点击Finish

AcyxribbonApplication.java

package com.acyx.ribbon;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
public class AcyxribbontwoApplication {

	public static void main(String[] args) {
		SpringApplication.run(AcyxribbontwoApplication.class, args);
	}

	@Bean
	@LoadBalanced
	public RestTemplate restTemplate() {
		return new RestTemplate();
	}
}

HomeRibbonService.java

package com.acyx.ribbon.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class HomeRibbonService {

    @Autowired
    private RestTemplate restTemplate;

    public String homeRibbon(String name) {

        return restTemplate.getForObject("http://ACYX-STOCK/home?name=" + name, String.class);
    }
}

HomeRibbonController.java

package com.acyx.ribbon.controller;

import com.acyx.ribbon.service.HomeRibbonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HomeRibbonController {

    @Autowired
    private HomeRibbonService homeRibbonService;

    @RequestMapping("/homeRibbon")
    public String homeRibbon(String name){
        return homeRibbonService.homeRibbon(name);
    }

}

application.yml

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8901
spring:
  application:
    name: acyx-ribbon

配置Hystrix

参考官方文档:https://cloud.spring.io/spring-cloud-static/spring-cloud-netflix/2.2.2.RELEASE/reference/html/#how-to-include-hystrix

在acyxribbontwo的pom.xml中添加如下依赖

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

在AcyxribbontwoApplication.java中添加注解@EnableHystrix,如下图

修改HomeRibbonService.java,添加注解@HystrixCommand(fallbackMethod = "homeError" )给指定的方法开启熔断,并使用fallbackMethod指定熔断后的回调方法。

package com.acyx.ribbon.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class HomeRibbonService {

    @Autowired
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "homeError" )
    public String homeRibbon(String name) {
        return restTemplate.getForObject("http://ACYX-STOCK/home?name=" + name, String.class);
    }

    public String homeError(String name) {
        return "sorry, this is ribbon error! Hystrix fallbackMethod execute";
    }
}

依次启动Eureka注册中心、acyxstock、acyxstocktwo、acyxribbontwo,然后在浏览器中访问:http://127.0.0.1:8761

如上图所示,ACYX-RIBBON已在注册中心成功注册。

此时,在浏览器中访问:http://127.0.0.1:8901/homeRibbon?name=Ribbon

刷新

再次刷新

此时,将acyxstock、acyxstocktwo这两个项目关闭,即将ACYX-STOCK服务关闭了,然后在浏览器中访问:http://127.0.0.1:8761

虽然在上图中ACYX-STOCK服务依然显示存在,但这acyxstock、acyxstocktwo这两个项目确实已经是关闭了。

在浏览器中访问:http://127.0.0.1:8901/homeRibbon?name=Ribbon

此时,再次启动acyxstock、acyxstocktwo这两个项目,刷新浏览器,如下图

再次刷新

猜你喜欢

转载自blog.csdn.net/chenbinqq/article/details/105860135