Instance analysis of routing allocation code of spring cloud gateway

Time:2020-3-29

This article mainly introduces the example analysis of the routing distribution code of the spring cloud gateway. The example code is introduced in detail in this article, which has a certain reference value for your study or work. You can refer to the following for your friends

1. Create a new module based on the parent project

2. Add dependency to POM file


<dependencies>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-gateway</artifactId>
    </dependency>

    <dependency>
      <groupId>com.alibaba.cloud</groupId>
      <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
  </dependencies>

3. Add configuration


server:
 port: 9091

spring:
 application:
  name: gateway3
 cloud:
  nacos:
   discovery:
    server-addr: localhost:8848
    namespace: c22e5019-0bee-43b1-b80b-fc0b9d847501
    register-enabled: false

  gateway:
   routes:
    - id: demo_route
     uri: lb://demo
     predicates:
      - Path=/demo/**

    - id: demo2_test
     uri: lb://demo2
     predicates:
      - Path=/user/**

4. Write startup class


@SpringBootApplication
@EnableDiscoveryClient
public class Gateway3Application {

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

}

5. Visit http: / / localhost: 9091 / demo or http: / / localhost: 9091 / demo2 to route to the specified service

The above is the whole content of this article. I hope it will help you in your study, and I hope you can support developepaer more.