SpringCloud|SpringCloud - zuul网关

POM

4.0.0com.boolib.cloud server_zuul 0.0.1-SNAPSHOTjarserver_zuul Demo project for Spring Boot【SpringCloud|SpringCloud - zuul网关】org.springframework.boot spring-boot-starter-parent 2.0.4.RELEASE UTF-8UTF-8 1.8 Finchley.SR1 org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.cloud spring-cloud-starter-netflix-zuul org.springframework.boot spring-boot-devtools runtime org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin

zuul启动项
@SpringBootApplication @EnableZuulProxy @EnableEurekaClient public class ServerZuulApplication {public static void main(String[] args) { SpringApplication.run(ServerZuulApplication.class, args); } }

参数配置
eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/server.port=8763spring.application.name=service-zuulzuul.routes.api-a.path=/member/**zuul.routes.api-a.service-id=service-orderzuul.routes.api-b.path=/provide/**zuul.routes.api-b.service-id=service-providezuul.routes.api-c.path=/client/**zuul.routes.api-c.service-id=config-client

ps:api-c 这个是自己写的 service-id = 值 千万别写错!!!
filter
package com.boolib.cloud.server_zuul.filter; import com.netflix.zuul.ZuulFilter; import com.netflix.zuul.context.RequestContext; import com.netflix.zuul.exception.ZuulException; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest; import java.io.IOException; @Component @Slf4j public class MyFilter extends ZuulFilter {@Override public String filterType() { return "pre"; }@Override public int filterOrder() { return 0; }@Override public boolean shouldFilter() { return true; }@Override public Object run() throws ZuulException {RequestContext ctx = RequestContext.getCurrentContext(); HttpServletRequest request = ctx.getRequest(); Object token = request.getParameter("token"); if(token!=null) { return null; }ctx.setSendZuulResponse(false); ctx.setResponseStatusCode(401); try { ctx.getResponse().getWriter().write("token is empty"); } catch(IOException ex) {} return null; } }

    推荐阅读