标签:enc getc ribbon test 核心 alt 规则 lte OLE
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <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>
server: port: 5000 spring: application: name: zuul-demo eureka: client: registerWithEureka: true fetchRegistry: true serverUrl: defaultZone: http://127.0.0.1:8761/eureka zuul: routes: smsapi: path: /sms/** serviceId: sms-module consumerapi: path: /consumertest/** serviceId: consumer-module
@Component public class MyFilter extends ZuulFilter { @Override public String filterType() { return FilterConstants.PRE_TYPE; } @Override public int filterOrder() { return 0; } //是否走过滤逻辑 @Override public boolean shouldFilter() { return true; } //判断url中是否包含参数testParam,不包含则拦截请求 @Override public Object run() { RequestContext ctx = RequestContext.getCurrentContext(); HttpServletRequest request = ctx.getRequest(); String testParam = request.getParameter("testParam"); if( null == testParam){ System.out.println("缺失关键参数,驳回请求"); ctx.setSendZuulResponse(false);//是否把该请求转发给具体服务 ctx.setResponseStatusCode(401); try { ctx.getResponse().getWriter().write("qing jiancha canshu shifou wanzheng"); } catch (IOException e) { e.printStackTrace(); } return null; } return null; } }
@Component public class MyFallbackProvider implements ZuulFallbackProvider { @Override public String getRoute() { return "sms-module"; //匹配所有,用“*” } @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("{\"msg\":\"error, 少年,你的程序出异常了,这里是失败回调函数\"}".getBytes()); // return new ByteArrayInputStream("少年,你的程序出异常了,这里是失败回调函数".getBytes()); } @Override public HttpHeaders getHeaders() { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); return headers; } }; } }
标签:enc getc ribbon test 核心 alt 规则 lte OLE
原文地址:https://www.cnblogs.com/nevermorewang/p/9346730.html