6_Config分布式配置中心
前言
Github:https://github.com/HealerJean
1、ConfigServer_8888
配置中心
最初的时候,我讲
ConfigServer
的端口设置为5001
,在url
获取config配置中心
的时候,没有任何问题,但是在ConfigClient
使用服务中心的时候,发现了不论ConfigClient
怎么配置,都是去寻找的默认的8888 Config配置中心
。没办法,我总得测试呗,所以我将Config配置中心
的端口改成了8888
网络传言
1、是Bootstrap,但是很明显,我的不是这个问题。
2、
locahost
域名的的问题,这种情况我还没试
1.1、添加pom依赖
<!--config-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<?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>com.healerjean.proj</groupId>
<artifactId>hlj-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>hlj-config-5001</artifactId>
<version>${project.healerjean.version}</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--config-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
1.2、启动类
@EnableConfigServer
:添加分布式配置服务端支持
package com.healerjean.proj;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableConfigServer //添加分布式配置服务端支持
@SpringBootApplication
public class ConfigServer_8888_Application {
public static void main(String[] args) {
SpringApplication.run(ConfigServer_8888_Application.class, args);
}
}
2、Github存储配置信息
2.1、创建仓库cloud-config
2.1.1、根目录创建配置项目文件夹 config-client
2.1.2、创建配置文件properties
命名规则:
自定义项目名称-profile.properties
client.properties
from=github-default
client-dev.properties
from=github-dev
client-local.properties
from=github-local
client-prod.properties
from=github-prod
2.1.3、上传到Github
2.2、Config配置中心配置
2.1.1、application.properties
spring.application.name=hlj-config-server
server.port=8888
# config => git仓库
spring.cloud.config.server.git.uri=https://github.com/HealerJean/cloud-config/
# 配置文件所属文件夹路径
spring.cloud.config.server.git.searchPaths=config-client
# 分支名称
spring.cloud.config.label=master
#公开仓库不需要写用户名和密码,如果是私有仓库则需要些用户名和密码
spring.cloud.config.server.git.username=
spring.cloud.config.server.git.password=
2.3、启动测试
2.3.1、http访问路径解释
application:properties代表的项目名
profile:properties代表的Profile
label:Gihub分支名称
http路径 |
---|
/{application}/{profile}[/{label}] |
/{label}/{application}-{profile}.yml |
/{application}-{profile}.yml |
/{application}-{profile}.properties |
http://localhost:8888/client/prod/master
接口返回:
{
"name": "client",
"profiles": [
"prod"
],
"label": "master",
"version": null,
"state": null,
"propertySources": [
{
"name": "https://github.com/HealerJean/cloud-config/config-client/client-prod.properties",
"source": {
"from": "github-prod"
}
},
{
"name": "https://github.com/HealerJean/cloud-config/config-client/client.properties",
"source": {
"from": "github-default"
}
}
]
}
http://localhost:8888/client/master
接口返回:
{
"name": "client",
"profiles": [
"master"
],
"label": null,
"version": null,
"state": null,
"propertySources": [
{
"name": "https://github.com/HealerJean/cloud-config/config-client/client.properties",
"source": {
"from": "github-default"
}
}
]
}
3、ConfigClient_6001
:url访问
3.1、pom.xml依赖
<!--starter-config-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<?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>com.healerjean.proj</groupId>
<artifactId>hlj-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>hlj-config-client-6001</artifactId>
<version>${project.healerjean.version}</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--starter-config-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.2、Url
获取Config
配置
3.2.1、bootstrap.properties
spring.application.name=client
server.port=6001
#1、 通过地址访问配置文件
spring.cloud.config.profile=prod
spring.cloud.config.label=master
spring.cloud.config.uri=http://localhost:8888/
3.2.2、ConfigClientController
@RefreshScope
:spring-cloud
实现更新配置不用重启服务
package com.healerjean.proj.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RefreshScope //`spring-cloud` 实现更新配置不用重启服务**
@RestController
@RequestMapping("api/config/client")
public class ConfigClientController {
@Value("${from}")
private String from;
@GetMapping("fromValue")
public String from() {
return this.from;
}
}
3.2.3、启动测试
http://127.0.0.1:6001/api/config/client/fromValue
接口返回:
github-prod
3、ConfigClient_6001
:启用服务发现访问
3.1、Config配置中心_8888
3.1.1、pom.xml
依赖
<!--starter-eureka-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<?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>com.healerjean.proj</groupId>
<artifactId>hlj-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>hlj-config-server-5001</artifactId>
<version>${project.healerjean.version}</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--config-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!--starter-eureka-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.1.2、application.properties
spring.application.name=hlj-config-server
server.port=8888
# config => git仓库
spring.cloud.config.server.git.uri=https://github.com/HealerJean/cloud-config/
# 配置文件所属文件夹路径
spring.cloud.config.server.git.searchPaths=config-client
# 分支名称
spring.cloud.config.label=master
#公开仓库不需要写用户名和密码,如果是私有仓库则需要些用户名和密码
spring.cloud.config.server.git.username=
spring.cloud.config.server.git.password=
#看是否需要:指定服务注册中心
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/,http://localhost:1112/eureka/
3.1.3、启动类
package com.healerjean.proj;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableConfigServer //添加分布式配置服务端支持
@EnableEurekaClient //添加服务发现
@SpringBootApplication
public class Config_5001_Application {
public static void main(String[] args) {
SpringApplication.run(Config_5001_Application.class, args);
}
}
3.2、Config客户端_6001
3.2.1、pom.xml依赖
<!--starter-eureka-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<?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>com.healerjean.proj</groupId>
<artifactId>hlj-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>hlj-config-client-6001</artifactId>
<version>${project.healerjean.version}</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--hlj-common-->
<dependency>
<groupId>com.healerjean.proj</groupId>
<artifactId>hlj-common</artifactId>
<version>${project.healerjean.version}</version>
</dependency>
<!--starter-config-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!--starter-eureka-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!--starter-web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.2.2、bootstrap.properties
spring.application.name=client
server.port=6001
## 1、通过Url地址访问配置文件
#spring.cloud.config.profile=prod
#spring.cloud.config.label=master
#spring.cloud.config.uri=http://localhost:8888/
# 2、通过服务发现访问配置文件
spring.cloud.config.enabled=true
spring.cloud.config.profile=prod
spring.cloud.config.label=master
spring.cloud.config.discovery.service-id=HLJ-CONFIG-SERVER
##指定服务注册中心
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/,http://localhost:1112/eureka/
4.2.2、启动类
package com.healerjean.proj;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class ConfigClient_6001_Application {
public static void main(String[] args) {
SpringApplication.run(ConfigClient_6001_Application.class, args);
}
}
4.2.4、启动测试
http://127.0.0.1:6001/api/config/client/fromValue
接口返回:
github-prod
4、ConfigServer_8888
本地存储配置信息
4.1、ConfigServer_8888
4.1.1、创建文件夹config
将我们之前Githu存储的额放到这里来,修改下里面的信息
client.properties
from=config-default
client-dev.properties
from=config-dev
client-local.properties
from=config-local
client-prod.properties
from=config-prod
4.1.2、application.properties
spring.application.name=hlj-config-server
server.port=8888
## 1、 Github存储配置信息
#spring.cloud.config.server.git.uri=https://github.com/HealerJean/cloud-config/
## 配置文件所属文件夹路径
#spring.cloud.config.server.git.searchPaths=config-client
## 分支名称
#spring.cloud.config.label=master
##公开仓库不需要写用户名和密码,如果是私有仓库则需要些用户名和密码
#spring.cloud.config.server.git.username=
#spring.cloud.config.server.git.password=
#看是否需要:指定服务注册中心
#eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/,http://localhost:1112/eureka/
# 2、本次存储配置稀信息
spring.profiles.active=native
spring.cloud.config.server.native.search-locations=classpath:/config
#指定服务注册中心
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/,http://localhost:1112/eureka/
4.1.3、启动类
package com.healerjean.proj;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableConfigServer //添加分布式配置服务端支持
@EnableEurekaClient //添加服务发现
@SpringBootApplication
public class ConfigServer_8888_Application {
public static void main(String[] args) {
SpringApplication.run(ConfigServer_8888_Application.class, args);
}
}
4.1.4、启动测试
http://localhost:8888/client/prod
接口返回:
{
"name": "client",
"profiles": [
"prod"
],
"label": null,
"version": null,
"state": null,
"propertySources": [
{
"name": "classpath:/config/client-prod.properties",
"source": {
"from": "config-prod"
}
},
{
"name": "classpath:/config/client.properties",
"source": {
"from": "config-default"
}
}
]
}
4.2、ConfigClient_6001
4.2.1、bootstrap.properties
这里的和之前的是一样的,没有变过
spring.application.name=client
server.port=6001
## 1、通过Url地址访问配置文件
#spring.cloud.config.profile=prod
#spring.cloud.config.label=master
#spring.cloud.config.uri=http://localhost:8888/
# 2、通过服务发现访问配置文件
spring.cloud.config.enabled=true
spring.cloud.config.profile=prod
spring.cloud.config.label=master
spring.cloud.config.discovery.service-id=HLJ-CONFIG-SERVER
##指定服务注册中心
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/,http://localhost:1112/eureka/
4.2.1、启动类
package com.healerjean.proj;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class ConfigClient_6001_Application {
public static void main(String[] args) {
SpringApplication.run(ConfigClient_6001_Application.class, args);
}
}
4.2.1、ConfigClientController
package com.healerjean.proj.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RefreshScope //`spring-cloud` 实现更新配置不用重启服务**
@RestController
@RequestMapping("api/config/client")
public class ConfigClientController {
@Value("${from}")
private String from;
@GetMapping("fromValue")
public String from() {
return this.from;
}
}
4.3、启动测试
http://127.0.0.1:6001/api/config/client/fromValue
接口返回:
config-prod
5、Config安全性配置
5.1、ConfigServer
#开启安全性认证
security.basic.enabled=true
security.user.name=username
security.user.password=password
spring.application.name=hlj-config-server
server.port=8888
## 1、 Github存储配置信息
#spring.cloud.config.server.git.uri=https://github.com/HealerJean/cloud-config/
## 配置文件所属文件夹路径
#spring.cloud.config.server.git.searchPaths=config-client
## 分支名称
#spring.cloud.config.label=master
##公开仓库不需要写用户名和密码,如果是私有仓库则需要些用户名和密码
#spring.cloud.config.server.git.username=
#spring.cloud.config.server.git.password=
#看是否需要:指定服务注册中心
#eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/,http://localhost:1112/eureka/
# 2、本次存储配置稀信息
spring.profiles.active=native
spring.cloud.config.server.native.search-locations=classpath:/config
#指定服务注册中心
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/,http://localhost:1112/eureka/
#开启安全性认证
security.basic.enabled=true
security.user.name=username
security.user.password=password
5.2、ConfigClient
spring.application.name=client
server.port=6001
## 1、通过Url地址访问配置文件
#spring.cloud.config.profile=prod
#spring.cloud.config.label=master
#spring.cloud.config.uri=http://localhost:8888/
# 2、通过服务发现访问配置文件
spring.cloud.config.enabled=true
spring.cloud.config.profile=prod
spring.cloud.config.label=master
spring.cloud.config.discovery.service-id=HLJ-CONFIG-SERVER
##指定服务注册中心
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/,http://localhost:1112/eureka/
# 安全性登陆
spring.cloud.config.username=username
spring.cloud.config.password=password