Spring Cloud微服务(6)之spring cloud config分布式配置中心

1.简介

spring cloud config 是 spring cloud 框架当中的配置管理工具包,让用户可以吧配置放到远程服务器上,集中化管理集群配置。spring cloud config 是云端存储配置信息

的,具有中心化、版本控制、支持动态更新、平台独立、语言独立等特性。 spring cloud config 可以从SVN、GIT 或文件资源中获取配置,并支持动态更新,无须重启等特性。


2.如何使用。

spring cloud config 目前支持三种存储方式:本地资源、SVN和GIT。

本地资源存储方式如何使用。

第一步:创建 cofig sever ,其他服务通过配置服务读取本地配置文件的内容。

(1)首选创建配置服务工程 config-service。

目录结构如图:


(2)pom.xml文件增加依赖。

<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</artifactId>
		</dependency>

(3)启动类增加 @EnableConfigServer 注解,开启 Config Server功能。

package com.hole;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;

@EnableConfigServer
@SpringBootApplication
@EnableDiscoveryClient
public class ConfigServiceApplication {

	public static void main(String[] args) {
		SpringApplication.run(ConfigServiceApplication.class, args);
	}
}
(4)application.properties 配置注册地址及服务信息。

spring.application.name=config-service
server.port=8888
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
eureka.instance.lease-renewal-interval-in-seconds=50
eureka.instance.lease-expiration-duration-in-seconds=30
spring.profiles.active=native

spring.profiles.active=native 表示采用本地存储配置方式。


(5)资源文件 orderservice-local.properties 内容为 

configParam=local development

资源文件命名按照如下规范命名。

/{application}-{profile}-yml

/{label}/{application}-{profile}.yml

/{application}-{profile}.properties

/{label}/{application}-{profiel}.propertites

(6)启动成功后按下面格式发起请求。

http://localhost:8888/{appname}/{env}/{label},其中label可以省略。

例如本例中为:http://localhost:8888/orderservice/local

返回结果:



第二步:创建config client ,通过配置中心获取配置参数,本例中也就是获取 cofigParam的值。


(1)创建配置客户端工程。

工程目录如下:


(2)pom.xml 中增加依赖

<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>

(3)启动代码:

package com.hole;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableDiscoveryClient
@RefreshScope
@EnableFeignClients
@RestController
public class OrderConfigServiceApplication {

	@Value("${configParam}")
	private String configParam;

	public static void main(String[] args) {
		SpringApplication.run(OrderConfigServiceApplication.class, args);
	}

	@RequestMapping(value = "/configParam")
	public String configParam(){
		return this.configParam;
	}

	@RequestMapping(value = "/hello",method = RequestMethod.GET)
	public ResponseEntity<String> hello(){
		return new ResponseEntity<String>("hello order config service!", HttpStatus.OK);
	}

	public String getConfigParam() {
		return configParam;
	}

	public void setConfigParam(String configParam) {
		this.configParam = configParam;
	}
}

增加了 @RefreshScope 和 @Value 注解,直接引用外部的配置,这样就可以通过 rest请求方式来获取配置参数。

(4)bootstrap.properties 配置如下。

spring.application.name=orderservice
server.port=3332
spring.cloud.config.profile=local
spring.cloud.config.uri=http://localhost:8888


其中 spring-application.name 和 spring.cloud.config.profile 值对应配置服务器上资源文件名称(命名规则)

spring.cloud.config.uie 指向配置服务地址

(5)启动并验证

成功启动服务后访问 客户端服务的 /configParam 就可以获取到配置。



GIT 远程存储方式使用

GIT配置存储和本地差不多,只需稍作修改下就可以了,so easy,这里就不再赘述了。


猜你喜欢

转载自blog.csdn.net/u013084910/article/details/76460343