springcloud hystrix接口监控

1.新建项目hystrixDashboard, pom.xml中hystrix相关依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<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>

2.hystrixDashboard application.properties

server.port=8080
spring.application.type=hystrixDashboard
spring.application.group=group
spring.application.name=${spring.application.group}-${spring.application.type}
dashboard.register.host=http://
eureka.client.service-url.defaultZone=${dashboard.register.host}/eureka
eureka.instance.lease-renewal-interval-in-seconds=10
eureka.instance.lease-expiration-duration-in-seconds=30
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.cloud.client.ipAddress}:${server.port}
eureka.client.eureka-server-read-timeout-seconds=60

3.hystrixDashboard 启动入口配置

@EnableEurekaClient
@SpringBootApplication
@EnableHystrix
@EnableHystrixDashboard
public class HystrixDashboardApplication {

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

4. 启动后访问 http://localhost:8080/hystrix


5. 新建测试项目hystrix, pom.xml依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>

6.hystrix application.properties

server.port=8081
spring.application.type=hystrix
spring.application.group=group
spring.application.name=${spring.application.group}-${spring.application.type}
dashboard.register.host=http://
eureka.client.service-url.defaultZone=${dashboard.register.host}/eureka
eureka.instance.lease-renewal-interval-in-seconds=10
eureka.instance.lease-expiration-duration-in-seconds=30
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.cloud.client.ipAddress}:${server.port}
eureka.client.eureka-server-read-timeout-seconds=60

7. 测试项目启动入口配置

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@RestController
@EnableHystrix
public class HystrixApplication {

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

    @RequestMapping("/test")
    @HystrixCommand(fallbackMethod = "error")
    public String test(String name) {
        return new RestTemplate().getForObject("http://一个不可访问的地址/hi?name=" + name, String.class);
    }

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

    public String error(String name) {
        return "error";
    }
}

8. 启动hystrix, 访问http://localhost:8081/test?name=123

9.访问hystrix dashboard





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud是一个用于构建分布式系统的开发工具集合,它提供了许多有用的组件,包括Zuul、Ribbon、Hystrix和@FeignClient。 Zuul是Spring Cloud中的网关服务,它可以将各个微服务的请求路由到相应的微服务上。Zuul具有负载均衡和过滤器等功能,可以对请求进行拦截和处理。 Ribbon是一个负载均衡器,它可以根据负载情况将请求分发给不同的微服务实例。Ribbon可以与Eureka等注册中心配合使用,动态地获取可用的服务实例列表,并根据一定的负载均衡策略选择合适的实例。 Hystrix是一个容错和延迟容忍的库,可以帮助我们构建稳定的分布式系统。它可以防止由于某一微服务的故障或延迟而导致整个系统的崩溃。通过为每个外部服务的调用添加断路器,Hystrix可以在外部服务不可用时提供备选方案,并且可以对外部服务的调用进行监控和度量。 @FeignClient是一个用于声明式REST客户端的注解。通过在接口中添加@FeignClient注解,并指定要访问的微服务名称,我们可以方便地进行REST调用。Feign会根据接口定义自动生成实现类,并将请求发送到相应的微服务。 综上所述,Spring Cloud中的Zuul、Ribbon、Hystrix和@FeignClient是用于构建分布式系统的重要组件。它们可以帮助我们解决微服务架构中的路由、负载均衡、容错和服务间调用等问题。利用这些组件,我们可以更方便地构建可靠、高效的分布式系统。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值