Spring Cloud (12) Service Gateway-Basic

Source: Internet
Author: User

Through the previous introduction, you can already build a simple microservices architecture, such as:

Through the Eureka implementation of service registries and service registration discovery, the use of the ribbon or feign to achieve the consumption of services and load balancing, through the Spring Cloud Config to achieve the application of multi-environment external configuration and version management. In order to make the cluster more robust, the Hystrix partition mechanism is used to avoid avalanches caused by the exception of individual services in the MicroServices architecture.

In this architecture, our service cluster consists of internal services service A and service B, they are registered to Eureka Server, and the open service is an external service that is exposed to the service callers through load balancing.

There is a shortage of such architectures:

First of all, to break the service stateless features, in order to ensure the security of external services, we need to achieve access to service access control, and the Open service access control mechanism will run through and pollute the entire open services business logic, the most direct problem is that the service cluster is destroyed the rest API stateless features. From the point of view of specific development and testing, in addition to considering the actual business in the work, additional control processing of interface access will be required.

Secondly, we can not directly reuse the existing interface, when we need to access the interface in an existing cluster, to achieve external service access, we have to increase the verification logic on the original interface, or add a proxy call to achieve the permission control, can not directly reuse the original interface.

In order to solve these problems, we need to take control of such things out of our service unit, and the place for these logic is in the forefront of external access, we need a more powerful load balancer---service gateway.

Service Gateway

Service gateways are an integral part of the microservices architecture. In addition to the service Routing and load balancing functions, the service gateway provides the rest API to the external system, and also has the functions of privilege control. The Zuul in Spring Cloud Netflix is a role that provides front-door protection for microservices architectures, while colleagues migrate permissions to control these heavier non-business logic content to the service routing plane, enabling service cluster principals to be more reusable and testable.

Building a service Gateway

Create a basic spring boot project, named David-gateway, and introduce dependencies in Pom.xml:

<?XML version= "1.0" encoding= "UTF-8"?><Projectxmlns= "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.david</groupId>    <Artifactid>Gateway</Artifactid>    <version>0.0.1-snapshot</version>    <Packaging>Jar</Packaging>    <name>Gateway</name>    <Description>Demo Project for Spring Boot</Description>    <Parent>        <groupId>Org.springframework.boot</groupId>        <Artifactid>Spring-boot-starter-parent</Artifactid>        <version>1.5.9.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>    </Properties>    <Dependencies>        <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>Edgware.sr2</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>

Using @enablezuulproxy to turn on the gateway feature in the Startup class

@EnableZuulProxy @springbootapplication  Public class gatewayapplication {    publicstaticvoid  main (string[] args) {        Springapplication.run (gatewayapplication. class , args);}    }

Editing a configuration file application.yml

Spring:  application:    name:david-gatewayserver:  8769Eureka:  Client:    serviceurl:      defaultzone:http://localhost:8761/eureka/

Launch Eureka, feign, Gateway input URL: http://localhost:8769/david-feign/test

found that the test method in feign can already be executed.

When our Gateway app launches and registers to Eureka, the service Gateway discovers the feign service that we started, when Zuul creates a routing rule that contains two parts, one for the external request, and one for the service ID of the route.

The request rules forwarded to the feign service are:/david-feign/**

Spring Cloud (12) Service Gateway-Basic

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.