Hystrix

  1. Hystix的初步整合

        1.1 在Produce服務層導入須要的依賴java

    

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

      1.2 編寫須要訪問的Controllerweb

package com.shi.prodect.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.shi.core.model.Dept;
import com.shi.prodect.service.DeptService;

@Controller
public class DeptContoller {
	
	@Autowired
	private DeptService deptService;
	
	@Autowired
	private DiscoveryClient client;
	
	/**
	 * 測試使用熔斷器Hystrix的使用
	 * @author SHF
	 * @version 建立時間:2018年11月30日  下午4:51:37
	 *  @param id
	 *  @return
	 */
	@GetMapping("/dept/get/{id}")
	@ResponseBody
	//一旦服務調用失敗並拋出了錯誤信息後,會自動調用@HystrixCommand標註好的fallbackMethod中指定的方法
	@HystrixCommand(fallbackMethod="Get_DeptError")
	public Dept get(@PathVariable(name="id") Long id) {
		Dept dept = deptService.get(id);
		if(dept == null) {
			throw new RuntimeException("該"+id+"暫時沒有對應的信息");
		}
		return dept;
	}
	
	public Dept Get_DeptError(@PathVariable(name="id") Long id) {
		Dept dept = new Dept();
		dept.setDeptno(id);
		dept.setDname("該數據庫中"+id+"尚未改id的存在 @HystrixCommand ");
		dept.setDb_source("no data_source exit...");
		return dept;
	}
}

    1.3 在主啓動類上面添加對熔斷器的支持spring

@EnableCircuitBreaker //對Hystrix熔斷器的支持

2. 服務降級(服務降級是在客戶端完成的)數據庫

 2.1 如今core包中配置接口的降級處理方案json

package com.shi.core.service;

import java.util.List;

import org.springframework.stereotype.Component;
import com.shi.core.model.Dept;
import feign.hystrix.FallbackFactory;

/**
 * @Component 該註解必定要在customer 容器初始化中被初始化到 否則加載不到該註解,沒法初始化該對象,
 * 會報錯:No fallbackFactory instance of type
 * @author SHF
 * @version 建立時間:2018年12月3日  下午2:14:06
 */
@Component //不要忘記添加,不要忘記添加,不要忘記添加
public class DeptClientServiceFallbackFactory implements FallbackFactory<DeptClientService>{

	@Override
	public DeptClientService create(Throwable arg0) {
		
		return new DeptClientService() {
			
			@Override
			public List<Dept> list() {
				// TODO Auto-generated method stub
				return null;
			}
			
			@Override
			public Dept get(long id) {
				// TODO Auto-generated method stub
				Dept dept = new Dept();
				dept.setDeptno(id);
				dept.setDname("該數據庫中"+id+"尚未改id的存在,Consumer客戶端提供的降級信息,此刻服務Provider已經關閉");
				dept.setDb_source("no data_source exit...");
				return dept;
			}
			
			@Override
			public boolean add(Dept dept) {
				// TODO Auto-generated method stub
				return false;
			}
		};
	}

}
package com.shi.core.service;

import java.util.List;

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;

import com.shi.core.model.Dept;
/**
 * 讓該接口實現熔斷機制,讓該接口在使用的中遠程調用出錯就調用 DeptClientServiceFallbackFactory 中指定的方法
 * @author SHF
 * @version 建立時間:2018年12月3日  上午10:36:25
 */
//@FeignClient(value = "SPRINGCLOUD04-PRODECT-8001")
@FeignClient(name = "SPRINGCLOUD04-PRODECT-8001",fallbackFactory = DeptClientServiceFallbackFactory.class) //指定爲哪一個微服務提供接口
public interface DeptClientService
{
	@RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET)
	public Dept get(@PathVariable("id") long id);

	@RequestMapping(value = "/dept/list", method = RequestMethod.GET)
	public List<Dept> list();

	@RequestMapping(value = "/dept/add", method = RequestMethod.POST)
	public boolean add(Dept dept);
}

 

2.3 編寫客戶端類增長配置app

feign: 
  hystrix: 
    enabled: true
package com.shi.customer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;


@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages= {"com.shi.core.service"})//feign服務類的包名
@ComponentScan(basePackages = "com.shi") //必定要保證掃描到core包中@Component 註解
public class Customer7001Feign {

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

}

2.4 測試ide

先走通整個流程,而後把服務提供者(product)關閉掉,看調用是否正常微服務

相關文章
相關標籤/搜索