Skip to content

Latest commit

 

History

History
231 lines (174 loc) · 6 KB

3.springcloud-config-server.md

File metadata and controls

231 lines (174 loc) · 6 KB

Spring Cloud 配置服务器

搭建Spring Cloud Config Server

基于文件系统

创建本地仓库

1.激活应用配置服务器

在引导类上配置@EnableConfigServer

@SpringBootApplication
@EnableConfigServer
public class Lesson3Application {

	public static void main(String[] args) {
		SpringApplication.run(Lesson3Application.class, args);
//        System.out.printf(System.getProperty("user.dir"));
	}

}

2.创建本地目录

理解Java中${user.dir}

在IDEA中的src/main/resources目录下创建一个configs,他的绝对路径:${user.dir}/src/main/resources/configs

3.配置git本地仓库URI

## 配置服务器文件系统git 仓库
## ${user.dir} 减少平台文件系统的不一致
spring.cloud.config.server.git.uri = ${user.dir}/src/main/resources/configs

4.给应用"app1"创建三个环境的配置文件

-rw-r--r--  1 app1-prod.properties
-rw-r--r--  1 app1-test.properties
-rw-r--r--  1 app1.properties

三个文件的环境 profile 分别(从上至下)是:prodtestdefault

5.初始化git仓库

git init
git add .
git commit -m "First commit"
[master (root-commit) 9bd81bd] First commit
 3 files changed, 9 insertions(+)
 create mode 100644 app1-prod.properties
 create mode 100644 app1-test.properties
 create mode 100644 app1.properties

测试配置服务器

通过浏览器测试应用为"segmentfault",Profile为:“test”的配置内容 : http://localhost:9090/app1/test

{
    "name": "app1",
    "profiles": [
        "test"
    ],
    "label": null,
    "version": "233fb61e4c3bd8d2971f596d51a16d1c47888534",
    "state": null,
    "propertySources": [
        {
            "name": "file://E:\\JavaProjects\\JavaStudySpace\\springcloud-lesson\\lesson-3/src/main/resources/configs/file:E:\\JavaProjects\\JavaStudySpace\\springcloud-lesson\\lesson-3\\src\\main\\resources\\configs\\app1-test.properties",
            "source": {
                "name": "app1-test"
            }
        },
        {
            "name": "file://E:\\JavaProjects\\JavaStudySpace\\springcloud-lesson\\lesson-3/src/main/resources/configs/file:E:\\JavaProjects\\JavaStudySpace\\springcloud-lesson\\lesson-3\\src\\main\\resources\\configs\\app1.properties",
            "source": {
                "name": "app1"
            }
        }
    ]
}

请注意:当指定了profile 时,默认的 profile(不指定)配置信息也会输出:

{
    "name": "app1",
    "profiles": [
        "default"
    ],
    "label": null,
    "version": "233fb61e4c3bd8d2971f596d51a16d1c47888534",
    "state": null,
    "propertySources": [
        {
            "name": "file://E:\\JavaProjects\\JavaStudySpace\\springcloud-lesson\\lesson-3/src/main/resources/configs/file:E:\\JavaProjects\\JavaStudySpace\\springcloud-lesson\\lesson-3\\src\\main\\resources\\configs\\app1.properties",
            "source": {
                "name": "app1"
            }
        }
    ]
}

基于git仓库

  1. 激活应用配置服务器

在引导类上标注@EnableConfigServer

  1. 配置远程 Git 仓库地址

  2. .本地 clone 远程Git 仓库 git clone https://gitee.com/Finen/tmp.git

$ ls -als
total 4
0 drwxr-xr-x 1 FineN 197609 0 2月  26 12:30 ./
0 drwxr-xr-x 1 FineN 197609 0 2月  26 12:30 ../
4 drwxr-xr-x 1 FineN 197609 0 2月  26 12:30 .git/

4.给应用"app1"创建三个环境的配置文件

-rw-r--r--  1 app1-prod.properties
-rw-r--r--  1 app1-test.properties
-rw-r--r--  1 app1.properties

5.提交到 远程 Git 仓库

$ git add -A

$ git commit -m "app1"
[master (root-commit) 61e03d7] app1
 3 files changed, 6 insertions(+)
 create mode 100644 app1-prod.properties
 create mode 100644 app1-test.properties
 create mode 100644 app1.properties

$ git remote add origin https://gitee.com/Finen/tmp.git
fatal: remote origin already exists.

$ git push -u origin master
Counting objects: 5, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (5/5), 393 bytes | 393.00 KiB/s, done.
Total 5 (delta 0), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-3.0]
To https://gitee.com/Finen/tmp.git
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

6.配置强制拉去内容

## 强制拉去 Git 内容
spring.cloud.config.server.git.force-pull = true

7.重启应用

配置 Spring Cloud 配置客户端

  1. 创建 Spring Cloud Config Client 应用

    创建一个名为 spring-cloud-lesson-3-config-client 应用

  2. ClassPath 下面创建 bootstrap.properties

  3. 配置 bootstrap.properties

    配置 以spring.cloud.config. 开头配置信息

## 配置客户端应用关联的应用
## spring.cloud.config.name 是可选的
## 如果没有配置,采用 ${spring.application.name}
spring.cloud.config.name = segmentfault
## 关联 profile
spring.cloud.config.profile = prod
## 关联 label
spring.cloud.config.label = master
## 配置配置服务器URI
spring.cloud.config.uri = http://127.0.0.1:9090/

application.properties 信息

## 配置客户端应用名称
spring.application.name = spring-cloud-config-client

## 配置客户端应用服务端口
server.port = 8080

## 关闭管理端actuator 的安全
## /env /health 端口完全开放
management.security.enabled = false

​ 4.启动应用

[           main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at: http://127.0.0.1:9090/
[           main] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=segmentfault, profiles=[prod], label=master, version=15342a7ecdb59b691a8dd62d6331184cca3754f4, state=null
[           main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource [name='configService', proper

测试 Spring Cloud 配置客户端

通过浏览器访问 http://localhost:8080/env

image-20210226164930579