SpringC...

spring cloud微服务架构(五):spring clou

2018-08-03  本文已影响0人  mxjsxz

1 spring cloud整合Hystrix

Hystrix主要用来保护调用者这一方的服务,所以Eureka服务器和服务提供的代码和第一篇文章中相同,只需要修改服务调用者的代码即可。

创建一个spring boot项目用于服务调用,pom文件中增加Hystrix依赖如下:

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

在启动类上,加入熔断器的注解@EnableCircuitBreaker

@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
public class ConSumerApp {
    
    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
    
    public static void main(String[] args) {
        new SpringApplicationBuilder(ConSumerApp.class)
        .web(true).run(args);
    }

}

创建一个服务类,用于编写Hystrix命令。使用注解@HystrixCommand表明getMember方法是一个Hystrix命令

  1. fallbackMethod参数指名回退方法;
  2. commandProperties参数用来配置命令,本例中配置超时时间为1秒;
  3. threadPoolProperties参数用来配置线程池,本例中配置线程池并发数为2。
@Service
//@DefaultProperties(defaultFallback = "getMemberFallback")
public class MemberService {

    @Autowired
    private RestTemplate restTpl;

    @HystrixCommand(fallbackMethod = "getMemberFallback", 
            commandProperties = {
            @HystrixProperty(name = "execution.isolation
            .thread.timeoutInMilliseconds", value = "1000")
    }, threadPoolProperties = {
            @HystrixProperty(name = "coreSize", value = "2")
    })
    public Member getMember(Integer id) {
        try {
            Thread.sleep(2000);
        } catch (Exception e) {
            
        }
        
        Member member = restTpl.getForObject(
                "http://spring-hy-member/member/{id}", Member.class, id);
        return member;
    }

    public Member getMemberFallback(Integer id) {
        Member m = new Member();
        m.setId(1);
        m.setName("error member");
        return m;
    }
}

下面编写测试类,由于设置超时时间为1秒,故会执行回退方法。

@RestController
public class TestController {

    @Autowired
    private MemberService memberService;

    @RequestMapping(value = "/router", method = RequestMethod.GET, 
            produces = MediaType.APPLICATION_JSON_VALUE)
    public Member router() {
        return memberService.getMember(1);
    }

}

2 Hystrix 监控

为服务调用者加入Actuator依赖,可以对服务调用者的健康情况进行实时监控。

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>1.5.3.RELEASE</version>
        </dependency>

然后新建一个监控spring boot项目,pom文件内容如下:

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>1.5.3.RELEASE</version>
        </dependency>
    </dependencies>

启动类代码如下:

@SpringBootApplication
@EnableHystrixDashboard
public class DashboardApp {

    public static void main(String[] args) {
        new SpringApplicationBuilder(DashboardApp.class).properties("server.port=8082").run(args);
    }

}

访问项目地址:http://localhost:8082/hystrix,显示监控页面

image.png

输入要监控的项目地址:http://localhost:8081/hystrix.stream,能够看到该项目的命令调用情况。

image.png

第一排Circuit为熔断器使用情况;第二排为线程池使用情况。

上一篇下一篇

猜你喜欢

热点阅读