3 为什么要使用 spring cloud config 配置中心?
4 spring cloud config配置中心,它解决了什么问题?
===========================================================================
1.1 创建服务端项目
创建一个SpringCloud项目。
1.2 修改pom文件
我们需要添加config-server的依赖,具体如下
<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”>
4.0.0
org.springframework.boot
spring-boot-starter-parent
1.5.13.RELEASE
com.bobo
config-server
0.0.1-SNAPSHOT
org.springframework.cloud
spring-cloud-dependencies
Dalston.SR5
pom
import
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.cloud
spring-cloud-config-server
org.springframework.boot
spring-boot-maven-plugin
1.3 修改配置文件
在此处的配置文件中我们需要关联码云或者GitHub。以码云为例
码云处理
首先我们需要在码云上注册一个账号(https://gitee.com) 然后创建一个新的项目。
配置文件处理
在配置文件中添加如下配置
spring.application.name=config-server
server.port=9050
设置服务注册中心地址,指向另一个注册中心
eureka.client.serviceUrl.defaultZone=http://dpb:123456@eureka1:8761/eureka/,http://dpb:123456@eureka2:8761/eureka/
#Git 配置
spring.cloud.config.server.git.uri=https://gitee.com/dengpbs/config
#spring.cloud.config.server.git.username=
#spring.cloud.config.server.git.password=
创建四个配置文件
四个配置文件都有一个e-book属性,只是值不一样。然后将这个四个配置文件上传到码云中我们新创建的仓库
然后将项目中的四个配置文件删除
1.4 修改启动类
我们需要在启动类中添加eureka客户端和config服务端的注解,具体如下:
@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class ConfigServerStart {
public static void main(String[] args) {
SpringApplication.run(ConfigServerStart.class, args);
}
}
1.5 访问测试
启动服务,访问测试
http://localhost:9050/config-client/test
http://localhost:9050/config-client/default
http://localhost:9050/config-client/dev
通过访问,我们获取到了位于码云仓库中的属性信息。
1.6 配置文件的命名规则与访问
注意,上面案例中的配置文件的名称,并不是随便命名的,而是有一定的规则来约束的,具体如下:
2.1 创建项目
创建一个SpringCloud项目
2.2 pom文件修改
配置中心的客户端使用的依赖需要注意,不是config-server了,具体如下:
<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”>
4.0.0
org.springframework.boot
spring-boot-starter-parent
1.5.13.RELEASE
com.bobo
config-client
0.0.1-SNAPSHOT
org.springframework.cloud
spring-cloud-dependencies
Dalston.SR5
pom
impo
【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】
浏览器打开:qq.cn.hn/FTf 开源分享
rt
org.springframework.boot
spring-boot-starter-web