一、Hystrix断路器
原理:
package cn.tedu.controller;
import cn.tedu.feign.HelloFeign;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
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.RestController;
@RestController//不做业务 去调用服务的提供者
public class HelloController {
@Autowired
private HelloFeign helloFeign;
@RequestMapping("/hello/{name}")
@HystrixCommand(fallbackMethod = "fllbackHello")
public String hello(@PathVariable String name){
return helloFeign.hello(name);
}
//断路器方法,返回值要和方法一致 参数也一致
public String fllbackHello(String name){
return "tony";//当发生异常时,直接返回默认值 这种形式成为 降级
}
}
其他配置和消费者工程相同
二、网关zuul
*1、zuul实现API网关
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
</dependencies>
@SpringBootApplication
@EnableZuulProxy
public class RunAppZuul {
public static void main(String[] args) {
SpringApplication.run(RunAppZuul.class,args);
}
}
application.yml
zuul:
routes:
app-a:
path:/user/**
serviceId:consumer-hystrix
*
2.zuul实现断路器
实现服务合并、转发、验证、 异构协议
一、zuul实现API网关
org.springframework.cloud
spring-cloud-starter-eureka
@SpringBootApplication
@EnableZuulProxy
public class RunAppZuul {
public static void main(String[] args) {
SpringApplication.run(RunAppZuul.class,args);
}
}
application.yml
zuul:
routes:
app-a:
path:/user/**
serviceId:consumer-hystrix
二、zuul实现断路器
package cn.tedu.fallback;
import org.springframework.cloud.netflix.zuul.filters.route.ZuulFallbackProvider;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
@Component //zuul环境中启动它作为一个断路器
public class HelloFallback implements ZuulFallbackProvider {
@Override
public String getRoute() {
return "*";
}
@Override
public ClientHttpResponse fallbackResponse() {
return new ClientHttpResponse() {
@Override//状态码
public HttpStatus getStatusCode() throws IOException {
return HttpStatus.BAD_REQUEST;
}
@Override
public int getRawStatusCode() throws IOException {
return HttpStatus.BAD_REQUEST.value();
}
@Override //状态文字描述
public String getStatusText() throws IOException {
return HttpStatus.BAD_REQUEST.getReasonPhrase();
}
@Override
public void close() {
}
@Override //返回内容
public InputStream getBody() throws IOException {
String msg = "tonyzuul"; //可以构建对象Json
return new ByteArrayInputStream(msg.getBytes());
}
@Override //头信息
public HttpHeaders getHeaders() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
return headers;
}
};
}
}