springcloud hystrix实战(二)
我們前面介紹完了springcloud hystrix的相關(guān)作用,大家也有了一個(gè)認(rèn)識(shí),這個(gè)熔斷器的作用這個(gè)就不在重復(fù)。
下面我們就接著進(jìn)行代碼實(shí)戰(zhàn),我們是接著之前的微服務(wù)的工程繼續(xù)的,如果有什么不明白請(qǐng)跟查看前面相關(guān)的文章
1,首先我們創(chuàng)建一個(gè)微服務(wù)工程? microservicecloud-provider-dept-hystrix-8001(注意參考前面的服務(wù)提供者8001)
2,將microservicecloud-provider-dept-8001的相關(guān)都拷貝到新的微服務(wù)工程
3,修改pom文件
<!--引入熔斷器Hystrix 相關(guān)的依賴包--><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 {@Autowiredprivate DeptService service = null;@RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET)//一旦調(diào)用服務(wù)方法失敗并拋出了錯(cuò)誤信息后,會(huì)自動(dòng)調(diào)用@HystrixCommand標(biāo)注好的fallbackMethod調(diào)用類中的指定方法@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 + "沒有沒有對(duì)應(yīng)的信息");}return dept;}public Dept processHystrix_Get(@PathVariable("id") Long id) {return new Dept().setDeptno(id).setDname("該ID:" + id + "沒有沒有對(duì)應(yīng)的信息,null--@HystrixCommand").setDb_source("no this database in MySQL");} }對(duì)于為什么controller這么寫我這里做個(gè)思路說(shuō)明:
這里我們要查詢dept的信息,我們傳入id為100的id,我們有數(shù)據(jù)庫(kù)信息知道只有五條記錄沒有deptno=100的記錄,這時(shí)候我們查詢的結(jié)果會(huì)返回null,這時(shí)候我們手動(dòng)拋出異常。
一旦調(diào)用服務(wù)方法失敗并拋出了錯(cuò)誤信息后,會(huì)自動(dòng)調(diào)用@HystrixCommand標(biāo)注好的fallbackMethod調(diào)用類中的指定方法
?
?
6,修改主啟動(dòng)類
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 //本服務(wù)啟動(dòng)后會(huì)自動(dòng)注冊(cè)進(jìn)eureka服務(wù)中 @EnableDiscoveryClient //服務(wù)發(fā)現(xiàn) @EnableCircuitBreaker//對(duì)hystrixR熔斷機(jī)制的支持 public class DeptProvider8001_Hystrix_App {public static void main(String[] args) {SpringApplication.run(DeptProvider8001_Hystrix_App.class, args);} }我們上面已經(jīng)加入熔斷器,我們當(dāng)然需要告訴主啟動(dòng)類使用這個(gè)熔斷器處理異常(調(diào)用fallback方法)
通過(guò)注解就@EnableCircuitBreaker可以告訴主啟動(dòng)類開啟熔斷器的使用
?
到這來(lái)為止我們的熔斷器代碼就完成了,下面我們來(lái)測(cè)試下
?
啟動(dòng)之后我們?cè)L問eureka注冊(cè)中心:
通過(guò)消費(fèi)者端調(diào)用注冊(cè)中心暴露的微服務(wù)發(fā)現(xiàn)我們的服務(wù)正常:
?
?
之前我們不是說(shuō)了如果調(diào)用數(shù)據(jù)庫(kù)不存在的數(shù)據(jù)返回null,手動(dòng)拋異常,一旦拋異常就會(huì)調(diào)用fallback方法返回
由上面的結(jié)果可知我們的熔斷成功。關(guān)于熔斷器的實(shí)戰(zhàn)編碼就已經(jīng)完成
總結(jié)
以上是生活随笔為你收集整理的springcloud hystrix实战(二)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: zcmu-1951
- 下一篇: hdu-1422(简单dp)