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">
        <relativePath /> <!-- lookup parent from repository -->
    <description>Demo project for Spring Boot</description>






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
public class ZuulApplication {

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

1.3 configuration file of yaml

  port: 9000
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
    prefer-ip-address: true
    name: '@project.name@'
  prefix: /badger  #Prefix, null string by default
  ignored-services: '*' #Hide (ignore) all other paths not exposed by the gateway
    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 -- >, 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


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