springcloud 中gateway的搭建

  1. 创建maven工程添加pom依赖
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.7.RELEASE</version>
    </parent>
    <!--  还需要添加springcloud依赖  -->
    <dependencyManagement>
        <dependencies>
            <!--      springcloud      -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${springcloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
    <dependencies>
        <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>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    
    
  2. 修改启动类
    @SpringBootApplication
    @EnableEurekaClient
    public class GatewayApplication {
        public static void main(String[] args) {
            SpringApplication.run(GatewayApplication.class, args);
        }
    }
    
    
  3. 创建Resources 资源目录,创建application.yml文件,并添加如下配置
    server:
      port: 7004
    spring:
      application:
        name: gateway
      cloud:
        gateway:
          globalcors:
            cors-configurations:
              '[/**]': # 匹配所有请求
                allowCredentials: true
                allowedOrigins: "*" # 支持跨域,允许所有的域
                allowedMethods: "*" # 支持的方法,允许所有的方法
                allowedHeaders: "*"
          discovery:
            locator:
              enabled: true
          routes:
            - id: CLIENT01
              uri: lb://CLIENT01
              predicates:
                - Path=/CLIENT01/user/**
              filters:
                - StripPrefix=1
    eureka:
      client:
        service-url:
          defaultZone: http://127.0.0.1:7001/eureka
      instance:
        prefer-ip-address: true
    
    
  4. 启动eureka启动gateway即可
posted @ 2022-08-29 11:39  lambertlt  阅读(174)  评论(0编辑  收藏  举报