Enterprise Distribution Micro Service Cloud Springcloud springboot MyBatis (v) 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, 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> <packaging>jar</packaging> <name>service-zuu L</name> <description>demo Project for Spring boot</description> <parent> <groupId>org.springframework.boot</groupId> &        Lt;artifactid>spring-boot-starter-parent</artifactid> <version>1.5.2.RELEASE</version> <relativePath/> <!--lookup parent from repository to </parent> <properties> <pro Ject.build.sourceencoding>utf-8</project.build.sourceencoding> <project.reporting.outputencoding >UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties&    Gt            <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> </dependencies> <depen dencymanagement> <dependencies> <dependency> <groupid>org.springfram Ework.cloud</groupid> <artifactId>spring-cloud-dependencies</artifactId> & Lt;version>dalston.rc1</version> <type>pom</type> <scope>Import</scope> </dependency> </dependencies> </dependencyManagement> &LT;BUILD&G        T                <plugins> <plugin> <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins>            </build> <repositories> <repository> <id>spring-milestones</id> <name>spring milestones</name> <url>https://repo.spring.io/milestone</url><snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories></project>

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

@EnableZuulProxy @enableeurekaclient@springbootapplication  Public class servicezuulapplication {     publicstaticvoid  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:   8769Spring:  application:    name:service-Zuulzuul:  routes:    API-A:       /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;

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.

The schema code is as follows:

Source source technical support for complete projects 2147775633

Enterprise Distribution Micro Service Cloud Springcloud springboot MyBatis (v) 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.