首先是自己没有仔细买两本书看看,真的是不应该,这导致自己的水平的确不好
@ComponentScan和Application启动类的扫描二者不可得兼?
one ------ zuul
过滤器修改转发的url
https://blog.youkuaiyun.com/u012930316/article/details/80846553
@Component
public class UrlRedirectFilter extends ZuulFilter implements AbstractLogger{
@Override
public Object run() {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
String url = request.getRequestURI();
String newPath = "/one/two";
// 将替换掉的url set进去,在对应的转发请求的url就会使用这个url
ctx.put(FilterConstants.REQUEST_URI_KEY, newpath);
}
}
return null;
}
@Override
public boolean shouldFilter() {
return true;
}
//filterOrder:过滤的顺序
@Override
public int filterOrder() {
return 1;
}
/* (non-Javadoc)filterType:返回一个字符串代表过滤器的类型,在zuul中定义了四种不同生命周期的过滤器类型,具体如下:
pre:路由之前
routing:路由之时
post: 路由之后
error:发送错误调用
*/
@Override
public String filterType() {
return FilterConstants.ROUTE_TYPE;
}
}
filterType必须是FilterConstants.ROUTE_TYPE,否则不生效,切记切记
pre:可以在请求被路由之前调用
route:在路由请求时候被调用
post:在route和error过滤器之后被调用
error:处理请求时发生错误时被调用
two------ 获取ek的地址,用来远程调用
得到地址

@Autowired
private DiscoveryClient discoveryClient;
### 传入ID

List instances = discoveryClient.getInstances(“S17-DCS”);
//必须是下面这个,http://ip + port + path
instances.get(1).getUri().toString()+ “/api/v1/device”
three------ 远程调用
@Autowired
private RestTemplate http;
ResponseResult response = null;
try {
response = http.exchange(serviceInstance.getUri().toString()+ "/api/v1/device", HttpMethod.GET, new HttpEntity<>(d, null), ResponseResult.class).getBody();
} catch (RestClientException e) {
log.error(" use dcs api is error once {}",e);
try {
response = http.exchange(instances.get(1).getUri().toString()+ "/api/v1/device", HttpMethod.GET, new HttpEntity<>(d, null), ResponseResult.class).getBody();
} catch (RestClientException ex) {
@Configuration
public class GlobalComponent {
@Bean
public RestTemplate rest(){
return new RestTemplate();
}
}
four------ 监控
/actuator/routes
five------ 坑
InputStream responseDataStream = ctx.getResponseDataStream();
StreamUtils.copyToString(responseDataStream, Charset.forName(“UTF-8”))
001 如果你从post的filter中通过流拿出了结果,万一你不放回去,那就呵呵了,结果没了
002 如果想要修改路由的路径,必须在route中进行修改,不然不生效。
003 如果想要get类型转化为post,呵呵,请使用包装接口吧
004 get请求的参数在 getParam中,post请求在流中 ,混淆了,会导致你娶不到值的
six 配置参数
https://blog.youkuaiyun.com/xx326664162/article/details/83625104
本文深入探讨了Zuul过滤器的实现细节,包括如何修改转发URL,远程调用服务实例,以及监控和常见问题解决方案。通过具体代码示例,展示了如何自定义过滤器类型,并解释了在不同生命周期阶段过滤器的作用。
2174

被折叠的 条评论
为什么被折叠?



