spring cloud备忘笔记-7-分布式配置中心
笔记索引:
spring cloud备忘笔记-0-目录索引
为什么要用分布式的配置中心?
因为分布式系统中,我们如过服务的数量很多(100多-几千上万个微服务),就需要对服务配置文件进行统一管理,实时更新
。(云配置
)为了方便完成这一功能,Spring Cloud提供了分布式配置中心组件 Spring Cloud Config .其他别的配置管理中心如:Apollo(阿波罗)
了解Spring Cloud Config
Spring Cloud Config支持配置服务放在配置服务的内存中(即本地),也支持放在远程 Git 仓库中。
这个组件本身分为服务端和客户端。
学习更多:点击进入官网
如何使用Spring Cloud Config
服务端
用于管理所有的配置文件。
依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
pom文件
<?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>
<parent>
<groupId>com.momomian</groupId>
<artifactId>hi-spring-cloud-dependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../hi-spring-cloud-dependencies/pom.xml</relativePath>
</parent>
<artifactId>hi-spring-cloud-config</artifactId>
<packaging>jar</packaging>
<name>hi-spring-cloud-config</name>
<inceptionYear>2019-Now</inceptionYear>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.momomian.hi.spring.cloud.config.ConfigApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
注意这里的端口要是88881,否则服务端运行不了。
配置文件中指定git仓库,这里配置uri是我们存放配置文件的仓库地址(github的地址),search-paths是该地址下的路径。用户名密码是github的用户名密码。label是分支,用的是中央集中式管理(中心化),就一个master。
这里我们使用GitLab,新建一个项目,名为:spring-cloud-config,分布式配置中心。
添加自述文件README.md(初始化仓库)
将项目的SSH地址2复制下来,克隆[^3]到我们的代码(找到项目文件夹右击git clone进去)中,
注意这里有ssh方式和http方式。我们应该把http方式的地址配置到hi-spring-cloud-config的application.yml中。
项目中就会出现这个文件夹,用于放配置。
创建一个文件夹仓库:respo
.所有的配置就往里面扔,上传到GitLab.在SSH地址那边将SSH换成HTTP的方式将地址复制下来,放到我们的application.yml得uri中如:http://ip/xxx/spring-cloud-config.git
之后我们就将之前服务里写的application.yml配置文件复制到spring-cloud-config得respo包里,由于名字不能重名,要改掉,改成项目得名字,如hi-spring-cloud-service-admin这个项目的配置文件复制过来,并且我们就将其名字改为service-admin.yml即可。git提交即可。(也可以用码云)
application.yml
spring:
application:
name: hi-spring-cloud-config
cloud:
config:
label: master
server:
git:
uri: https://git.lug.ustc.edu.cn/xxx/spring-cloud-config.git
search-paths: respo
username:xxx
password:xxx
server:
port: 8888
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8780/eureka/
主入口中增加@EnableConfigServer注解,这是一个服务提供者。要注册到Eureka。
ConfigApplication.java
package com.momomian.hi.spring.cloud.config;
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;
@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class,args);
}
}
服务端测试
之后先启动eureka,再启动这个项目,如果能够访问到仓库里的配置,就成功了。访问地址:
localhost:8888/service-admin/master
其中service-admin是你访问的哪个配置文件名
客户端
服务端ok,那我们要通过客户端去取出我们托管的配置文件。
以这个项目为例:hi-spring-cloud-service-admin
我们已经把它的配置文件托管给云了,下面就要在这个项目中取到配置文件的信息。
添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
我们现在的所有配置都是基于开发环境的。实际开发中,我们的环境还有测试环境和生产环境。那个时候我们的eureka是单独部署在一台服务器上的。
实际环境
- 开发环境-本地服(或局域网)dev
- 测试环境-测服 test
- 生产环境-正服 prod
我们不可能每次手动去改配置,就要分开配置。我们可以使用spring boot profile
来区分配置文件,同时我们的配置文件名也需要加以区分,service-admin.yml改为service-admin-dev.yml(dev表示开发环境)
我们在service-admin项目中的application.yml添加如下配置:。
spring:
application:
name: hi-spring-cloud-service-admin
cloud:
config:
uri: http://localhost:8888
name:service-admin
label: master
profile: dev
注意,配置文件名为service-admin-dev.yml,name配service-admin,profile配dev。
如果再配置一下生产环境的,我们再创建一个配置文件-prod,把端口改掉即可。再在service-admin的项目中的配置文件application.yml:把profile: dev改成profile: prod即可。
但是我们实际上以后在实际开发的时候不去手动改,而是写两个配置文件:
application.yml
application-prod.yml
然后在启动项目的时候直接是运行jar包指定使用哪个配置文件:
启动项目时只需要增加一个命令参数 --spring.profiles.active=环境配置
java -jar hello-spring-cloud-service-admin-1.0.0-SNAPSHOT.jar --spring.profiles.active=prod
客户端测试
这样我们创建一个 Controller 来测试一下通过远程仓库的配置文件注入 一个属性比如:demo,提交上传到gitlab去。
然后在service-admin项目中写一个接口,去拿这个配置,如果成功拿到,即可。
注意:之前我们名字改掉了,测试服务端的配置文件要用localhost:8888/service-admin/dev/master
TestConfigController .java
package com.momomian.hi.spring.cloud.service.admin.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestConfigController {
@Value("${demo}")
private String demo;
@RequestMapping(value = "/demo", method = RequestMethod.GET)
public String getDemo() {
return demo;
}
}
笔记索引:
spring cloud备忘笔记-0-目录索引
如果想改端口8888,可以写一个配置文件bootstrap.properties(或yml)文件写server.port=。就可以改了。因为spring boot正常的启动时bootstrap配置文件是会优先加载的。 ↩︎
如果没有SSH地址需要自己添加:>打开本地git bash,使用如下命令生成ssh公钥和私钥对
ssh-keygen -t rsa -C 'xxx@xxx.com'
然后一路回车(-C 参数是你的邮箱地址)
之后在你的用户目录打开~/.ssh/id_rsa.pub
文件(表示用户目录,比如我的windows就是C:\Users\Administrator),复制其中的内容(用记事本打开即可)
打开gitlab,找到Profile Settings-->SSH Keys--->Add SSH Key
,并把上一步中复制的内容粘贴到Key所对应的文本框,在Title对应的文本框中给这个sshkey设置一个名字,点击Add key按钮
[^3]右击没有克隆的,用命令:先进入目录,然后给git clone 复制的ssh地址
↩︎