当前位置: 首页 > news >正文

SpringBoot增加网关服务

一、新建gateway项目

二、添加依赖

dependencies {implementation 'org.springframework.cloud:spring-cloud-starter-gateway:4.0.0'
}

三、增加路由规则配置

一个web服务、一个service服务

bootstrap.yaml:

server:port: 80
spring:application:name: gatewaycloud:nacos:discovery:server-addr: ${NACOS_ADDR:127.0.0.1:8848}namespace: ${NACOS_NAMESPACE:demo}username: nacospassword: nacosconfig:#nacos配置中心服务器地址server-addr: ${NACOS_ADDR:127.0.0.1:8848}#nacos配置中心命名空间namespace: ${NACOS_NAMESPACE:demo}#文件后缀格式file-extension: yaml#nacos配置中心分组group: ${spring.application.name}username: nacospassword: nacos#扩展文件,目前用于全局共享,下面的优先级高ext-config:- data-id: commom.yamlgroup: commonrefresh: true

application.yaml:

spring:main:web-application-type: reactivecloud:gateway:routes:#业务服务- id: web_routeuri: lb://webpredicates:- Path=/web/**filters:- RewritePath=/(?<segment>.*),/$\{segment}#支撑服务- id: service_routeuri: lb://servicepredicates:- Path=/service/**filters:- RewritePath=/(?<segment>.*),/$\{segment}

四、增加过滤器(可选)

比如认证、鉴权、日志打印等

例如:打印日志,用@Slf4j 注解来简化日志记录、

增加依赖

		compileOnly 'org.projectlombok:lombok:1.18.24' // 使用最新版本annotationProcessor 'org.projectlombok:lombok:1.18.24'implementation 'org.slf4j:slf4j-api:2.0.6'runtimeOnly 'ch.qos.logback:logback-classic:1.4.5'

代码:

@Slf4j
@Component
public class RequestLogFilter implements GlobalFilter, Ordered {private static final String START_TIME_KEY = "startTime";@Overridepublic Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {ServerHttpRequest request = exchange.getRequest();String method = Objects.requireNonNull(request.getMethod()).name();String requestURI = request.getPath().toString();String remoteAddr = Objects.requireNonNull(request.getRemoteAddress()).getHostString();System.out.println("method=" + method +";requestURI= "+ requestURI+ "; remoteAddr="+remoteAddr);log.info("method=" + method +";requestURI= "+ requestURI+ "; remoteAddr="+remoteAddr);return chain.filter(exchange);}@Overridepublic int getOrder() {return 1;}
}

五、增加启动类并运行测试

@SpringBootApplication(scanBasePackages = {"com.example"})
@EnableDiscoveryClient
public class GatewayApplication {public static void main(String[] args) {SpringApplication.run(GatewayApplication.class, args);}}

运行访问,用80端口

六、遇到的问题

1、启动报错

缺少依赖的包

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2024-07-16T16:27:44.792+08:00 ERROR 9924 --- [gateway] [           main] o.s.b.d.LoggingFailureAnalysisReporter   : ***************************
APPLICATION FAILED TO START
***************************Description:Web application could not be started as there was no org.springframework.boot.web.reactive.server.ReactiveWebServerFactory bean defined in the context.Action:Check your application's dependencies for a supported reactive web server.
Check the configured web application type.

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • Linux发行版CentOS 8 利用Docker安装应用
  • 初谈Linux信号-=-信号的产生
  • Kafka Producer发送消息流程之消息异步发送和同步发送
  • 【整体介绍】HTML和JS编写多用户VR应用程序的框架
  • node.js的安装及学习(node/nvm/npm的区别)
  • 【闲聊】-Perl的基础语法
  • Python3 学习——基础语法2
  • pyinstall 打包基于PyQt5和PaddleOCR的项目为.exe
  • phpinfo
  • ubuntu24.04 linux bcm94352hmb 无线网卡(带蓝牙功能)无法连接蓝牙设备的解决办法
  • LINUX多进程相关
  • Hadoop3:HDFS存储优化之小文件归档
  • XML 编辑器:功能、选择与使用技巧
  • 北斗高精度服务:无人驾驶领域的创新引擎
  • 基于opencv的图片加水印实现方案
  • @angular/forms 源码解析之双向绑定
  • bootstrap创建登录注册页面
  • conda常用的命令
  • CSS实用技巧干货
  • Cumulo 的 ClojureScript 模块已经成型
  • ES2017异步函数现已正式可用
  • JS专题之继承
  • MySQL的数据类型
  • nginx 配置多 域名 + 多 https
  • PHP 7 修改了什么呢 -- 2
  • SpringBoot几种定时任务的实现方式
  • Spring思维导图,让Spring不再难懂(mvc篇)
  • 翻译 | 老司机带你秒懂内存管理 - 第一部(共三部)
  • 分布式事物理论与实践
  • 关于Java中分层中遇到的一些问题
  • 好的网址,关于.net 4.0 ,vs 2010
  • 免费小说阅读小程序
  • 浅谈web中前端模板引擎的使用
  • 详解移动APP与web APP的区别
  • 项目管理碎碎念系列之一:干系人管理
  • 移动互联网+智能运营体系搭建=你家有金矿啊!
  • 运行时添加log4j2的appender
  • 格斗健身潮牌24KiCK获近千万Pre-A轮融资,用户留存高达9个月 ...
  • 智能情侣枕Pillow Talk,倾听彼此的心跳
  • ​二进制运算符:(与运算)、|(或运算)、~(取反运算)、^(异或运算)、位移运算符​
  • # Apache SeaTunnel 究竟是什么?
  • # SpringBoot 如何让指定的Bean先加载
  • (1)SpringCloud 整合Python
  • (1)svelte 教程:hello world
  • (1/2) 为了理解 UWP 的启动流程,我从零开始创建了一个 UWP 程序
  • (LNMP) How To Install Linux, nginx, MySQL, PHP
  • (NO.00004)iOS实现打砖块游戏(十二):伸缩自如,我是如意金箍棒(上)!
  • (zt)基于Facebook和Flash平台的应用架构解析
  • (附表设计)不是我吹!超级全面的权限系统设计方案面世了
  • (附源码)apringboot计算机专业大学生就业指南 毕业设计061355
  • (力扣记录)1448. 统计二叉树中好节点的数目
  • (四)activit5.23.0修复跟踪高亮显示BUG
  • (一) 初入MySQL 【认识和部署】
  • (转)Android中使用ormlite实现持久化(一)--HelloOrmLite
  • (转载)从 Java 代码到 Java 堆