Spring Cloud Config for JDBC Backend

本文档介绍了在SpringBoot2.4.4版本下,SpringCloudConfig客户端无法连接到服务端的问题。问题源于spring-cloud-bootstrap依赖的缺失或spring.cloud.bootstrap.enabled参数未设置。解决方案包括添加spring-cloud-starter-bootstrap依赖或启动时添加-Dspring.cloud.bootstrap.enabled=true参数。此外,提供了配置服务器的MySQL数据库初始化脚本,以及客户端和服务器的配置文件示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring Cloud Config 3.0.3  jdbc mysql & springboot.version 2.4.4

一开始client一直没有连上server,也想到可能是版本问题,通过debug发现,org.springframework.cloud.bootstrap.BootstrapApplicationListener类里面的onApplicationEvent方法,第一个if中的bootstrapEnabled返回false.

后来通过网上搜索,也发现是版本问题(springboot2.4之后的版本,需要增加spring-cloud-starter-bootstrap依赖或启动时加参数-Dspring.cloud.bootstrap.enabled=true),

其实官网也有介绍.https://docs.spring.io/spring-cloud-config/docs/current/reference/html/#config-first-bootstrap

  • 完整的代码

https://gitee.com/hok021/config-server-mysql.git

https://gitee.com/hok021/config-client.git

public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
		ConfigurableEnvironment environment = event.getEnvironment();
		if (!bootstrapEnabled(environment) && !useLegacyProcessing(environment)) {
			return;
		}
		// don't listen to events in a bootstrap context
		if (environment.getPropertySources().contains(BOOTSTRAP_PROPERTY_SOURCE_NAME)) {
			return;
		}
		ConfigurableApplicationContext context = null;
		String configName = environment.resolvePlaceholders("${spring.cloud.bootstrap.name:bootstrap}");
		for (ApplicationContextInitializer<?> initializer : event.getSpringApplication().getInitializers()) {
			if (initializer instanceof ParentContextApplicationContextInitializer) {
				context = findBootstrapContext((ParentContextApplicationContextInitializer) initializer, configName);
			}
		}
		if (context == null) {
			context = bootstrapServiceContext(environment, event.getSpringApplication(), configName);
			event.getSpringApplication().addListeners(new CloseContextOnFailureApplicationListener(context));
		}

		apply(context, event.getSpringApplication(), environment);
	}
  • bootstrapEnabled方法 
public static boolean bootstrapEnabled(Environment environment) {
		return environment.getProperty(BOOTSTRAP_ENABLED_PROPERTY, Boolean.class, false) || MARKER_CLASS_EXISTS;
	}
  • MARKER_CLASS_EXISTS
/**
	 * Property name for bootstrap marker class name.
	 */
	public static final String MARKER_CLASS = "org.springframework.cloud.bootstrap.marker.Marker";

	/**
	 * Boolean if bootstrap marker class exists.
	 */
	public static final boolean MARKER_CLASS_EXISTS = ClassUtils.isPresent(MARKER_CLASS, null);
  • 其中org.springframework.cloud.bootstrap.marker.Marker类是需要引用另外一个jar:spring-cloud-starter-bootstrap或者启动的时候加上 -Dspring.cloud.bootstrap.enabled=true 参数
  • <dependency>
    			<groupId>org.springframework.cloud</groupId>
    			<artifactId>spring-cloud-starter-bootstrap</artifactId>
    			<version>3.0.2</version>
    		</dependency>

 

  • Spring Cloud Config-server

  • init sql

CREATE TABLE `config_properties` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `key1` varchar(50) COLLATE utf8_bin NOT NULL,
  `value1` varchar(500) COLLATE utf8_bin DEFAULT NULL,
  `application` varchar(50) COLLATE utf8_bin NOT NULL,
  `profile` varchar(50) COLLATE utf8_bin NOT NULL,
  `label` varchar(50) COLLATE utf8_bin DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

insert into `config_properties` (`id`, `key1`, `value1`, `application`, `profile`, `label`) values('1','server.port','8083','config-client','dev','master');
insert into `config_properties` (`id`, `key1`, `value1`, `application`, `profile`, `label`) values('2','foo','bar-jdbc','config-client','dev','master');
  • pom.xml
<?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>
	<groupId>com.config.client</groupId>
	<artifactId>config-server-mysql</artifactId>
	<version>2.0.1-SNAPSHOT</version>
	<name>config-server-mysql</name>
	<description>Spring cloud config server project for Spring Boot</description>
	<packaging>jar</packaging>
	<properties>
		<java.version>11</java.version>
		<springboot.version>2.4.4</springboot.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
			<version>${springboot.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<version>${springboot.version}</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</artifactId>
			<version>3.0.2</version>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>8.0.22</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
			<version>${springboot.version}</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<version>2.4.4</version>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.1</version>
				<configuration>
					<verbose>true</verbose>
					<fork>true</fork>
					<source>1.8</source>
					<target>1.8</target>
					<executable><!-- path-to-javac -->
					</executable>
					<compilerVersion>1.8</compilerVersion>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>
  • application.yml

    spring:
      profiles:
         active: jdbc
      application:
         name: config-jdbc-server
      datasource:
         url: jdbc:mysql://localhost:3306/test?useSSL=true&serverTimezone=GMT
         username: root
         password: root123
         driver-class-name: com.mysql.cj.jdbc.Driver
      cloud:
         config:
           label: master
           server:
             jdbc: 
               sql: select key1, value1 from config_properties where application=? and profile=? and label=?
    server:
      port: 8888

     

  • ConfigServerApplication.java

package com.config.client;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

	public static void main(String[] args) {
		SpringApplication.run(ConfigServerApplication.class, args);
	}

}
  • Spring Cloud Config-client

  • pom.xml

<?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>
	<groupId>com.config.client</groupId>
	<artifactId>config-client</artifactId>
	<version>1.0.1-SNAPSHOT</version>
	<name>config-client</name>
	<description>Spring cloud config client project for Spring Boot 2.4.4</description>
	<properties>
		<java.version>11</java.version>
		<springboot.version>2.4.4</springboot.version>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
	</properties>
	<dependencies>
		<!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> 
			<version>${springboot.version}</version> </dependency> -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
			<version>${springboot.version}</version>
		</dependency>
		<!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> 
			<version>${springboot.version}</version> <scope>test</scope> </dependency> -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
			<version>3.0.2</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-context</artifactId>
			<version>3.0.2</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-commons-dependencies</artifactId>
			<version>3.0.2</version>
			<type>pom</type>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-bootstrap</artifactId>
			<version>3.0.2</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.1</version>
				<configuration>
					<verbose>true</verbose>
					<fork>true</fork>
					<source>11</source>
					<target>11</target>
					<executable><!-- path-to-javac -->
					</executable>
					<compilerVersion>11</compilerVersion>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>
  • bootstrap.yml

spring:
  application:
    name: config-client
  cloud:
    config:
      uri: http://localhost:8888
      fail-fast: true
      label: master

  profiles:
    active: dev
  • ConfigClientApplication.java
package com.config.client;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;

@SpringBootApplication
@Configuration
@EnableAutoConfiguration
public class ConfigClientApplication {

	public static void main(String[] args) {
		SpringApplication.run(ConfigClientApplication.class, args);
	}
	
	
}
  • IndexController.java

package com.config.client.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Configuration
@EnableAutoConfiguration
public class IndexController {
	@Value("${foo}")
	String foo;

	
	@RequestMapping(value = "/foo")
	public String hi(){
		return foo;
	}
	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值