在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在spring cloud config 组件中,分两个角色,一是config server,二是config client。
先从服务端开始
引入pom依赖
<!--config的server端支持-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!--因为目前讲的都是eureka的注册中心所以当然也引入一下-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>

test.yml在config目录下面是我的测试文件
application.yml是配置的server的主要配置
application.yml
spring:
profiles:
active: native #必须有的 设置使用本地配置
cloud:
config:
server:
native:
searchLocations: classpath:/config #本地配置的路径 /resources/config
#给服务起一个名字
application:
name: server-config
server:
port: 8888 #端口号
test.yml
ak: 47
在启动类上修改
package com.tg.config_server;
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//开启configserver服务
@EnableEurekaClient //开启eureka的服务
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run( ConfigServerApplication.class, args );
}
}
客户端
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
yml文件
spring:
application:
name: test-dev
cloud:
config:
uri: http://localhost:8089
profile: dev #环境
name: test #文件前缀
#注册中心地址
eureka:
client:
service-url:
defaultZone: http://localhost:8876/eureka/eureka
#服务地址
server:
port: 9999
config-client客户端代码
package com.tg.config_client;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run( ConfigClientApplication.class, args );
}
@Value("${ak}")
private String host;
//写一个小demo获取test.yml中的数据
@RequestMapping(value = "/hi", method = RequestMethod.GET)
public String hi() {
return host;
}
}
浏览器进行访问 显示47证明是获取到了

cloud的采用Git仓库
其实比较之下也就是修改了一点点的yml配置
#================采用Git的方式仓库管理===============#
spring:
application:
name: config-server
#config的默认端口就是8888 不能修改 要修改只能修改yml名字为bootstrap.yml降低配置
cloud:
config:
server:
git:
uri: https://github.com/forezp/SpringcloudConfig/
#这个是个公共仓库 是没有密码的 所以我这里就不填写 大家可以使用 也可以自己新建一个仓库 https://github.com/仓库名称/项目名称/
password:
username:
search-paths: repo #配置是在repo文件夹下面的所以这样写
server:
port: 8888
#eureka的注册中心地址
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/eureka
#配置Git参数配置
客户端几乎不用修改
spring:
application:
name: zuul-dev
cloud:
config:
uri: http://localhost:8089
profile: dev # 环境 # dev 开发环境配置文件 | test 测试环境 | pro 正式环境
name: zuul #文件前缀
#注册中心地址
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/eureka
#服务地址
server:
port: 9999
java
@Value("${server.port}")
private String host;
@RequestMapping(value = "/hi", method = RequestMethod.GET)
public String hi() {
return "配置文件中的端口号" + host;
}

浏览器访问

注意点:采用本地仓库的话需要设置 spring.profiles.active=native 以及指定配置文件地址
spring.cloud.server.config.native.searchLocations=classpath:/config 本地资源路径地址
可以理解为2步:1.设置本地配置2.设置本地配置路径 Git配置也一样:1.设置git的配置信息 url username等 profile环境 name配置文件前缀等信息
网友评论