6. First acquaintance of Spring Cloud - --- Zool routing

Keywords: Java Spring Maven Apache

Preface:

In the production environment, it is impossible for us to leak out the true information of each service, because it is too unsafe.

We will choose to use the routing agent to forward the real service information.

Create a new Zool:

1. Adding dependencies

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.xm.cloud</groupId>
    <artifactId>cl_zool</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>cl_zool</name>
    <description>This is a Web about springcloud</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.SR2</spring-cloud.version>
    </properties>

    <dependencies>
        <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>

        <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>
            </plugin>
        </plugins>
    </build>


</project>

2. Modify configuration

server.port=9090
spring.application.name=cl-zool-gateway

eureka.client.service-url.defaultZone=http://127.0.0.1:7001/eureka/
#Uniform prefix of service name
zuul.prefix=/xm
#True name hiding for all services
zuul.ignored-services="*"
zuul.routes.myHello.service-id=CL-HELLO-PRODUCER
zuul.routes.myHello.path=/myHello/**

3. Open Notes

package com.xm.cloud;

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

@EnableDiscoveryClient
@EnableZuulProxy
@SpringBootApplication
public class ClZoolApplication {

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

Test:

Open Eureka 7001, service producer 8001, route zool 9090

1. Access the Eureka client:

2. Visit the service producer localhost:8001/hello

3. Access the unprefixed real service name http://localhost:9090/CL-HELLO-PRODUCER/hello:

4. Visit the prefixed real service name http://localhost:9090/xm/CL-HELLO-PRODUCER/hello:

5. Access the unprefixed routing service name http://localhost:9090/myHello/hello:

6. Access the prefixed routing service name http://localhost:9090/xm/myHello/hello:

Posted by jengle52 on Thu, 24 Jan 2019 14:33:15 -0800