SpringCloud创建Config模块
1.说明
本文详细介绍Spring Cloud创建Config模块的方法,
基于已经创建好的Spring Cloud父工程,
请参考SpringCloud创建项目父工程,
创建Config模块这个子工程,
作为Spring Cloud的配置中心,
这个配置中心可以独立启动。
然后配合创建好的Eureka子工程,
请参考SpringCloud创建Eureka模块,
把Config服务注册到Eureka。
2.创建config模块
这一步创建一个Maven Module,
作为Spring Cloud的父工程下的一个子工程:
在父工程spring-cloud-demo上右键 -> New -> Other... -> Maven -> Maven Project
勾选Create a simple project(skip archetype selection),
输入Module Name:config-server,
查看Parent Project:spring-cloud-demo,
如果不是自己选择的父工程,请重新选择。
点击Finish完成工程创建。
3.添加依赖
在pom.xml中增加spring-cloud-config-server的依赖:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
4.新增配置文件
在src/main/resource目录下新增application.yml文件,
并且增加如下配置:
server:
port: 9009
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://gitee.com/bugzeroman/spring-cloud-config.git
主要是配置中心需要使用到的git仓库,
这里使用https协议的git地址。
如果使用git协议的地址,
需要配置ssh的私钥privateKey,
同时strictHostKeyChecking要为false,
否则会报错"reject HostKey : gitee.com",
参考配置如下:
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: git@gitee.com:bugzeroman/spring-cloud-config.git
strictHostKeyChecking: false
privateKey: |
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAuThLh+LHK0g7g/dJ4IUCyOYg7JNXQDGYt4khUYnJIw5pxX/a
... please input real id_rsa ...
UZp40gCDS64ve++Z/leNTMYH0YiAxNkcIp2Czvwm8P+hCkxvdgWgkbb5nuXfR1Gp
2OrvwOk/z076Wo3kTl0Oh+Tt9EB7bf1h2MC23QrRXUim8r0+Vjl3
-----END RSA PRIVATE KEY-----
5.新增主启动类
在src/main/java目录下新增主启动类,
Package:com.yuwen.spring.config
Name:ConfigApplication
然后修改ConfigApplication.java如下,
注意一定要有EnableConfigServer注解,
表示这是一个配置中心:
package com.yuwen.spring.config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}
6.git仓库
在上面我们指定了spring-cloud-config仓库的地址:
https://gitee.com/bugzeroman/spring-cloud-config.git
git@gitee.com:bugzeroman/spring-cloud-config.git
在这个仓库下面我们上传各种配置文件,
这样就可以通过配置中心config拉取到各种配置。
现在新增一个application.yml如下,
然后上传到仓库中即可。
spring:
profiles:
active:
- dev
---
# 开发环境
spring:
profiles: dev
application:
name: config-server-dev
---
# 测试环境
spring:
profiles: test
application:
name: config-server-test
7.启动config
右键主启动类ConfigApplication.java,
Run As ... -> Java Application
成功启动日志如下,
可以看到对外提供的服务端口是9009:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.1.RELEASE)
2020-09-09 15:53:28.680 INFO 19844 --- [ main] c.yuwen.spring.config.ConfigApplication : No active profile set, falling back to default profiles: default
2020-09-09 15:53:29.325 INFO 19844 --- [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=1d5d8597-77d3-3e14-9239-79e89f24c7a4
2020-09-09 15:53:29.825 INFO 19844 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 9009 (http)
2020-09-09 15:53:29.833 INFO 19844 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-09-09 15:53:29.833 INFO 19844 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.36]
2020-09-09 15:53:29.918 INFO 19844 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-09-09 15:53:29.918 INFO 19844 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1226 ms
2020-09-09 15:53:30.162 INFO 19844 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-09-09 15:53:31.160 INFO 19844 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 2 endpoint(s) beneath base path '/actuator'
2020-09-09 15:53:31.201 INFO 19844 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 9009 (http) with context path ''
2020-09-09 15:53:31.489 INFO 19844 --- [ main] c.yuwen.spring.config.ConfigApplication : Started ConfigApplication in 3.957 seconds (JVM running for 4.284)
2020-09-09 15:54:03.392 INFO 19844 --- [nio-9009-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-09-09 15:54:03.392 INFO 19844 --- [nio-9009-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2020-09-09 15:54:03.397 INFO 19844 --- [nio-9009-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 5 ms
2020-09-09 15:54:04.760 INFO 19844 --- [nio-9009-exec-1] o.s.c.c.s.e.NativeEnvironmentRepository : Adding property source: file:/C:/Users/yuwen/AppData/Local/Temp/config-repo-5990891065379764128/application.yml (document #0)
8.测试效果
在浏览器中输入如下URL,
分别对应不同的文本输出,
表名拉取到了仓库中对应的配置。
8.1.master下的application.yml
http://localhost:9009/master/application.yml
{
"name": "master",
"profiles": [
"application.yml"
],
"label": null,
"version": "c8a86decaab4b20d7470e09e6b9da50ed69d5d32",
"state": null,
"propertySources": [
{
"name": "https://gitee.com/bugzeroman/spring-cloud-config.git/application.yml (document #0)",
"source": {
"spring.profiles.active[0]": "dev"
}
}
]
}
8.2.开发环境
http://localhost:9009/master/application-dev.yml
spring:
application:
name: config-server-dev
profiles:
active:
- dev
8.3.测试环境
http://localhost:9009/master/application-test.yml
spring:
application:
name: config-server-test
profiles:
active:
- dev
9.Config服务注册到Eureka
SpringCloud创建Config模块的更多相关文章
- SpringCloud创建Config读取本地配置
1.说明 Config Server获取配置支持的方式很多, 包括Git仓库(github/gitee等),任何与JDBC兼容的数据库, Subversion,Hashicorp Vault,Cred ...
- SpringCloud创建Config Client配置读取
1.说明 本文详细介绍配置中心客户端使用方法, 即Config Client到Config Server读取配置, 这里以创建Config Client服务为例, 基于已经创建好的Config Ser ...
- SpringCloud创建Eureka模块集群
1.说明 本文详细介绍Spring Cloud创建Eureka模块集群的方法, 基于已经创建好的Spring Cloud Eureka Server模块, 请参考SpringCloud创建Eureka ...
- SpringCloud创建Gateway模块
1.说明 本文详细介绍Spring Cloud创建Gateway模块的方法, 基于已经创建好的Spring Cloud父工程, 请参考SpringCloud创建项目父工程, 和已经创建好的Eureka ...
- SpringCloud创建Config Client通过Eureka访问Config
1.说明 本文详细介绍配置中心客户端使用方法, 即Config Client到Config Server读取配置. 读取配置的方式有两种, 第一种是直接配置Configer Server的URL, 第 ...
- SpringCloud创建Eureka模块
1.说明 本文详细介绍Spring Cloud创建Eureka模块的方法, 基于已经创建好的Spring Cloud父工程, 请参考SpringCloud创建项目父工程, 在里面创建Eureka模块, ...
- SpringCloud创建Config多客户端公共配置
1.说明 基于已经创建好的Spring Cloud配置中心, 在配置中心仅保存一套配置文件, 多个客户端可以通过配置中心读取到相同的配置, 而不需要在每个客户端重复配置一遍, 下面以一个Config ...
- SpringCloud创建Eureka Client服务注册
1.说明 本文详细介绍微服务注册到Eureka的方法, 即Eureka Client注册到Eureka Server, 这里用任意一个Spring Cloud服务为例, 比如下面已经创建好的Confi ...
- SpringCloud之Config
1.背景 在前的学习中,我们几乎解决了springCloud的所有常规应用,但是大家有没有想过这样一个问题: 是使用微服务后,有非常多的application.yml文件,每个模块都有一个,实际开发中 ...
随机推荐
- Linux基础命令---mget获取ftp文件
mget 使用lftp登录mftp服务器之后,可以使用mget指令从服务器获取文件.mget指令可以使用通配符,而get指令则不可以. 1.语法 mget [-E] [-a] [- ...
- SpringMVC(3):AJAX
一,AJAX 简介 AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML) AJAX 不是新的编程语言,而是一种使用现有标准的新方法 ...
- 【Java 8】函数式接口(二)—— 四大函数接口介绍
前言 Java8中函数接口有很多,大概有几十个吧,具体究竟是多少我也数不清,所以一开始看的时候感觉一脸懵逼,不过其实根本没那么复杂,毕竟不应该也没必要把一个东西设计的很复杂. 几个单词 在学习了解之前 ...
- synchronized底层浅析(一)
之前说过hashMap,我们知道hashMap是一种非线程安全的集合,主要原因是它在多线程的情况下,插入.删除.扩容的时候容易导致数据丢失或者链表环 那我们也知道ConcurrentHashMap.h ...
- 【C/C++】金币
做了一下去年的题目,今年看起来就没这么难了 从上到下的可以从下到上考虑,会简单很多,dp入门 题目 金币 小招在玩一款游戏,在一个N层高的金字塔上,以金字塔顶为第一层,第i层有i个落点,每个落点有若干 ...
- js--生成器总结
前言 生成器gengrator是es6 新增的函数功能,它允许你定义一个包含自有迭代算法的函数, 同时它可以自动维护自己的状态. 本文来总结一下JavaScript 中生成器的相关知识点. 正文 1. ...
- 【简】题解 P5283 [十二省联考2019]异或粽子
传送门:P5283 [十二省联考2019]异或粽子 题目大意: 给一个长度为n的数列,找到异或和为前k大的区间,并求出这些区间的异或和的代数和. QWQ: 考试时想到了前缀异或 想到了对每个数按二进制 ...
- [BUUCTF]REVERSE——[FlareOn4]IgniteMe
[FlareOn4]IgniteMe 附件 步骤: 例行检查,32位程序,无壳 32位ida载入 当满足第10行的if条件时,输出G00d j0b!提示我们成功,看一下sub_401050函数 3.s ...
- 36、有效的数独 | 算法(leetode,附思维导图 + 全部解法)300题
零 标题:算法(leetode,附思维导图 + 全部解法)300题之(36)有效的数独 前言 1)码农三少 ,一个致力于 编写极简.但齐全题解(算法) 的博主. 2)文末附赠 价值上百美刀 资料. 一 ...
- LuoguP1785 漂亮的绝杀 题解
Content 因太占排版,请自己去题面查看. Solution 声明:以下和题面相同的变量的意义均和题面相同. 这个题目 \(\texttt{if}\) 操作很多,其他的就是纯模拟. 首先,我们先判 ...