在线教育项目-day18【GATEWAY网关后端整合】

1.创建新模块

 更改infrastructure的pom.xml

记得加上pom类型

 2.在子模块中引入依赖

<dependencies>
        <dependency>
            <groupId>com.dm</groupId>
            <artifactId>common_utils</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

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

        <!--gson-->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
        </dependency>

        <!--服务调用-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    </dependencies>

3.创建启动类

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

4.创建配置文件

# 服务端口
server.port=8222
# 服务名
spring.application.name=service-gateway
# nacos服务地址
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

#使用服务发现路由
spring.cloud.gateway.discovery.locator.enabled=true

#设置路由id
spring.cloud.gateway.routes[0].id=service-acl
#设置路由的uri   lb://nacos注册服务名称
spring.cloud.gateway.routes[0].uri=lb://service-acl
#设置路由断言,代理servicerId为auth-service的/auth/路径
spring.cloud.gateway.routes[0].predicates= Path=/*/acl/**

#配置service-edu服务
spring.cloud.gateway.routes[1].id=service-edu
spring.cloud.gateway.routes[1].uri=lb://service-edu
spring.cloud.gateway.routes[1].predicates= Path=/eduservice/**

说明:

id:随意起

url:配置文件中的模块名

predicates:路径匹配

新建一个:

5.把Edu模块的controller的@CrossOrigin注解全注释掉

6.测试

先用正常的端口号去测试

再用我们gateway的端口去测试

 可以看到查询了两次

我们把配置全部书写

#配置service-edu服务
spring.cloud.gateway.routes[1].id=service-edu
spring.cloud.gateway.routes[1].uri=lb://service-edu
spring.cloud.gateway.routes[1].predicates= Path=/eduservice/**

#配置service-ucenter服务
spring.cloud.gateway.routes[2].id=service-ucenter
spring.cloud.gateway.routes[2].uri=lb://service-ucenter
spring.cloud.gateway.routes[2].predicates= Path=/educenter/**

#配置service-order
spring.cloud.gateway.routes[3].id=service-order
spring.cloud.gateway.routes[3].uri=lb://service-order
spring.cloud.gateway.routes[3].predicates= Path=/eduorder/**

#配置service-statistics
spring.cloud.gateway.routes[4].id=service-statistics
spring.cloud.gateway.routes[4].uri=lb://service-statistics
spring.cloud.gateway.routes[4].predicates= Path=/edustatistics/**

#配置service-oss
spring.cloud.gateway.routes[5].id=service-oss
spring.cloud.gateway.routes[5].uri=lb://service-oss
spring.cloud.gateway.routes[5].predicates= Path=/eduoss/**

#配置service-vod
spring.cloud.gateway.routes[6].id=service-vod
spring.cloud.gateway.routes[6].uri=lb://service-vod
spring.cloud.gateway.routes[6].predicates= Path=/eduvod/**
原文地址:https://www.cnblogs.com/dmzna/p/12901551.html