[SpringBoot] Zuul Gateway of SpringBoot (15) [SpringCloud] Quickstart of SpringCloud (1)

Gateway concept

  1. What is a routing gateway

  The gateway is the only external entrance of the system. It is the middle layer between the client and the server. It handles non-business functions and provides functions such as routing requests, authentication, monitoring, caching, and current limiting. It converts the "1 to N" problem into a "1 to 1" problem.

  Through the function of service routing, you can only expose the call address configured in the gateway when providing services to the outside world, and the caller does not need to know the specific micro service host on the back end.

  2. Why use microservice gateway

  Different microservices generally have different network addresses, and the client may need to call multiple service interfaces to complete a business requirement. If the client directly communicates with each microservice, there will be the following problems:

  1) The client will request different microservices multiple times, increasing the complexity of the client

  2) There is a cross-domain request and the processing is relatively complicated

  3) Certification is complex, and each service requires independent certification

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

  as follows:

  

  3. The advantages of the gateway

  The microservice gateway is in the middle layer between the server and the client. All external service requests will first pass through the microservice gateway. Clients can only interact with the microservice gateway without calling specific microservice interfaces, which simplifies development.

  

  General understanding of gateway advantages

  Service gateway = routing forwarding + filter

  (1) Routing and forwarding: Receive all external requests and forward them to the back-end microservices.

  (2) Filter: A series of cross-cutting functions can be completed in the service gateway, such as permission verification, current limiting and monitoring, etc., which can be completed by filters (in fact, routing and forwarding are also achieved by filters).

 About Zuul

  Zuul is Netflix's open source microservice gateway, which can be used in conjunction with Eureka, Ribbon, Hystrix and other components.

  The core of Zuul is a series of filters, these filters can complete the following functions:

   1. Identity authentication and security: Identify the verification requirements of each resource and reject those requests that do not conform to the requirements;

   2. Review and monitoring: tracking meaningful data and statistical results at the edge position to bring an accurate production view;

   3. Dynamic routing: dynamically route requests to different back-end clusters;

   4. Stress test: gradually increase the flow to the cluster to understand the performance;

   5. Load distribution: allocate the corresponding capacity for each load type, and discard the request exceeding the limit value;

   6. Static response processing: part of the response is established directly at the edge to avoid forwarding it to the internal cluster;

   7. Multi-region flexibility: routing requests across AWS Regions.

   Spring Cloud has integrated and enhanced Zuul.

Zuul Gateway Project Construction

  

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

    (1) cloud-eureka-server: 8761 Eureka Registration Center

    (2) cloud-gateway-zuul: 5555 Zuul gateway

    (3) Cloud-order: 8000 service consumers

    (4) Cloud-payment-service: 8001 service provider

  Registration center, service consumer, service provider to build, reference: [SpringCloud] SpringCloud quick start (1)

  Build Zuul Gateway

  1. Create a new module (springcloud-gateway-zuul5555) and introduce Zuul and Eureka dependencies

 1 <!-- zuul -->
 2 <dependency>
 3     <groupId>org.springframework.cloud</groupId>
 4     <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
 5 </dependency>
 6 
 7 <!-- eureka client -->
 8 <dependency>
 9     <groupId>org.springframework.cloud</groupId>
10     <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
11 </dependency>

    You can see that Zuul depends on Hystrix and Ribbon

        

    The complete dependencies are as follows:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <parent>
 6         <artifactId>test-springcloud</artifactId>
 7         <groupId>com.test</groupId>
<8         version>1.0-SNAPSHOT</version>
 9     </parent>
