spring cloud 组件之 gateway 网关搭建

  • Post author:
  • Post category:其他


POM依赖,注册中心用的是eureka

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

yml配置

server:
  port: 8882 # 服务端口
spring:
  application:
    name: gateway # 服务名称

  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
      #          lower-case-service-id: true
      routes:
        - id: flowable
          #eureka注册中心存在的服务名称
          uri: lb://flowable
          predicates:
            - Path=/flowable/** #路径配置
          filters:
            - StripPrefix=1     #忽略Path配置的个数,此处为1代表访问/api/customer/**时,会将api忽略,真实的访问地址为lb://admin-api/customer/**,如果为2,则为lb://admin-api/**

#         多个服务的话就以此类推
#        - id: flowable2
#            #eureka注册中心存在的服务名称
#            uri: lb://flowable
#            predicates:
#              - Path=/flowable/** #路径配置
#            filters:
#              - StripPrefix=1     #忽略Path配置的个数,此处为1代表访问/api/customer/**时,会将api忽略,真实的访问地址为lb://admin-api/customer/**,如果为2,则为lb://admin-api/**

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8880/eureka # 指定服务注册中心

启动类 增加注解

@EnableEurekaClient

注册到eureka注册中心里

@EnableEurekaClient
@SpringBootApplication
public class GatewayApplication {

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

}



版权声明:本文为qq_45194508原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。