在分布式系统中,由于访服务数量很多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件,在Spring Cloud中,有分布式配置中心组件Spring Cloud Config,它支持配置服务放在配置服务的内存中(本地)。也支持放在远程Git仓库中,在Spring Cloud Config组件中,分两个角色,一是Config Server,二时Config Client。
分布式配置中心服务端
创建一个工程名为hello-spring-cloud-config的项目
pom.xml
<?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.funtl</groupId>
<artifactId>hello-spring-cloud-dependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../hello-spring-cloud-dependencies/pom.xml</relativePath>
</parent>
<artifactId>hello-spring-cloud-config</artifactId>
<packaging>jar</packaging>
<name>hello-spring-cloud-config</name>
<url>http://www.funtl.com</url>
<inceptionYear>2018-Now</inceptionYear>
<dependencies>
<!-- Spring Boot Begin -->
<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>
<!-- Spring Boot End -->
<!-- Spring Cloud Begin -->
<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>
<!-- Spring Cloud End -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.funtl.hello.spring.cloud.config.ConfigApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
主要增加了注解@EnableConfigServer
package com.funtl.hello.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);
}
}
application.yml
spring:
application:
name: hello-spring-cloud-config
cloud:
config:
label: master #分支
server:
git:
uri: https://gitee.com/***/spring-cloud-config.git
search-paths: respo #项目
username: *** #gitlab仓库账号
password: *** #gitlab仓库密码
server:
port: 8888 #必须为8888除非创建bootstarp.properties文件
#在其中添加server.port=需要设置的端口号
#bootstrap.properties是spring boot的默认文件
#优先级较高
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
相关配置说明:
spring.cloud.config.label:配置仓库的分支
spring.cloud.config.server.git.uri:配置Git仓库地址
spring.cloud.config.server.git.search-paths:配置仓库路径
spring.cloud.config.server.git.username:访问Git仓库的账号
spring.cloud.config.server.git.password:访问Git仓库的密码
注意事项:
如果是使用GitLab作为仓库,spring.cloud.config.server.git.uri的末尾需要加上.git
新建一个仓库spring-cloud-config,然后创建respo目录,复制feign服务的application.yml并上传。
服务测试
启动后浏览器访问 http://localhost:8888/web-admin/feign/master
出现这个页面表明配置中心服务端配置成功。
分布式配置中心客户端
以Feign服务为例,其他服务也是主要操作。
主要增加了spring-cloud-starter-comfig依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
application.yml
修改配置文件为:
spring:
cloud:
config:
uri: http://localhost:8888
name: web-admin-feign
label: master
profile: dev
相关配置说明:
spring.cloud.config.uri:配置服务中心的地址
spring.cloud.config.name:配置文件的前缀
spring.cloud.config.label:配置仓库的分支
spring.cloud.config.profile:配置文件的环境标识
dev:表示开发环境
test:表示测试环境
prod:表示生成环境
修改配置文件仓库中的Feign的配置文件为
开发环境:web-admin-feign-dev.yml
spring:
application:
name: hello-spring-cloud-web-admin-feign
thymeleaf:
cache: false
mode: LEGACYHTML5
encoding: UTF-8
servlet:
content-type: text/html
server:
port: 8765
feign:
hystrix:
enabled: true
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/ #Eureka服务注册中心地址
生产环境:web-admin-feign-prod.yml
spring:
application:
name: hello-spring-cloud-web-admin-feign
thymeleaf:
cache: false
mode: LEGACYHTML5
encoding: UTF-8
servlet:
content-type: text/html
server:
port: 7765
feign:
hystrix:
enabled: true
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/ #Eureka服务注册中心地址
关注我
觉得有点东西就点一下“赞和在看”吧!感谢大家的支持了!