标签:服务中心 rri www. getc pat nio second ble else
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</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>
package com.miniooc.eurekazuul; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; /** * EurekaZuulApplication * * @author 宋陆 * @version 1.0.0 */ @EnableZuulProxy @SpringBootApplication public class EurekaZuulApplication { public static void main(String[] args) { SpringApplication.run(EurekaZuulApplication.class, args); } }
server: port: 8080 spring: application: name: eureka-gateway eureka: instance: hostname: localhost # 表示eureka client间隔多久去拉取服务注册信息,默认为30秒,如果要迅速获取服务注册状态,可以缩小该值 lease-renewal-interval-in-seconds: 5 # 表示eureka server至上一次收到client的心跳之后,等待下一次心跳的超时时间,在这个时间内若没收到下一次心跳,则将移除该instance。 # 默认为90秒 # 如果该值太大,则很可能将流量转发过去的时候,该instance已经不存活了。 # 如果该值设置太小了,则instance则很可能因为临时的网络抖动而被摘除掉。 # 该值至少应该大于 leaseRenewalIntervalInSeconds lease-expiration-duration-in-seconds: 10 client: serviceUrl: defaultZone: http://localhost:9527/eureka/,http://localhost:9528/eureka/,http://localhost:9529/eureka/ zuul: routes: api-ribbon: path: /ribbon/* serviceId: eureka-discovery-ribbon api-feign: path: /feign/* serviceId: eureka-discovery-feign
【路由网关】按照路由转发规则,把 /ribbon/* 的请求,转发到了 eureka-discovery-ribbon 服务,把 /feign/* 的请求,转发到了 eureka-discovery-feign
zuul过滤器有四种类型分别是:pre 路由前,route 路由时,post 路由完毕,error 发生错误时。
package com.miniooc.eurekazuul.filter; import com.netflix.zuul.ZuulFilter; import com.netflix.zuul.context.RequestContext; import com.netflix.zuul.exception.ZuulException; import org.apache.commons.lang.StringUtils; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * EurekaZuulApplication * * @author 宋陆 * @version 1.0.0 */ public class AuthFilter extends ZuulFilter { @Override public String filterType() { return "pre"; // 在请求被路由之前调用 } @Override public int filterOrder() { return 0; // filter执行顺序,通过数字指定 ,优先级为0,数字越大,优先级越低 } @Override public boolean shouldFilter() { return true; // 是否执行该过滤器,此处为true,说明需要过滤 } @Override public Object run() throws ZuulException { RequestContext ctx = RequestContext.getCurrentContext(); HttpServletRequest request = ctx.getRequest(); String token = request.getParameter("token");// 获取请求的参数 // 如果有token参数并且token值为miniooc,才进行路由 if (StringUtils.isNotBlank(token) && token.equals("miniooc")) { ctx.setSendZuulResponse(true); //对请求进行路由 ctx.setResponseStatusCode(200); ctx.set("code", 1); } else { ctx.setSendZuulResponse(false); //不对其进行路由 ctx.setResponseStatusCode(401); HttpServletResponse response = ctx.getResponse(); response.setHeader("content-type", "text/html;charset=utf8"); ctx.setResponseBody("网关认证失败,停止路由"); ctx.set("code", 0); } return null; } }
package com.miniooc.eurekazuul.config; import com.miniooc.eurekazuul.filter.AuthFilter; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * EurekaZuulConfig * 应用配置类,初始化 Bean和配置信息 * * @author 宋陆 * @version 1.0.0 */ @Configuration public class EurekaZuulConfig { @Bean public AuthFilter authFilter(){ return new AuthFilter(); } }
没有通过校验,zuul过滤器停止路由转发
通过校验,zuul过滤器正常路由转发
至此,一个简单的【网关路由】搭建完成。
SpringCloud2.0 Zuul 网关路由 基础教程(十)
标签:服务中心 rri www. getc pat nio second ble else
原文地址:https://www.cnblogs.com/songlu/p/9979777.html