//代码是测试过的,不会有问题有报错可联系本人
server 服务端的配置
server.port=20000
spring.application.name=config-server
#配置配置中心相关
#git仓库的地址
spring.cloud.config.server.git.uri=https: 自己的github地址.git
#配置用户名
spring.cloud.config.server.git.username=自己的用户
#配置密码
spring.cloud.config.server.git.password=自己的密码
#配置代码库使用的是那个分支
#spring.cloud.config.server.git.default-label=master
#配置加载的配置文件所在的目录,可以配置多个 多个目录之间用逗号分开
spring.cloud.config.server.git.search-paths=config
server的启动类上加的注解
@SpringBootApplication
@EnableConfigServer是spring cloud定义的注解
这篇文章里有详细的注解讲解
http://www.mamicode.com/info-detail-1680630.html
Client 配置
#配置配置中心服务
#允许使用配置中心服务
spring.cloud.config.discovery.enabled=true
#使用的配置中心的服务的名称
spring.cloud.config.discovery.service-id=config-server
spring.cloud.config.uri=localhost:20000
#加载的配置文件的名字
#spring.cloud.config.name=test
#配置文件所在的分支的名称
spring.cloud.config.label=master
#暴露所有的端点
management.endpoints.web.exposure.include=*
spring.cloud.config.fail-fast=true
#spring.cloud.config.profile=test
spring.cloud.config.name=test
#这个地方需要配name而不是profile
server.port=20001
spring.application.name=config-client
我在Git中test.properties中放着的是
datasource.url=jdbc:mysql://localhost:3306/test
datasource.username=你好啊兄弟,我成了
datasource.password=password
所以这里的类这样配
@Component
@ConfigurationProperties(prefix = "datasource")
//prefix =datasource是属性的前缀
@Data
@RefreshScope
public class MyTestConfig {
private String url;
private String username;
private String password;
}
首先是父类的pom
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR1</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
</project>
Clent客户端的pom
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.51</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
</dependencies>
</project>
server服务端的pom
<dependencies>
<!-- config server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
</project>