集成gateway

方玄天
2023-12-01
pom
<!-- nacos 服务注册中心-->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

<!-- nacos 服务配置中心 -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>

<!-- gateway的启动器 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

<!-- 负载均衡器 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>

yml

server:
  port: 8888
spring:
  application:
    name: cs-gateway
  cloud:
    nacos:
      discovery:
        server-addr: 111.111.111.111:9001
        username: nacos
        password: nacos
        namespace: c4ad03f3-bdc0-4c63-9f95-198040bf01e8
      config:
        server-addr: 111.111.111.111:9001
    gateway:
      routes:
        - id: cs-custom          # 客户微服务
          uri: lb://cs-custom    # 微服务的名称 实现负载均衡
          predicates:
            - Path=/**

        - id: cs-product          # 内容微服务
          uri: lb://cs-product    # 微服务的名称 实现负载均衡
          predicates:
            - Path=/product/**
      # 全局的跨域配置
      globalcors:
        # 解决options请求被拦截问题
        add-to-simple-url-handler-mapping: true
        corsConfigurations:
          '[/**]':
            # 允许哪些域名和端口的跨域请求
            allowedOrigins: "*"
            # 允许的跨域ajax的请求方式
            allowedMethods:
              - "GET"
              - "POST"
              - "DELETE"
              - "PUT"
              - "OPTIONS"
            # 允许在请求中携带的头信息
            allowedHeaders: "*"

 类似资料: