pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.mi</groupId>
<artifactId>config</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>config</name>
<description>config</description>
<properties>
<java.version>17</java.version>
<spring-cloud.version>2023.0.2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties
github 仓库
spring.application.name=config
server.port=8888
eureka.instance.hostname=localhost
eureka.client.service-url.default=http://localhost:8761/eureka/
spring.profiles.active=dev
spring.cloud.config.server.git.uri=https://github.com/xxxx/config-repo.git
spring.cloud.config.server.git.username=xxx
spring.cloud.config.server.git.password=xxxx
spring.cloud.config.server.git.default-label=develop
spring.cloud.config.server.git.search-paths=config
spring.cloud.config.label=develop
server:
port: 8082
eureka:
instance:
hostname: localhost
client:
service-url:
default: http://localhost:8761/eureka/
spring:
application:
name: product
profiles:
active: dev
cloud:
config:
name: ${spring.application.name}
uri: http://localhost:8888
label: develop
profile: dev
discovery:
enable: true
service-id: config
本地仓库
spring.application.name=config
server.port=8888
eureka.instance.hostname=localhost
eureka.client.service-url.default=http://localhost:8761/eureka/
spring.profiles.active=native
spring.cloud.config.server.native.search-locations=J:/java/project/config-repo/config
server:
port: 8082
eureka:
instance:
hostname: localhost
client:
service-url:
default: http://localhost:8761/eureka/
spring:
application:
name: product
profiles:
active: dev
cloud:
config:
name: ${spring.application.name}
uri: http://localhost:8888
label: develop
profile: dev
discovery:
enable: true
service-id: config
DB
spring.application.name=config
server.port=8888
eureka.instance.hostname=localhost
eureka.client.service-url.default=http://localhost:8761/eureka/
# 固定值
spring.profiles.active=jdbc
spring.datasource.url=jdbc:mysql://192.168.1.11:3306/mi
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.cloud.config.server.jdbc.sql=SELECT `KEY`, `VALUE` from properties where APPLICATION=? and PROFILE=? and LABEL=?
server:
port: 8082
eureka:
instance:
hostname: localhost
client:
service-url:
default: http://localhost:8761/eureka/
spring:
application:
name: product
profiles:
active: dev
cloud:
config:
name: ${spring.application.name}
uri: http://localhost:8888
label: develop
profile: dev
discovery:
enable: true
service-id: config
ConfigApplication
package com.mi.config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}