Gateway
Micro-service architecture, there will be multiple services, each service has a different address, users request a service, may perform multiple requests at this time, we need to be forwarded to the gateway. Gateway is located after request to initiate, the middle layer before access services, all access, all need to go through a gateway, for example, when a user accesses api, request a link for / login, then it is forwarded to the login service request link to / shop , it is forwarded to the service shop.
Zuul
zuul is a netflix open source API Gateway server, essentially a web servlet application, the main function is to Zuul route forwarding and filters.
Zuul on the cloud platform provides dynamic routing, monitoring, frame flexibility, security and other edge services. The entire project will be likened to a big house, then Zuul equivalent to the doorman all requested Web sites and back-end equipment Netflix streaming applications, guided by its request to enter the room they requested.
Building project
In the project, create Module1, and the Eureka Zuul introduction, the new file is good pom.xml file as follows:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 6 <groupId>cn.ponytech</groupId> 7 <artifactId>zuul</artifactId> 8 <version>0.0.1-SNAPSHOT</version> 9 <packaging>jar</packaging> 10 11 <name>zuul</name> 12 <description>Demo project for Spring Boot</description> 13 14 <parent> 15 <groupId>org.springframework.boot</groupId> 16 <artifactId>spring-boot-starter-parent</artifactId> 17 <version>2.0.3.RELEASE</version> 18 <relativePath/> <!-- lookup parent from repository --> 19 </parent> 20 21 <properties> 22 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 23 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 24 <java.version>1.8</java.version> 25 <spring-cloud.version>Finchley.RELEASE</spring-cloud.version> 26 </properties> 27 28 <dependencies> 29 <dependency> 30 <groupId>org.springframework.cloud</groupId> 31 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> 32 </dependency> 33 <dependency> 34 <groupId>org.springframework.cloud</groupId> 35 <artifactId>spring-cloud-starter-netflix-zuul</artifactId> 36 </dependency> 37 38 <dependency> 39 <groupId>org.springframework.boot</groupId> 40 <artifactId>spring-boot-starter-test</artifactId> 41 <scope>test</scope> 42 </dependency> 43 </dependencies> 44 45 <dependencyManagement> 46 <dependencies> 47 <dependency> 48 <groupId>org.springframework.cloud</groupId> 49 <artifactId>spring-cloud-dependencies</artifactId> 50 <version>${spring-cloud.version}</version> 51 <type>pom</type> 52 <scope>import</scope> 53 </dependency> 54 </dependencies> 55 </dependencyManagement> 56 57 <build> 58 <plugins> 59 <plugin> 60 <groupId>org.springframework.boot</groupId> 61 <artifactId>spring-boot-maven-plugin</artifactId> 62 </plugin> 63 </plugins> 64 </build> 65 </project>
Class at the inlet was added @ EnableZuulProxy, @ EnableEurekaClient annotation, and Eureka open Zuul
1 @SpringBootApplication 2 @EnableZuulProxy 3 @EnableEurekaClient 4 public class ZuulApplication { 5 public static void main(String[] args) { 6 SpringApplication.run(ZuulApplication.class, args); 7 } 8 }
In the properties file, specify the port number and other information, with the URL / forward ribbon ribbon service requests to the URL with / forward requests to feign feign service.
1 server.port=9005 2 eureka.client.service-url.defaultZone= http://localhost:9000/eureka/ 3 spring.application.name=zuul 4 zuul.routes.ribbon.path=/ribbon/** 5 zuul.routes.ribbon.service-id=ribbon 6 zuul.routes.feign.path=/feign/** 7 zuul.routes.feign.service-id=feign
Start turn 5 project, visit http: // localhost: 9005 / ribbon / welcome name = Cheng, appear?:
Cheng welcome you, here are some words to say to you:! Hey This is Spring Cloud
Visit http: // localhost: 9005 / feign / welcome name = Cheng, appear?:
Cheng welcome you, here are some words to say to you:! Hey This is Spring Cloud
Description two transponders Zuul are successful, played a role in the route.
Services filter
Zuul Another major feature is the service filter, for example, before the user login, you can request the service to filter to a specific page.
In the project, add a MyFilter class. code show as below:
@Component public class MyFilter extends ZuulFilter { @Override public String filterType() { return FilterConstants.PRE_TYPE; } @Override public int filterOrder() { return 0; } @Override public boolean shouldFilter() { return true; } @Override public Object run(){ RequestContext ctx = RequestContext.getCurrentContext(); HttpServletRequest request = ctx.getRequest(); Object accessToken = request.getParameter("token"); if(accessToken == null) { ctx.setSendZuulResponse(false); ctx.setResponseStatusCode(401); try { ctx.getResponse().setHeader("Content-Type", "text/html;charset=UTF-8"); ctx.getResponse().getWriter().write("登录信息为空!"); }catch (Exception e){} return null; } return null; } }
Wherein, filterType method returns a string representative of the type of filter, the filter type defines four different zuul in the life cycle, as follows:
pre: Before routing routing: routing the post: The routes error: Error sending call filterOrder: filter order shouldFilter: here you can write logic determines whether to enable the filter run: the specified logical filter. Available are complex, including check sql, nosql in the end to determine whether the request has no access.
Generally we use, no hand to play "pre" these types, but by calling the Zuul have been written FilterConstants class, which encapsulates all of the filter types. This avoids the wrong character and leads to errors.
Next, we visit http: // localhost: 9005 / feign / welcome name = Cheng show?:
Log information is empty!
Followed by the token, http: // localhost:? 9005 / feign / welcome name = Cheng & token = 111, show:
Cheng welcome you, here are some words to say to you:! Hey This is Spring Cloud
This proves that our filters play a role.