From f862d7503b81970bd7fd9cb1bb34ce94f9c72e62 Mon Sep 17 00:00:00 2001 From: jieyuu <645634619@qq.com> Date: Sat, 9 Nov 2024 21:09:19 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BD=91=E5=85=B3=E5=BC=80=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- xdclass-gateway/pom.xml | 17 ++++++ .../java/net/jieyuu/GatewayApplication.java | 14 +++++ .../src/main/resources/application.yml | 57 +++++++++++++++++++ .../src/main/resources/bootstrap.yml | 6 ++ 4 files changed, 94 insertions(+) create mode 100644 xdclass-gateway/src/main/java/net/jieyuu/GatewayApplication.java create mode 100644 xdclass-gateway/src/main/resources/application.yml create mode 100644 xdclass-gateway/src/main/resources/bootstrap.yml diff --git a/xdclass-gateway/pom.xml b/xdclass-gateway/pom.xml index ff388a5..e38e9df 100644 --- a/xdclass-gateway/pom.xml +++ b/xdclass-gateway/pom.xml @@ -11,6 +11,23 @@ xdclass-gateway + + + org.springframework.cloud + spring-cloud-starter-gateway + + + + + + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + + + 8 8 diff --git a/xdclass-gateway/src/main/java/net/jieyuu/GatewayApplication.java b/xdclass-gateway/src/main/java/net/jieyuu/GatewayApplication.java new file mode 100644 index 0000000..d8f1032 --- /dev/null +++ b/xdclass-gateway/src/main/java/net/jieyuu/GatewayApplication.java @@ -0,0 +1,14 @@ +package net.jieyuu; + + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; + +@EnableDiscoveryClient +@SpringBootApplication +public class GatewayApplication { + public static void main(String[] args) { + SpringApplication.run(GatewayApplication.class, args); + } +} diff --git a/xdclass-gateway/src/main/resources/application.yml b/xdclass-gateway/src/main/resources/application.yml new file mode 100644 index 0000000..be9de93 --- /dev/null +++ b/xdclass-gateway/src/main/resources/application.yml @@ -0,0 +1,57 @@ +server: + port: 8889 + +spring: + application: + name: api-gateway + cloud: + #注册中心地址 + nacos: + discoveru: + server-addr: 8.134.32.140:8848 + + gateway: + routes: #数组形式 + - id: product-service #商品服务 路由唯一标识 + uri: lb://xdclass-product-service #从nocas进行转发 + order: 1 #优先级,数字越小优先级越高 + predicates: #断言 配置哪个路径才转发,前端访问路径统一加上XXX-server,网关判断转发对应的服务,如果是回调业务记得修改 + - Path=/product-server/** + filters: #过滤器,请求在传递过程中通过过滤器修改 + - StripPrefix=1 #去掉第一层前缀,转发给后续的路径 + + - id: user-service #用户服务 路由唯一标识 + uri: lb://xdclass-user-service #从nocas进行转发 + order: 2 #优先级,数字越小优先级越高 + predicates: #断言 配置哪个路径才转发,前端访问路径统一加上XXX-server,网关判断转发对应的服务,如果是回调业务记得修改 + - Path=/user-service/** + filters: #过滤器,请求在传递过程中通过过滤器修改 + - StripPrefix=1 #去掉第一层前缀,转发给后续的路径 + + - id: coupon-service #商品服务 路由唯一标识 + uri: lb://xdclass-coupon-service #从nocas进行转发 + order: 3 #优先级,数字越小优先级越高 + predicates: #断言 配置哪个路径才转发,前端访问路径统一加上XXX-server,网关判断转发对应的服务,如果是回调业务记得修改 + - Path=/coupon-server/** + filters: #过滤器,请求在传递过程中通过过滤器修改 + - StripPrefix=1 #去掉第一层前缀,转发给后续的路径 + + - id: order-service #商品服务 路由唯一标识 + uri: lb://xdclass-order-service #从nocas进行转发 + order: 3 #优先级,数字越小优先级越高 + predicates: #断言 配置哪个路径才转发,前端访问路径统一加上XXX-server,网关判断转发对应的服务,如果是回调业务记得修改 + - Path=/order-server/** + filters: #过滤器,请求在传递过程中通过过滤器修改 + - StripPrefix=1 #去掉第一层前缀,转发给后续的路径 + + #开启网关拉取nacos的服务 + discovery: + locator: + enabled: true + +#设置日志级别,ERROR/WARN/INFO/DEBUG,默认是INFO以上才显示 +logging: + level: + root: INFO + #nacos日志问题 + com.alibaba.nacos.client.config.impl: WARN diff --git a/xdclass-gateway/src/main/resources/bootstrap.yml b/xdclass-gateway/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..5dd5b3e --- /dev/null +++ b/xdclass-gateway/src/main/resources/bootstrap.yml @@ -0,0 +1,6 @@ +spring: + cloud: + #注册中心地址 + nacos: + config: + server-addr: 8.134.32.140:8848