In the micro-service architecture, several basic service governance components are needed, including service registration and discovery, service consumption, load balancing, circuit breakers, intelligent routing, configuration management, etc., which are co-organized by these basic components to form a simple microservices system. A brief micro-service system such as:
Note: A service and b services can be called each other, when the drawing is forgotten. And the configuration service is also registered with the service registry.
In the spring Cloud microservices system, a common load balancing method is that the client's request is first load-balanced (Zuul, Ngnix), then the service Gateway (Zuul cluster), and then to the specific services. , the service is registered to a highly available service registry cluster, all the configuration files for the service are managed by the Configuration service (the next article), the configuration files of the Config service are placed in the Git repository, so that developers can change the configuration at any time.
I. Introduction of Zuul
The main function of Zuul is routing forwarding and filtering. The routing feature is part of the microservices, such as/api/user forwarding to the User Service, and/api/shop forwarding to the shop service. The Zuul default and ribbon combine to achieve load balancing capabilities.
Zuul has the following features:
- Authentication
- Insights
- Stress Testing
- Canary Testing
- Dynamic Routing
- Service Migration
- Load Shedding
- Security
- Static Response Handling
- Active/active Traffic Management
Ii. preparatory work
Continue with the previous section of the project. In the original project, create a new project.
Iii. Creation of Service-zuul project
Its pom.xml file is 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"3xsi: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>com.forezp</groupId>7<artifactId>service-zuul</artifactId>8<version>0.0.1-SNAPSHOT</version>9<packaging>jar</packaging>Ten One<name>service-zuul</name> A<description>demo Project forSpring boot</description> - -<parent> the<groupId>com.forezp</groupId> -<artifactId>sc-f-chapter5</artifactId> -<version>0.0.1-SNAPSHOT</version> -</parent> + -<dependencies> +<dependency> A<groupId>org.springframework.cloud</groupId> at<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> -</dependency> -<dependency> -<groupId>org.springframework.boot</groupId> -<artifactId>spring-boot-starter-web</artifactId> -</dependency> in<dependency> -<groupId>org.springframework.cloud</groupId> to<artifactId>spring-cloud-starter-netflix-zuul</artifactId> +</dependency> -</dependencies> the</project>
In its entry Applicaton class plus annotation @enablezuulproxy, the function of Zuul is turned on:
1 @SpringBootApplication2 @EnableZuulProxy3 @EnableEurekaClient4 @EnableDiscoveryClient5 Public classservicezuulapplication {6 7 Public Static voidMain (string[] args) {8Springapplication.run (servicezuulapplication.class, args);9 }Ten}
Add the configuration file application.yml with the following configuration code:
1 Eureka:2 Client:3 serviceurl:4Defaultzone:http://localhost:8761/eureka/5 Server:6port:87697 Spring:8 Application:9name:service-ZuulTen Zuul: One routes: Aapi-A: -Path:/api-a/** - Serviceid:service-ribbon the Api-b: - Path:/api-b/** - serviceid:service-feign
First specify that the service registry address is http://localhost:8761/eureka/, the service port is 8769, the service name is Service-zuul, and the requests beginning with/api-a/are forwarded to the Service-ribbon service; api-b/the beginning of the request is forwarded to the Service-feign service;
Run these five projects sequentially; Open browser access: HTTP://LOCALHOST:8769/API-A/HI?NAME=FOREZP; Browser display:
Hi forezp,i am from port:8762
Open browser access: HTTP://LOCALHOST:8769/API-B/HI?NAME=FOREZP; Browser display:
Hi forezp,i am from port:8762
This means that Zuul is acting as a route.
Iv. Service Filtering
Zuul is not only a route, but also a filter to do some security verification. Continue to reform the project;
1 @Component2 Public classMyfilterextendsZuulfilter {3 4 Private StaticLogger log = Loggerfactory.getlogger (myfilter.class);5 @Override6 PublicString FilterType () {7 return"Pre";8 }9 Ten @Override One Public intFilterorder () { A return0; - } - the @Override - Public BooleanShouldfilter () { - return true; - } + - @Override + PublicObject Run () { ARequestContext CTX =Requestcontext.getcurrentcontext (); atHttpServletRequest request =ctx.getrequest (); -Log.info (String.Format ("%s >>>%s", Request.getmethod (), Request.getrequesturl (). toString ( )); -Object Accesstoken = Request.getparameter ("token"); - if(Accesstoken = =NULL) { -Log.warn ("token is empty"); -Ctx.setsendzuulresponse (false); inCtx.setresponsestatuscode (401); - Try { toCtx.getresponse (). Getwriter (). Write ("token is empty"); +}Catch(Exception e) {} - the return NULL; * } $Log.info ("OK");Panax Notoginseng return NULL; - } the}
- FilterType: Returns a String representing the type of filter, and defines the filter type for four different lifecycles in Zuul, as follows:
- Pre: Before routing
- Routing: The time of routing
- Post: After routing
- Error: Sending the wrong call
- Filterorder: The Order of filtering
- Shouldfilter: Here can write logical judgments, whether to filter, this article true, filter forever.
- Run: The specific logic of the filter. Can be complicated, including checking sql,nosql to see if the request has access.
At this time visit: HTTP://LOCALHOST:8769/API-A/HI?NAME=FOREZP; page display:
Token is empty
Access to http://localhost:8769/api-a/hi?name=forezp&token=22;
Page display:
Hi forezp,i am from port:8762
This article source code download:
Https://github.com/forezp/SpringCloudLearning/tree/master/sc-f-chapter5
V. References:
Http://cloud.spring.io/spring-cloud-static/Finchley.RELEASE/single/spring-cloud.html
Thank you for your attention! Add QQ1 Group: 135430763,QQ2 Group: 454796847,QQ3 Group: 187424846. QQ Group into the group password: xttblog, want to add a group of friends, you can search: Xmtxtt, note: "Xttblog", add Assistant pull you into the group. Note errors do not agree with a friend application. Thanks again for your attention! Follow up with wonderful content will be sent to you the first time! Please send the original article to [email protected] email. Business cooperation can add assistants to communicate!
Amateur Grass Springcloud Tutorial | Fifth article: Routing Gateway (Zuul) (Finchley version)