Spring-Cloud-Zuul-接口网关
1.如何使用Zuul默认通过以服务名作为ContextPath来创建路由映射
1.在启动类中增加@EnableZuulPorxy @EnableZuulProxy @SpringBootApplication public class ZuulApp { public static void main(String[] args) { new SpringApplicationBuilder(ZuulApp.class).web(true).run(args); } //在创建Zuul过滤器时需要创建如下内容 @Bean public LoginFilter newLoginFilter(){ return new LoginFilter(); } } 2.application.properties # routes to serviceId 这里边是通过serviceid来绑定地址,当在路径后添加/api-a/ 则是访问service-A对应的服务。 zuul.routes.api-a.path=/api-zuul/** zuul.routes.api-a.serviceId=server-ribbonservice zuul.routes.api-b.path=/api-zuul1/** zuul.routes.api-b.serviceId=server-ribbonservice1 3.pom.xml <dependencies> <!-- 添加对Web的支持 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- SpringBoot Test --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- 要注册的需要引用 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <!-- Api接口网关 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId> </dependency> </dependencies> <!--依赖管理,用于管理spring-cloud的依赖,其中Camden.SR3是版本号 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR3</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> 4.Zuul过滤器 1.过滤器需继承ZuulFilter基类 package com.cn.filter; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest;import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;public class LoginFilter extends ZuulFilter{
public Object run() {
System.out.println("进入网关过滤器======"); RequestContext requestContext = RequestContext.getCurrentContext(); HttpServletRequest httpServletRequest = requestContext.getRequest(); // TODO Auto-generated method stub Cookie[] cookie = httpServletRequest.getCookies(); for (Cookie cookie2 : cookie) { System.out.println(" key " + cookie2.getName() + " Value " + cookie2.getValue()); if("token".equals(cookie2.getName())){ requestContext.setSendZuulResponse(true); requestContext.setResponseStatusCode(200); // requestContext.setResponseBody("当前用户登录允许操作"); System.out.println("从 cookie中获取到值啦 正常请求!!value " + cookie2.getValue()); return null; } } System.out.println("后台未获取到相关信息------->"); requestContext.setSendZuulResponse(false); requestContext.setResponseStatusCode(401); requestContext.setResponseBody("当前用户未登录不允许其他操作"); return null; } //;// 是否执行该过滤器,此处为true,说明需要过滤 public boolean shouldFilter() { // TODO Auto-generated method stub return true; } // 优先级为0,数字越大,优先级越低 @Override public int filterOrder() { // TODO Auto-generated method stub return 0; }@Override
public String filterType() { // TODO Auto-generated method stub return "pre";// 前置过滤器 } }