spring cloud 学习(四) Hystrix

来源:互联网 发布:程序员多少钱一个月 编辑:程序博客网 时间:2024/06/06 00:35

参考:http://blog.csdn.net/forezp/article/details/69934399

1.ribbon断路器

pom.xml加入依赖:

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

改造ribbon项目。

改造Ribbon.java:

package 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.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
public class Ribbon {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        SpringApplication.run(Ribbon.class, args);
    }

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

}

改造RibbonService.java:

package ribbon.service;

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

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

@Service
public class RibbonService {

    @Autowired
    RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "Service0Error")
    public String Service0(String name) {
        return restTemplate.getForObject("http://SERVICE0/service0/" + name, String.class);
    }
   
    public String Service0Error(String name) {
        return "Service0Error:"+name+",error!";
    }

}

启动discovery,service0,ribbon,访问以下地址http://127.0.0.1:8085/service0/qq:

image

关闭service0,刷新浏览器:

image

添加监控仪表盘,改造Ribbon.java:

package 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.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
@EnableHystrixDashboard
public class Ribbon {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        SpringApplication.run(Ribbon.class, args);
    }

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

}


访问地址:http://127.0.0.1:8085/hystrix,效果如下:

image

image

2.Feign断路器

改造service1项目。

改造Server0Client.java:

package service1.client;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@FeignClient(value="service0",fallback = Server0ClientHystric.class) //service0的application中的name属性一致,采用FeignClient会自动负载均衡
public interface Server0Client {
   
    @RequestMapping(method=RequestMethod.GET,path="/service0/{value}")
    public String service0(@PathVariable("value") String value);//与service0controller的方法一致

}

image

添加类Server0ClientHystric.java:

package service1.client;

import org.springframework.stereotype.Component;

@Component
public class Server0ClientHystric implements Server0Client{

    @Override
    public String service0(String value) {
        // TODO Auto-generated method stub
        return "sorry "+value;
    }

}

启动discovery,service1,访问http://127.0.0.1:8082/service1/qq,这个时候service0还没有启动,所以service1调用不到,会出错,提示如下:

image

说明断路器工作正常!

启动service0,访问http://127.0.0.1:8082/service1/qq,正常访问如下:

image

原创粉丝点击