新建springboot项目
依赖:
<!--apollo装的什么版本这里就用什么版本-->
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-core</artifactId>
<version>1.3.0</version>
</dependency>
1.启动类添加注解:
@MapperScan({“com.ohaotian.tydic.apollotest.dao”})
@Configuration
@EnableApolloConfig
2.由于mapper.xml不是在resources下pom添加:
里添加
<!--编译不放在resources下的mapper-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
3.application.properties添加:
server.port=8081
#数据源
#spring.datasource.hikari.driver-class-name = com.mysql.jdbc.Driver
#spring.datasource.hikari.jdbc-url = jdbc:mysql://192.168.169.134:3306/student?useUnicode=true&characterEncoding=utf-8
#spring.datasource.hikari.username = root
#spring.datasource.hikari.password = 123456
spring.datasource.url=jdbc:mysql://192.168.169.134:3306/student?useUnicode=true&characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=123456
#mapper
mybatis.mapper-locations = classpath:com/ohaotian/tydic/apollotest/mapper/*.xml
4.运行测试一遍,确定配置文件没问题
5.resources中添加:
内容:
app.id=apollo-test
apollo.env=dev
apollo.meta=http://192.168.169.131:8080
#启动环境配置的方式
#VM: -Denv=dev -Ddev_meta=http://192.168.169.131:8080
6.把配置文件中的配置全部复制到apollo中,并删除配置文件
7.运行
注:
apollo有缓存配置文件的功能(一旦连接不上apollo,就会用缓存文件,测试的时候,有时没连上apollo,配置项目中的文件,其实是不起作用的,用的是缓存的配置文件)
本地缓存路径
Mac/Linux: /opt/data/{appId}/config-cache
Windows: C:\opt\data{appId}\config-cache
参考:https://blog.csdn.net/weixin_38187317/article/details/84584668