06、SpringCloud Ribbon整合Hystrix

2020-03-29  本文已影响0人  adced06edef5

一、代码示例

说明:此处使用的SpringBoot版本为2.1.13.RELEASE,SpringCloud版本为Greenwich.SR5。
此处不再贴server与client代码
1、maven依赖

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

2、application.yml

server:
  port: 9004
spring:
  application:
    name: ribbon-hystrix
eureka:
  instance:
    hostname: localhost
    prefer-ip-address: true
    instance-id: ribbon-hystrix-9004
  client:
    service-url:
      defaultZone: http://eureka7001:7001/eureka/,http://eureka7002:7002/eureka/,http://eureka7003:7003/eureka/
#info信息
info:
  app:
    name: ribbon-hystrix-9004
  company:
    name: www.xxx.com
  build:
    artifactId: ${project.artifactId}
    version: ${project.version}

3、启动类
启动类增加@EnableHystrix注解开启Hystrix

package org.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableHystrix
public class RibbonHystrix9004Application {

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

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

4、其他java类
Controller类
Controller类增加@HystrixCommand注解,通过fallbackMethod指定熔断对应的方法,fallbackMethod对应的值为Controller类下的方法

package org.example.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class RibbonHystrixController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/hello/{name}")
    @HystrixCommand(fallbackMethod = "helloError")
    public String hello(@PathVariable String name){
        return restTemplate.getForObject("http://client/hello/"+name,String.class);
    }

    public String helloError(String name){
        return "helloError,"+name;
    }
}

2、测试验证

先后启动服务server、client与本服务,访问http://localhost:7001/

image.png
说明服务均启动成功。
再访问http://localhost:9004/hello/zs
image.png

image.png
然后,再把client停了,再次访问
image.png
说明代码生效。
github:
https://github.com/panli1988/cloud01
https://github.com/panli1988/cloud02
参考:
https://blog.csdn.net/forezp/article/details/70148833
http://www.itmuch.com/spring-cloud/spring-cloud-index/
还有尚硅谷周阳老师的视频
上一篇 下一篇

猜你喜欢

热点阅读