Springcloud Routing Gateway Zuul (Fri)

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, 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:

<?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.forezp</ groupid> <artifactId>service-zuul</artifactId> <version>0.0.1-SNAPSHOT</version> <p Ackaging>jar</packaging> <name>service-zuul</name> <description>demo Project for Spring B oot</description> <parent> <groupId>com.forezp</groupId> <artifactid>sc-f-c         hapter5</artifactid> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> &LT;ARTIFACTID&G T;spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupid>org.springframework.boo        T</groupid> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactid>spri Ng-cloud-starter-netflix-zuul</artifactid> </dependency> </dependencies></project>

In its entry Applicaton class plus annotation @enablezuulproxy, the function of Zuul is turned on:

@SpringBootApplication@EnableZuulProxy@EnableEurekaClient@EnableDiscoveryClientpublic class ServiceZuulApplication {    public static void main(String[] args) {        SpringApplication.run( ServiceZuulApplication.class, args );    }}

Add the configuration file application.yml with the following configuration code:

eureka:  client:    serviceUrl:      defaultZone: http://localhost:8761/eureka/server:  port: 8769spring:  application:    name: service-zuulzuul:  routes:    api-a:      path: /api-a/**      serviceId: service-ribbon    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;

Iv. Service Filtering
Zuul is not only a route, but also a filter to do some security verification. Continue to reform the project;

@Componentpublic class Myfilter extends Zuulfilter {private static Logger log = Loggerfactory.getlogger (myfilter.class    );    @Override public String FilterType () {return ' pre ';    } @Override public int filterorder () {return 0;    } @Override public Boolean shouldfilter () {return true;        } @Override public 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) {} return null;      } log.info ("OK");  return null; }}

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.

Springcloud Routing Gateway Zuul (Fri)

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.