使用mysql作为配置文件的地址
server端配置
POM文件
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.31</version> </dependency>
配置文件
properties.yml
server: port: 9000 spring: application: name: config-server-jdbc cloud: config: profile: jdbc # 使用jdbc作为配置文件 label: master server: jdbc: sql: SELECT `key`,`value` from config_properties where application_name=? and profile=? and lable=? # sql refresh: refreshable: none # 使用Jdbc作为配置文件会造成循环依赖,使用此注解可解决问题 profiles: active: jdbc #默认文件类型 jdbc datasource: driver-class-name: com.mysql.jdbc.Driver username: root password: root url: jdbc:mysql://localhost:3306/spring_cloud # main: # allow-bean-definition-overriding: true 使用Jdbc作为配置文件会造成循环依赖,使用此注解可解决问题
config Client
POM文件
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
配置文件
spring cloud config 的配置文件需要提前加载所以需要在resources
目录下新建文件bootstrap.yml
文件
spring: profiles: active: dev # 默认加载配置文件 application: name: config cloud: config: uri: http://localhost:9000 # 配置服务器地址 name: ${spring.application.name} # application Name profile: ${spring.profiles.active} # 那个类型的配置为文件