让我们开始将 Spring-Boot 从
升级到3.3.6
。3.4.0
//build.gradle
//old
plugins {
id 'java'
id 'org.springframework.boot' version '3.3.6'
id 'io.spring.dependency-management' version '1.1.6'
id 'org.sonarqube' version '5.1.0.4882'
id 'jacoco'
id 'com.gorylenko.gradle-git-properties' version '2.4.2'
id 'project-report'
}
ext {
set('springCloudGcpVersion', "5.8.0")
set('springCloudVersion', "2023.0.4")
}
dependencies {
implementation platform('com.google.cloud:spring-cloud-gcp-dependencies:5.8.0')
implementation platform('org.springframework.cloud:spring-cloud-dependencies:2023.0.4')
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
implementation 'org.bouncycastle:bcprov-jdk18on:1.79'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'com.google.cloud:spring-cloud-gcp-starter-secretmanager'
implementation 'com.google.cloud:spring-cloud-gcp-starter-storage'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.6.0'
implementation 'org.apache.commons:commons-collections4:4.4'
implementation 'org.mapstruct:mapstruct:1.6.3'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.6.3'
annotationProcessor 'org.projectlombok:lombok-mapstruct-binding:0.2.0'
annotationProcessor 'org.projectlombok:lombok'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'org.postgresql:postgresql'
}
dependencyManagement {
imports {
mavenBom "com.google.cloud:spring-cloud-gcp-dependencies:${springCloudGcpVersion}"
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
//build.gradle
//new
plugins {
id 'java'
id 'org.springframework.boot' version '3.4.0'
id 'io.spring.dependency-management' version '1.1.6'
id 'org.sonarqube' version '5.1.0.4882'
id 'jacoco'
id 'com.gorylenko.gradle-git-properties' version '2.4.2'
id 'project-report'
}
ext {
set('springCloudGcpVersion', "5.9.0")
set('springCloudVersion', "2024.0.0")
}
dependencies {
implementation platform('com.google.cloud:spring-cloud-gcp-dependencies:5.9.0')
implementation platform('org.springframework.cloud:spring-cloud-dependencies:2024.0.0')
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.cloud:spring-cloud-starter-bootstrap'
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
implementation 'org.bouncycastle:bcprov-jdk18on:1.79'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'com.google.cloud:spring-cloud-gcp-starter-secretmanager'
implementation 'com.google.cloud:spring-cloud-gcp-starter-storage'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.7.0'
implementation 'org.apache.commons:commons-collections4:4.4'
implementation 'org.mapstruct:mapstruct:1.6.3'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.6.3'
annotationProcessor 'org.projectlombok:lombok-mapstruct-binding:0.2.0'
annotationProcessor 'org.projectlombok:lombok'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'org.postgresql:postgresql'
}
dependencyManagement {
imports {
mavenBom "com.google.cloud:spring-cloud-gcp-dependencies:${springCloudGcpVersion}"
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
现在,数据源连接失败,因为使用
注册数据库连接信息。secretmanager
# application.yml
spring:
config:
import: "sm://"
datasource:
url: jdbc:postgresql://localhost:5432/cehr?currentSchema=XXXX
username: ${sm://psql-username}
password: ${sm://psql-password}
@Service
public class XXXService extends CommonService {
@Autowired
private SecretManagerTemplate secretManagerTemplate;
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;
@Value("${sm://psql-username}")
private String username2;
@Value("${sm://psql-password}")
private String password2;
public void execute() {
System.out.println("username = " + username);
System.out.println("password = " + password);
System.out.println("username2 = " + username2);
System.out.println("password2 = " + password2);
System.out.println("psql-username = " + secretManagerTemplate.getSecretString("sm://psql-username"));
System.out.println("psql-password = " + secretManagerTemplate.getSecretString("sm://psql-password"));
}
}
# output
username = //psql-username
password = //psql-password
username2 = //psql-username
password2 = //psql-password
psql-username = ********** (correct)
psql-password = ********** (correct)
secretmanager
使用 SecretManagerTemplate
secretmanager
请勿使用 application.yml
# example
server:
port: ${SERVER_PORT:8080}
special characters
的 Expression Language ( ${ } )
之一。
使用此字符设置 value
和 default value
Environment Variable
SERVER_PORT,则项目使用您设置的端口,否则项目使用默认端口 8080。special character
,必须添加Escape Character
。# application.yml
spring:
config:
import: "sm://"
datasource:
url: jdbc:postgresql://localhost:5432/cehr?currentSchema=XXXX
username: ${sm\://psql-username}
password: ${sm\://psql-password}
@Service
public class XXXService extends CommonService {
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;
@Value("${sm://psql-username}")
private String username2;
@Value("${sm://psql-password}")
private String password2;
@Value("${sm\\://psql-username}")
private String username3;
@Value("${sm\\://psql-password}")
private String password3;
public void execute() {
System.out.println("username = " + username);
System.out.println("password = " + password);
System.out.println("username2 = " + username2);
System.out.println("password2 = " + password2);
System.out.println("username3 = " + username3);
System.out.println("password3 = " + password3);
System.out.println("psql-username = " + secretManagerTemplate.getSecretString("sm://psql-username"));
System.out.println("psql-password = " + secretManagerTemplate.getSecretString("sm://psql-password"));
}
}
# output
username = ********** (correct)
password = ********** (correct)
username2 = //psql-username
password2 = //psql-password
username3 = ********** (correct)
password3 = ********** (correct)
psql-username = ********** (correct)
psql-password = ********** (correct)
在这篇文章中,我们发现了Spring-Boot升级到3.4.0版本后如何使用gcp Secretmanager。我认为这是一个奢侈的麻烦。 另请参阅:官方网站。 享受它吧。