春季-Zuul为多个zuul路由实现Multiple ZuulFallbackProvider
作者:互联网
我如何实现多个zuul路由的多个zuulFallbackProvider.
除了公开restcontroller并使用hystrixcommand实现方法外,我看不到如何使用属性的答案.
我可以使用自己的zuulFallBackProvider bean提供每项服务吗?
application.yml
server:
port: 8080
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 20000
ribbon:
ReadTimeout: 20000
ConnectTimeout: 20000
zuul:
prefix: /api
ignoredServices: '*'
host:
connect-timeout-millis: 20000
socket-timeout-millis: 20000
routes:
kicks-service:
path: /kicks/**
serviceId: kicks-service
stripPrefix: false
sensitiveHeaders:
kicks-inventory:
path: /inventory/**
serviceId: kicks-inventory
stripPrefix: false
sensitiveHeaders:
这是我的示例应用
@SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
@Bean
public Prefilter prefilter(){
return new Prefilter();
}
@Bean
public ZuulFallbackProvider zuulFallbackProvider() {
return new ZuulFallbackProvider() {
@Override
public String getRoute() {
return "kicks-inventory";
}
@Override
public ClientHttpResponse fallbackResponse() {
return new ClientHttpResponse() {
@Override
public HttpStatus getStatusCode() throws IOException {
return HttpStatus.OK;
}
@Override
public int getRawStatusCode() throws IOException {
return 200;
}
@Override
public String getStatusText() throws IOException {
return "OK";
}
@Override
public void close() {
}
@Override
public InputStream getBody() throws IOException {
return new ByteArrayInputStream("fallback".getBytes());
}
@Override
public HttpHeaders getHeaders() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
return headers;
}
};
}
};
}
}
解决方法:
每条路线将需要一个单独的bean.他们应该为getRoute方法返回不同的路由.
请检查此-http://tachniki.blogspot.in/2017/02/generic-ZuulFallbackProvider.html.希望它会使其更容易一些.
标签:spring-cloud-netflix,spring-boot,netflix-zuul,netflix,spring 来源: https://codeday.me/bug/20191111/2021864.html