springcloud-Routing Gateway (Zuul)

Source: Internet
Author: User


In the micro-service architecture, several basic service governance components are needed, including service registration and discovery, service consumption, load balancing, circuit breakers, routing, configuration Management, etc., which are co-organized by these basic components to form a simple microservices system.

In the spring Cloud microservices system, a common load balancing method is that the client's request is first load balanced (Zuul, Nginx), then to the service Gateway (Zuul cluster), then to the specific service, the service is registered to a highly available service registry cluster, All the configuration files of the service are managed by the configuration service, and the configuration files of the configuration service are placed in the Git repository, so that developers can change their configuration at any time.

Zuul Introduction

Zuul as long as the function is route forwarding and filters. The routing feature is part of the microservices, such as/api/user forwarding to the User Service,/api/shop forwarding to the shop service, Zuul the default and ribbon combine to achieve load balancing.
Zuul has the following functions
? Authentication
? Insights
? Stress Testing
? Canary Testing
? Dynamic Routing
? Service Migration
? Load Shedding
? Security
? Static Response Handling
? Active/active Traffic Management

Create a Service-zuul Project

Pom as follows

<?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. springcloud</groupid><artifactid>service-zuul</artifactid><version>0.0.1-snapshot</ Version><packaging>jar</packaging><name>service-zuul</name><description> Service-zuul-description</description><parent><groupid>org.springframework.boot</groupid ><artifactid>spring-boot-starter-parent</artifactid><version>1.5.13.release</version ><relativePath/> <!--lookup parent from repository--></parent><properties>< Project.build.sourceencoding>utf-8</project.build.sourceencoding><project.reporting.outputencoding >utf-8</project.reporting.outputencoding><java.version>1.8</java.version><spring-cloud.version>edgware.sr3</ Spring-cloud.version></properties><dependencies><dependency><groupid> org.springframework.boot</groupid><artifactid>spring-boot-starter-web</artifactid></ Dependency><dependency><groupid>org.springframework.cloud</groupid><artifactid> Spring-cloud-starter-eureka-server</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-test</artifactid><scope>test</scope></dependency></dependencies ><dependencymanagement><dependencies><dependency><groupid>org.springframework.cloud </groupid><artifactid>spring-cloud-depEndencies</artifactid><version>${spring-cloud.version}</version><type>pom</type> <scope>import</scope></dependency></dependencies></dependencyManagement>< Build><plugins><plugin><groupid>org.springframework.boot</groupid><artifactid >spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

In the program entrance application class plus annotation @enablezuulproxy, turn on the function of Zuul

@EnableZuulProxy @enableeurekaclient@springbootapplicationpublic class Servicezuulapplication {public static void Main (string[] args) {Springapplication.run (servicezuulapplication.class, args);}}

Application.yml as follows

spring:application:name:service-zuulserver:port:8769eureka:client:serviceurl:defaultzone:http://localhost:8761 /eureka/zuul:routes:api-a:path:/api-a/**serviceid:service-ribbonapi-b:path:/api-b/**serviceid:service-feign

Requests beginning with/api-a/are forwarded to the Service-ribbon service, and the requests beginning with/api-b/are forwarded to the Service-feign service
Start Eureka-server, Eureka-client,service-ribbon,service-feign,service-zuul
Request HTTP://LOCALHOST:8769/API-A/HI?NAME=FZ

Http://localhost:8769/api-b/hi?name=fz

This means that Zuul is acting as a route.

Service filtering

Zuul not only route, but also filter, do some security verification, as follows

@Componentpublic class Myfilter extends Zuulfilter {private static Logger log = Loggerfactory.getlogger (Myfilter.class); @Overridepublic String FilterType () {return ' pre ';} @Overridepublic int Filterorder () {return 0;} @Overridepublic Boolean shouldfilter () {return true;} @Overridepublic Object Run () {RequestContext CTX = Requestcontext.getcurrentcontext (); HttpServletRequest 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); Ctx.setresponsestatuscode (401); try {ctx.getresponse (). Getwriter (). Write ("token is empty"); catch (Exception e) {e.printstacktrace ();} return null;} Log.info ("OK"); return null;}}

FilterType: Returns a String that represents the type of filter, and defines the filter type for the 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 be logic to determine whether it is necessary to filter, true, filter forever
Run: The specific logic of the filter. Can be complicated, including checking sql,nosql to see if the request has access.

Restart Project test

Add token

Success

springcloud-Routing Gateway (Zuul)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.