1 创建表
create table DSP_CONFIG
(
key VARCHAR2(1000) not null,
value VARCHAR2(2000),
application VARCHAR2(100) not null,
profile VARCHAR2(100) not null,
label VARCHAR2(100),
remark VARCHAR2(200)
)
2 引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>
3 增加配置
spring.cloud.config.server.bootstrap=true
spring.cloud.config.server.prefix=/config
spring.cloud.config.server.jdbc.sql=SELECT KEY, VALUE from DSP_CONFIG where APPLICATION=? and PROFILE=? and LABEL=?
spring.profiles.active=jdbc
spring.datasource.url=jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=127.0.0.1)(PORT=1521))(LOAD_BALANCE=YES)(FAILOVER=YES)(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=DCDB)))
spring.datasource.username=dspmgr
spring.datasource.password=qaz_Mgr123
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
4 将原配置文件导入数据库
package com.cares.dspconfigserver;
import org.springframework.core.io.ClassPathResource;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Properties;
import java.util.stream.Collectors;
public class LoadPropsToDb {
class Pros {
private String key;
private String value;
private String profile;
private String application;
private String label;
private String remark;
public Pros(String key, String value, String profile, String application, String label, String remark) {
this.key = key;
this.value = value;
this.profile = profile;
this.application = application;
this.label = label;
this.remark = remark;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getProfile() {
return profile;
}
public void setProfile(String profile) {
this.profile = profile;
}
public String getApplication() {
return application;
}
public void setApplication(String application) {
this.application = application;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
}
public List<Pros> load(String filename) throws IOException {
String profile, app;
String[] proAndapp = filename.split("\\.")[0].split("-");
profile = proAndapp.length==2?proAndapp[1]:"default";
app = proAndapp[0];
Properties properties = new Properties();
properties.load(new ClassPathResource("/config/"+filename).getInputStream());
return properties.stringPropertyNames().stream().map(s -> {
Pros pros = new Pros(s,properties.getProperty(s),profile,app,"master","");
return pros;
}).collect(Collectors.toList());
}
public void insert(List<Pros> list,Connection connection){
try{
connection.setAutoCommit(false); //设置手动提交
//预编译sql对象,只编译一回
PreparedStatement ps = connection.prepareStatement(
"insert into DSP_CONFIG(KEY,VALUE,APPLICATION,PROFILE,LABEL)values(?,?,?,?,?)");
list.stream().forEach(pros -> {
try {
ps.setString(1,pros.getKey());
ps.setString(2,pros.getValue());
ps.setString(3,pros.getApplication());
ps.setString(4,pros.getProfile());
ps.setString(5,pros.getLabel());
ps.addBatch();//添加到批次
} catch (SQLException throwables) {
throwables.printStackTrace();
}
});
ps.executeBatch();//提交批处理
connection.commit();//执行
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
package com.cares.dspconfigserver;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.sql.DataSource;
import java.io.IOException;
import java.sql.SQLException;
@RunWith(SpringRunner.class)
@SpringBootTest
public class DspConfigServerApplicationTests {
@Autowired
DataSource source;
@Test
public void contextLoads() {
LoadPropsToDb lt = new LoadPropsToDb();
try {
lt.insert(lt.load("db-dev.properties"),source.getConnection());
lt.insert(lt.load("db-test.properties"),source.getConnection());
lt.insert(lt.load("eureka.properties"),source.getConnection());
lt.insert(lt.load("configmanager.properties"),source.getConnection());
lt.insert(lt.load("configmonitor.properties"),source.getConnection());
lt.insert(lt.load("common.properties"),source.getConnection());
lt.insert(lt.load("rabbit-dev.properties"),source.getConnection());
lt.insert(lt.load("rabbit-test.properties"),source.getConnection());
lt.insert(lt.load("redis-test.properties"),source.getConnection());
lt.insert(lt.load("redis-dev.properties"),source.getConnection());
lt.insert(lt.load("mybatis.properties"),source.getConnection());
lt.insert(lt.load("leaderview.properties"),source.getConnection());
lt.insert(lt.load("gateway.properties"),source.getConnection());
lt.insert(lt.load("xxljobadmin.properties"),source.getConnection());
} catch (IOException | SQLException e) {
e.printStackTrace();
}
}
}
本博客所有文章除特别声明外,均采用 CC BY-SA 3.0协议 。转载请注明出处!