gpt4 book ai didi

spring - Zuul 为多个 zuul 路由实现多个 ZuulFallbackProvider

转载 作者:行者123 更新时间:2023-12-01 12:23:02 25 4
gpt4 key购买 nike

我如何为多个 zuul 路由实现多个 zuulFallbackProvider。除了公开 restcontroller 并使用 hystrixcommand 实现方法之外,我看不到如何仅使用属性来做到这一点的答案。

我可以用自己的 zuulFallBackProvider bean 制作我的每个服务吗?

应用程序.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 - Zuul 为多个 zuul 路由实现多个 ZuulFallbackProvider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42216079/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com