1.可以实现请求路由(nginx、F5)和请求过滤(spring filter)功能
2.zuul可以实现高可用性,通过nginx放到其前部就可以实现了
请求路由
<?xml version="1.0" encoding="UTF-8"?> <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> <groupId>com.midea</groupId> <artifactId>base</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>base</name> <!-- 设定仓库,按设定顺序进行查找. --> <!--<repositories> <repository> <id>public</id> <name>Team Nexus Repository</name> <url>http://10.33.183.113:8081/nexus/content/groups/public</url> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>public</id> <name>Team Nexus Repository</name> <url>http://10.33.183.113:8081/nexus/content/groups/public</url> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> </snapshots> </pluginRepository> </pluginRepositories> --> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.4.0</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.4.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> </build> </project>
server.port=8766 #logging.pattern.level=INFO #服务器路径 server.context-path=/zuul # #eureka.instance.hostname=localHost #表示是否注册自身到eureka服务器,因为当前这个应用就是eureka服务器,没必要注册自身,所以这里是false。 #eureka.client.registerWithEureka=false #fetchRegistry表示是否从eureka服务器获取注册信息,同上,这里不需要 #eureka.client.fetchRegistry=false #设置eureka服务器所在的地址,查询服务和注册服务都需要依赖这个地址。 #eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/ eureka.client.serviceUrl.defaultZone=http\://localhost\:8761/eurekaServer/eureka/ spring.application.name=zuul ##====================传统路由方式======================== ##访问的路径(“/myusers/*”只能匹配一个层级,但"/myusers/**"可以匹配多级.) #zuul.routes.ribbonTest.path=/ribbonTest/** ##转发到的URL(如:http://localhost:8766/ribbonTest/CallTest/test 会转发到 http://localhost:8764/ribbon/CallTest/test) #zuul.routes.ribbonTest.url=http://localhost:8764/ribbon/ ##访问的路径 #zuul.routes.feignTest.path=/feignTest/** #zuul.routes.feignTest.url=http://localhost:8765/feign/ #====================面向服务路由方式======================== #访问的路径 zuul.routes.ribbonTest.path=/ribbon/** #转发到的URL(如:http://localhost:8766/ribbonTest/CallTest/test 会转发到 http://localhost:8764/ribbon/CallTest/test) zuul.routes.ribbonTest.serviceId=ribbon #如果为true(或不设置)则/ribbon/**中的/ribbon/去掉,否则会在转发中出会加入 zuul.routes.ribbonTest.stripPrefix=false #访问的路径 zuul.routes.feignTest.path=/feign/** zuul.routes.feignTest.serviceId=feign zuul.routes.feignTest.stripPrefix=false #访问的路径 zuul.routes.eurekaClientTest.path=/eurekaClient/** zuul.routes.eurekaClientTest.serviceId=eureka-Client zuul.routes.eurekaClientTest.stripPrefix=false
package com; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.feign.EnableFeignClients; import org.springframework.cloud.netflix.hystrix.EnableHystrix; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication //spring boot 开启应用 //@EnableDiscoveryClient //Discovery Service”有多种实现,比如:eureka, consul, zookeeper。 @EnableEurekaClient //只能为eureka作用 @EnableZuulProxy //开启zuul public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
请求过滤
package com.filter; import com.netflix.zuul.ZuulFilter; import com.netflix.zuul.context.RequestContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest; @Component public class MyFilter extends ZuulFilter { private static Logger log = LoggerFactory.getLogger(MyFilter.class); // filterType:返回一个字符串代表过滤器的类型,在zuul中定义了四种不同生命周期的过滤器类型,具体如下: // pre:路由之前 // routing:路由之时 // post: 路由之后 // error:发送错误调用 @Override public String filterType() { return "pre"; } // filterOrder:过滤的顺序 @Override public int filterOrder() { return 0; } // shouldFilter:这里可以写逻辑判断,是否要过滤,本文true,永远过滤。 @Override public boolean shouldFilter() { return true; } // run:过滤器的具体逻辑。可用很复杂,包括查sql,nosql去判断该请求到底有没有权限访问。 @Override public Object run() { RequestContext ctx = RequestContext.getCurrentContext(); HttpServletRequest request = ctx.getRequest(); log.info(request.getMethod() + " , " + request.getRequestURL().toString()); Object accessToken = request.getParameter("token"); if (accessToken == null) { log.warn("token is empty"); ctx.setSendZuulResponse(false); ctx.setResponseStatusCode(401); try { ctx.getResponse().getWriter().write("token is empty"); } catch (Exception e) { } return null; } log.info("ok"); return null; } }
测试地址:
http://localhost:8766/zuul/ribbon/CallTest/test
http://localhost:8766/zuul/feign/CallTest/test
http://localhost:8766/zuul/ribbon/CallTest/test?token=xing
http://localhost:8766/zuul/feign/CallTest/test?token=xing
http://localhost:8766/zuul/eurekaClient/Test/test?token=xing