1、原理图
2、GIT仓库地址
https://github.com/duanledexuanc/microconfig/blob/master/microcloud-config-client.yml
3、config-server
pom.xml
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.zemel</groupId>
<artifactId>microcloud</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>microcloud-config-7101</artifactId>
<name>microcloud-config-7101</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<!-- 无安全配置,可去除 -->
<groupId>com.zemel</groupId>
<artifactId>microcloud-security</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
</project>
application.yml
server:
port: 7101
spring:
application:
name: provider-config
cloud: # 进行SpringCloud的相关配置
config: #sprignCloudConfig的相关配置
server: # 设置服务的连接地址
git: # 本次使用的是GIT作为仓库的管理,设置git的连接地址
uri: https://github.com/duanledexuanc/microconfig.git
Config7101App.java
package com.zemel;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class Config7101App
{
public static void main( String[] args )
{
SpringApplication.run(Config7101App.class, args);
}
}
启动访问:
4、config-client
pom.xml
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.zemel</groupId>
<artifactId>microcloud</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>microcloud-config-8201</artifactId>
<name>microcloud-config-8201</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<!-- 无安全模块可去除 -->
<groupId>com.zemel</groupId>
<artifactId>microcloud-security</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
</project>
application.yml
spring:
application:
name: microcloud-config-client
server:
port:8888 # 和git的配置重复,但是会被覆盖
bootstrap.yml
spring:
cloud:
config:
name: microcloud-config-client #定义要读取的资源文件的名称
profile: beta #定义profile的名称
label: master #定义配置文件所在的分支
uri: http://config-7101.com:7101 #获取的服务地址
username: wendy #连接的用户名
password: wendy #连接的密码
关于bootstrap.yml和application.yml
ConfigClientRest.java
package com.zemel.rest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConfigClientRest {
@Value("${spring.application.name}")
private String applicationName; // 服务名称
@Value("${eureka.client.serviceUrl.defaultZone}")
private String eurekaServers; // 服务信息项
@GetMapping("/config")
public String getConfig(){
return "ApplicationName = " + this.applicationName + "、EurekaServers = " + this.eurekaServers;
}
}
Config8201App.java
package com.zemel;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Config8201App
{
public static void main( String[] args )
{
SpringApplication.run(Config8201App.class, args);
}
}
启动访问: