springcloud hystrix实战(二)

我们前面介绍完了springcloud hystrix的相关作用,大家也有了一个认识,这个熔断器的作用这个就不在重复。

下面我们就接着进行代码实战,我们是接着之前的微服务的工程继续的,如果有什么不明白请跟查看前面相关的文章

1,首先我们创建一个微服务工程  microservicecloud-provider-dept-hystrix-8001(注意参考前面的服务提供者8001)

2,将microservicecloud-provider-dept-8001的相关都拷贝到新的微服务工程

3,修改pom文件

  <!--引入熔断器Hystrix 相关的依赖包-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
        </dependency>

4,修改yml文件

5,修改controller类

package com.atguigu.springcloud.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.atguigu.springcloud.entities.Dept;
import com.atguigu.springcloud.service.DeptService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

@RestController
public class DeptController {
	@Autowired
	private DeptService service = null;

	@RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET)
	//一旦调用服务方法失败并抛出了错误信息后,会自动调用@HystrixCommand标注好的fallbackMethod调用类中的指定方法
	@HystrixCommand(fallbackMethod = "processHystrix_Get")
	public Dept get(@PathVariable("id") Long id) {
		Dept dept = this.service.get(id);
		if (null == dept) {
			throw new RuntimeException("该ID:" + id + "没有没有对应的信息");
		}
		return dept;
	}

	public Dept processHystrix_Get(@PathVariable("id") Long id) {
		return new Dept().setDeptno(id).setDname("该ID:" + id + "没有没有对应的信息,null--@HystrixCommand")
				.setDb_source("no this database in MySQL");
	}
}

对于为什么controller这么写我这里做个思路说明:

这里我们要查询dept的信息,我们传入id为100的id,我们有数据库信息知道只有五条记录没有deptno=100的记录,这时候我们查询的结果会返回null,这时候我们手动抛出异常。

一旦调用服务方法失败并抛出了错误信息后,会自动调用@HystrixCommand标注好的fallbackMethod调用类中的指定方法

 

 

6,修改主启动类

package com.atguigu.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient //本服务启动后会自动注册进eureka服务中
@EnableDiscoveryClient //服务发现
@EnableCircuitBreaker//对hystrixR熔断机制的支持
public class DeptProvider8001_Hystrix_App {
	public static void main(String[] args) {
		SpringApplication.run(DeptProvider8001_Hystrix_App.class, args);
	}
}

我们上面已经加入熔断器,我们当然需要告诉主启动类使用这个熔断器处理异常(调用fallback方法)

通过注解就@EnableCircuitBreaker可以告诉主启动类开启熔断器的使用

 

到这来为止我们的熔断器代码就完成了,下面我们来测试下

 

启动之后我们访问eureka注册中心:

通过消费者端调用注册中心暴露的微服务发现我们的服务正常:

 

 

之前我们不是说了如果调用数据库不存在的数据返回null,手动抛异常,一旦抛异常就会调用fallback方法返回

由上面的结果可知我们的熔断成功。关于熔断器的实战编码就已经完成

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值