Spring Cloud 入门教程(四): 断路器(Hystrix)(Greenwich.RELEASE)

参考网址:https://blog.csdn.net/forezp/article/details/81040990

一、准备工作

启动前几篇的项目:erurekaserver,eurekaclient1

二、修改项目service-ribbon

1、修改pox.xml 添加如下依赖

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

2、修改ServiceRibbonApplication.java 添加@EnableHystrix注解

package wg;

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

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableHystrix
public class ServiceRibbonApplication {
	public static void main(String[] args) {
        SpringApplication.run( ServiceRibbonApplication.class, args );
    }

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

3、修改HelloService.java,在方法上添加@HystrixCommand(fallbackMethod = "helloError"),并新建方法helloError。

package wg.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 HelloService {
	@Autowired
    RestTemplate restTemplate;

	@HystrixCommand(fallbackMethod = "helloError")
    public String helloService() {
        return restTemplate.getForObject("http://EUREKACLIENT/",String.class);
    }
	
	public String helloError() {
        return "hi,wg,sorry,error!";
    }
}

4、启动service-ribbon,访问:http://localhost:8764/hello

出现:hello world from port 8763

5、关闭eurekaclient1,访问:http://localhost:8764/hello

出现:hi,wg,sorry,error!

熔断成功。

三、修改项目service-feign

1、修改HelloService.java,添加 fallback = HelloServiceImpl.class

package wg.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@FeignClient(value = "EUREKACLIENT",fallback = HelloServiceImpl.class)
public interface HelloService {
	@RequestMapping(value = "/",method = RequestMethod.GET)
    String sayHello();
}

2、新建HelloServiceImpl.java

package wg.service;

import org.springframework.stereotype.Component;

@Component
public class HelloServiceImpl implements HelloService{

	@Override
	public String sayHello() {
		// TODO Auto-generated method stub
		return "sorry,error!";
	}

}

3、修改application.yml

server:
  port: 8765
spring:
  application:
    name: service-feign
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
feign:
  hystrix:
    enabled: true

4、启动 service-feign,访问:http://localhost:8765/hello

出现:hello world from port 8763

5、关闭eurekaclient1,访问:http://localhost:8765/hello

出现:sorry,error! 

熔断成功。

上一篇:Spring Cloud 入门教程(三): 服务消费者(Feign)(Greenwich.RELEASE)

下一篇:Spring Cloud 入门教程(五): 路由网关(zuul) (Greenwich.RELEASE)

转载于:https://my.oschina.net/pipi1919/blog/3062058

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值