一 目录结构
重点关注红框内的文件
pom文件内容
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.xunqiu</groupId>
<artifactId>eggs-boot-dependencies</artifactId>
<version>23.10.26-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xunqiu</groupId>
<artifactId>eggs-gateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eggs-gateway</name>
<description>eggs-gateway</description>
<properties>
<java.version>11</java.version>
<knife4j.version>3.0.3</knife4j.version>
</properties>
<dependencies>
<!--nacos service discovery client依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--nacos config client 依赖-->
<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>com.alibaba.csp</groupId>
<artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
</dependency>
<!--引入webflux-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<!-- Java Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<!--解决转发问题不然会报503错误-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!--编译插件-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
<!--编译跳过测试文件检查的生命周期-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<!--打包插件-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version> <!-- 版本号可以根据您的需要进行更改 -->
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.xunqiu.eggsgateway.EggsGatewayApplication</mainClass> <!-- 这里是你的主类 -->
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
EggsGatewayApplication.class文件内容
package com.xunqiu.eggsgateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @author xiaominghu
*/
@SpringBootApplication
@EnableDiscoveryClient
public class EggsGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(EggsGatewayApplication.class, args);
System.out.println("EggsGatewayApplication =====> started success");
}
}
bootstrap.yml 文件内容
spring:
application:
name: xunqiu-eggs-gateway
config:
use-legacy-processing: true
profiles:
active: test
main:
allow-bean-definition-overriding: true
# Spring Actuator监控
management:
metrics:
tags:
application: ${spring.application.name}
#---多个文档的开头,...多个文旦的结尾可以省略
---
spring:
config:
activate:
on-profile: test
nacos.server-address: 123.56.84.157
nacos.namespace: d43a77ac-e43a-473c-a005-4eea2da08504
nacos.port: 8848
nacos.discovery.ip:
nacos.discovery.port: 8081
---
spring:
config:
activate:
on-profile: prod
nacos.server-address: 123.56.84.157
nacos.namespace: d43a77ac-e43a-473c-a005-4eea2da08504
nacos.port: 8848
nacos.discovery.ip:
nacos.discovery.port: 8081
---
spring:
cloud:
nacos:
discovery:
server-addr: ${spring.nacos.server-address}:${spring.nacos.port}
namespace: ${spring.nacos.namespace}
ip: ${spring.nacos.discovery.ip}
port: ${spring.nacos.discovery.port}
config:
file-extension: yaml
server-addr: ${spring.nacos.server-address}:${spring.nacos.port}
namespace: ${spring.nacos.namespace}
shared-configs[0]:
data-id: application.yml
refresh: true
shared-configs[1]:
data-id: j2cache.yaml
refresh: true
application.yml文件内容
server:
port: ${spring.nacos.discovery.port}
dubbo.dubbo.port: 8009
spring:
cloud:
gateway:
# 路由数组[路由 就是指定当请求满足什么条件的时候转到哪个微服务]
routes:
# 我们⾃定义的路由 ID,保持唯⼀
- id: xunqiu-eggs-service
# ⽬标服务地址(部署多实例)
uri: lb://xunqiu-eggs-service
# gateway⽹关从服务注册中⼼获取实例信息然后负载后路由
# 断⾔:路由条件,Predicate 接受⼀个输⼊参数,返回⼀个布尔值结果。该接⼝包含多种默认⽅法来将 Predicate 组合成其他复杂的逻辑(⽐如:与,或,⾮)。
predicates:
- Path=/eggs-service/**
filters: # 过滤器,请求在传递过程中可以通过过滤器对其进行一定的修改
- StripPrefix=1 # 转发之前去掉1层路径s
测试地址
http://127.0.0.1:8081/eggs-service/open/v1/dubboService/hello
eggs-service是路由的前缀,open/v1/dubboService/hello是微服务xunqiu-eggs-service的服务地址