10     <modelVersion>4.0.0</modelVersion>
11 
12     <artifactId>springcloud-gateway-zuul5555</artifactId>
13 
14     <dependencies>
15 
16         <!-- zuul -->
17         <dependency>
18             <groupId>org.springframework.cloud</groupId>
19             <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
20         </dependency>
21 
22         <!-- eureka client -->
23         <dependency>
24             <groupId>org.springframework.cloud</groupId>
25             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
26         </dependency>
27 
28         <!-- spring boot -->
29         <dependency>
30             <groupId>org.springframework.boot</groupId>
31             <artifactId>spring-boot-starter-web</artifactId>
32         </dependency>
33         <dependency>
34             <groupId>org.springframework.boot</groupId>
35             <artifactId>spring-boot-starter-actuator</artifactId>
36         </dependency>
37         <dependency>
38             <groupId>org.springframework.boot</groupId>
39             <artifactId>spring-boot-devtools</artifactId>
40             <scope>runtime</scope>
41             <optional>true</optional>
42         </dependency>
43 
44         <dependency>
45             <groupId>org.projectlombok</groupId>
46             <artifactId>lombok</artifactId>
47             <optional>true</optional>
48         </dependency>
49         <dependency>
50             <groupId>org.springframework.boot</groupId>
51             <artifactId>spring-boot-starter-test</artifactId>
52             <scope>test</scope>
53         </dependency>
54 
55     </dependencies>
56 </project>
pom.xml

  2. Edit the configuration file application.yml as follows: 

 1 # 端口
 2 server:
 3   port: 5555
 4 
 5 spring:
 6   application:
 7     name: cloud-gateway-zuul
 8 
 9 eureka:
10   client:
11     register-with-eureka: true
12     fetch-registry: true
13     service-url:
14       defaultZone: http://localhost:8761/eureka

  3. Edit the main startup class and use the annotation @EnableZuulProxy to start the Zuul gateway

1 // 启用Zuul网关
2 @EnableZuulProxy
3 @SpringBootApplication
4 public class Zuul5555 {
5     public static void main(String[] args) {
6         SpringApplication.run(Zuul5555.class, args);
7     }
8 }

  4. Test

    1) Start the project,

    2) Verify that the default route of the Zuul gateway is in effect

      Visit address: http: // localhost: 5555 / cloud-order / consumer / payment / get / 1

      

      Visit address: http: // localhost: 5555 / cloud-payment-service / payment / get / 1

      

      Judging from this, the default route of the Zuul gateway has taken effect   

    3) Add configuration in application.yml and add gateway prefix

1  Zuul:
 2    # gateway prefix, default-free 
3    prefix: / the Test

      Restart the project, visit the address: http: // localhost: 5555 / test / cloud-payment-service / payment / get / 1

      

      Judging from this, the Zuul gateway prefix has taken effect  

    4) Add configuration in application.yml, turn off Zuul to recognize the default route

1  Zuul:
 2    # gateway prefix, default-free 
3    prefix: / the Test
 4    # disable this micro-Service Service the Order 
5    # ignored-Services: the Order-Service 
6    # disable all micro-services 
7    ignored-Services: " * "

      Restart the project, visit the address: http: // localhost: 5555 / test / cloud-payment-service / payment / get / 1

      Address cannot be accessed

    5) Add configuration to application.yml and specify service routing

. 1  Zuul:
 2    # gateway prefix, default no 
. 3    prefix: / Test
 . 4    # disable the microcells Order--Service 
. 5    # ignored-Services: Order--Service 
. 6    # disable all micro-services 
. 7    ignored-Services: " * " 
. 8    routes :
 . 9      API-Payment: # corresponding to the service name, can be customized (preferably consistent) 
10        path: / payment_api / **
 . 11        the serviceId: Cloud-Payment-service- # corresponding to the service name

      Restart the project, visit the address: http: // localhost: 5555 / test / cloud-payment-service / payment / get / 1

      Judging from this, Zuul designated routing configuration takes effect

    6) Add configuration in application.yml and increase monitoring information ports routes and filters

1 management:
2   endpoints:
3     web:
4       exposure:
5         include: health,info,routes,filters

      Restart the project,

      View routing information: http: // localhost: 5555 / actuator / routes

      View filter information: http: // localhost: 5555 / actuator / filters

  

Guess you like

Origin www.cnblogs.com/h--d/p/12729578.html