SpringCloud案例day03.md

Hystrix

1:Rest实现服务熔断

测试Service-A是正常的
http://localhost:9001/person/find/1

然后让Cosumer-C9006去访问服务A

(1)引入hystrix的依赖
在工程中添加Hystrix的相关依赖

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

(2)在启动类中激活Hystrix
在启动类中添加 @EnableCircuitBreaker 注解开启对熔断器的支持。

@SpringBootApplication
//激活eurekaClient
@EnableEurekaClient
//@EnableDiscoveryClient
@EnableFeignClients
@EnableCircuitBreaker
public class C9006 {

    //创建RestTemplate,由@Bean将实例放到spring容器里面
    @Bean
    @LoadBalanced
    RestTemplate restTemplate(){
        RestTemplate r = new RestTemplate();
        return r;
    }
    public static void main(String[] args) {
        SpringApplication.run(C9006.class,args);
    }
}

(3)配置熔断触发的降级逻辑
(4)在需要收到保护的接口上使用@HystrixCommand配置

@RestController
@RequestMapping("/testHystrixRest")
public class TestHystrixRestController {

    @Autowired
    private RestTemplate restTemplate ;

    @RequestMapping(method = RequestMethod.GET,value = "/get/{id}")
    @HystrixCommand(fallbackMethod = "error")
    public Person getByID(@PathVariable Long id){
        Person p = restTemplate.getForObject("http://Service-A9001/person/find/"+id,Person.class);
        return p;
    }
    //降级方法,默认值,在不能正常访问ServieA获取的数据
    public Person error(Long id){
        Person p = new Person();
        p.setId(id);
        p.setName("当前是服务降级方法的数据,为了快速返回结束9006");
        return p;
    }
}

注意事项:

(1)因为熔断的降级逻辑方法必须跟正常逻辑方法保证: 相同的参数列表和返回值声明

(2)在 getByID 方法上 HystrixCommand(fallbackMethod = “error”) 用来声明一个降级逻辑的方法

(3)测试的话只要打开A服务,B服务,先访问B服务
对比,关闭A服务,再访问B服务

2:Feign实现服务熔断

测试Service-A是正常的
http://localhost:9001/person/find/1

然后让Cosumer-C9006去访问服务A

编写Feign

@FeignClient(name="Service-A9001")
public interface ServiceAFeigin {
    @RequestMapping(method = RequestMethod.GET,value = "person/find/{id}")
    public Person findById(@PathVariable Long id);
}

调用Feign

@RestController
@RequestMapping("/useHystrixFeign")
public class TestHystrixFeignController {
    @Autowired
    ServiceAFeigin feigin;
    @RequestMapping(method = RequestMethod.GET,value = "/get/{id}")
      public Person getByID(@PathVariable Long id){
        Person p =feigin.findById(id);
        return p;
    }
}

》1:在Feign中已经内置了hystrix,但是默认是关闭的需要在工程的 application.yml 中开启对hystrix的支持

feign:
  hystrix: #在feign中开启hystrix熔断
    enabled: true

》2:配置FeignClient接口的实现类
基于Feign实现熔断降级,那么降级方法需要配置到FeignClient接口的实现类中
package com.dev1.feign.impl;

@Component
public class ServiceAFeignFallBack implements ServiceAFeigin {
    @Override
    public Person findById(Long id) {
        Person person = new Person();
        person.setId(id);
        person.setName("我是服务降级Fein");
        return person;
    }
}

》3:修改FeignClient添加hystrix熔断
在@FeignClient注解中添加降级方法
@FeignClient 注解中以fallback声明降级方法

@FeignClient(name="Service-A9001",fallback = ServiceAFeignFallBack.class)
public interface ServiceAFeigin {
    @RequestMapping(method = RequestMethod.GET,value = "person/find/{id}")
    public Person findById(@PathVariable Long id);
}

3:Dashborad服务监控

在Cosumer-C9007中添加

》1:添加依赖

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

》2:在Application中添加

@EnableHystrixDashboard

》3:添加application.yml配置

management:
  endpoints:
    web:
      exposure:
        include: '*'

》4:访问测试

http://localhost:9006/hystrix

输入监控断点展示监控的详细数据

http://localhost:9006/actuator/hystrix.stream

在这里插入图片描述

说明:
在这里插入图片描述

4:Turbine聚合监控

》1:添加依赖

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

》2:进行application.yml的配置

server:
  port: 9007
spring:
  application:
    name: Turbine-C9007
eureka:
  instance:
    prefer-ip-address: true #使用ip地址注册
    instance-id:  ${spring.cloud.client.ip-address}:${server.port} #向注册中心中注册服务id
    lease-renewal-interval-in-seconds: 5 #向注册中心中注册服务id
    lease-expiration-duration-in-seconds: 10 #续约到期的时间
  client:
    service-url:
      defaultZone: http://localhost:9000/eureka/,http://localhost:8000/eureka/

turbine:
  # 要监控的微服务列表,多个用,分隔
  appConfig: Cosumer-C9006
  clusterNameExpression: "'default'"

》3:在启动中开启

@SpringBootApplication
@EnableTurbine
@EnableHystrixDashboard
public class C9007 {


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

》4:测试

http://localhost:9007/turbine.stream
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

翁老师的教学团队

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值