Springcloud (7)---Gateway concept, Zuul project construction

Source: Internet
Author: User

Springcloud (7)---Gateway concept, Zuul project construction

First, Gateway concept1. What is a routing gateway

Gateway is the only external portal of the system, between the client and the server side of the middle tier , the processing of non-business functions to provide routing requests, authentication, monitoring, caching, current limit and other functions . It translates the "1-to-n" problem into a "1-to-1" problem.

Through the function of service routing, it is possible to expose only the call address configured in the gateway when serving externally, and the caller does not need to know the backend specific microservices host.

2. Why to use the micro-service Gateway

Different microservices typically have different network addresses, and clients may need to invoke multiple service interfaces to complete a business requirement, and if the client communicates directly with each of the microservices, the following issues occur:

(1) clients will request different microservices multiple times, increasing client complexity

(2) There are cross-domain requests, processing is relatively complex

(3) Complex authentication, each service requires independent certification

(4) Difficult to refactor, multiple services may be merged into one or split into multiple

3, the advantages of the gateway

The MicroServices gateway is a middle tier between the server and the client, and all external service requests go through the MicroServices Gateway customer only interacts with the MicroServices gateway, eliminating the need to invoke a specific microservices interface, making development easier

Total understanding of Gateway benefits

Service Gateway = Route forwarding + filter

(1) route forwarding : Receive all external requests, forwarded to the back-end of the micro-service up.

(2) filter : In the service Gateway can complete a series of crosscutting functions, such as permission check, current limit and monitoring, which can be done through the filter (in fact, the route forwarding is also through the filter implementation).

4. Service Gateway Technology Selection

The MicroServices architecture after the introduction of the service gateway, as a whole, consists of three parts: service gateways, Open-service, and services.

(1) Overall process

Service gateways, Open-service, and services are registered to the registry at the start-up;

When the user requests the gateway directly, the gateway does intelligent route forwarding (including service discovery, load balancing) to Open-service, which includes permission checking, monitoring, current limit operation, etc.

Open-service Aggregate Internal service response, return to the gateway, and return the gateway to the user

(2) Note points for introducing gateways

Increased the gateway, a layer of forwarding (the original user requests direct access to Open-service), performance will be reduced (but not small, usually, the Gateway machine performance will be good, and gateway and Open-service access is usually

Is the intranet access, speed quickly);

(3) Service Gateway basic function

Intelligent Routing : Receive all external requests, and forward to the back-end of the external services open-service up;

Note: We only forward external requests, the requests between the services do not go through the gateway, which means full link tracing, internal service API monitoring, fault-tolerant calls between internal services, intelligent routing can not be completed at the gateway;

Of course, you can also take all the service calls to the gateway, then almost all the functions can be integrated into the gateways, but in this way, the pressure of the gateway will be very heavy, overwhelmed.

authorization Check : Can be authenticated on the MicroServices gateway, and then forward the request to the microservices, no need to authenticate each micro-service, do not verify the internal service request. Is there a need to verify the internal service request?

API Monitoring : only monitor requests through the gateway, and some performance metrics of the gateway itself (for example, GC, etc.);

Current limit : In conjunction with the monitoring, the current limit operation;

API Log Unified Collection : Similar to a aspect slice, logging the interface's entry and exit related logs.

Ii. Construction of Zuul project

The components used include: Eureka, feign, Zuul, including the following four items:

(1) EUREKA-SERVER:7001 Registration Center

(2) product-server:8001 commodity micro-service

(3) order-server:9001 Order micro-service

(4) zuul-gateway:6001 Zuul Gateway

Registration Center, Commodity microservices, order in the previous blog has been set up, here is not repeated writing. Here only the Zuul-gateway micro-service is written.

1, Pom.xml
        <!--Client jar package, this in order micro service, commodity microservices are to be added -        <Dependency>            <groupId>Org.springframework.cloud</groupId>            <Artifactid>Spring-cloud-starter-netflix-eureka-client</Artifactid>        </Dependency>        <!--Zuuljar Bag -        <Dependency>            <groupId>Org.springframework.cloud</groupId>            <Artifactid>Spring-cloud-starter-netflix-zuul</Artifactid>        </Dependency>
2, Application.yml
 Server: port:6001#name of the service Spring: application: name: Zuul-Gateway#Specify a registry address Eureka: client: serviceurl: defaultzone: http:localhost:7001/eureka/#Custom Route Mappings Zuul: routes: order -service:/apigateway/order/**Product-service:/apigateway/product/**#Unified entry for the above configuration, other entrances ignored  Ignored-patterns:/*-service/**#ignoring the entire service and providing interfaces externally  ignored-services: Order-service
3. Springboot Startup class
@SpringBootApplication // Add net attention to the solution @EnableZuulProxy  Public class zuulserverapplication {    publicstaticvoid  main (string[] args) {        Springapplication.run (zuulserverapplication. class , args);}    }
4. Testing

(1) Direct Order Service Transfer goods service

(2) through the Zuul gateway to achieve the Order Interface Adjustment goods Service

5. Pay attention to some details

(1) The URL cannot be duplicated, otherwise it will overwrite

    Order-service:    /apigateway/order/**    product-service:  /apigateway/product/** 

(2) After Zuul, the cookie value in request cannot be obtained, because the gateway is filtered out.

    @RequestMapping ("list")    publicvoid list (@RequestParam ("user_id")int  int  productId, httpservletrequest request) {        = Request.getheader ("token");         = Request.getheader ("cookie");        // The token value can be found and the cookie cannot be retrieved because the gateway filters out the sensitive words        System.out.println ("token=" +token);        System.out.println ("cookie=" +cookie);    }

If you want to not filter out the cookie value, configure it in the configuration.

Zuul:  #处理http请求头为空的问题  sensitive-headers:
6. Questions

I encountered two problems recorded, later to think about the solution.

1, now through the order service address can directly access the order micro-service, how to configure the order micro-service can not directly service, only through the gateway access.

Thinking, is not after the order micro-service configuration to the intranet will not have this problem.

2. When my order service is abnormal, direct access to the order micro-service fuse degradation can be completed, through the gateway unexpectedly directly reported abnormal.

I add in the product MicroServices related interface:

// sleep two seconds, micro-service default one second time out, so will go to the demotion method TimeUnit.SECONDS.sleep (2);

Direct order service, downgrade information returned to normal. If accessed through the gateway.

Return is an exception, this is not very painful, there is always a solution to let the downgrade information back, and later to write.

Reference

Spring Cloud: Service Gateway Zuul

I just occasionally calm down and ponder over all the past. It's not worth condemning those old times that have been naïve and dull. After all, the days ahead are still long. Keep encouraging yourself,

The day is bright, is a new beginning, but also the unknown Journey (Colonel 9)

Springcloud (7)---Gateway concept, Zuul project construction

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.