SpringCloud_Gateway_zuul 淩亂°似流年 2021-10-06 03:22 388阅读 0赞 ### SpringCloud\_Gateway\_zuul ### * 依赖pom.xml * 配置文件bootstrap.yml * 配置源码 基于zuul的网关 # 依赖pom.xml # <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.pingruan</groupId> <artifactId>vander-framework-parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>org.pingruan.springboot</groupId> <artifactId>vander-gateway-zuul-center</artifactId> <dependencies> <!-- 服务监控 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> <dependency> <groupId>org.springframework.retry</groupId> <artifactId>spring-retry</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> </project> # 配置文件bootstrap.yml # server: port: 9003 spring: application: name: vander-gateway-zuul-center #打开负载局衡器支持重试开关 cloud: loadbalancer: retry: enabled: true #------------网关配置------------------ zuul: retryable: true # 开启重试功能 routes: client1: path: /client1/** # service-id: vander-client-demo client2: path: /client2/** service-id: vander-client-demo-a ##要支持重试的微服务的配置 #vander-client-demo-a: # ribbon: # ConnectionTimeout: 3000 # ReadTimeout: 6000 # #打开所有操作都支持重试的开关 # OkToRetryOnAllOperations: true # #在其他Server上重试的次数(除去首次) # MaxAutoRetriesNextServer: 3 # #在出错或者超时的Server上重试的次数(除去首次) # MaxAutoRetries: 2 #------------注册中心配置------------------ eureka: client: registerWithEureka: true #服务注册开关 fetchRegistry: true #服务发现开关 serviceUrl: #Eureka客户端与Eureka服务端进行交互的地址,多个中间用逗号分隔 defaultZone: ${EUREKA_SERVERS:http://root:qwe123@server1:9001/eureka/} instance: prefer-ip-address: true #将自己的ip地址注册到Eureka服务中 ip-address: ${IP_ADDRESS:127.0.0.1} instance-id: ${spring.application.name}:${server.port} #指定实例id # 配置源码 # /** * client-demo 服务熔断处理 * * @author vander * */ @Component @Slf4j public class ClientDemoFallback implements FallbackProvider{ @Override public String getRoute() { return "vander-client-demo";//服务id } @Override public ClientHttpResponse fallbackResponse(String route, Throwable cause) { log.info("route..."+route); return new ClientHttpResponse() { @Override public HttpHeaders getHeaders() { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); return headers; } @Override public InputStream getBody() throws IOException { return new ByteArrayInputStream("The service is unavailable.".getBytes()); } @Override public String getStatusText() throws IOException { return "ok"; } @Override public HttpStatus getStatusCode() throws IOException { return HttpStatus.OK; } @Override public int getRawStatusCode() throws IOException { return 200; } @Override public void close() { } }; } } /** * 全局过滤器 * * @author vander * */ @Component @Slf4j public class GlobalRouteFilter extends ZuulFilter { //判断该过滤器是否需要被执行 @Override public boolean shouldFilter() { return true;//表示是否需要执行该filter,true表示执行,false表示不执行 } @Override public Object run() throws ZuulException { RequestContext ctx = RequestContext.getCurrentContext(); HttpServletRequest req = ctx.getRequest(); String token = req.getHeader("token"); log.info("token...."+token); String ipAddr = this.getIpAddr(req); log.info("请求IP地址为:[{}]", ipAddr); // 配置本地IP白名单,生产环境可放入数据库或者redis中 List<String> ips = new ArrayList<String>(); ips.add("127.0.0.1"); if (!ips.contains(ipAddr)) { log.info("IP地址校验不通过!!!"); ctx.setResponseStatusCode(401);//设置了其返回的错误码 ctx.setSendZuulResponse(false);//通过ctx.setSendZuulResponse(false)令zuul过滤该请求,不对其进行路由 ctx.setResponseBody("IpAddr is forbidden!");//返回body内容 } log.info("IP校验通过。"); return null;//filter需要执行的具体操作 } @Override public String filterType() { return "pre";//定义filter的类型,有pre(路由前)、route(路由中)、post(路由后)、error四种 } //过滤器的执行顺序 @Override public int filterOrder() { return 0;//定义filter的顺序,数字越小表示顺序越高,越先执行 } /** * 获取Ip地址 * * @param request * @return */ public String getIpAddr(HttpServletRequest request) { String ip = request.getHeader("X-Forwarded-For"); if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("Proxy-Client-IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("WL-Proxy-Client-IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("HTTP_CLIENT_IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("HTTP_X_FORWARDED_FOR"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); } return ip; } } /** * zuul网关中心 * * @author vander * */ @EnableDiscoveryClient @EnableZuulProxy @SpringBootApplication @EnableRetry //开启重试 public class SpringBootZuulGateway { public static void main(String[] args) { SpringApplication.run(SpringBootZuulGateway.class, args); } }
还没有评论,来说两句吧...