Springcloud (10): Service Gateway zuul/spring Cloud Learning (6)-Zuul Micro Service Gateway

Source: Internet
Author: User
Springcloud (10): Service Gateway Zuul

In the previous article we introduced the Eureka used for service registration in discovery, feign support service invocation and balance load, hystrix processing services to fuse to prevent failure spread, Spring Cloud Config Service cluster Configuration center, it seems that a micro-service framework has been completed.

We are still less concerned about the external application of how to access the internal various micro-services. In a micro-service architecture, back-end services are often not directly open to the caller, but are routed to the corresponding service via an API gateway based on the requested URL. When the API gateway is added, a wall is created between the Third-party caller and the service provider, which is directly controlled by the caller communication, and the request is distributed evenly to the backend server.


Why do I need API Gateway

1. Simplify the complexity of client invocation

The number of instances of backend services in the micro-service architecture mode is generally dynamic, and it is difficult for clients to discover the access address information of dynamically changing service instances. Therefore, in order to simplify the calling logic of the front-end in the micro-service project, the API gateway is usually introduced as a lightweight gateway, and the relevant authentication logic is also implemented in the API gateway to simplify the complexity of mutual invocation between internal services.

2. Data cropping and aggregation

Typically, different clients are inconsistent with the need for data to be displayed, such as a mobile phone or web side, or in a low latency network environment or a high latency network environment.

Therefore, in order to optimize the client experience, the API gateway can tailor the generic response data to meet the needs of different clients. You can also aggregate multiple API invocation logic to reduce the number of client requests and optimize the client user experience

3. Multi-channel support

Of course we can also provide different API Gateway for different channels and clients, the use of this pattern is called backend for front-end in another well-known way, in backend for front-end mode, We can create their BFF for different clients, further understand BFF can refer to this article: Pattern:backends for Frontends

4, micro-service transformation of legacy systems

For system systems, micro-service transformation is usually due to the existing system has more or less problems, such as technical debt, code quality, maintainability, scalability and so on. The model of API Gateway also applies to the transformation of this kind of legacy system, and realizes the restoration of the problems in the original system gradually through the transformation of micro-service, so as to enhance the response ability of the original business. By introducing the abstraction layer, you gradually replace the old implementation with the new implementation.

In the spring Cloud system, Spring Cloud Zuul is an API gateway that provides load balancing, reverse proxy, and permission authentication. Spring Cloud Zuul

Spring Cloud Zuul Routing is an integral part of the micro-service architecture, providing edge services such as dynamic routing, monitoring, resiliency, and security. Zuul is a Netflix-produced load balancer based on JVM Routing and server-side.

Here's a simple use of code to understand how Zuul works

1. Add dependencies

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId> Spring-cloud-starter-zuul</artifactid>
</dependency>

Introduction of Spring-cloud-starter-zuul Package

2. Configuration file

Spring.application.name=gateway-service-zuul
server.port=8888

#这里的配置表示, Access/it/** Direct redirect to http:// www.ityouknow.com/**
zuul.routes.baidu.path=/it/**
zuul.routes.baidu.url=http://www.ityouknow.com/

3. Start class

@SpringBootApplication
@EnableZuulProxy Public
class Gatewayservicezuulapplication {public

    static void Main (string[] args) {
        springapplication.run (gatewayservicezuulapplication.class, args);
    }
}

The start class adds @enablezuulproxy to support gateway routing.

The simplest Zuul case in history has been configured.

4, testing

Start Gateway-service-zuul-simple project, access in browser: Http://localhost:8888/it/spring-cloud, see page returned: http://www.ityouknow.com The information for the/spring-cloud page is as follows:

We test the requested redirection with the example code spring-cloud-producer in the previous article, adding in the configuration file:

zuul.routes.hello.path=/hello/**
Zuul.routes.hello.url=http://localhost:9000/

Start Spring-cloud-producer, reboot gateway-service-zuul-simple, access: http://localhost:8888/hello/hello?name=%E5%B0%8F%E6 %98%8e, back: Hello Xiaoming, this is the Messge

The request to access Gateway-service-zuul-simple is automatically forwarded to the Spring-cloud-producer and the result is returned.


Service of

URL mapping to achieve zull forwarding has limitations, such as each additional service needs to configure a piece of content, and the back-end of the service if it is dynamic to provide, you can not use this scheme to configure. In fact, when implementing a micro-service architecture, the relationship between the service name and the service instance address already exists in the Eureka server, so the mapping of Serviceid can be achieved only if Zuul is registered to the Eureka server to discover other services.

We combine the examples to illustrate that the above example project Gateway-service-zuul-simple on the basis of the transformation.

1. Add dependencies

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId> Spring-cloud-starter-eureka</artifactid>
</dependency>

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.