SpringCloud-Config 搭建配置中心时遇到的坑

本文档详细解析了Spring Cloud Config客户端在启动过程中遇到的五种常见问题,包括数据源配置错误、时区配置问题、配置中心访问障碍、配置文件加载失败以及测试依赖冲突,并提供了具体的解决策略。

先看配置文件:

问题1:config client启动Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

解决:1、检查datasource的一些相关配置信息是否正确

           2、一般在配置中心有配置,没问题,客户端不想用或用不到可以启动类添加

              @SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})


问题2:config client启动报错(无限刷):The server time zone value ‘й׼ʱ’ is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.

原因:时区错误(jdbc 6.0以上都有这个问题)

解决:在url的字符串中添加 servertime=UTC

String url="jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=UTC";


useUnicode=true  表示使用Unicode字符,因此可以使用中文
characterEncoding=utf8  设置编码方式
useSSL=true   设置安全连接
serverTimezone=UTC    设置全球标准时间


问题3:配置中心无法访问

原因:保证容器文件访问的安全性,即保证所有的网络资源请求都需要登录,通过springboot配置属性之security,配置security.user.name和security.user.password可以初步达到安全访问的效果,所以要添加security依赖。

解决:

安全设置见:https://www.cnblogs.com/heqiyoujing/p/9441914.html


问题4:config client无法加载配置中心配置文件信息

报错:pplicationContextException: Unable to start web serve;

WebServerException: Unable to start embedded Tomcat;

BeanCreationException: Error creating bean with name 'servletEndpointRegistrar' defined in class path resource;

UnsatisfiedDependencyException: Error creating bean with name 'healthEndpoint' defined in class path resource;

SQLException: url not set;等等异常

原因:Spring cloud client在配置的时候,配置文件要用 bootstrap.properties(yml)

具体原因:https://blog.youkuaiyun.com/u013100581/article/details/92759519

解决:修改client配置文件命名


问题5:@Runwith,@SpringRun,@SpringbootTest爆红,依赖不到

原因:common模块主要是其他各模块都几乎要用到的Jar,我将spring-boot-starter-test的依赖也放到这里了,问题就出现在这里,因为引入时,指定的scope范围是test,在某一模块,引入common包时,并不能成功引入测试依赖。

解决:测试单独依赖。

### 搭建 Spring Cloud Alibaba 项目 #### 环境准备 为了成功搭建 `Spring Cloud Alibaba` 项目,需确保开发环境中已安装以下工具并满足版本要求: - JDK 版本:建议使用 JDK 8 或更高版本。 - Maven 版本:推荐使用 Maven 3.5.x 或以上版本。 以下是基于最新稳定版的环境配置说明: | 组件 | 版本 | |-------------------------|---------------| | Spring Boot | 2.4.2 | | Spring Cloud | 2020.0.0 | | Spring Cloud Alibaba | 2021.1 | 这些版本之间的兼容性已在官方文档中验证[^2]。 --- #### 创建父 POM 文件 创建一个 Maven 的父项目用于统一管理依赖项。通过 `<dependencyManagement>` 节点引入所需的 BOM(Bill of Materials),从而简化子模块的依赖声明。 ```xml <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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <!-- 定义基本信息 --> <groupId>com.example</groupId> <artifactId>spring-cloud-alibaba-parent</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <!-- 引入依赖管理 --> <dependencyManagement> <dependencies> <!-- Spring Boot Dependencies --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.4.2</version> <type>pom</type> <scope>import</scope> </dependency> <!-- Spring Cloud Dependencies --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>2020.0.0</version> <type>pom</type> <scope>import</scope> </dependency> <!-- Spring Cloud Alibaba Dependencies --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2021.1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project> ``` 此文件定义了一个基础框架,供后续子模块继承[^3]。 --- #### 子模块配置示例 假设我们希望实现服务注册与发现功能,则可以按照如下方式构建子模块。 ##### 添加 Nacos 注册中心支持 在子模块的 `pom.xml` 中添加必要的依赖项: ```xml <dependencies> <!-- Spring Boot Starter Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Cloud Alibaba Nacos Discovery --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> </dependencies> ``` ##### 配置 application.yml 编辑 `application.yml` 文件以完成基本设置: ```yaml server: port: 8080 spring: cloud: nacos: discovery: server-addr: localhost:8848 # 替换为实际 Nacos 地址 ``` 如果需要集成配置中心功能,可进一步扩展配置文件内容: ```yaml spring: cloud: nacos: config: server-addr: localhost:8848 file-extension: yaml ``` 上述配置允许应用程序从 Nacos 获取动态配置数据[^1]。 --- #### 测试运行 启动本地 Nacos Server 并部署测试应用。确认服务能够正常注册到 Nacos 控制台,并加载远程配置信息。 --- #### 常见问题排查 1. **Maven 构建失败** 如果遇到依赖冲突或找不到某些组件的情况,请检查是否正确导入了对应的 BOM 文件[^3]。 2. **Nacos 连接异常** 确认 Nacos Server 是否正在运行以及网络连通性良好。必要调整防火墙规则或修改绑定 IP 地址。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值