Gateway Netfilx Zuul:—(Eureka high availability operation)

Before we created and completed the service operations of three Eureka clients, you will find that we still have not been able to access him through the microservice, or must access it through the port number of our own service, then our microservice is If there is nothing that can be completed, we need to operate through the gateway at this time

In fact, the gateway is to provide a unified entrance for the client, and then forward through the routing of the gateway to find the corresponding service. The biggest function of the gateway is routing, and the function of routing is to access different microservices through the url of the customer Enough.

Zuul has a special ability called load balancing —– the default load balancing strategy is polling strategy

The role of the API gateway;

Create a gateway project (when I can’t load it here and create it, I can’t call it gateway like me):

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.pb</groupId>
    <artifactId>kehuduan</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>kehuduan</name>
    <description>kehuduan</description>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR5</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Let’s adjust some details, for example, as shown in the figure below:

You need to open the yml file of the gateway project for configuration:

spring:
application:
name: gateway
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
server:
port: 9000
zuul:
routes:
client: /c/** #Indicates to change all request addresses to start with c, and later support access to all

Finally, enter the address in the browser:http://localhost:9000/c/msgJust test it

Eureka high availability operation:

We see that all services will be registered to the registration center,Once the registration center is down, it is absolutely impossible, >So let’s learn about the high availability operation of the registration center

In fact, the configuration is very simple. For eureka, we only need it to be both a server and a client, so we only need to register the services with each other

We use the convenience provided by Idea to perform the following operations:

Make another copy of 8761 and change it to 8762:

8761 registers with 8762

Then immediately start the 8761 service after the configuration

8762 registers with 8761:

CompleteStart the service of 8762 immediately after configuration

We will see the following in the browser to indicate that the mutual registration is successful, as shown below:

8761’s service

8762’s service

If there is an error

Let’s start the eureak client to operate and register with the eureak service,

You will find that there are post-client services in the service registration, as follows:

But there is still a very serious problem. When we register in the erueak client, we only register with the Eureak service of 8761, so if 8761 hangs up, all the services will also hang up. In order to avoid this situation, we can register with multiple Eureak servers at the same time on the Eureka client, as in the modification of the yml file below:

spring:
application:
name: client

eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/, http://localhost:8762/eureka/
server:
port: 8003