• 沒有找到結果。

1.6 使用微服务引擎功能

1.6.2 使用配置中心

1.6.2.1 配置中心概述

配置中心用来管理微服务应用的配置。微服务连接配置中心,能够从配置中心获取配 置信息及其变化。配置中心还是其他微服务管控功能的核心部件,比如服务治理规则 的下发,也是通过配置中心实现的。

微服务引擎支持的配置中心有:config-center。

本章节介绍不同微服务开发框架使用配置中心的一些开发细节,包括如何配置依赖、

连接配置中心有关的配置项等,并简单的介绍微服务应用中如何读取配置和响应配置 变化。

微服务引擎

微服务引擎使用config-center作为配置中心。微服务默认会读取配置中心全局配置、

服务配置。全局配置指环境和微服务相同的配置;服务配置指环境、应用、微服务名 称和微服务相同的配置。

微服务引擎只支持key-value的配置项。如果用户需要使用yaml格式的配置文件,可以 使用具体SDK提供的fileSource功能。通过在配置文件中指定fileSource的key列表,

SDK会将这些key对应的value全部当成yaml解析。以Spring Cloud为例,在 bootstrap.yml中增加配置项:

spring:

cloud:

servicecomb:

config:

fileSource: file1.yaml,file2.yaml

并且在配置中心创建配置,“配置项”和“值”如下所示:

file1.yaml: |

cse.example.key1: value1 cse.example.key2: value2 file2.yaml: |

cse.example.key3: value3 cse.example.key4: value4

配置创建方法请参考配置微服务中的“添加配置”操作。

应用程序中会发现4个配置项:cse.example.key1=value1,

cse.example.key2=value2,cse.example.key3=value3和cse.example.key4=value4。

1.6.2.2 Java Chassis 使用配置中心

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

<dependency>

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

<artifactId>config-cc</artifactId>

</dependency>

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

1-9 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.config.clie

nt.serverUri 访问地址,格式为 http(s)://{ip}:{port},以

nt.refreshMode 配置更新模式,0为主动 push,1为微服务周期 pull

0 config-center

servicecomb.config.clie

nt.refreshPort 主动push端口 30104 config-center servicecomb.config.clie

nt.tenantName 应用的租户名称 default config-center

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和动态配置的值。

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

1.6.2.3 Spring Cloud 使用配置中心

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

<dependency>

<groupId>com.huaweicloud</groupId>

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

</dependency>

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

1-10 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 配置中心类型,可 选值: config-center spring.cloud.servicecomb.c

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

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

-

-配置项 含义 缺省值 备注 spring.cloud.servicecomb.c

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

分割

- 主要用途是config-center支持yaml文 件,配置项的yaml 内容被解析,然后 将yaml的配置项值 设置到配置系统里 面。

spring.cloud.servicecomb.c

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

隔(毫秒) 10000 config-center

config-center是一个配置管理器,支持使用key-value的格式添加配置项。对于Spring Cloud用户,经常需要在配置中心增加yaml格式的配置文件。Spring Cloud Huawei提 供了配置项spring.cloud.servicecomb.config.fileSource,使得用户在使用config-center的情况下,也能够配置yaml格式的配置文件。这个配置项的值是key-value系统 的key列表,多个key以逗号分隔,这些key的值是yaml格式的文本内容,Spring Cloud Huawei会对这些key的值进行特殊处理和解析。

接入配置中心以后,Spring Cloud应用可以采用@Value、@ConfigurationProperties 等标签的方式注入配置,也可以使用Environment读取配置,并且使用@RefreshScope 来支持配置的动态更新,详细内容请参考社区开发指南。

1.6.2.4 Dubbo 使用配置中心

Dubbo当前版本只支持config-center。Dubbo使用配置中心,需要在项目中增加如下 依赖:

<dependency>

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

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

</dependency>

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

1-11 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 配置中心地

址 - 集群地址使用“,”

分割 dubbo.servicecomb.config.t

ype 配置中心类

型 config-center 可选值:config-center

dubbo.servicecomb.config.f

ileSource 内容为yaml 的配置项列 表,使用“,”分割

- 主要用途是config-center支持yaml文

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

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