提交 f6fc046e 编写于 作者: Q qinyingjie

fix:网关

上级 c6736811
...@@ -32,6 +32,11 @@ ...@@ -32,6 +32,11 @@
<artifactId>spring-cloud-starter-netflix-zuul</artifactId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
package com.kwan.springcloud;
import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider;
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;
/**
* FallbackProvider(请求异常处理)
* <p>
* 网关的默认回退FallbackProvider的机制只适用于通过配置文件中配置的url来跨服务调用,而不拦截通过FeignClient跨服务调用,
* 也就是说要自己实现一个FeignClient服务的fallback才可以,但是如果通过FeignClient的服务要是特别多怎么办,每一个都要写一个不得累死,
* 这里想到了通过网关的错误过滤器来统一处理FeignClient跨服务调用出错的问题
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/1/11 12:48
*/
@Component
public class GoodsServiceFallBackProvider implements FallbackProvider {
@Override
public String getRoute() {
return "goods-service";
}
@Override
public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
return new ClientHttpResponse() {
@Override
public HttpHeaders getHeaders() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
return headers;
}
@Override
public InputStream getBody() throws IOException {
return new ByteArrayInputStream("order-service is not available!".getBytes());
}
@Override
public HttpStatus getStatusCode() throws IOException {
return HttpStatus.BAD_REQUEST;
}
@Override
public int getRawStatusCode() throws IOException {
return this.getStatusCode().value();
}
@Override
public String getStatusText() throws IOException {
return this.getStatusCode().getReasonPhrase();
}
@Override
public void close() {
}
};
}
}
package com.kwan.springcloud; package com.kwan.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@EnableZuulProxy @EnableZuulProxy
@SpringBootApplication @SpringBootApplication
public class ZuulApplication { public class ZuulApplication {
......
package com.kwan.springcloud.filter;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
@Component
public class MyZuulFilter extends ZuulFilter {
/**
* 表示当前的过滤器是否被调用
*/
@Override
public boolean shouldFilter() {
return true;
}
/**
* filterOrder表示执行的优先级,值越小表示优先级越高
*/
@Override
public int filterOrder() {
return 0;
}
/**
* pre: 在请求被路由之前调用
* routing: 在请求被路由之中调用
* post: 在请求被路由之后调用
* error: 处理请求发生错误时调用
*/
@Override
public String filterType() {
return "pre";
}
/**
* 真正执行Filter逻辑的方法
*/
@Override
public Object run() throws ZuulException {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
System.err.println("--------------uri: -------------- " + request.getRequestURI());
return ctx;
}
}
\ No newline at end of file
...@@ -6,6 +6,17 @@ spring: ...@@ -6,6 +6,17 @@ spring:
application: application:
name: zuul-service #服务名称 name: zuul-service #服务名称
#路由规则:
zuul:
sensitiveHeaders: Cookie,Set-Cookie,Authorization
routes:
api-portal:
path: /portal-service/** #访问路径:http:/localhost:5001/hello-service/hello
serviceId: portal-service
api-goods:
path: /goods-service/**
serviceld: goods-service
#服务提供者 #服务提供者
eureka: eureka:
client: client:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册