Spring Cloud 的 Hystrix 通过Fallback Factory 检查回退的错误原因

一、Feign指向

         注意采用的是fallbackFactory指向
package com.itmuch.cloud;

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(name="cloud-service", fallbackFactory = FeignClientFallbackFactory.class)   // 服务端提供者的name
public interface UserFeignClient {

	@RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
	public User findById(@PathVariable("id") Long id); 

}

二、创建一个Feign接口实现类

package com.itmuch.cloud;

import org.springframework.stereotype.Component;

import feign.hystrix.FallbackFactory;

@Component
public class FeignClientFallbackFactory implements FallbackFactory {

	@Override
	public UserFeignClient create(Throwable cause) {
		return new UserFeignClient() {

			@Override
			public User findById(Long id) {
				System.out.println("fallback; reason was:" + cause);
				User user = new User();
				user.setId(-1L);
				user.setName("NULL");
				return user;
			}
			
		};
	}


}


你可能感兴趣的:(Spring,Cloud)