0601-zuul Building API Gateway-api Gateway Introduction

Source: Internet
Author: User

Introduction of API Gateway

See: http://www.cnblogs.com/bjlhx/p/8794437.html

Ii. Zuul Introduction "Routers and filters: Zuul"

The components of the MicroServices architecture are routed. For example,/may map to your Web application,/api/users maps to the User Service, and/Api/shop maps to the store service. The Zuul is a JVM-based router and server-side load balancer provided by Netflix.

Netflix uses Zuul to do the following:

Certifications, insights, stress testing, canary testing, dynamic routing, service migration, load shedding, security, static response processing, active/active traffic management

Zuul's rule engine allows rules and filters to be written in essentially any JVM language, and supports Java and groovy.

Note 1: The configuration property zuul.max.host.connections has been superseded by two new properties Zuul.host.maxTotalConnections and zuul.host.maxPerRouteConnections, with default values of 200 and 20, respectively.

NOTE 2: The default Hystrix isolation mode (executionisolationstrategy) for all routes is semaphore. If this isolation mode is preferred, zuul.ribbonisolationstrategy can be changed to thread.

2.1, Embedded Zuul reverse proxy

Spring Cloud has created an embedded Zuul agent to mitigate the development of very common use cases where the UI application wants to invoke the calling agent to one or more backend services. This feature is useful for the backend services required by the user interface proxy, thus avoiding the need to independently manage all backend cors and authentication issues.

To enable it, use @enablezuulproxy to annotate a spring boot main class and forward local calls to the appropriate service. By convention, a service with an ID of "user" will receive a request from a proxy that is located/users (stripped of the prefix). The agent uses the Ribbon to locate an instance to be forwarded through the Ribbon, and all requests are executed in the Hystrix command, so the fault will be shown in the Hystrix indicator, and the agent will not attempt to contact the service once the circuit breaker is open.

Note: The Zuul launcher does not contain discovery clients, so for service ID-based routing, you need to provide one of these in the classpath (for example, Eureka is a choice).

2.1.1, using POM references
        <Dependency>            <groupId>Org.springframework.cloud</groupId>            <Artifactid>Spring-cloud-starter-netflix-eureka-client</Artifactid>        </Dependency>        <Dependency>            <groupId>Org.springframework.cloud</groupId>            <Artifactid>Spring-cloud-starter-netflix-zuul</Artifactid>        </Dependency>
Start class Add @enablezuulproxy
@SpringBootApplication @enablezuulproxy  Public class zuulapplication {    publicstaticvoid  main (string[] args) {        Springapplication.run (zuulapplication. class , args);}    }
Configuration file
Spring:  application:    name:microservice-gateway-zuulserver:  8040Eureka:  Client:    serviceurl:      defaultzone:http://user:[email protected]:8761/eureka/   instance:    prefertrue

Start the Eureka project, start the User-provider project, and start the Zuul project

Access by Zuul: HTTP://192.168.199.211:8040/MICROSERVICE-PROVIDER-USER/SAMPLE/1

2.1.2, routing Rule 1, simplifying the microservices name

Route Microservice-provider-user to User

Zuul:  routes:    microservice-provider-user:/user/**
2, only want to reverse proxy microservice-provider-user User Services, other non-agent
Zuul:  ignoredservices: ' * '  routes:    users:/myusers/**

or ignore the specific, use "," delimited

3. For better control of routing, you can specify paths and Serviceid independently:
Zuul:  routes:    user-controller: #名称任意      path:/user/**      Serviceid:microservice-provider-user

This means that the HTTP call "/myusers" is forwarded to the "Users_service" service. The route must have a path that can be specified as an ant style, so "/myusers/*" matches only one level, but "/myusers/**" is layered.

4. You can specify the location of the backend as "serviceId" (For services from discovery) or "url" (for physical locations)
Zuul:  routes:    user-controller: #名称任意      path:/user/**      url:http://127.0.0.1:7900/
5. Load Balancing

These simple URL routes do not execute as Hystrixcommand and do not use the Ribbon to load multiple URLs.

Method one, for this purpose, you can specify a serviceid with a static list of servers:

Zuul:  routes:    Echo:       #名称任意      path:/microservice/**      serviceid:microservice-provider-user      Stripprefix:truehystrix:  command:    microservice-provider-user:      execution:        isolation:          Thread:            timeoutinmilliseconds:10000microservice-provider-user: #微服务的ID  Ribbon:    NIWSServerListClassName:com.netflix.loadbalancer.ConfigurationBasedServerList    listofservers:http:// localhost:7900,http://localhost:7901    connecttimeout:1000    readtimeout:3000    maxtotalhttpconnections:500    maxconnectionsperhost:100

Method Two, another method is to specify a service route and configure a Ribbon client for Serviceid (this requires disabling Eureka support in the Ribbon: For more information, see above).

Ribbon:  Eureka:    enabled:false    Zuul:  routes:    user-controller: #名称任意      path:/user/**      Serviceid:microservice-provider-usermicroservice-provider-user: #微服务的ID  Ribbon:    listofservers:http:// localhost:7900,http://localhost:7901

Note: Top 6 reference codes: Https://github.com/bjlhx15/spring-cloud/tree/master/microservice-gateway-zuul

6. Using regular configuration You can use Regexmapper to provide conventions between Serviceid and routing. It uses regular expression-named groups to extract variables from Serviceid and inject them into the routing pattern.
@Bean  Public patternserviceroutemapper Serviceroutemapper () {    returnnew  Patternserviceroutemapper ("(? <name>^.+)-(? <version>v.+$)""${version}/${name}" );}

This means that Serviceid "MYUSERS-V1" will be mapped to route "/v1/myusers/* *". Any regular expression is accepted, but all named groups must exist in Servicepattern and Routepattern. If the Servicepattern does not match the Serviceid, the default behavior is used.

See Code: HTTPS://GITHUB.COM/BJLHX15/SPRING-CLOUD/TREE/MASTER/MICROSERVICE-GATEWAY-ZUUL-REG-EXP

7. Map prefix

To add a prefix to all mappings, set Zuul.prefix to a value, such as/API. The proxy prefix is stripped from the request before the request is forwarded by default (using Zuul.stripprefix = False to turn off this behavior). You can also turn off service-specific prefixes from a single route, such as

Set the prefix:

Zuul:   /api    

Request Address: HTTP://192.168.199.211:8040/API/V1/MICROSERVICE-PROVIDER-USER/SAMPLE/1

Peel Prefix

Zuul:  routes:    users:      /myusers/**      stripprefix:false

The Zuul.stripprefix is particularly suitable for zuul.prefix and is a global;zuul.routes.<route>. Stripprefix for zuul.routes.<route>. Path, It's local.

Note: Zuul.stripprefix only applies to prefixes that are set in Zuul.prefix. It has no effect on prefixes defined in a given path path.

Log tracking code

Logging: Level  :    com.netflix:debug
8, ignore the specific path
Zuul:   /** /admin/**  routes:    users:/myusers/**

This means that all calls such as "/myusers/101" will be forwarded to "/101" on the "user" service. However, calls that include "/admin/" will not be resolved.

9. Use routing for a service, other not applicable
Zuul:  routes:    users:      /myusers/**    Legacy:      path:/**

Use/myusers/** for user routing, other use/**

Note: If you need your order to retain its priority, you need to use a Yaml file, because using a property file loses priority.

  

0601-zuul Building API Gateway-api Gateway Introduction

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.