spring boot 2.x spring cloud Greenwich.SR1 gateway component zuul

spring boot 2.x spring cloud Greenwich.SR1 gateway component zuul

Article directory


This example is based on the budget spring cloud API project built last time;

springboot 2.x – spring cloud Greenwich.SR1 service registration and discovery – eureka building and cluster building

Deployment of eureka cluster in docker and docker compose environments

spring boot 2.x spring cloud Greenwich.SR1 load balancing ribbon building and use

Specific code information can be viewed< Code cloud

1. Project construction

1.1. Build a new project, badger spring cloud zuul. The pom file is as follows

It is mainly to add the package spring cloud starter Netflix eureka client of the registration center eureka, and the package spring cloud starter Netflix zuul of the gateway zuul

<?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.1.3.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.badger</groupId>
    <artifactId>badger-spring-cloud-zuul</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>badger-spring-cloud-zuul</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</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>

1.2. Add gateway annotation @ EnableZuulProxy and eureka annotation @ EnableEurekaClient to the main class

/**
 * @EnableEurekaClient Client marked eureka
 * @EnableZuulProxy Open zuul gateway agent
 * @author liqi
 *
 */
@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class ZuulApplication {

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

1.3 configuration file of yaml

server:
  port: 9000
eureka:
  client:
    serviceUrl:
      defaultZone: http://172.16.2.50:8761/eureka/
  instance:
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
    prefer-ip-address: true
spring:
  application:
    name: '@project.name@'
zuul:
  prefix: /badger  #Prefix, null string by default
  ignored-services: '*' #Hide (ignore) all other paths not exposed by the gateway
  routes:
    api: #Alias of application
      path: /api/** #Mapped name of the path of the service
      service-id: badger-spring-cloud-api #Visited apps

Here, the project is completed

2, Project start, test and use

The starting steps are as follows:

  1. Start the service of eureka, port 8761;
  2. Modify the configuration file of yaml. The start port is 7001. Start the provider of the service, badger spring cloud API;
  3. The customer who started the service, badger spring cloud zuul, has a port of 9000;

Visit http://localhost:9000/badger/api/demo to see the results

My address is -- > 172.16.2.50:7001, indicating that the test is successful

Explain the route of the access address http: / / localhost: 9000 / budget / API / demo, and observe with the above yaml configuration file

Badger: in zuul.prefix=/badger, it indicates the prefix of access. The default is empty string

api: zuul.routes.api.path=/api / * * indicates the access path of the badger spring cloud api of the accessed application (registered in eureka);

demo: indicates the access route of the controller applying the badger spring cloud API

Finally, the meaning of zuul. Ignored services = '*' attribute

Since we can use zuul.routes.api.path=/api / * * api to replace the application's badger spring cloud api, we can directly use the badger spring cloud api to access? The answer is yes. The address is as follows

http://localhost:9000/badger/badger-spring-cloud-api/demo

As you can see, the access is not successful; the access route cannot be found;

The main reason is that the zuul. Ignored services = '*' attribute hides (ignores) all other paths that are not exposed by the gateway; we annotate this attribute, restart zuul's project, and access it again, and it will succeed;

Specific code information can be viewed< Code cloud

Published 82 original articles, won praise 119, visited 270000+
Private letter follow

Tags: Spring Maven Apache Attribute

Posted on Wed, 15 Jan 2020 06:18:12 -0500 by DeathStar