依赖文件在项目搭建时已经添加,这里进行完善

配置文件

新建bootstrap.yaml配置文件,内容如下:

spring:
application:
name: trip-gateway
cloud:
nacos:
server-addr: xxx.xxx.xxx.xxx:8848
config:
file-extension: yaml
namespace: trip_cloud_dev
profiles:
active: dev

Nacos配置文件

  • ID:trip-gateway-dev.yaml

  • Group:DEFAULT_GROUP

  • 描述:旅游项目网关配置

  • 配置内容:

    feign-dev.yaml
    server:
    port: 9000
    spring:
    cloud:
    nacos:
    server-addr: 124.221.23.47:8848
    discovery:
    namespace: ${spring.cloud.nacos.config.namespace}
    gateway:
    discovery:
    locator:
    enabled: true
    globalcors:
    cors-configurations:
    '[/**]':
    allowedOrigins: "*"
    allowedMethods:
    - GET
    - POST
    - PUT
    - PATCH
    - DELETE
    - OPTIONS
    allowedHeaders: "*"
    allowCredentials: true
    routes:
    - id: trip_user
    uri: lb://user-service
    predicates:
    - Path=/users/**
    - Path=/sms/**

启动类

创建包com.swx.gateway,创建启动类 TripGatewayApplication

TripGatewayApplication
@SpringBootApplication
@EnableDiscoveryClient
public class TripGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(TripGatewayApplication.class, args);
}
}

启动时,需要先启动Nacos