Use zuul to build a microservice gateway

Use zuul to build a microservice gateway

Previous: Provider cluster and load balancing ribbon
Next: Zuul realizes file upload For
example, if you have a lot of access addresses for an application, that's for sure. An access address is equivalent to an entrance.

http://localhost:8010/user/1 入口
http://localhost:8001/search/3/zhangsan/zz 入口

To use zuul is to let visitors visit zuul first, and then the zuul gateway will find the corresponding entrance —> request a
unified entrance for different services ----" http://localhost:8040/1 designated entrance-------" request Different services

Introduction of zuul

The microservice architecture has begun to take shape, but there are still some problems—different microservices generally have different network addresses, and external clients (such as mobile apps) may need to call multiple service interfaces to complete a business requirement. For example, a mobile phone APP for movie ticket purchase may call multiple micro-service interfaces to complete the business process of one-time ticket purchase
Insert picture description here

If the client is allowed to directly communicate with each microservice, there will be the following problems:

  1. The client will request different microservices multiple times, which increases the complexity of the client.
  2. There are cross-domain requests, and processing is relatively complicated in certain scenarios.
  3. Certification is complex, and each service requires independent certification.
  4. It is difficult to refactor. With the iteration of the project, it may be necessary to re-divide microservices. For example, it is possible to merge multiple services into one or split one service into multiple. If the client communicates directly with the microservice, refactoring will be difficult to implement.
  5. Some microservices may use protocols that are not friendly to the firewall browser, and direct access may be difficult.

The above problems can be solved with the help of a microservice gateway. The microservice gateway is an intermediate layer between the client and the server. All external requests will pass through the microservice gateway first. After using the microservice gateway, the architecture can evolve into the figure.
Insert picture description here
The microservice gateway encapsulates the internal structure of the application, and the client only needs to interact with the gateway without directly calling the interface of a specific microservice. In this way, development can be simplified. Not only that, there are the following advantages of using a microservice gateway:

• Easy to monitor. The monitoring data can be collected at the microservice gateway and pushed to the external system for analysis.

• Easy to authenticate. Authentication can be performed on the microservice gateway, and then the request is forwarded to the backend microservice, without the need for authentication in each microservice.

• Reduce the number of interactions between the client and each microservice.

Zuul brief introduction

Zuul is Nettlix's open source microservice gateway. It can be used with components such as Eureka, Ribbon, and Hystrix.
The core of Zuul is a series of filters, these filters can complete the following functions.

. Identity authentication and security: Identify the verification requirements of each resource, and reject those requests that do not meet the requirements.
. Review and monitoring: Track meaningful data and statistical results at the edge to bring accurate production views.
. Dynamic routing: dynamically route requests to different back-end clusters.
. Stress test: Gradually increase the stray mice pointed at the cluster to understand the performance.
. Load distribution: allocate corresponding capacity for each load type, and discard requests that exceed the limit value.
. Static response processing: Part of the response is directly established at the edge to avoid forwarding it to the internal cluster.
. Multi-regional flexibility: Request routing across AWSRegion is designed to achieve ELB (Elastic Load Balancing)
. The diversification of use, and the edge of the system closer to the users of the system.

Spring Cloud has integrated and enhanced Zuul. Currently, the default HTTP client used by Zuul is Apache HTTP Client, and RestClient or okhttp3.0kHttpClient can also be used. If you want to use RestClient, you can set ribbon.restclient.enabled=true; if you want to use okhttp3.0kHttpClient, you can set
ribbon.okhttp.enabled=true.

Write a microservice gateway zuul

Function: Write a microservice gateway. Register zuul to eureka-server

Create a springboot project, add dependency support

1 Jar package introduced in pom.xml

	<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-client -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!--zuul核心依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
    <!-- 引入spring cloud的依赖 -->
    <dependencyManagement>
        <dependencies>

            <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.SR3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

        </dependencies>

2 The content of application.yml is as follows:

server:
  port: 8040
spring:
  application:
    name: zuul
eureka:
  client:
    service-url:
      defaultZone: http://root:root@peer1:8761/eureka/,http://root:root@peer2:8762/eureka/
  instance:
    prefer-ip-address: true

ribbon:
  ReadTimeout: 12000
  ConnectTimeout: 12000
  eureka:
    enabled: true
zuul:
  host:
    socket-timeout-millis: 12000
    connect-timeout-millis: 12000
info:
  head: head
  body: body
  app:
      name: @project.artifactId@
      encoding: @project.build.sourceEncoding@
      java:
        source: @java.version@
        target: @java.version@
management:
  endpoints:
    web:
      exposure:
        #加载所有的端点,默认只加载了info、health
        include: '*'
  endpoint:
    health:
      show-details: always
    #可以关闭指定的端点
    shutdown:
      enabled: false    

3 Add annotations to the startup class

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
@EnableDiscoveryClient
public class ZuulApplication {
    
    

    public static void main(String[] args) {
    
    
        SpringApplication.run(ZuulApplication.class, args);
    }

}

In this way, a simple microservice gateway has been written. From the configuration, it can be seen that at this time, only the dependency of Zuul is added and Zuul is registered on the Eureka Server.
4 Start sequence

1 Start eureka service registration component
2 Start provider project
3 Start consumer project
4 Start zuul project

5 Test
Visit the service discovery component http://peer1:8761/,
you can see that Zuul has been registered in the service discovery component
Insert picture description here

Configure routing to access consumers

1 Add in zuul configuration file

zuul:
  host:
    socket-timeout-millis: 60000
    connect-timeout-millis: 60000
  routes:
    consumer: /consumer/**
    provider: /provider/**

Insert picture description here
2 Test the load balance of multiple providers and multiple consumers.
All of them can be accessed, indicating that the configuration is successful.
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_39095899/article/details/107466136