本篇文章讲解一下 如何将数据库文件放到配置中心中,
并且启动项目之后,如果修改配置中心内容,在不重启项目的基础上,更新配置,加载最新配置
喜欢的小伙伴记得点个关注
Apollo搭建
2.1 创建ApolloPortalDB
在MySQL中创建一个名为ApolloPortalDB的数据库,并导入SQL文件(SQL文件在Apollo官方文件中,可以去官方下载,如果不知道的小伙伴可以点击关注私信我)
2.2 创建ApolloConfigDB
在MySQL中创建一个名为ApolloConfigDB的数据库,并导入SQL文件(SQL文件在Apollo官方文件中,可以去官方下载,如果不知道的小伙伴可以点击关注私信我)
2.3 配置demo.sh
\apollo-build-scripts-master\apollo-build-scripts-master\demo.sh
2.4 启动
在\apollo-build-scripts-master\apollo-build-scripts-master 中右键---》git bash here(我使用的是window系统配置的,需要安装git)
Administrator@PC-20201002XZXA MINGW64 /d/AAAAAA/apollo-build-scripts-master/apollo-build-scripts-master $ ./demo.sh start
此时可以访问 http://127.0.0.1:8080
客户端http://127.0.0.1:8070/signin
2.5 配置build.bat 并启动
将apollo1的jar包编译到maven仓库中。
D:\AAAAAA\apollo-master\apollo-master\scripts\build.bat
2.6 springboot项目中导入依赖
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-core</artifactId>
<version>1.0.0</version>
</dependency>
2.7 apollo-env.properties连接配置中心
local.meta=http://127.0.0.1:8080
dev.meta=http://127.0.0.1:8080
fat.meta=${fat_meta}
uat.meta=${uat_meta}
lpt.meta=${lpt_meta}
pro.meta=${pro_meta}
######
2.9 springboot中添加META-INF/app.properties
app.id=aaa001
2.10 修改系统settings
C:\opt\settings\server.properties(Windows)文件,设置env为DEV:
env=DEV
2.11 启动类
此时我们搭建好了apollo,可以在springboot项目中使用。
启动类添加注解@EnableApolloConfig
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableCircuitBreaker
@EnableApolloConfig
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class,args);
}
}
此时我们可以这样读取apollo的配置信息
@RestController
@RequestMapping("/stu")
public class StudentController {
/***
读取配置中心的配置
*/
@Value("${myname:heihei}")
private String haha;
@GetMapping("listAll")
@HystrixCommand(fallbackMethod = "listAllFallBackMethod")
public Result listAll(){
return Result.success(haha);
}
}
如果完成到这一步,基本上就算成功了。
此时如果我们希望将数据库的配置放到配置中心中。
我们可以将数据库的配置信息放到配置中心中,例如:
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
mybatis-plus.type-aliases-package=com.aaa.entity
mybatis-plus.mapper-locations=classpath:mapper/*.xml
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
此时我们需要一个配置类,来监听远程配置中心的内容,并且实时更新
@Configuration
@EnableConfigurationProperties(DataSourceProperties.class)
public class DataSourceConfig {
@Autowired
ApplicationContext context;
@Autowired
private org.springframework.cloud.context.scope.refresh.RefreshScope refreshScope;
@ApolloConfigChangeListener()
private void onChange(ConfigChangeEvent changeEvent) {
DataSourceProperties dataSourceProperties = context.getBean(DataSourceProperties.class);
changeEvent.changedKeys().stream().forEach(s -> {
// 注意 我此时只修改了password 如果需要全部都修改的小伙伴 可以多加几个判断
if (s.contains("spring.datasource.password")) {
dataSourceProperties.setPassword(changeEvent.getChange(s).getNewValue());
}
});
boolean dataSource = refreshScope.refresh("dataSource");
System.out.println("-----------------" + dataSource);
}
@RefreshScope
@Primary
@Bean
public DataSource dataSource(DataSourceProperties dataSourceProperties) {
return dataSourceProperties.initializeDataSourceBuilder().build();
}
}
本文暂时没有评论,来添加一个吧(●'◡'●)