Spring Cloud 综合配置 结合feign hystrix eureka 以及yml配置

这个是启动类配置
@SpringBootApplication  //开启整体应用  
@EnableEurekaClient    //开启客户端服务发现
@EnableCircuitBreaker //开启断路器支持
@EnableFeignClients //开启feign支持
public class PassionApp {

	@Bean //配置bean,配置在容器中 注意是配置!
	@LoadBalanced //netflix提供的自动化轮询负载均衡注解,不是ribbon,也不是feign中的
      //import org.springframework.cloud.client.loadbalancer.LoadBalanced;
public RestTemplate getRestTemplate() {return new RestTemplate();}public static void main(String[] args) {new SpringApplicationBuilder(Passion.class).web(true).run(args);//自行百度}}


Feign接口,包含回退方法

@FeignClient(name = "spring-member-server", fallback = HelloClientFallback.class)//第一个 指向provider的名称,第二个 包含回退方法的类
public interface HelloClient {
	@GetMapping("/hello")
	public String hello();

	@GetMapping("/toHello")
	public String toHello();

	@GetMapping("/member/{id}")
	public Member getMember(@PathVariable("id") Integer id);//注意这里面的PathVariable中的id必须填写
}

Fallback类,自定义hystrix回退方法指定的类

@Component //注意记得把这个类扔到SpringApplicationContext中
public class HelloClientFallback implements HelloClient {
	public String hello() {
		return "fall back method the 'hello'";
	}

	public String toHello() {
		return "fall back method the 'toHello'";
	}

	public Member getMember(Integer id) {
		return null;
	}
}

Controller测试类

@RestController
public class FeiController {
	@Autowired //开个小灶给你们讲一下,这个是通过byType规则,把helloClinet实现类,通过setter方法指定到该实例中.很多贤弟不知道这是做什么的
	private HelloClient helloClient;

	@GetMapping("/hello")
	public String hello() {
		return helloClient.hello();
	}

	@GetMapping("/toHello")
	public String toHello() {
		String result = helloClient.toHello();//我到现在也不是很懂 commandkey与groupkey是干什么的,都使用的是默认的
		HystrixCircuitBreaker breaker = HystrixCircuitBreaker.Factory  //这里置顶的commandKey是后面yml中配置的
				.getInstance(HystrixCommandKey.Factory.asKey("HelloClient#toHello()"));
		System.out.println("断路器状态: " + breaker.isOpen());
		return result;
	}

	@GetMapping("/yaya")
	public Member getMember() {
		return helloClient.getMember(1);
	}
}

Final Yml Config

server:
  port: 9001
spring:
  application:
    name: spring-passion-server
feign: 
  hystrix: 
    enabled: true //feign开启hystrix支持
hystrix: 
  command: 
    HelloClient#toHello()://这个不配默认是全局
      execution: 
        isolation:
          thread:
            timeoutInMilliseconds: 500 //线程超时,调用Fallback方法
      circuitBreaker:
        requestVolumeThreshold: 3  //10秒内出现3个以上请求(已临近阀值),并且出错率在50%以上,开启断路器.断开服务,调用Fallback方法
eureka:
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:8761/eureka/
BEAN
public class Member { //这里需要讲一下,务必提供一个无参构造器;务必提供方法 getter/setter方法,否则你复制不了provider传过来的对象.
	private Integer	id;
	private String	name;
	private String	msg;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

}

以上是CONSUMER 的代码,   PROVIDER的代码请自行脑补.

若有疑义,留下问题

你可能感兴趣的:(SpringBoot,SpringCloud,Feign,Eureka,Hystrix)