6_config分布式配置中心
前言
代码还是使用之前的代码,如果需要完整代码,请拉倒文章底部获取
1、新建工程config-server
1.1、添加pom依赖
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.didispace</groupId>
<artifactId>config-server</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>config-server</name>
<description>Spring Cloud project</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.SR5</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>
1.2、注解添加分布式配置服务端支持
@EnableConfigServer
@SpringBootApplication
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}
2、第一种:我们以github仓库为例进行讲解
2.1、创建github仓库SpringcloudConfig
2.1.1、其根目录下同时创建文件夹respo
2.1.2、该文件夹下创建配置文件 命名规则 :自定义项目名称-profile.properties,分别写入内容
from=git-dev-1.0
from=git-prod-1.0
from=git-test-1.0
from=git-default-1.0
2.2、配置工程的配置文件,添加git仓库的属性,
spring.application.name=config-server
server.port=7001
# git仓库
spring.cloud.config.server.git.uri=https://github.com/HealerJean/SpringcloudConfig/
spring.cloud.config.server.git.searchPaths=respo
spring.cloud.config.label=master
#公开仓库不需要写用户名和密码,如果是私有仓库则需要些用户名和密码
spring.cloud.config.server.git.username=
spring.cloud.config.server.git.password=
2.3、测试启动这个项目7001端口
浏览器访问http://localhost:7001/didispace/prod/master //+分支名称
解释:妈的,居然真的有显示,证明配置服务中心可以从远程程序获取配置信息。
name:应用名称 didispace
label 表示分支名称 master
version 表示git上对应的版本号
{
"name": "didispace",
"profiles": ["prod"],
"label": "master",
"version": "68a075d11aea67d4989989c70152a9ac53367e82",
"propertySources": [{
"name": "https://github.com/HealerJean123/SpringcloudConfig/respo/didispace-prod.properties",
"source": {
"from": "git-prod-1.0"
}
}, {
"name": "https://github.com/HealerJean123/SpringcloudConfig/respo/didispace.properties",
"source": {
"from": "git-default-1.0"
}
}]
}
2.4、http请求地址和资源文件映射如下:
• /{application}/{profile}[/{label}] 上上面的链接http://localhost:7001/didispace/prod/master
• /{application}-{profile}.yml
• /{label}/{application}-{profile}.yml
• /{label}/{application}-{profile}.properties
• /{application}-{profile}.properties http://localhost:7001/didispace-dev.properties
3、客户端映射
3.1、创建工程config-client,pom.xml如下
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.didispace</groupId>
<artifactId>config-client</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>config-client</name>
<description>Spring Cloud project</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.SR5</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>
3.2、创建bootstrap.properties,来获取配置文件config-server的位置
server.port=7002
spring.application.name=didispace
spring.cloud.config.profile=dev
spring.cloud.config.label=master
spring.cloud.config.uri=http://localhost:7001/
3.3、创建controller开始测试,通过后台的from(只要在配置文件中,其实都是from)
**@RefreshScope
**spring-cloud 实现更新配置不用重启服务 @FreshScope
@RefreshScope
@RestController
public class TestController {
@Value("${from}")
private String from;
@RequestMapping("/from")
public String from() {
return this.from;
}
}
3.4、浏览器访问 http://localhost:7002/from
4、添加Eurake服务注册中心
使用之前的注册中心 1111
4.1、配置config-server
4.1.1、添加eurake依赖包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
4.1.2、启用注册发现,和配置服务注册地址
@EnableDiscoveryClient
@EnableConfigServer
@SpringBootApplication
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}
4.1.3、指定注册中心
#eurake
#指定服务注册中心
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
4.2、配置config-client
4.2.1、添加eurake依赖包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
4.2.2、启用注册发现
@EnableDiscoveryClient
@SpringBootApplication
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}
4.2.3、配置服务注册中心和服务端的config
server.port=7002
spring.application.name=didispace
spring.cloud.config.enabled=true
spring.cloud.config.discovery.service-id=CONFIG-SERVER
spring.cloud.config.profile=dev
4.2.4、开始测试,成功
HealerJean-代码下载
5、项目实战中的config
5.1、新建项目sso-config
5.1.1、添加pom依赖
<dependencies>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.carl.auth</groupId>
<artifactId>sso-config</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>sso-config</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Edgware.SR2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</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>
5.1.2、添加配置管理器支持注解
@EnableConfigServer
@SpringBootApplication
public class SsoConfigApplication {
public static void main(String[] args) {
SpringApplication.run(SsoConfigApplication.class, args);
}
}
5.1.3、编辑config服务端配置文件
server:
#服务端口
port: 8888
#访问路径
context-path: /config
#指定日志输出文件
logging:
file: "logs/sso-config.log"
info:
name : "配置中心"
---
spring:
profiles:
#本地配置文件
active:
#配置文件本地化
- native
application:
#指定应用名称
name: sso-config
5.1.4、运行这个spring boot项目,浏览器访问 http://localhost:8888/config/ 说明下面的启动成功
5.1.5、配置中心的位置,创建resource 下创建config文件夹,创建sso-dev.propetries 编辑配置文件
1、上面我们提到配置是放置于配置中心,而不放在服务内2、由于配置是放在与本地,那么spring的默认配置在目录为resources/config
写入内容
hello_key=value
5.1.6、重新启动spring boot项目,访问 http://localhost:8888/config/sso/dev
name:应用名称 sso -dev.properties配置文件的名称
label 表示分支名称 master
version 表示git上对应的版本号
{
"name": "sso",
"profiles": ["dev"],
"label": null,
"version": null,
"state": null,
"propertySources": [{
"name": "classpath:/config/sso-dev.properties",
"source": {
"hello_key": "value"
}
}]
}
5.2、接入客户端
#指定日志文件
logging.file=logs/cas.log
info.name=单点登录系统
#定义application.name的id
spring.application.name=sso
#寻找配置中心为sso-dev.properties
spring.profiles.active=dev
#指定配置中心地址
spring.cloud.config.uri=http://localhost:8888/config
#开启配置中心
spring.cloud.config.enabled=true
#支持自动任务去配置中心刷新配置
spring.cloud.config.watch.enabled=true
#30秒刷新一次
spring.cloud.config.watch.initialDelay=30000
#请求配置中心超市
spring.cloud.config.watch.delay=1000
#检查配置健康
health.config.enabled=true
感兴趣的,欢迎添加博主微信,
哈,博主很乐意和各路好友交流,如果满意,请打赏博主任意金额,感兴趣的在微信转账的时候,备注您的微信或者其他联系方式。添加博主微信哦。
请下方留言吧。可与博主自由讨论哦
微信 | 微信公众号 | 支付宝 |
---|---|---|
![]() |
![]() |
![]() |