• 沒有找到結果。

(以Spring Cloud为例):

spring:

1. 把配置中心当成数据库的一个表tbl_configurations,key是主键,每个label都是 属性。

2. 客户端会根据如下3个条件查询配置:

– 自定义配置

select * from tbl_configurations where

customLabel=customLabelValue & match=false – 应用级配置

select * from tbl_configurations where app=demo_app &

environment=demo_environment & match=true – 服务级配置

select * from tbl_configurations where app=demo_app &

environment=demo_environment & service=demo_service &

match=true

其中,match为true的时候,表示有且只有条件里面指定的属性;match为false的 时候,表示除了条件里面的属性,允许有其他的属性。还可以给标签app指定多个

开发指南 6 使用微服务引擎功能

应用,或者给标签service指定多个服务,这样配置项就可以对多个服务和应用生 效,非常灵活。

微服务引擎的TEXT、XML等类型,SDK会简单的当成key-value对使用;YAML和 Properties类型, SDK会解析内容,应用程序将内容作为实际的应用程序配置项。比 如:

类型:TEXT

key: cse.examples.hello value: World

应用程序会发现1个配置项: cse.examples.hello = World。

类型:YAML

key: cse.examples.hello value: |

cse:

key1: value1 key2: value2

应用程序会发现2个配置项: cse.key1 = value1和cse.key2 = value2。

6.2.2 Java Chassis 使用配置中心

Java Chassis使用以kie命名的配置中心,需要在项目中增加如下依赖:

<dependency>

<groupId>org.apache.servicecomb</groupId>

<artifactId>config-kie</artifactId>

</dependency>

如果项目已经直接或者间接包含如上依赖,则无需添加。Java Chassis包含如表6-5所 示配置项,这些配置项的值指定了微服务在配置中心的身份,以及微服务和配置中心 之间的交互。

6-5 Java Chassis 常用配置项

配置项 含义 缺省值 备注

servicecomb.service.ap

plication 所属应用 default

-servicecomb.service.na

me 微服务名称 defaultMicros

ervice -servicecomb.service.ver

sion 微服务版本 1.0.0.0

-servicecomb.service.env

ironment 环境 - production,

development 等

servicecomb.kie.server

Uri kie访问地址,格式为

http(s)://{ip}:{port},

以“,”分隔多个地址

- kie

servicecomb.kie.enable

LongPolling 长轮询(Long

Polling)模式是否开启 true kie

开发指南 6 使用微服务引擎功能

配置项 含义 缺省值 备注 servicecomb.kie.firstRef

reshInterval 首次刷新配置项的时间

间隔,单位为毫秒 3000 kie servicecomb.kie.refresh

_interval 刷新配置项的时间间

隔,单位为毫秒 3000 kie

servicecomb.kie.domai

nName 应用的租户名称 default kie

Java Chassis有多种方式可以读取动态配置,第一种是使用archaius API,例子如下:

DynamicDoubleProperty myprop = DynamicPropertyFactory.getInstance() .getDoubleProperty("trace.handler.sampler.percent", 0.1);

archaius API支持callback处理配置变更:

myprop.addCallback(new Runnable() { public void run() {

// 当配置项的值变化时,该回调方法会被调用

System.out.println("trace.handler.sampler.percent is changed!");

} });

第二种方式是使用Java Chassis提供的配置注入机制,使用这种方式能够非常简单的处 理复杂配置,和配置优先级,例子如下:

@InjectProperties(prefix = "jaxrstest.jaxrsclient") public class Configuration {

/*

* 1) jaxrstest.jaxrsclient.override.high * 2) jaxrstest.jaxrsclient.override.low *

* 测试用例:

* jaxrstest.jaxrsclient.override.high: hello high * jaxrstest.jaxrsclient.override.low: hello low * 预期:

* hello high */

@InjectProperty(prefix = "jaxrstest.jaxrsclient.override", keys = {"high", "low"}) public String strValue;

执行注入:

ConfigWithAnnotation config = SCBEngine.getInstance().getPriorityPropertyManager() .createConfigObject(Configuration.class,

"key", "k");

第三中方式在和Spring、Spring Boot集成的时候使用,可以按照Spring、Spring Boot 的原生方式读取配置,比如@Value、@ConfigurationProperties。Java Chassis将配置 层次应用于Spring Environment中,Spring和Spring Boot读取配置的方式,也能够读 取到microservice.yaml和动态配置的值。

开发指南 6 使用微服务引擎功能

有关Java Chassis读取配置的更多内容,请参考社区开发指南。

6.2.3 Spring Cloud 使用配置中心

Spring Cloud使用配置中心,需要在项目中增加如下依赖:

<dependency>

<groupId>com.huaweicloud</groupId>

<artifactId>spring-cloud-starter-huawei-config</artifactId>

</dependency>

如果项目已经直接或者间接包含这个依赖,则无需添加。Spring Cloud包含如表6-6所 示配置项,这些配置项的值指定了微服务在配置中心的身份,以及微服务和配置中心 之间的交互。

6-6 Spring Cloud 常用配置项

配置项 含义 缺省值 备注

spring.cloud.servicecomb.d

iscovery.appName 所属应用 default -spring.cloud.servicecomb.d

iscovery.serviceName 微服务名称 - 如果没有,使用 spring.application.

name spring.cloud.servicecomb.d

iscovery.version 微服务版本 -

-server.env 环境 - production,

development等 spring.cloud.servicecomb.c

onfig.enabled 是否启用动态配置 true -spring.cloud.servicecomb.c

onfig.serverType 配置中心类型

config-center 需要设置为kie spring.cloud.servicecomb.c

onfig.serverAddr 访问地址,格式为 http(s)://{ip}:

{port},以“,”分隔 多个地址

-

-spring.cloud.servicecomb.c

onfig.fileSource 内容为yaml的配置 项列表,使用“,”

分割

- kie直接支持yaml 格式的配置项。

spring.cloud.servicecomb.c

onfig.watch.delay pull模式轮询时间间

隔(毫秒) 10000 kie spring.cloud.servicecomb.c

onfig.enableLongPolling 是否开启长轮询

(Long Polling) true kie spring.cloud.servicecomb.c

接入配置中心以后,Spring Cloud应用可以采用@Value、@ConfigurationProperties

<groupId>com.huaweicloud.dubbo-servicecomb</groupId>

<artifactId>dubbo-servicecomb-config-center</artifactId>

</dependency>

如果项目已经直接或者间接包含这个依赖,则无需添加。Dubbo ServiceComb包含如 表6-7所示配置项,和配置中心有关的信息,需要在“dubbo.properties”配置。

6-7 Dubbo 常用配置项

配置项 含义 缺省值 备注

dubbo.servicecomb.service.

application 所属应用 default -dubbo.servicecomb.service.

name 微服务名称 defaultMicros erviceName -dubbo.servicecomb.service.

version 微服务版本 1.0.0.0 -dubbo.servicecomb.service.

environment 环境 - production,

development等 dubbo.servicecomb.config.

address 配置中心地

址 - 集群地址使用“,”

型 config-center 需要设置为kie

接入配置中心以后,可以采用Spring、Spring Boot原生的方式读取配置,比如

@Value、@ConfigurationProperties等标签的方式注入配置,也可以使用

相關文件