From e919a71a573d67d6a88496362dcdf67e0a785682 Mon Sep 17 00:00:00 2001 From: flystar32 Date: Tue, 18 Dec 2018 20:17:50 +0800 Subject: [PATCH] update docs --- .../src/main/asciidoc-zh/acm.adoc | 1 - .../src/main/asciidoc-zh/ans.adoc | 1 - .../asciidoc-zh/dependency-management.adoc | 4 - .../src/main/asciidoc-zh/nacos-config.adoc | 1 - .../src/main/asciidoc-zh/nacos-discovery.adoc | 1 - .../src/main/asciidoc-zh/oss.adoc | 1 - .../src/main/asciidoc-zh/rocketmq.adoc | 250 ------------ .../src/main/asciidoc-zh/schedulerx.adoc | 131 ------- .../src/main/asciidoc-zh/sentinel.adoc | 318 --------------- .../asciidoc-zh/spring-cloud-alibaba.adoc | 32 -- .../src/main/asciidoc/acm.adoc | 186 ++++++++- .../src/main/asciidoc/ans.adoc | 112 +++++- .../main/asciidoc/dependency-management.adoc | 34 ++ .../src/main/asciidoc/nacos-config.adoc | 361 ++++++++++++++++++ .../src/main/asciidoc/nacos-discovery.adoc | 316 +++++++++++++++ .../src/main/asciidoc/oss.adoc | 158 +++++++- .../src/main/asciidoc/rocketmq.adoc | 251 +++++++++++- .../src/main/asciidoc/schedulerx.adoc | 131 +++++++ .../src/main/asciidoc/sentinel.adoc | 336 ++++++++++++++++ .../main/asciidoc/spring-cloud-alibaba.adoc | 6 +- .../config/AbstractDataSourceProperties.java | 3 + .../DataSourcePropertiesConfiguration.java | 3 + 22 files changed, 1890 insertions(+), 747 deletions(-) delete mode 100644 spring-cloud-alibaba-docs/src/main/asciidoc-zh/acm.adoc delete mode 100644 spring-cloud-alibaba-docs/src/main/asciidoc-zh/ans.adoc delete mode 100644 spring-cloud-alibaba-docs/src/main/asciidoc-zh/dependency-management.adoc delete mode 100644 spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-config.adoc delete mode 100644 spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-discovery.adoc delete mode 100644 spring-cloud-alibaba-docs/src/main/asciidoc-zh/oss.adoc delete mode 100644 spring-cloud-alibaba-docs/src/main/asciidoc-zh/rocketmq.adoc delete mode 100644 spring-cloud-alibaba-docs/src/main/asciidoc-zh/schedulerx.adoc delete mode 100644 spring-cloud-alibaba-docs/src/main/asciidoc-zh/sentinel.adoc delete mode 100644 spring-cloud-alibaba-docs/src/main/asciidoc-zh/spring-cloud-alibaba.adoc diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/acm.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/acm.adoc deleted file mode 100644 index 488baab6..00000000 --- a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/acm.adoc +++ /dev/null @@ -1 +0,0 @@ -== Spring Cloud AliCloud ACM diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/ans.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/ans.adoc deleted file mode 100644 index b9b34ce2..00000000 --- a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/ans.adoc +++ /dev/null @@ -1 +0,0 @@ -== Spring Cloud AliCloud ANS diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/dependency-management.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/dependency-management.adoc deleted file mode 100644 index d3f0d885..00000000 --- a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/dependency-management.adoc +++ /dev/null @@ -1,4 +0,0 @@ -== 依赖管理 - -Spring Cloud Alibaba BOM 包含了它所使用的所有依赖的版本。 - diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-config.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-config.adoc deleted file mode 100644 index e933cff8..00000000 --- a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-config.adoc +++ /dev/null @@ -1 +0,0 @@ -== Spring Cloud Alibaba Nacos Config diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-discovery.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-discovery.adoc deleted file mode 100644 index 21a1177b..00000000 --- a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-discovery.adoc +++ /dev/null @@ -1 +0,0 @@ -== Spring Cloud Alibaba Nacos Discovery diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/oss.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/oss.adoc deleted file mode 100644 index 20b43f75..00000000 --- a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/oss.adoc +++ /dev/null @@ -1 +0,0 @@ -== Spring Cloud AliCloud OSS diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/rocketmq.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/rocketmq.adoc deleted file mode 100644 index 3724fd41..00000000 --- a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/rocketmq.adoc +++ /dev/null @@ -1,250 +0,0 @@ -== Spring Cloud Alibaba RocketMQ Binder - -### RocketMQ 介绍 - -https://rocketmq.apache.org[RocketMQ] 是一款开源的分布式消息系统,基于高可用分布式集群技术,提供低延时的、高可靠的消息发布与订阅服务。同时,广泛应用于多个领域,包括异步通信解耦、企业解决方案、金融支付、电信、电子商务、快递物流、广告营销、社交、即时通信、移动应用、手游、视频、物联网、车联网等。 - -具有以下特点: - -* 能够保证严格的消息顺序 - -* 提供丰富的消息拉取模式 - -* 高效的订阅者水平扩展能力 - -* 实时的消息订阅机制 - -* 亿级消息堆积能力 - -### RocketMQ 基本使用 - -* 下载 RocketMQ - -下载 https://www.apache.org/dyn/closer.cgi?path=rocketmq/4.3.2/rocketmq-all-4.3.2-bin-release.zip[RocketMQ最新的二进制文件],并解压 - -解压后的目录结构如下: - -``` -apache-rocketmq -├── LICENSE -├── NOTICE -├── README.md -├── benchmark -├── bin -├── conf -└── lib -``` - -* 启动 NameServer - -```bash -nohup sh bin/mqnamesrv & -tail -f ~/logs/rocketmqlogs/namesrv.log -``` - -* 启动 Broker - -```bash -nohup sh bin/mqbroker -n localhost:9876 & -tail -f ~/logs/rocketmqlogs/broker.log -``` - -* 发送、接收消息 - -发送消息: - -```bash -sh bin/tools.sh org.apache.rocketmq.example.quickstart.Producer -``` - -发送成功后显示:`SendResult [sendStatus=SEND_OK, msgId= ...` - -接收消息: - -```bash -sh bin/tools.sh org.apache.rocketmq.example.quickstart.Consumer -``` - -接收成功后显示:`ConsumeMessageThread_%d Receive New Messages: [MessageExt...` - -* 关闭 Server - -```bash -sh bin/mqshutdown broker -sh bin/mqshutdown namesrv -``` - -### Spring Cloud Stream 介绍 - -Spring Cloud Stream 是一个用于构建基于消息的微服务应用框架。它基于 SpringBoot 来创建具有生产级别的单机 Spring 应用,并且使用 `Spring Integration` 与 Broker 进行连接。 - -Spring Cloud Stream 提供了消息中间件配置的统一抽象,推出了 publish-subscribe、consumer groups、partition 这些统一的概念。 - -Spring Cloud Stream 内部有两个概念:Binder 和 Binding。 - -* Binder: 跟外部消息中间件集成的组件,用来创建 Binding,各消息中间件都有自己的 Binder 实现。 - -比如 `Kafka` 的实现 `KafkaMessageChannelBinder`,`RabbitMQ` 的实现 `RabbitMessageChannelBinder` 以及 `RocketMQ` 的实现 `RocketMQMessageChannelBinder`。 - -* Binding: 包括 Input Binding 和 Output Binding。 - -Binding 在消息中间件与应用程序提供的 Provider 和 Consumer 之间提供了一个桥梁,实现了开发者只需使用应用程序的 Provider 或 Consumer 生产或消费数据即可,屏蔽了开发者与底层消息中间件的接触。 - -.Spring Cloud Stream -image::https://docs.spring.io/spring-cloud-stream/docs/current/reference/htmlsingle/images/SCSt-overview.png[] - -使用 Spring Cloud Stream 完成一段简单的消息发送和消息接收代码: - -```java -MessageChannel messageChannel = new DirectChannel(); - -// 消息订阅 -((SubscribableChannel) messageChannel).subscribe(new MessageHandler() { - @Override - public void handleMessage(Message message) throws MessagingException { - System.out.println("receive msg: " + message.getPayload()); - } -}); - -// 消息发送 -messageChannel.send(MessageBuilder.withPayload("simple msg").build()); -``` - -这段代码所有的消息类都是 `spring-messaging` 模块里提供的。屏蔽具体消息中间件的底层实现,如果想用更换消息中间件,在配置文件里配置相关消息中间件信息以及修改 binder 依赖即可。 - -**Spring Cloud Stream 底层也是基于这段代码去做了各种抽象。** - -### Spring Cloud Alibaba RocketMQ Binder 实现原理 - -.RocketMQ Binder处理流程 -image::https://cdn.nlark.com/lark/0/2018/png/64647/1543560843558-24525bf4-1d0e-4e10-be5f-bdde7127f6e6.png[] - - -RocketMQ Binder 的核心主要就是这3个类:`RocketMQMessageChannelBinder`,`RocketMQInboundChannelAdapter` 和 `RocketMQMessageHandler`。 - -`RocketMQMessageChannelBinder` 是个标准的 Binder 实现,其内部构建 `RocketMQInboundChannelAdapter` 和 `RocketMQMessageHandler`。 - -`RocketMQMessageHandler` 用于 RocketMQ `Producer` 的启动以及消息的发送,其内部会根据 `spring-messaging` 模块内 `org.springframework.messaging.Message` 消息类,去创建 RocketMQ 的消息类 `org.apache.rocketmq.common.message.Message`。 - -在构造 `org.apache.rocketmq.common.message.Message` 的过程中会根据 `org.springframework.messaging.Message` 的 Header 构造成 `RocketMQMessageHeaderAccessor`。然后再根据 `RocketMQMessageHeaderAccessor` 中的一些属性,比如 tags、keys、flag等属性设置到 RocketMQ 的消息类 `org.apache.rocketmq.common.message.Message` 中。 - -`RocketMQInboundChannelAdapter` 用于 RocketMQ `Consumer` 的启动以及消息的接收。其内部还支持 https://github.com/spring-projects/spring-retry[spring-retry] 的使用。 - -在消费消息的时候可以从 Header 中获取 `Acknowledgement` 并进行一些设置。 - -比如使用 `MessageListenerConcurrently` 进行异步消费的时候,可以设置延迟消费: - -```java -@StreamListener("input") -public void receive(Message message) { - RocketMQMessageHeaderAccessor headerAccessor = new RocketMQMessageHeaderAccessor(message); - Acknowledgement acknowledgement = headerAccessor.getAcknowledgement(message); - acknowledgement.setConsumeConcurrentlyStatus(ConsumeConcurrentlyStatus.RECONSUME_LATER); - acknowledgement.setConsumeConcurrentlyDelayLevel(1); -} -``` - -比如使用 `MessageListenerOrderly` 进行顺序消费的时候,可以设置延迟消费: - -```java -@StreamListener("input") -public void receive(Message message) { - RocketMQMessageHeaderAccessor headerAccessor = new RocketMQMessageHeaderAccessor(message); - Acknowledgement acknowledgement = headerAccessor.getAcknowledgement(message); - acknowledgement.setConsumeOrderlyStatus(ConsumeOrderlyStatus.SUSPEND_CURRENT_QUEUE_A_MOMENT); - acknowledgement.setConsumeOrderlySuspendCurrentQueueTimeMill(5000); -} -``` - -Provider端支持的配置: - -:frame: topbot -[width="60%",options="header"] -|==== -^|配置项 ^|含义 ^| 默认值 -|`spring.cloud.stream.rocketmq.bindings.your-output-binding.producer.enabled`|是否启用producer|true -|`spring.cloud.stream.rocketmq.bindings.your-output-binding.producer.max-message-size`|消息发送的最大字节数|0(大于0才会生效,RocketMQ 默认值为4M = 1024 * 1024 * 4) -|`spring.cloud.stream.rocketmq.bindings.your-output-binding.producer.transactional`|是否启用 `TransactionMQProducer` 发送事务消息|false -|`spring.cloud.stream.rocketmq.bindings.your-output-binding.producer.executer`|事务消息对应的 `org.apache.rocketmq.client.producer.LocalTransactionExecuter` 接口实现类 全类名。 比如 `org.test.MyExecuter`| -|`spring.cloud.stream.rocketmq.bindings.your-output-binding.producer.transaction-check-listener`|事务消息对应的 `org.apache.rocketmq.client.producer.TransactionCheckListener` 接口实现类 全类名。 比如 `org.test.MyTransactionCheckListener`| -|==== - -Consumer端支持的配置: - -:frame: topbot -[width="60%",options="header"] -|==== -^|配置项 ^|含义| 默认值 -|`spring.cloud.stream.rocketmq.bindings.your-input-binding.consumer.enabled`|是否启用consumer|true -|`spring.cloud.stream.rocketmq.bindings.your-input-binding.consumer.tags`|Consumer订阅只有包括这些tags的topic消息。多个标签之间使用 "\|\|" 分割(不填表示不进行tags的过滤,订阅所有消息)| -|`spring.cloud.stream.rocketmq.bindings.your-input-binding.consumer.sql`|Consumer订阅满足sql要求的topic消息(如果同时配置了tags内容,sql的优先级更高)| -|`spring.cloud.stream.rocketmq.bindings.your-input-binding.consumer.broadcasting`|Consumer是否是广播模式|false -|`spring.cloud.stream.rocketmq.bindings.your-input-binding.consumer.orderly`|顺序消费 or 异步消费|false -|==== - -### Endpoint支持 - -在使用Endpoint特性之前需要在 Maven 中添加 `spring-boot-starter-actuator` 依赖,并在配置中允许 Endpoints 的访问。 - -* Spring Boot 1.x 中添加配置 `management.security.enabled=false`。暴露的 endpoint 路径为 `/rocketmq_binder` -* Spring Boot 2.x 中添加配置 `management.endpoints.web.exposure.include=*`。暴露的 endpoint 路径为 `/actuator/rocketmq-binder` - -Endpoint 会统计消息最后一次发送的数据,消息发送成功或失败的次数,消息消费成功或失败的次数等数据。 - -```json -{ - "runtime": { - "lastSend.timestamp": 1542786623915 - }, - "metrics": { - "scs-rocketmq.consumer.test-topic.totalConsumed": { - "count": 11 - }, - "scs-rocketmq.consumer.test-topic.totalConsumedFailures": { - "count": 0 - }, - "scs-rocketmq.producer.test-topic.totalSentFailures": { - "count": 0 - }, - "scs-rocketmq.consumer.test-topic.consumedPerSecond": { - "count": 11, - "fifteenMinuteRate": 0.012163847780107841, - "fiveMinuteRate": 0.03614605351360527, - "meanRate": 0.3493213353657594, - "oneMinuteRate": 0.17099243039490175 - }, - "scs-rocketmq.producer.test-topic.totalSent": { - "count": 5 - }, - "scs-rocketmq.producer.test-topic.sentPerSecond": { - "count": 5, - "fifteenMinuteRate": 0.005540151995103271, - "fiveMinuteRate": 0.01652854617838251, - "meanRate": 0.10697493212602836, - "oneMinuteRate": 0.07995558537067671 - }, - "scs-rocketmq.producer.test-topic.sentFailuresPerSecond": { - "count": 0, - "fifteenMinuteRate": 0.0, - "fiveMinuteRate": 0.0, - "meanRate": 0.0, - "oneMinuteRate": 0.0 - }, - "scs-rocketmq.consumer.test-topic.consumedFailuresPerSecond": { - "count": 0, - "fifteenMinuteRate": 0.0, - "fiveMinuteRate": 0.0, - "meanRate": 0.0, - "oneMinuteRate": 0.0 - } - } -} -``` - -注意:要想查看统计数据需要在pom里加上 https://mvnrepository.com/artifact/io.dropwizard.metrics/metrics-core[metrics-core依赖]。如若不加,endpoint 将会显示 warning 信息而不会显示统计信息: - -```json -{ - "warning": "please add metrics-core dependency, we use it for metrics" -} -``` \ No newline at end of file diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/schedulerx.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/schedulerx.adoc deleted file mode 100644 index 0c33b407..00000000 --- a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/schedulerx.adoc +++ /dev/null @@ -1,131 +0,0 @@ -== Spring Cloud Alibaba Cloud SchedulerX - -SchedulerX(分布式任务调度) 是隶属于阿里云EDAS产品的组件, Spring Cloud AliCloud SchedulerX 提供了在Spring Cloud的配置规范下,分布式任务调度的功能支持。SchedulerX可提供秒级、精准、高可靠、高可用的定时任务调度服务,并支持多种类型的任务调度,如简单单机任务、简单多机任务、脚本任务以及网格任务。 - -=== 如何引入 Spring Cloud AliCloud SchedulerX - -Spring Cloud Alibaba 已经发布了0.1.1版本,需要首先导入依赖管理POM。 - -[source,xml] ----- - - - - org.springframework.cloud - spring-cloud-alibaba-dependencies - 0.1.1.RELEASE - pom - import - - - ----- - -接下来引入 Spring Cloud AliCloud SchedulerX Starter 即可。 - -[source,xml] ----- - - org.springframework.cloud - spring-cloud-starter-alicloud-schedulerX - ----- - -=== 启动SchedulerX任务调度 - -当客户端引入了 Spring Cloud AliCloud SchedulerX Starter 以后,只需要进行一些简单的配置,就可以自动初始化SchedulerX的任务调度服务。 - -以下是一个简单的应用示例。 - -[source,java] ----- -@SpringBootApplication -public class ScxApplication { - - public static void main(String[] args) { - SpringApplication.run(ScxApplication.class, args); - } - -} ----- - -在application.properties中,需要加上以下配置。 - -[source,properties] ----- -server.port=18033 -# 其中cn-test是SchedulerX的测试区域 -spring.cloud.alicloud.scx.group-id=*** -spring.cloud.alicloud.edas.namespace=cn-test ----- - -在获取group-id之前,需要首先 https://account.aliyun.com/register/register.htm?spm=5176.8142029.388261.26.e9396d3eEIv28g&oauth_callback=https%3A%2F%2Fwww.aliyun.com%2F[注册阿里云账号] ,然后 https://common-buy.aliyun.com/?spm=5176.11451019.0.0.6f5965c0Uq5tue&commodityCode=edaspostpay#/buy[开通EDAS服务] ,并 https://edas.console.aliyun.com/#/edasTools[开通分布式任务管理组件] 。 - -其中group-id的获取,请参考 https://help.aliyun.com/document_detail/98784.html?spm=a2c4g.11186623.2.17.23c87da9P2F3tG[这里]。 - -NOTE: 在创建group的时候,要选择"测试"区域。 - -=== 编写一个简单任务 - -简单任务是最常用的任务类型,只需要实现 ScxSimpleJobProcessor 接口即可。 - -以下是一个简单的单机类型任务示例。 - -[source,java] ----- -public class SimpleTask implements ScxSimpleJobProcessor { - - @Override - public ProcessResult process(ScxSimpleJobContext context) { - System.out.println("-----------Hello world---------------"); - ProcessResult processResult = new ProcessResult(true); - return processResult; - } - -} ----- - -=== 对任务进行调度 - -进入 https://edas.console.aliyun.com/#/edasSchedulerXJob?regionNo=cn-test[SchedulerX任务列表] 页面,选择上方"测试"区域,点击右上角"新建Job",创建一个Job,即如下所示。 - -[source,text] ----- -Job分组:测试——***-*-*-**** -Job处理接口:org.springframework.cloud.alibaba.cloud.examples.SimpleTask -类型:简单Job单机版 -定时表达式:默认选项——0 * * * * ? -Job描述:无 -自定义参数:无 ----- - -以上任务类型选择了"简单Job单机版",并且制定了Cron表达式为"0 * * * * ?",这意味着,每过一分钟,任务将会被执行且只执行一次。 - -更多任务类型,请参考 https://help.aliyun.com/document_detail/43136.html[SchedulerX官方文档]。 - -=== 生产环境使用 - -以上使用的都是SchedulerX的"测试"区域,主要用于本地调试和测试。 - -在生产级别,除了上面的group-id和namespace以外,还需要一些额外的配置,如下所示。 - -[source,properties] ----- -server.port=18033 -# 其中cn-test是SchedulerX的测试区域 -spring.cloud.alicloud.scx.group-id=*** -spring.cloud.alicloud.edas.namespace=*** -# 当应用运行在EDAS上时,以下配置不需要手动配置。 -spring.cloud.alicloud.access-key=*** -spring.cloud.alicloud.secret-key=*** -# 以下配置不是必须的,请参考SchedulerX文档 -spring.cloud.alicloud.scx.domain-name=*** ----- - -其中group-id与之前的获取方式一样,namespace则是从EDAS控制台左侧"命名空间"列表中获取命名空间ID。 - -NOTE: group-id必须创建在namespace当中。 - -access-key以及secret-key为阿里云账号的AK/SK信息,如果应用在EDAS上部署,则不需要填写这两项信息,否则请前往 https://usercenter.console.aliyun.com/#/manage/ak[安全信息管理]获取。 - -domain-name并不是必须的,具体请参考 https://help.aliyun.com/document_detail/35359.html[SchedulerX官方文档]。 diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/sentinel.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/sentinel.adoc deleted file mode 100644 index c1e699b1..00000000 --- a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/sentinel.adoc +++ /dev/null @@ -1,318 +0,0 @@ -== Spring Cloud Alibaba Sentinel - -### Sentinel 介绍 - -随着微服务的流行,服务和服务之间的稳定性变得越来越重要。 https://github.com/alibaba/Sentinel[Sentinel] 以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。 - -https://github.com/alibaba/Sentinel[Sentinel] 具有以下特征: - - -* *丰富的应用场景*: Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景,例如秒杀(即突发流量控制在系统容量可以承受的范围)、消息削峰填谷、实时熔断下游不可用应用等。 -* *完备的实时监控*: Sentinel 同时提供实时的监控功能。您可以在控制台中看到接入应用的单台机器秒级数据,甚至 500 台以下规模的集群的汇总运行情况。 -* *广泛的开源生态*: Sentinel 提供开箱即用的与其它开源框架/库的整合模块,例如与 Spring Cloud、Dubbo、gRPC 的整合。您只需要引入相应的依赖并进行简单的配置即可快速地接入 Sentinel。 -* *完善的 SPI 扩展点*: Sentinel 提供简单易用、完善的 SPI 扩展点。您可以通过实现扩展点,快速的定制逻辑。例如定制规则管理、适配数据源等。 - -### 如何使用 Sentinel - -如果要在您的项目中引入 Sentinel,使用 group ID 为 `org.springframework.cloud` 和 artifact ID 为 `spring-cloud-starter-alibaba-sentinel` 的 starter。 - -```xml - - org.springframework.cloud - spring-cloud-starter-alibaba-sentinel - -``` - -下面这个例子就是一个最简单的使用 Sentinel 的例子: - -```java -@SpringBootApplication -public class Application { - - public static void main(String[] args) { - SpringApplication.run(ServiceApplication.class, args); - } - -} - -@RestController -public class TestController { - - @GetMapping(value = "/hello") - @SentinelResource("hello") - public String hello() { - return "Hello Sentinel"; - } - -} -``` - -@SentinelResource 注解用来标识资源是否被限流、降级。上述例子上该注解的属性 'hello' 表示资源名。 - -@SentinelResource 还提供了其它额外的属性如 `blockHandler`,`blockHandlerClass`,`fallback` 用于表示限流或降级的操作,更多内容可以参考 https://github.com/alibaba/Sentinel/wiki/%E6%B3%A8%E8%A7%A3%E6%94%AF%E6%8C%81[Sentinel注解支持]。 - -##### Sentinel 控制台 - -Sentinel 控制台提供一个轻量级的控制台,它提供机器发现、单机资源实时监控、集群资源汇总,以及规则管理的功能。您只需要对应用进行简单的配置,就可以使用这些功能。 - -*注意*: 集群资源汇总仅支持 500 台以下的应用集群,有大概 1 - 2 秒的延时。 - -.Sentinel Dashboard -image::https://github.com/alibaba/Sentinel/wiki/image/dashboard.png[] - -开启该功能需要3个步骤: - -###### 获取控制台 - -您可以从 https://github.com/alibaba/Sentinel/releases[release 页面] 下载最新版本的控制台 jar 包。 - -您也可以从最新版本的源码自行构建 Sentinel 控制台: - -* 下载 https://github.com/alibaba/Sentinel/tree/master/sentinel-dashboard[控制台] 工程 -* 使用以下命令将代码打包成一个 fat jar: `mvn clean package` - - -###### 启动控制台 - -Sentinel 控制台是一个标准的 SpringBoot 应用,以 SpringBoot 的方式运行 jar 包即可。 - -```shell -java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar -``` - -如若8080端口冲突,可使用 `-Dserver.port=新端口` 进行设置。 - -#### 配置控制台信息 - -.application.yml ----- -spring: - cloud: - sentinel: - transport: - port: 8719 - dashboard: localhost:8080 ----- - -这里的 `spring.cloud.sentinel.transport.port` 端口配置会在应用对应的机器上启动一个 Http Server,该 Server 会与 Sentinel 控制台做交互。比如 Sentinel 控制台添加了1个限流规则,会把规则数据 push 给这个 Http Server 接收,Http Server 再将规则注册到 Sentinel 中。 - -更多 Sentinel 控制台的使用及问题参考: https://github.com/alibaba/Sentinel/wiki/%E6%8E%A7%E5%88%B6%E5%8F%B0[Sentinel控制台] - -### Feign 支持 - -Sentinel 适配了 https://github.com/OpenFeign/feign[Feign] 组件。如果想使用,除了引入 `sentinel-starter` 的依赖外还需要 3 个步骤: - -* 配置文件打开 sentinel 对 feign 的支持:`feign.sentinel.enabled=true` -* 加入 `feign starter` 依赖触发 `sentinel starter` 的配置类生效: -```xml - - org.springframework.cloud - spring-cloud-starter-openfeign - -``` -* `feign` 内部的 `loadbalance` 功能依赖 Netflix 的 `ribbon` 模块(如果不使用 `loadbalance` 功能可不引入): -```xml - - org.springframework.cloud - spring-cloud-starter-netflix-ribbon - -``` - -这是一个 `FeignClient` 对应的接口: - -```java -@FeignClient(name = "service-provider", fallback = EchoServiceFallback.class, configuration = FeignConfiguration.class) -public interface EchoService { - @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET) - String echo(@PathVariable("str") String str); -} - -class FeignConfiguration { - @Bean - public EchoServiceFallback echoServiceFallback() { - return new EchoServiceFallback(); - } -} - -class EchoServiceFallback implements EchoService { - @Override - public String echo(@PathVariable("str") String str) { - return "echo fallback"; - } -} -``` - -NOTE: Feign 对应的接口中的资源名策略定义:httpmethod:protocol://requesturl - -`EchoService` 接口中方法 `echo` 对应的资源名为 `GET:http://service-provider/echo/{str}`。 - -请注意:`@FeignClient` 注解中的所有属性,Sentinel 都做了兼容。 - -### RestTemplate 支持 - -Spring Cloud Alibaba Sentinel 支持对 `RestTemplate` 的服务调用使用 Sentinel 进行保护,在构造 `RestTemplate` bean的时候需要加上 `@SentinelRestTemplate` 注解。 - -```java -@Bean -@SentinelRestTemplate(blockHandler = "handleException", blockHandlerClass = ExceptionUtil.class) -public RestTemplate restTemplate() { - return new RestTemplate(); -} -``` - -`@SentinelRestTemplate` 注解的参数跟 `@SentinelResource` 一致,使用方式也一致。 - -限流的资源规则提供两种粒度: - -* `schema://host:port/path`:协议、主机、端口和路径 - -* `schema://host:port`:协议、主机和端口 - -NOTE: 以 `https://www.taobao.com/test` 这个 url 为例。对应的资源名有两种粒度,分别是 `https://www.taobao.com:80` 以及 `https://www.taobao.com:80/test` - -### 动态数据源支持 - -#### 在版本 0.2.0.RELEASE 或 0.1.0.RELEASE 之前 - -需要3个步骤才可完成数据源的配置: - -* 配置文件中定义数据源信息。比如使用文件: - -.application.properties ----- -spring.cloud.sentinel.datasource.type=file -spring.cloud.sentinel.datasource.recommendRefreshMs=3000 -spring.cloud.sentinel.datasource.bufSize=4056196 -spring.cloud.sentinel.datasource.charset=utf-8 -spring.cloud.sentinel.datasource.converter=flowConverter -spring.cloud.sentinel.datasource.file=/Users/you/yourrule.json ----- - -* 创建一个 Converter 类实现 `com.alibaba.csp.sentinel.datasource.Converter` 接口,并且需要在 `ApplicationContext` 中有该类的一个 Bean - -```java -@Component("flowConverter") -public class JsonFlowRuleListParser implements Converter> { - @Override - public List convert(String source) { - return JSON.parseObject(source, new TypeReference>() { - }); - } -} -``` - -这个 Converter 的 bean name 需要跟 `application.properties` 配置文件中的 converter 配置一致 - -* 在任意一个 Spring Bean 中定义一个被 `@SentinelDataSource` 注解修饰的 `ReadableDataSource` 属性 - -```java -@SentinelDataSource("spring.cloud.sentinel.datasource") -private ReadableDataSource dataSource; -``` - -`@SentinelDataSource` 注解的 value 属性表示数据源在 `application.properties` 配置文件中前缀。 该在例子中,前缀为 `spring.cloud.sentinel.datasource` 。 - -如果 `ApplicationContext` 中存在超过1个 `ReadableDataSource` bean,那么不会加载这些 `ReadableDataSource` 中的任意一个。 只有在 `ApplicationContext` 存在一个 `ReadableDataSource` 的情况下才会生效。 - -如果数据源生效并且规则成功加载,控制台会打印类似如下信息: - -``` -[Sentinel Starter] load 3 flow rules -``` - -#### 在版本 0.2.0.RELEASE 或 0.1.0.RELEASE 之后 - -只需要1个步骤就可完成数据源的配置: - -* 直接在 `application.properties` 配置文件中配置数据源信息即可 - -比如配置了4个数据源: - -``` -spring.cloud.sentinel.datasource.ds1.file.file=classpath: degraderule.json - -spring.cloud.sentinel.datasource.ds2.nacos.server-addr=localhost:8848 -spring.cloud.sentinel.datasource.ds2.nacos.dataId=sentinel -spring.cloud.sentinel.datasource.ds2.nacos.groupId=DEFAULT_GROUP -spring.cloud.sentinel.datasource.ds2.nacos.data-type=json - -spring.cloud.sentinel.datasource.ds3.zk.path = /Sentinel-Demo/SYSTEM-CODE-DEMO-FLOW -spring.cloud.sentinel.datasource.ds3.zk.server-addr = localhost:2181 - -spring.cloud.sentinel.datasource.ds4.apollo.namespace-name = application -spring.cloud.sentinel.datasource.ds4.apollo.flow-rules-key = sentinel -spring.cloud.sentinel.datasource.ds4.apollo.default-flow-rule-value = test - -``` - -这样配置方式参考了 Spring Cloud Stream Binder 的配置,内部使用了 `TreeMap` 进行存储,comparator 为 `String.CASE_INSENSITIVE_ORDER` 。 - -NOTE: d1, ds2, ds3, ds4 是 `ReadableDataSource` 的名字,可随意编写。后面的 `file` ,`zk` ,`nacos` , `apollo` 就是对应具体的数据源。 它们后面的配置就是这些数据源各自的配置。 - -每种数据源都有两个共同的配置项: `data-type` 和 `converter-class` 。 - -`data-type` 配置项表示 `Converter`,Spring Cloud Alibaba Sentinel 默认提供两种内置的值,分别是 `json` 和 `xml` (不填默认是json)。 如果不想使用内置的 `json` 或 `xml` 这两种 `Converter`,可以填写 `custom` 表示自定义 `Converter`,然后再配置 `converter-class` 配置项,该配置项需要写类的全路径名。 - -这两种内置的 `Converter` 只支持解析 json 数组 或 xml 数组。内部解析的时候会自动判断每个 json 对象或xml对象属于哪4种 Sentinel 规则(`FlowRule`,`DegradeRule`,`SystemRule`,`AuthorityRule`)。 - -比如10个规则数组里解析出5个限流规则和5个降级规则。 这种情况下该数据源不会注册,日志里页会进行警告。 - -如果10个规则里有9个限流规则,1个解析报错了。这种情况下日志会警告有个规则格式错误,另外9个限流规则会注册上去。 - -这里 json 或 xml 解析用的是 `jackson`。`ObjectMapper` 或 `XmlMapper` 使用默认的配置,遇到不认识的字段会解析报错。 因为不这样做的话限流 json 也会解析成 系统规则(系统规则所有的配置都有默认值),所以需要这样严格解析。 - -当然还有一种情况是 json 对象或 xml 对象可能会匹配上所有4种规则(比如json对象里只配了 `resource` 字段,那么会匹配上4种规则),这种情况下日志里会警告,并且过滤掉这个对象。 - -用户使用这种配置的时候只需要填写正确的json或xml就行,有任何不合理的信息都会在日志里打印出来。 - -如果数据源生效并且规则成功加载,控制台会打印类似如下信息: - -``` -[Sentinel Starter] DataSource ds1-sentinel-file-datasource load 3 DegradeRule -[Sentinel Starter] DataSource ds2-sentinel-nacos-datasource load 2 FlowRule -``` - -NOTE: 默认情况下,xml 格式是不支持的。需要添加 `jackson-dataformat-xml` 依赖后才会自动生效。 - -关于 Sentinel 动态数据源的实现原理,参考: https://github.com/alibaba/Sentinel/wiki/%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%99%E6%89%A9%E5%B1%95[动态规则扩展] - -### Endpoint 支持 - -在使用 Endpoint 特性之前需要在 Maven 中添加 `spring-boot-starter-actuator` 依赖,并在配置中允许 Endpoints 的访问。 - -* Spring Boot 1.x 中添加配置 `management.security.enabled=false`。暴露的 endpoint 路径为 `/sentinel` -* Spring Boot 2.x 中添加配置 `management.endpoints.web.exposure.include=*`。暴露的 endpoint 路径为 `/actuator/sentinel` - -### More - -下表显示当应用的 `ApplicationContext` 中存在对应的Bean的类型时,会进行的一些操作: - -:frame: topbot -[width="60%",options="header"] -|==== -^|存在Bean的类型 ^|操作 ^|作用 -|`UrlCleaner`|`WebCallbackManager.setUrlCleaner(urlCleaner)`|资源清理(资源(比如将满足 /foo/:id 的 URL 都归到 /foo/* 资源下)) -|`UrlBlockHandler`|`WebCallbackManager.setUrlBlockHandler(urlBlockHandler)`|自定义限流处理逻辑 -|==== - -下表显示 Spring Cloud Alibaba Sentinel 的所有配置信息: - -:frame: topbot -[width="60%",options="header"] -|==== -^|配置项 ^|含义 ^|默认值 -|`spring.cloud.sentinel.enabled`|Sentinel自动化配置是否生效|true -|`spring.cloud.sentinel.eager`|取消Sentinel控制台懒加载|false -|`spring.cloud.sentinel.transport.port`|应用与Sentinel控制台交互的端口,应用本地会起一个该端口占用的HttpServer|8721 -|`spring.cloud.sentinel.transport.dashboard`|Sentinel 控制台地址| -|`spring.cloud.sentinel.transport.heartbeatIntervalMs`|应用与Sentinel控制台的心跳间隔时间| -|`spring.cloud.sentinel.filter.order`|Servlet Filter的加载顺序。Starter内部会构造这个filter|Integer.MIN_VALUE -|`spring.cloud.sentinel.filter.spring.url-patterns`|数据类型是数组。表示Servlet Filter的url pattern集合|/* -|`spring.cloud.sentinel.metric.charset`|metric文件字符集|UTF-8 -|`spring.cloud.sentinel.metric.fileSingleSize`|Sentinel metric 单个文件的大小| -|`spring.cloud.sentinel.metric.fileTotalCount`|Sentinel metric 总文件数量| -|`spring.cloud.sentinel.log.dir`|Sentinel 日志文件所在的目录| -|`spring.cloud.sentinel.log.switch-pid`|Sentinel 日志文件名是否需要带上pid|false -|`spring.cloud.sentinel.servlet.blockPage`| 自定义的跳转 URL,当请求被限流时会自动跳转至设定好的 URL | -|`spring.cloud.sentinel.flow.coldFactor`| https://github.com/alibaba/Sentinel/wiki/%E9%99%90%E6%B5%81---%E5%86%B7%E5%90%AF%E5%8A%A8[冷启动因子] |3 -|==== diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/spring-cloud-alibaba.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/spring-cloud-alibaba.adoc deleted file mode 100644 index 5cde376e..00000000 --- a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/spring-cloud-alibaba.adoc +++ /dev/null @@ -1,32 +0,0 @@ -[[spring-cloud-alibaba-reference]] -= Spring Cloud Alibaba 参考文档 -xiaojing; xiaolongzuo; jim fang; bingting peng -:doctype: book -:toc: -:toclevels: 4 -:source-highlighter: prettify -:numbered: - -== 介绍 - -Spring Cloud Alibaba 致力于提供分布式应用服务开发的一站式解决方案。此项目包含开发分布式应用服务的必需组件,方便开发者通过 Spring Cloud 编程模型轻松使用这些组件来开发分布式应用服务。 - -依托 Spring Cloud Alibaba,您只需要添加一些注解和少量配置,就可以将 Spring Cloud 应用接入阿里分布式应用解决方案,通过阿里中间件来迅速搭建分布式应用系统。 - -include::dependency-management.adoc[] - -include::nacos-discovery.adoc[] - -include::nacos-config.adoc[] - -include::sentinel.adoc[] - -include::rocketmq.adoc[] - -include::ans.adoc[] - -include::acm.adoc[] - -include::oss.adoc[] - -include::schedulerx.adoc[] \ No newline at end of file diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc/acm.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc/acm.adoc index 488baab6..f488a802 100644 --- a/spring-cloud-alibaba-docs/src/main/asciidoc/acm.adoc +++ b/spring-cloud-alibaba-docs/src/main/asciidoc/acm.adoc @@ -1 +1,185 @@ -== Spring Cloud AliCloud ACM +== Spring Cloud Alibaba Cloud ACM + +Spring Cloud Alibaba Cloud ACM is an implementation of the commercial product Application Configuration Management(ACM) in the client side of Spring Cloud, and is free of charge. + +Spring Cloud Alibaba Cloud ACM is an alternative solution for Config Server and Client. The concepts on the client and server have the same abstractions with Spring Environment and PropertySource. In special Bootstrap phases, configurations are loaded to the Spring environment. During the application lifecycle from development, deployment, test to production, you can manage the configurations across all the environments, and make sure that all information required for application migration is ready when needed. + +=== How to Introduce Spring Cloud Alibaba Cloud ACM + +We’ve released Spring Cloud Alibaba version 0.2.1. You will need to add dependency management POM first. + +[source,xml] +---- + + + + org.springframework.cloud + spring-cloud-alibaba-dependencies + 0.2.1.RELEASE + pom + import + + + +---- + +Next we need to introduce Spring Cloud Alibaba Cloud ACM Starter. + +[source,xml] +---- + + org.springframework.cloud + spring-cloud-starter-alicloud-acm + +---- + +=== Use ACM to Manage Configurations + +When Spring Cloud Alibaba Cloud ACM Starter is introduced into the client, the application will automatically get configuration information from the configuration management server when it starts, and inject the configuration into Spring Environment. + +The following is a simple illustration. + +[source,java] +---- +@SpringBootApplication +public class ProviderApplication { + + public static void main(String[] args) { + ConfigurableApplicationContext applicationContext = SpringApplication.run(ProviderApplication.class, args); + String userName = applicationContext.getEnvironment().getProperty("user.name"); + String userAge = applicationContext.getEnvironment().getProperty("user.age"); + System.err.println("user name :" +userName+"; age: "+userAge); + } +} +---- + +As we need to obtain configuration information from the configuration server, we will need to configure the address of the server. We also need to add the following information in bootstrap.properties. + +[source,properties] +---- +# Required. The application name will be used as part of the keyword to get the configuration key from the server. +spring.application.name=acm-config +server.port=18081 +# The following is the IP and port number of the configuration server. +spring.cloud.alicloud.acm.server-list=127.0.0.1 +spring.cloud.alicloud.acm.server-port=8080 +---- + +NOTE: By now the configuration center is not started yet, so you will get an error message if your application is started. Therefore, start the configuration center before you start your application. + + +=== Start Configuration Center + +ACM uses two types of configuration centers. One is a lightweight configuration center which is totally free, the other is ACM which is used on Alibaba Cloud. Generally, you can use the lightweight version for application development and local testing, and use ACM for canary deployment or production. + +==== Start Lightweight Configuration Center + +Refer to the https://help.aliyun.com/document_detail/44163.html[Configure Lightweight Configuration Center] for details about how to download and install lightweight configuration center. + +NOTE: You only need to perform step 1(Download lightweight configuration center) and step 2(Start lightweight configuration center). Step 3(Configure hosts) is not required if you use ACM at the same time. + + +==== Use ACM on the Cloud + +Using ACM on the cloud saves you from the tedious work of server maintenance while at the same time provides a better stability. There is no difference at the code level between using ACM on cloud and lightweight configuration center, but there are some differences in configurations. + +The following is a simple sample of using ACM. You can view configuration details on https://acm.console.aliyun.com[ACM Console] + +[source,properties] +---- +# The application name will be used as part of the keyword to obtain configuration key from the server, and is mandatory. +spring.application.name=ans-provider +# Configure your own port number +server.port=18081 +# The following is the IP and port number of the configuration center. +spring.cloud.alicloud.acm.server-mode=EDAS +spring.cloud.alicloud.access-key=Your Alibaba Cloud AK +spring.cloud.alicloud.secret-key=Your Alibaba Cloud SK +spring.cloud.alicloud.acm.endpoint=acm.aliyun.com +spring.cloud.alicloud.acm.namespace=Your ACM namespace(You can find the namespace on the ACM console) +---- + +NOTE: EDAS provides application hosting service and will fill in all configurations automatically for the hosted applications. + +=== Add Configuration in the Configuration Center + +1. After you start the lightweight configuration center, add the following configuration on the console. + +[source,subs="normal"] +---- +Group: DEFAULT_GROOUP + +DataId: acm-config.properties + +Content: user.name=james + user.age=18 +---- + +NOTE: The format of dataId is `{prefix}. {file-extension}`. “prefix” is obtained from spring.application.name by default, and the value of “file-extension” is "properties” by default. + +=== Start Application Verification + +Start the following example and you can see that the value printed on the console is the value we configured in the lightweight configuration center. + +[source,subs="normal"] +---- +user name :james; age: 18 +---- + +=== Modify Configuration File Extension + +The default file extension of dataId in spring-cloud-starter-alicloud-acm is properties. In addition to properties, yaml is also supported. +You can set the file extension using spring.cloud.nacos.config.file-extension. Just set it to `yaml` or `yml`for yaml format. + +NOTE: After you change the file extension, you need to make corresponding format changes in the DataID and content of the configuration center. + +=== Dynamic Configuration Updates + +spring-cloud-starter-alicloud-acm supports dynamic configuration updates. Context Refresh in Spring is triggered when you update configuration in the configuration center. +All classes with @RefreshScope and @ConfigurationProperties annotations will be refershed automatically. + +NOTE: You can disable automatic refresh by this setting: spring.cloud.nacos.config.refresh.enabled=false + +=== Configure Profile Granularity + +When configuration is loaded by spring-cloud-starter-alicloud-acm, configuration with dataid {spring.application.name}. {file-extension} will be loaded first. If there is content in spring.profiles.active, the content of spring.profile, and configuration with the dataid format of{spring.application.name}-{profile}. {file-extension} will also be loaded in turn, and the latter has higher priority. + +spring.profiles.active is the configuration metadata, and should also be configured in bootstrap.properties or bootstrap.yaml. For example, you can add the following content in bootstrap.properties. + +[sources,properties] +---- +spring.profiles.active={profile-name} +---- + +Note: You can also configure the granularity through JVM parameters such as -Dspring.profiles.active=develop or --spring.profiles.active=develop, which have higher priority. Just follow the specifications of Spring Boot. + + + +=== Support Custom Group Configurations + +DEFAULT_GROUP is used by default when no `{spring.cloud.nacos.config.group}` configuration is defined. If you need to define your own group, you can use the following method: + +[source,properties] +---- +spring.cloud.nacos.config.group=DEVELOP_GROUP +---- + +NOTE: This configuration must be placed in the bootstrap.properties file, and the value of Group must be the same with the value of `spring.cloud.nacos.config.group`. + +==== Support Shared Configurations + +ACM provides a solution to share the same configuration across multiple applications. You can do this by adding the `spring.application.group` configuration in Bootstrap. + +[source,properties] +---- +spring.application.group=company.department.team +---- + +Then, you application will retrieve configurations from the following DataId in turn before it retrieves its own configuration: company:application.properties, company.department:application.properties, company.department.team:application.properties。 +After that, it also retrieves configuration from {spring.application.group}: {spring.application.name}. {file-extension} +The later in order, the higer the priority, and the unique configuration of the application itself has the highest priority. + + +NOTE: The default suffix of DataId is properties, and you can change it using spring.cloud.nacos.config.file-extension. `{spring.application.group}: {spring.application.name}. {file-extension}` 。 + +NOTE: If you configured `spring.profiles.active` , then the DataId format of `{spring.application.group}: {spring.application.name}-{spring.profiles.active}. {file-extension}` is also supported, and has higher priority than `{spring.application.group}: {spring.application.name}. {file-extension}` diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc/ans.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc/ans.adoc index b9b34ce2..2aec5ed4 100644 --- a/spring-cloud-alibaba-docs/src/main/asciidoc/ans.adoc +++ b/spring-cloud-alibaba-docs/src/main/asciidoc/ans.adoc @@ -1 +1,111 @@ -== Spring Cloud AliCloud ANS +== Spring Cloud Alibaba Cloud ANS + +ANS(Application Naming Service) is a component of EDAS. Spring Cloud Alibaba Cloud ANS provides the commercial version of service registration and discovery in conformity with the Spring Cloud specifications, so that you can develop your applications locally and run them on the cloud. + +=== How to Introduce Spring Cloud Alibaba Cloud ANS + +We’ve released Spring Cloud Alibaba version 0.2.1. You will need to add dependency management POM first. + +[source,xml] +---- + + + + org.springframework.cloud + spring-cloud-alibaba-dependencies + 0.2.1.RELEASE + pom + import + + + +---- + +Next we need to introduce Spring Cloud AliCloud ANS Starter. + +[source,xml] +---- + + org.springframework.cloud + spring-cloud-starter-alicloud-ans + +---- + +=== Use ANS to Register Service + +When Spring Cloud AliCloud ANS Starter is introduced on the client, the metadata of the service such as IP, port number and weright will be registered to the registration center automatically. The client will maintain heartbeat with the server to prove that it is capable of providing service properly. + +The following is a simple illustration. + +[source,java] +---- +@SpringBootApplication +@EnableDiscoveryClient +@RestController +public class ProviderApplication { + + @RequestMapping("/") + public String home() { + return "Hello world"; + } + + public static void main(String[] args) { + SpringApplication.run(ProviderApplication.class, args); + } + +} +---- + +As the service will registered to the registration center, we will need to configure the address of the registration center. We also need to add the following address in application.properties. + +[source,properties] +---- +# The application name will be used as the service name, therefore it is mandatory. +spring.application.name=ans-provider +server.port=18081 +# The following is the IP and port number of the registration center. +spring.cloud.alicloud.ans.server-list=127.0.0.1 +spring.cloud.alicloud.ans.server-port=8080 +---- + +NOTE: By now the registration center is not started yet, so you will get an error message if your application is started. Therefore, start the registration center before you start your application. + +=== Start Registration Center + +ANS uses two types of registration centers. One is the free lightweight configuration center and the other is the registration center on cloud, which is provided through EDAS. Generally, you can use the lightweight version for application development and local testing, and use EDAS for canary deployment or production. + +==== Start Lightweight Configuration Center + +Refer to the https://help.aliyun.com/document_detail/44163.html[Configure Lightweight Configuration Center] for details about how to download and install lightweight configuration center. + +NOTE: You only need to perform step 1(Download lightweight configuration center) and step 2(Start lightweight configuration center). Step 3(Configure hosts) is not required if you use ANS at the same time. + +After you start the lightweight configuration center, start ProviderApplication directly, and you will be able to register your service to the configuration center. The default port of the lightweight configuration center is 8080, therefore you can open http://127.0.0.1:8080, click “Services” on the left and see the registered service. + +==== User Registration Center on the Cloud + +Using the registration center on the cloud saves you from the tedious work of server maintenance while at the same time provides a better stability. There is no difference at the code level between using the registration center on cloud and lightweight configuration center, but there are some differences in configurations. + +The following is a simple sample of using the registration center on the cloud. + +[source,properties] +---- +# The application name will be used the service name, and is therefore mandatory. +spring.application.name=ans-provider +# Configure your own port number +server.port=18081 +# The following is the IP and port number of the configuration center. The default value is 127.0.0.1 and 8080, so the following lines can be omitted. +spring.cloud.alicloud.ans.server-mode=EDAS +spring.cloud.alicloud.access-key=Your Alibaba Cloud AK +spring.cloud.alicloud.secret-key=Your Alibaba Cloud SK +spring.cloud.alicloud.edas.namespace=cn-xxxxx +---- + +The default value of server-mode is LOCAL. If you want to use the registration center on cloud, you need to change it to EDAS. + +Access-key and secret-key are the AK/SK of your Alibaba Cloud account. Register an Alibaba Cloud account first and log on to the Cloud Console https://usercenter.console.aliyun.com/#/manage/ak[Alibaba Cloud AK/SK] to copy your AccessKey ID and Access Key Secret. If you haven’t created one, click the “Create AccessKey” button. + +Namespace is a concept in EDAS, which is used to isolate environments, such as testing environment and production environment. To find your namespace, click to https://common-buy.aliyun.com/?spm=5176.11451019.0.0.6f5965c0Uq5tue&commodityCode=edaspostpay#/buy[Sign up for EDAS] first. You will not be charged under the pay-as-you-go mode. Then log on to the https://edas.console.aliyun.com/#/namespaces?regionNo=cn-hangzhou[EDAS Console] and you will be able to see your namespace, for example cn-hangzhou. + +NOTE: EDAS provides application hosting service and will fill in all configurations automatically for the hosted applications. + diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc/dependency-management.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc/dependency-management.adoc index 1c1640c1..d0c95a95 100644 --- a/spring-cloud-alibaba-docs/src/main/asciidoc/dependency-management.adoc +++ b/spring-cloud-alibaba-docs/src/main/asciidoc/dependency-management.adoc @@ -2,3 +2,37 @@ The Spring Cloud Alibaba Bill of Materials (BOM) contains the versions of all the dependencies it uses. +Version 0.2.1.RELEASE is compatible with the Spring Cloud Finchley. Version 0.1.1.RELEASE is compatible with the Spring Cloud Edgware. + +These artifacts are available from Maven Central and Spring Release repository via BOM: + +[source,xml] +---- + + + + org.springframework.cloud + spring-cloud-alibaba-dependencies + 0.2.1.RELEASE + pom + import + + + +---- + +If you want to use the latest BUILD-SNAPSHOT version, add Spring Snapshot Repository in pom.xml , Attention: BUILD-SNAPSHOT may be updated in any time + +[source,xml] +---- + + + spring-snapshot + Spring Snapshot Repository + https://repo.spring.io/snapshot + + true + + + +---- \ No newline at end of file diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc/nacos-config.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc/nacos-config.adoc index e933cff8..5f42987b 100644 --- a/spring-cloud-alibaba-docs/src/main/asciidoc/nacos-config.adoc +++ b/spring-cloud-alibaba-docs/src/main/asciidoc/nacos-config.adoc @@ -1 +1,362 @@ == Spring Cloud Alibaba Nacos Config + +Nacos provides key/value storage of configurations and other metadata as well as server and client support for externalized configurations in distributed systems. With Spring Cloud Alibaba Nacos Config, you can manage externalized configurations of your Spring Cloud applications in the Nacos Server. + +Spring Cloud Alibaba Nacos Config is an alternative solution for Config Server and Client. The concepts on the client and server have the same abstractions with Spring Environment and PropertySource. In special Bootstrap phases, configurations are loaded to the Spring environment. During the application lifecycle from development, deployment, test to production, you can manage the configurations across all the environments, and make sure that all information required for application migration is ready when needed. + +=== Quickstart + +===== Initialize Nacos Server + +1. Start Nacos Server. Refer to https://nacos.io/zh-cn/docs/quick-start.html[Nacos Documentation] for details about how to start the Nacos server. + +2. Add the following configurations in Nacos: + +[source,subs="normal"] +---- +Data ID: nacos-config.properties + +Group : DEFAULT_GROUP + +Configuration format: Properties + +Configuration content: user.name=nacos-config-properties + user.age=90 +---- + +NOTE: The default file extension of dataid is properties. + +===== Usage on the Client + +To use Nacos to manage externalized configurations for your applications, you need to add a Spring Boot Starter while building your application: org.springframework.cloud:spring-cloud-starter-alibaba-nacos-config The following is a basic configuration of maven dependency: + +[source,xml] +---- + + org.springframework.boot + spring-boot-starter-parent + 2.0.5.RELEASE + + + + + + + org.springframework.cloud + spring-cloud-dependencies + Finchley.SR1 + pom + import + + + org.springframework.cloud + spring-cloud-alibaba-dependencies + 0.2.1.RELEASE + pom + import + + + + + + + org.springframework.cloud + spring-cloud-starter-alibaba-nacos-config + + + org.springframework.boot + spring-boot-starter + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + +---- + +Now we can create a standard Spring Boot application. + +[source,java] +---- +@SpringBootApplication +public class ProviderApplication { + + public static void main(String[] args) { + ConfigurableApplicationContext applicationContext = SpringApplication.run(ProviderApplication.class, args); + String userName = applicationContext.getEnvironment().getProperty("user.name"); + String userAge = applicationContext.getEnvironment().getProperty("user.age"); + System.err.println("user name :" +userName+"; age: "+userAge); + } +} +---- + +Before running this example, we need to configure the address of the Nacos server in bootstrap.properties. For example: + +.bootstrap.properties +[source,properties] +---- +spring.application.name=nacos-config +spring.cloud.nacos.config.server-addr=127.0.0.1:8848 +---- + +NOTE: If you use domain name to access Nacos, the format of `spring.cloud.nacos.config.server-addr` should be `Domain name:port`. +For example, if the Nacos domain name is abc.com.nacos, and the listerner port is 80, then the configuration should be `spring.cloud.nacos.config.server-addr=abc.com.nacos:80`. +Port 80 cannot be omitted. + +Run this example and you can see the following output: + +[source,subs="normal"] +---- +2018-11-02 14:24:51.638 INFO 32700 --- [main] c.a.demo.provider.ProviderApplication : Started ProviderApplication in 14.645 seconds (JVM running for 15.139) +user name :nacos-config-properties; age: 90 +2018-11-02 14:24:51.688 INFO 32700 --- [-127.0.0.1:8848] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@a8c5e74: startup date [Fri Nov 02 14:24:51 CST 2018]; root of context hierarchy +2018-11 +---- + +=== Add Configurations with DataId in YAML Format + +spring-cloud-starter-alibaba-nacos-config supports yaml format as well. You only need to complete the following 2 steps. + +1. In the bootstrap.properties file, add the following line to claim that the format of dataid is yaml. As follows: + +.bootstrap.properties +[source,yaml] +---- +spring.cloud.nacos.config.file-extension=yaml +---- + +2. Add a configuration with the dataId in yaml format on the Nacos console, as shown below: + +[source,subs="normal"] +---- +Data ID: nacos-config.yaml + +Group : DEFAULT_GROUP + +Configuration format: YAML + +Configuration content: user.name: nacos-config-yaml + user.age: 68 +---- + +After completing the preivous two steps, restart the testing program and you will see the following result. + +[source,subs="normal"] +---- +2018-11-02 14:59:00.484 INFO 32928 --- [main] c.a.demo.provider.ProviderApplication:Started ProviderApplication in 14.183 seconds (JVM running for 14.671) +user name :nacos-config-yaml; age: 68 +2018-11-02 14:59:00.529 INFO 32928 --- [-127.0.0.1:8848] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@265a478e: startup date [Fri Nov 02 14:59:00 CST 2018]; root of context hierarchy +---- + +=== Support Dynamic Configuration Udpates + +spring-cloud-starter-alibaba-nacos-config also supports dynamic configuration updates. The code for starting Spring Boot application testing is as follows: + +[source,java] +---- +@SpringBootApplication +public class ProviderApplication { + + public static void main(String[] args) { + ConfigurableApplicationContext applicationContext = SpringApplication.run(ProviderApplication.class, args); + while(true) { + //When configurations are refreshed dynamically, they will be updated in the Enviroment, therefore here we retrieve configurations from Environment every other second. + String userName = applicationContext.getEnvironment().getProperty("user.name"); + String userAge = applicationContext.getEnvironment().getProperty("user.age"); + System.err.println("user name :" + userName + "; age: " + userAge); + TimeUnit.SECONDS.sleep(1); + } + } +} +---- + +When user.name is changed, the latest value can be retrieved from the application, as shown below: + +[source,subs="normal"] +---- +user name :nacos-config-yaml; age: 68 +user name :nacos-config-yaml; age: 68 +user name :nacos-config-yaml; age: 68 +2018-11-02 15:04:25.069 INFO 32957 --- [-127.0.0.1:8848] o.s.boot.SpringApplication : Started application in 0.144 seconds (JVM running for 71.752) +2018-11-02 15:04:25.070 INFO 32957 --- [-127.0.0.1:8848] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@10c89124: startup date [Fri Nov 02 15:04:25 CST 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@6520af7 +2018-11-02 15:04:25.071 INFO 32957 --- [-127.0.0.1:8848] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@6520af7: startup date [Fri Nov 02 15:04:24 CST 2018]; root of context hierarchy +//Read the updated value from Enviroment +user name :nacos-config-yaml-update; age: 68 +user name :nacos-config-yaml-update; age: 68 +---- + +NOTE: You can disable automatic refresh with this setting`spring.cloud.nacos.config.refresh.enabled=false`. + +=== Support configurations at the profile level + +When configurations are loaded by spring-cloud-starter-alibaba-nacos-config, basic configurations with dataid of `${spring.application.name}. ${file-extension:properties}` , and dataid of `${spring.application.name}-${profile}. ${file-extension:properties}` are also loaded. If you need to use different configurations from different environments, you can use the `${spring.profiles.active}` configuration provided by Spring. + +[source,properties] +---- +spring.profiles.active=develop +---- + +NOTE: When specified in configuration files, ${spring.profiles.active} must be placed in bootstrap.properties. + +Add a basic configuration in Nacos, with a dataid of nacos-config-develop.yaml, as shown below: + +[source,subs="normal"] +---- +Data ID: nacos-config-develop.yaml + +Group : DEFAULT_GROUP + +Configuration format: YAML + +Configuration content: current.env: develop-env +---- + +Run the following Spring Boot application testing code: + +[source,java] +---- +@SpringBootApplication +public class ProviderApplication { + + public static void main(String[] args) { + ConfigurableApplicationContext applicationContext = SpringApplication.run(ProviderApplication.class, args); + while(true) { + String userName = applicationContext.getEnvironment().getProperty("user.name"); + String userAge = applicationContext.getEnvironment().getProperty("user.age"); + //Get the current deployment environment + String currentEnv = applicationContext.getEnvironment().getProperty("current.env"); + System.err.println("in "+currentEnv+" enviroment; "+"user name :" + userName + "; age: " + userAge); + TimeUnit.SECONDS.sleep(1); + } + } +} +---- +After started, you can see the output as follows in the console: + +[source,subs="normal"] +---- +in develop-env enviroment; user name :nacos-config-yaml-update; age: 68 +2018-11-02 15:34:25.013 INFO 33014 --- [ Thread-11] ConfigServletWebServerApplicationContext : Closing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@6f1c29b7: startup date [Fri Nov 02 15:33:57 CST 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@63355449 +---- + +To switch to the production environment, you only need to change the parameter of `${spring.profiles.active}`. As show below: + +[source,properties] +---- +spring.profiles.active=product +---- + +At the same time, add the basic configuration with the dataid in the Nacos of your production environment. For example, you can add the configuration with the dataid of nacos-config-product.yaml in Nacos of your production environment: + +[source,subs="normal"] +---- +Data ID: nacos-config-product.yaml + +Group : DEFAULT_GROUP + +Configuration format: YAML + +Configuration content: current.env: product-env +---- + +Start the testing program and you will see the following result: + +[source,subs="normal"] +---- +in product-env enviroment; user name :nacos-config-yaml-update; age: 68 +2018-11-02 15:42:14.628 INFO 33024 --- [Thread-11] ConfigServletWebServerApplicationContext : Closing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@6aa8e115: startup date [Fri Nov 02 15:42:03 CST 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@19bb07ed +---- + + +NOTE: In this example, we coded the configuration in the configuration file by using the `spring.profiles.active=` method. In real scenarios, this variable needs to be different in different environment. You can use the `-Dspring.profiles.active=` parameter to specify the configuration so that you can switch between different environments easily. + +=== Support Custom Namespaces +For details about namespaces in Nacos, refer to https://nacos.io/zh-cn/docs/concepts.html[Nacos Concepts] + +[quote] +Namespaces are used to isolate configurations for different tenants. Groups and Data IDs can be the same across different namespaces. Typical scenarios of namespaces is the isolation of configurations for different environments, for example, isolation between development/testing environments and production environments(configurations and services and so on). + +The “Public” namespace of Nacos is used if no namespace is specified in `${spring.cloud.nacos.config.namespace}`. You can also specify a custom namespace in the following way: +[source,properties] +---- +spring.cloud.nacos.config.namespace=b3404bc0-d7dc-4855-b519-570ed34b62d7 +---- + +NOTE: This configuration must be in the bootstrap.properties file. The value of `spring.cloud.nacos.config.namespace` is the id of the namespace, and the value of id can be retrieved from the Nacos console. Do not select other namespaces when adding configurations. Otherwise configurations cannot be retrieved properly. + +=== Support Custom Groups + +DEFAULT_GROUP is used by default when no `{spring.cloud.nacos.config.group}` configuration is defined. If you need to define your own group, you can define it in the following property: + +[source,properties] +---- +spring.cloud.nacos.config.group=DEVELOP_GROUP +---- + +NOTE: This configuration must be in the bootstrap.properties file, and the value of Group must be the same with the value of `spring.cloud.nacos.config.group`. + +=== Support Custom Data Id + +As of Spring Cloud Alibaba Nacos Config, data id can be self-defined. For detailed design of this part, refer to https://github.com/spring-cloud-incubator/spring-cloud-alibaba/issues/141[Github issue]. +The following is a complete sample: + +[source,properties] +---- +spring.application.name=opensource-service-provider +spring.cloud.nacos.config.server-addr=127.0.0.1:8848 + +# config external configuration +# 1. Data Id is in the default group of DEFAULT_GROUP, and dynamic refresh of configurations is not supported. +spring.cloud.nacos.config.ext-config[0].data-id=ext-config-common01.properties + +# 2. Data Id is not in the default group, and dynamic refresh of configurations is not supported. +spring.cloud.nacos.config.ext-config[1].data-id=ext-config-common02.properties +spring.cloud.nacos.config.ext-config[1].group=GLOBALE_GROUP + +# 3. Data Id is not in the default group and dynamic referesh of configurations is supported. +spring.cloud.nacos.config.ext-config[2].data-id=ext-config-common03.properties +spring.cloud.nacos.config.ext-config[2].group=REFRESH_GROUP +spring.cloud.nacos.config.ext-config[2].refresh=true +---- + +We can see that: + +* Support multiple data ids by configuring `spring.cloud.nacos.config.ext-config[n].data-id`. +* Customize the group of data id by configuring `spring.cloud.nacos.config.ext-config[n].group`. If not specified, DEFAULT_GROUP is used. +* Control whether this data id supports dynamic refresh of configurations is supported when configurations are changed by configuring `spring.cloud.nacos.config.ext-config[n].refresh`. + It’s not supported by default. + + +NOTE: When multiple Data Ids are configured at the same time, the priority is defined by the value of “n” in `spring.cloud.nacos.config.ext-config[n].data-id`. The bigger the value, the higher the priority. + +NOTE: The value of `spring.cloud.nacos.config.ext-config[n].data-id` must have a file extension, and it could be properties or yaml/yml. +The setting in `spring.cloud.nacos.config.file-extension` does not have any impact on the custom Data Id file extension. + +The configuration of custom Data Id allows the sharing of configurations among multiple applications, and also enables support of multiple configurations for one application. + +To share the data id among multiple applications in a clearer manner, you can also use the following method: + +[source,properties] +---- +spring.cloud.nacos.config.shared-dataids=bootstrap-common.properties,all-common.properties +spring.cloud.nacos.config.refreshable-dataids=bootstrap-common.properties +---- + +We can see that: + +* Multiple shared data ids can be configured using `spring.cloud.nacos.config.shared-dataids` , and the data ids are separted by commas. +* `spring.cloud.nacos.config.refreshable-dataids` is used to control which data ids will be refreshed dynamically when configurations are updated, and that the latest configuration values can be retrieved by applications. Data ids are separated with commas. + If not specified, all shared data ids will not be dynamically refreshed. + +NOTE: When using `spring.cloud.nacos.config.shared-dataids` to configure multiple shared data ids, +we agree on the following priority between the shared configurations: Priorities are decided based on the order in which the configurations appear. The one that occurs later is higher in priority than the one that appears first. + +NOTE: When using `spring.cloud.nacos.config.shared-dataids`, the data Id must have a file extension, and it could be properties or yaml/yml. +And the configuration in `spring.cloud.nacos.config.file-extension` does not have any impact on the customized Data Id file extension. + +NOTE: When `spring.cloud.nacos.config.refreshable-dataids` specifies the data ids that support dynamic refresh, the corresponding values of the data ids should also specify file extensions. \ No newline at end of file diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc/nacos-discovery.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc/nacos-discovery.adoc index 21a1177b..16a3ca38 100644 --- a/spring-cloud-alibaba-docs/src/main/asciidoc/nacos-discovery.adoc +++ b/spring-cloud-alibaba-docs/src/main/asciidoc/nacos-discovery.adoc @@ -1 +1,317 @@ == Spring Cloud Alibaba Nacos Discovery + +This project provides seamless integration with Nacos for Spring Boot applications in terms of service registration and discovery through automatic configurations and other standard usages of the Spring programming model. +Nacos has endured the tough tests of the Double 11 Shopping festivals for years. By adding a few simple annotations, you can register a service quickly and use Nacos as the service registration center for a large scale distributed system. + +=== Service Registration and Discovery: Nacos Discovery Starter + +Service discovery is one of the key components in the microservices architecture. In such a architecture, configuring a service list for every client manually could be a daunting task, and makes dynamic scaling extremely difficult. + Nacos Discovery Starter helps you to register your service to the Nacos server automatically, and the Nacos server keeps track of the services and refreshes the service list dynamically. In addition, Nacos +Discovery Starter registers some of the metadata of the service instance, such as host, port, health check URL, homepage to Nacos. For details about how to download and start Nacos, refer to the https://nacos.io/zh-cn/docs/quick-start.html[Nacos Website]。 + +==== How to Introduce Nacos Discovery Starter + +To introduce Nacos Discovey Starter into your project, use the group ID of `org.springframework.cloud` and the artifact ID of `spring-cloud-starter-alibaba-nacos-discovery`. +pom.xml sample: + +[source,xml,indent=0] +---- + + + + + org.springframework.cloud + spring-cloud-alibaba-dependencies + 0.2.1.RELEASE + pom + import + + + + + + + + org.springframework.cloud + spring-cloud-starter-alibaba-nacos-discovery + + +---- + +==== Start a Provider Application + +If you use the Finchley.SR1 version of Spring Cloud , then you will need to be very cautious when selecting the Spring Boot version, because the version incompatibility might lead to many unexpected results. +The best practice for Spring Cloud Finchley.SR1 is to use the 2.0.6 release of Spring Boot. When starting a provider application, please also check if your Spring Boot version is +1.X.Y.RELEASE or 2.1.0.RELEASE. If not, please change it to 2.0.6.RELEASE. + +The following sample illustrates how to register a service to Nacos. + +1. Configuration of pom.xml The following is a complete example of pom.xml: +[source, xml] +---- + + + 4.0.0 + + open.source.test + nacos-discovery-test + 1.0-SNAPSHOT + nacos-discovery-test + + + org.springframework.boot + spring-boot-starter-parent + 2.0.6.RELEASE + + + + + UTF-8 + UTF-8 + 1.8 + + + + + + org.springframework.cloud + spring-cloud-dependencies + Finchley.SR1 + pom + import + + + org.springframework.cloud + spring-cloud-alibaba-dependencies + 0.2.1.RELEASE + pom + import + + + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-actuator + + + + org.springframework.cloud + spring-cloud-starter-alibaba-nacos-discovery + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + +---- + +2. Configuration of application.properties Some of the basic configurations of Nacos must be included in application.properties(or application.yaml), as shown below: +application.properties +[source,properties] +---- +server.port=8081 +spring.application.name=nacos-producer +spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 +management.endpoints.web.exposure.include=* +---- + + +NOTE: If you do not want to use Nacos for service registration and discovery, you can set `spring.cloud.nacos.discovery` to `false`. + +3. The following is a sample for starting Provider: +[source,java,indent=0] +---- +@SpringBootApplication +@EnableDiscoveryClient +public class NacosProviderDemoApplication { + + public static void main(String[] args) { + SpringApplication.run(NacosProducerDemoApplication.class, args); + } + + @RestController + public class EchoController { + @GetMapping(value = "/echo/{string}") + public String echo(@PathVariable String string) { + return "Hello Nacos Discovery " + string; + } + } +} +---- + +Now you can see the registered services on the Nacos console. + +NOTE: Before you start the provider application, please start Nacos first. Refer to https://nacos.io/zh-cn/docs/quick-start.html[Naco Website] for more details. + +=== Service EndPoint + +spring-cloud-starter-alibaba-nacos-discovery provides an EndPoint, and the address is `http://ip:port/actuator/nacos-discovery`. +The EndPoint mainly provides two types of information: + + 1. Subscribe: Shows the current service subscribers + 2. NacosDiscoveryProperties: Shows the current basic Nacos configurations of the current service + +The followings shows how a service instance accesses the EndPoint: + +[source, json] +---- +{ + "subscribe": [ + { + "jsonFromServer": "", + "name": "nacos-provider", + "clusters": "", + "cacheMillis": 10000, + "hosts": [ + { + "instanceId": "30.5.124.156#8081#DEFAULT#nacos-provider", + "ip": "30.5.124.156", + "port": 8081, + "weight": 1.0, + "healthy": true, + "enabled": true, + "cluster": { + "serviceName": null, + "name": null, + "healthChecker": { + "type": "TCP" + }, + "defaultPort": 80, + "defaultCheckPort": 80, + "useIPPort4Check": true, + "metadata": { + + } + }, + "service": null, + "metadata": { + + } + } + ], + "lastRefTime": 1541755293119, + "checksum": "e5a699c9201f5328241c178e804657e11541755293119", + "allIPs": false, + "key": "nacos-producer", + "valid": true + } + ], + "NacosDiscoveryProperties": { + "serverAddr": "127.0.0.1:8848", + "endpoint": "", + "namespace": "", + "logName": "", + "service": "nacos-provider", + "weight": 1.0, + "clusterName": "DEFAULT", + "metadata": { + + }, + "registerEnabled": true, + "ip": "30.5.124.201", + "networkInterface": "", + "port": 8082, + "secure": false, + "accessKey": "", + "secretKey": "" + } +} +---- + +=== Start a Consumer Application + +It might not be as easy as starting a provider application, because the consumer needs to call the RESTful service of the provider. In this example, we will use the most primitive way, that is, +combining the LoadBalanceClient and RestTemolate explicitly to access the RESTful service. +You can refer to section 1.2 for pom.xml and application.properties configurations. The following is the sample code for starting a consumer application. + +NOTE: You can also access the service by using RestTemplate and FeignClient with load balancing. + +[source, java] +---- +@SpringBootApplication +@EnableDiscoveryClient +public class NacosConsumerApp { + + @RestController + public class NacosController{ + + @Autowired + private LoadBalancerClient loadBalancerClient; + @Autowired + private RestTemplate restTemplate; + + @Value("${spring.application.name}") + private String appName; + + @GetMapping("/echo/app-name") + public String echoAppName(){ + //Access through the combination of LoadBalanceClient and RestTemolate + ServiceInstance serviceInstance = loadBalancerClient.choose("nacos-provider"); + String url = String.format("http://%s:%s/echo/%s",serviceInstance.getHost(),serviceInstance.getPort(),appName); + System.out.println("request url:" +url); + return restTemplate.getForObject(url,String.class); + } + + } + + //Instantiate RestTemplate Instance + @Bean + public RestTemplate restTemplate(){ + + return new RestTemplate(); + } + + public static void main(String[] args) { + + SpringApplication.run(NacosConsumerApp.class,args); + } +} +---- + +In this example, we injected a LoadBalancerClient instance, and instantiated a RestTemplate manually. At the same time, we injected the configuration value of `spring.application.name` into the application, +so that the current application name can be displayed when calling the service of the provider. + +NOTE: Please start Nacos before you start the consumer application. For details, please refer to https://nacos.io/zh-cn/docs/quick-start.html[Nacos Website]. + +Next, access the `http://ip:port/echo/app-name` interface provided by the consumer. Here we started the port of 8082. The access result is shown below: + + Address:http://127.0.0.1:8082/echo/app-name + Access result: Hello Nacos Discovery nacos-consumer + +=== More Information about Nacos Starter Configurations + +The following shows the other configurations of the starter of spring-cloud-starter-alibaba-nacos-discovery: + +:frame: topbot +[width="60%",options="header"] +|==== +^|Configuration ^|Key ^|Default Value ^|Description +|`Server address`|`spring.cloud.nacos.discovery.server-addr`|`No default value`|`IP and port of the Nacos Server listener` +|`Service name`|`spring.cloud.nacos.discovery.service`|`${spring.application.name}`|`Name the current service` +|`Weight`|`spring.cloud.nacos.discovery.weight`|`1`|`Value range: 1 to 100. The bigger the value, the greater the weight` +|`Network card name`|`spring.cloud.nacos.discovery.network-interface`|`No default value`|`If the IP address is not specified, the registered IP address is the IP address of the network card. If this is not specified either, the IP address of the first network card will be used by default.` +|`Registered IP address`|`spring.cloud.nacos.discovery.ip`|`No default value`|`Highest priority` +|`Registered port`|`spring.cloud.nacos.discovery.port`|`-1`|`Will be detected automatically by default. Do not need to be configured.` +|`Namespace`|`spring.cloud.nacos.discovery.namespace`|`No default value`|`A typical scenario is to isolate the service registration for different environment, such as resource (configurations, services etc.) isolation between testing and production environment` ` +|`AccessKey`|`spring.cloud.nacos.discovery.access-key`|`No default value`|`Alibaba Cloud account` +|`SecretKey`|`spring.cloud.nacos.discovery.secret-key`|`No default value`|`Alibaba Cloud account accesskey` +|`Metadata`|`spring.cloud.nacos.discovery.metadata`|`No default value`|`You can define some of the metadata for your services in the Map format` +|`Log file name`|`spring.cloud.nacos.discovery.log-name`|`No default value`| +|`Endpoint`|`spring.cloud.nacos.discovery.enpoint`|`UTF-8`|`The domain name of a certain service in a specific region. You can retrieve the server address dynamically with this domain name` +|`Integrate Ribbon or not`|`ribbon.nacos.enabled`|`true`|`Set to true in most cases` +|==== + diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc/oss.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc/oss.adoc index 20b43f75..6a73be89 100644 --- a/spring-cloud-alibaba-docs/src/main/asciidoc/oss.adoc +++ b/spring-cloud-alibaba-docs/src/main/asciidoc/oss.adoc @@ -1 +1,157 @@ -== Spring Cloud AliCloud OSS +== Spring Cloud Alibaba Cloud OSS + +OSS(Object Storage Service)is a storage product on Alibaba Cloud. Spring Cloud Alibaba Cloud OSS provides the commercialized storage service in conformity with Spring Cloud specifications. We provide easy-to-use APIs and supports the integration of Resource in the Spring framework. + +=== How to Introduce Spring Cloud Alibaba Cloud OSS + +We’ve released Spring Cloud Alibaba version 0.2.1. You will need to add dependency management POM first. + +[source,xml] +---- + + + + org.springframework.cloud + spring-cloud-alibaba-dependencies + 0.2.1.RELEASE + pom + import + + + +---- + +Next we need to introduce Spring Cloud Alibaba Cloud OSS Starter. + +[source,xml] +---- + + org.springframework.cloud + spring-cloud-starter-alicloud-oss + +---- + +=== How to Use OSS API + +==== Configure OSS + +Before you start to use Spring Cloud Alibaba Cloud OSS, please add the following configurations in application.properties. + +[source,properties] +---- +spring.cloud.alicloud.access-key=Your Alibaba Cloud AK +spring.cloud.alicloud.secret-key=Your Alibaba Cloud SK +spring.cloud.alicloud.oss.endpoint=***.aliyuncs.com +---- + +access-key and secret-key is the AK/SK of your Alibaba Cloud account. If you don’t have one, please register an account first, and log on to https://usercenter.console.aliyun.com/#/manage/ak[Alibaba Cloud AK/SK Management] to get your AccessKey ID and Access Key Secret . If you haven’t create the AccessKeys, click “Create AccessKey” to create one. + +For endpoint information, please refer to the OSS https://help.aliyun.com/document_detail/31837.html[Documentation] and get the endpoint for your region. + + +==== Introduce OSS API + +The OSS API of Spring Cloud Alibaba Cloud OSS is based on the official OSS SDK, and includes APIs for uploading, downloading, viewing files. + +Here is a simple application that uses the OSS API. + +[source,java] +---- +@SpringBootApplication +public class OssApplication { + + @Autowired + private OSS ossClient; + + @RequestMapping("/") + public String home() { + ossClient.putObject("bucketName", "fileName", new FileInputStream("/your/local/file/path")); + return "upload success"; + } + + public static void main(String[] args) throws URISyntaxException { + SpringApplication.run(OssApplication.class, args); + } + +} +---- + +Before you upload your files, please https://account.aliyun.com/register/register.htm?spm=5176.8142029.388261.26.e9396d3eaYK2sG&oauth_callback=https%3A%2F%2Fwww.aliyun.com%2F[Register an Alibaba Cloud Account]. If you already have one, please https://common-buy.aliyun.com/?spm=5176.8465980.unusable.dopen.4cdf1450rg8Ujb&commodityCode=oss#/open[Sign up for OSS]. + +Log on to the https://oss.console.aliyun.com/overview[OSS Console], click “Create New Bucket” and create a bucket as instructed. Replace the bucket name in the “bucketname” of the previous code with your new bucket name. "fileName” can be any name you like, and "/your/local/file/path” can be any local file path. Next you can run `curl http://127.0.0.1:port number/ to upload your files, and you will see your file on the https://oss.console.aliyun.com/overview[OSS Console]. + +For more instructions on OSS APIs, please refer to https://help.aliyun.com/document_detail/32008.html[OSS SDK Documentation]. + +=== Integrate with the Resource Specifications of Spring + +Spring Cloud Alibaba Cloud OSS integrates the Resource of the Spring framework, which allows you to use the OSS resources easily. + +The following is a simple example of how to use Resource. + +[source,java] +---- +@SpringBootApplication +public class OssApplication { + + @Value("oss://bucketName/fileName") + private Resource file; + + @GetMapping("/file") + public String fileResource() { + try { + return "get file resource success. content: " + StreamUtils.copyToString( + file.getInputStream(), Charset.forName(CharEncoding.UTF_8)); + } catch (Exception e) { + return "get resource fail: " + e.getMessage(); + } + } + + public static void main(String[] args) throws URISyntaxException { + SpringApplication.run(OssApplication.class, args); + } + +} +---- + +NOTE: A prerequisite for the above sample is that you need to have a bucket named “bucketName” on OSS, and you have a file named “fileName” in this bucket. + +=== Use STS Authentication + +In addition to AccessKeys, Spring Cloud Alibaba Cloud OSS also supports STS authentication. STS is an authentication method with temporary security tokens, and is usually used for a third party to access its resources temporarily. + +For a third party to access resources temporarily, it only needs to complete the following configurations. + +[source,properties] +---- +spring.cloud.alicloud.oss.authorization-mode=STS +spring.cloud.alicloud.oss.endpoint=***.aliyuncs.com +spring.cloud.alicloud.oss.sts.access-key=Your authenticated AK +spring.cloud.alicloud.oss.sts.secret-key=Your authenticated SK +spring.cloud.alicloud.oss.sts.security-token=Your authenticated ST +---- + +Among which, spring.cloud.alicloud.oss.authorization-mode is the enumeration type. Fill in STS here means that STS authentication is used. For endpoint information, refer to the https://help.aliyun.com/document_detail/31837.html[OSS Documentation] and fill in the endpoint for your region. + +Access-key, secret-key and the security-token need to be issued by the authentication side. For more information about STS, refer to https://help.aliyun.com/document_detail/31867.html[STS Documentation]. + +=== More Configurations for the Client + +In addition to basic configurations, Spring Cloud Alibaba Cloud OSS also supports many other configurations, which are also included in the application.properties file. + +Here are some examples. + +[source,properties] +---- +spring.cloud.alicloud.oss.authorization-mode=STS +spring.cloud.alicloud.oss.endpoint=***.aliyuncs.com +spring.cloud.alicloud.oss.sts.access-key=Your authenticated AK +spring.cloud.alicloud.oss.sts.secret-key=Your authenticated SK +spring.cloud.alicloud.oss.sts.security-token=Your authenticated ST + +spring.cloud.alicloud.oss.config.connection-timeout=3000 +spring.cloud.alicloud.oss.config.max-connections=1000 +---- + +For more configurations, refer to the table at the bottom of https://help.aliyun.com/document_detail/32010.html[OSSClient Configurations]. + +NOTE: In most cases, you need to connect the parameter names with “-” for the parameters in the table of https://help.aliyun.com/document_detail/32010.html[OSSClient Configurations] with “-”, and all letters should be in lowercase. For example, ConnectionTimeout should be changed to connection-timeout. \ No newline at end of file diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc/rocketmq.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc/rocketmq.adoc index 238f0b4b..81e36a94 100644 --- a/spring-cloud-alibaba-docs/src/main/asciidoc/rocketmq.adoc +++ b/spring-cloud-alibaba-docs/src/main/asciidoc/rocketmq.adoc @@ -1 +1,250 @@ -== Spring Cloud Alibaba Rocket Binder +== Spring Cloud Alibaba RocketMQ Binder + +### Introduction of RocketMQ + +https://rocketmq.apache.org[RocketMQ] is an open-source distributed message system. It is based on highly available distributed cluster technologies and provides message publishing and subscription service with low latency and high stability. RocketMQ is widely used in a variety of industries, such as decoupling of asynchronous communication, enterprise sulotions, financial settlements, telecommunication, e-commerce, logistics, marketing, social media, instant messaging, mobile applications, mobile games, vedios, IoT, and Internet of Vehicles. + +It has the following features: + +* Strict order of message sending and consumption + +* Rich modes of message pulling + +* Horizontal scalability of consumers + +* Real-time message subscription + +* Billion-level message accumulation capability + +### RocketMQ Usages + +* Download RocketMQ + +Download https://www.apache.org/dyn/closer.cgi?path=rocketmq/4.3.2/rocketmq-all-4.3.2-bin-release.zip[Latest Binary File of RocketMQ], and decompress it. + +The decompressed directory is as follows: + +``` +apache-rocketmq +├── LICENSE +├── NOTICE +├── README.md +├── benchmark +├── bin +├── conf +└── lib +``` + +* Start NameServer + +```bash +nohup sh bin/mqnamesrv & +tail -f ~/logs/rocketmqlogs/namesrv.log +``` + +* Start Broker + +```bash +nohup sh bin/mqbroker -n localhost:9876 & +tail -f ~/logs/rocketmqlogs/broker.log +``` + +* Send and Receive Messages + +Send messages: + +```bash +sh bin/tools.sh org.apache.rocketmq.example.quickstart.Producer +``` + +Output when the message is successfuly sent: `SendResult [sendStatus=SEND_OK, msgId= ...` + +Receive messages: + +```bash +sh bin/tools.sh org.apache.rocketmq.example.quickstart.Consumer +``` + +Output when the message is successfully received: `ConsumeMessageThread_%d Receive New Messages: [MessageExt...` + +* Disable Server + +```bash +sh bin/mqshutdown broker +sh bin/mqshutdown namesrv +``` + +### Introduction of Spring Cloud Stream + +Spring Cloud Stream is a microservice framework used to build architectures based on messages. It helps you to create production-ready single-server Spring applications based on SpringBoot, and connects with Broker using `Spring Integration`. + +Spring Cloud Stream provides unified abstractions of message middleware configurations, and puts forward concepts such as publish-subscribe, consumer groups and partition. + +There are two concepts in Spring Cloud Stream: Binder and Binding + +* Binder: A component used to integrate with external message middleware, and is used to create binding. Different message middleware products have their own binder implementations. + +For example, `Kafka` uses `KafkaMessageChannelBinder`, `RabbitMQ` uses `RabbitMessageChannelBinder`, while `RocketMQ` uses `RocketMQMessageChannelBinder`. + +* Binding: Includes Input Binding and Output Binding。 + +Binding serves as a bridge between message middleware and the provider and consumer of the applications. Developers only need to use the Provider or Consumer to produce or consume data, and do not need to worry about the interactions with the message middleware. + +.Spring Cloud Stream +image::https://docs.spring.io/spring-cloud-stream/docs/current/reference/htmlsingle/images/SCSt-overview.png[] + +Now let’s use Spring Cloud Stream to write a simple code for sending and receiving messages: + +```java +MessageChannel messageChannel = new DirectChannel(); + +// Message subscription +((SubscribableChannel) messageChannel).subscribe(new MessageHandler() { + @Override + public void handleMessage(Message message) throws MessagingException { + System.out.println("receive msg: " + message.getPayload()); + } +}); + +// Message sending +messageChannel.send(MessageBuilder.withPayload("simple msg").build()); +``` + +All the message types in this code are provided by the `spring-messaging`module. It shields the lower-layer implementations of message middleware. If you would like to change the message middleware, you only need to configure the related message middleware information in the configuration file and modify the binder dependency. + +**The lower layer of Spring Cloud Stream also implements various code abstractions based on the previous code.** + +### How Spring Cloud Alibaba RocketMQ Binder Works + +.RocketMQ Binder Workflow +image::https://cdn.nlark.com/lark/0/2018/png/64647/1543560843558-24525bf4-1d0e-4e10-be5f-bdde7127f6e6.png[] + + +The core of RocketMQ Binder are the 3 classes below: `RocketMQMessageChannelBinder`,`RocketMQInboundChannelAdapter` and `RocketMQMessageHandler`. + +`RocketMQMessageChannelBinder` is a standard implementation of Binder. It contains `RocketMQInboundChannelAdapter` and `RocketMQMessageHandler` as its internal constructions. + +`RocketMQMessageHandler` is used to start RocketMQ `Producer` and send messages. It creates message type of RocketMQ `org.apache.rocketmq.common.message.Message` based on the message type of `org.springframework.messaging.Message` in the `spring-messaging` module. + +When constructing `org.apache.rocketmq.common.message.Message`, it constructs `RocketMQMessageHeaderAccessor` based on the header of `org.springframework.messaging.Message`. Then, based on some of the properties in `RocketMQMessageHeaderAccessor` , it sets some of message attributes such as tags, keys, and flag in `org.apache.rocketmq.common.message.Message` of RocketMQ. + +`RocketMQInboundChannelAdapter` is used to start RocketMQ `Consumer` and receive messages. It also support the usage of https://github.com/spring-projects/spring-retry[spring-retry]. + +You can also obtain `Acknowledgement` from the Header and make some configurations. + +For example, you can set delayed message consumption when `MessageListenerConcurrently` is used for asynchronous message consumption: + +```java +@StreamListener("input") +public void receive(Message message) { + RocketMQMessageHeaderAccessor headerAccessor = new RocketMQMessageHeaderAccessor(message); + Acknowledgement acknowledgement = headerAccessor.getAcknowledgement(message); + acknowledgement.setConsumeConcurrentlyStatus(ConsumeConcurrentlyStatus.RECONSUME_LATER); + acknowledgement.setConsumeConcurrentlyDelayLevel(1); +} +``` + +You can also set delayed message consumption when `MessageListenerOrderly` is used for consuming ordered messages. + +```java +@StreamListener("input") +public void receive(Message message) { + RocketMQMessageHeaderAccessor headerAccessor = new RocketMQMessageHeaderAccessor(message); + Acknowledgement acknowledgement = headerAccessor.getAcknowledgement(message); + acknowledgement.setConsumeOrderlyStatus(ConsumeOrderlyStatus.SUSPEND_CURRENT_QUEUE_A_MOMENT); + acknowledgement.setConsumeOrderlySuspendCurrentQueueTimeMill(5000); +} +``` + +Supported Configurations of Provider: + +:frame: topbot +[width="60%",options="header"] +|==== +^|Configuration ^|Description ^| Default Value +|`spring.cloud.stream.rocketmq.bindings.your-output-binding.producer.enabled`|Whether to use producer|true +|`spring.cloud.stream.rocketmq.bindings.your-output-binding.producer.max-message-size`|Maximum bytes of messages sent|0(Take effect only when it’s bigger than 0. The default value of RocketMQ is 4M = 1024 * 1024 * 4) +|`spring.cloud.stream.rocketmq.bindings.your-output-binding.producer.transactional`|Whether to use `TransactionMQProducer` to send transaction messages|false +|`spring.cloud.stream.rocketmq.bindings.your-output-binding.producer.executer`|Full class name of the interface implementation class related to `org.apache.rocketmq.client.producer.LocalTransactionExecuter` For example, `org.test.MyExecuter`| +|`spring.cloud.stream.rocketmq.bindings.your-output-binding.producer.transaction-check-listener`|Full class name of the interface implementation class related to `org.apache.rocketmq.client.producer.TransactionCheckListener` For example, `org.test.MyTransactionCheckListener`| +|==== + +Supported Configurations of Consumer: + +:frame: topbot +[width="60%",options="header"] +|==== +^|Configuration ^|Description| Default Value +|`spring.cloud.stream.rocketmq.bindings.your-input-binding.consumer.enabled`|Whether to use consumer|true +|`spring.cloud.stream.rocketmq.bindings.your-input-binding.consumer.tags`|Consumer will only subscribe to messages with these tags Tags are separated by "\|\|" (If not specified, it means the consumer subscribes to all messages)| +|`spring.cloud.stream.rocketmq.bindings.your-input-binding.consumer.sql`|Consumer subscribes to the messages as requested in the SQL(If tags are also specified, SQL has a higher priority than tags.)| +|`spring.cloud.stream.rocketmq.bindings.your-input-binding.consumer.broadcasting`|If the consumer uses the broadcasting mode|false +|`spring.cloud.stream.rocketmq.bindings.your-input-binding.consumer.orderly`|Ordered message consumption or asychronous consumption|false +|==== + +### Endpoint Support + +Before you use the Endpoint feature, please add the `spring-boot-starter-actuator` dependency in Maven, and enable access of Endpoints in your configuration. + +* Add `management.security.enabled=false`in Spring Boot 1.x. The exposed endpoint path is `/rocketmq_binder` +* Add `management.endpoints.web.exposure.include=*`in Spring Boot 2.x. The exposed endpoint path is `/actuator/rocketmq-binder` + +Endpoint will collects data about the last message that is sent, the number of successes or failures of message sending, and the number of successes of failures of message consumption. + +```json +{ + "runtime": { + "lastSend.timestamp": 1542786623915 + }, + "metrics": { + "scs-rocketmq.consumer.test-topic.totalConsumed": { + "count": 11 + }, + "scs-rocketmq.consumer.test-topic.totalConsumedFailures": { + "count": 0 + }, + "scs-rocketmq.producer.test-topic.totalSentFailures": { + "count": 0 + }, + "scs-rocketmq.consumer.test-topic.consumedPerSecond": { + "count": 11, + "fifteenMinuteRate": 0.012163847780107841, + "fiveMinuteRate": 0.03614605351360527, + "meanRate": 0.3493213353657594, + "oneMinuteRate": 0.17099243039490175 + }, + "scs-rocketmq.producer.test-topic.totalSent": { + "count": 5 + }, + "scs-rocketmq.producer.test-topic.sentPerSecond": { + "count": 5, + "fifteenMinuteRate": 0.005540151995103271, + "fiveMinuteRate": 0.01652854617838251, + "meanRate": 0.10697493212602836, + "oneMinuteRate": 0.07995558537067671 + }, + "scs-rocketmq.producer.test-topic.sentFailuresPerSecond": { + "count": 0, + "fifteenMinuteRate": 0.0, + "fiveMinuteRate": 0.0, + "meanRate": 0.0, + "oneMinuteRate": 0.0 + }, + "scs-rocketmq.consumer.test-topic.consumedFailuresPerSecond": { + "count": 0, + "fifteenMinuteRate": 0.0, + "fiveMinuteRate": 0.0, + "meanRate": 0.0, + "oneMinuteRate": 0.0 + } + } +} +``` + +Note: To view statistics, add the https://mvnrepository.com/artifact/io.dropwizard.metrics/metrics-core[metrics-core dependency] in POM. If not added, the endpoint will return warning instead of statistics: + +```json +{ + "warning": "please add metrics-core dependency, we use it for metrics" +} +``` \ No newline at end of file diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc/schedulerx.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc/schedulerx.adoc index e69de29b..bffdc3fb 100644 --- a/spring-cloud-alibaba-docs/src/main/asciidoc/schedulerx.adoc +++ b/spring-cloud-alibaba-docs/src/main/asciidoc/schedulerx.adoc @@ -0,0 +1,131 @@ +== Spring Cloud Alibaba Cloud SchedulerX + +SchedulerX(Distributed job scheduling) is a component of EDAS, an Alibaba Cloud product. Spring Cloud Alibaba Cloud SchedulerX provides distributed job scheduling in conformity with the Spring Cloud specifications. SchedulerX provides timed job scheduling service with high accuracy with seconds, high stability and high availabiliy, and supports multiple job types, such as simple single-server jobs, simple multi-host jobs, script jobs, and grid jobs. + +=== How to Introduce Spring Cloud Alibaba Cloud SchedulerX + +We’ve released Spring Cloud Alibaba version 0.2.1. You will need to add dependency management POM first. + +[source,xml] +---- + + + + org.springframework.cloud + spring-cloud-alibaba-dependencies + 0.2.1.RELEASE + pom + import + + + +---- + +Next we need to introduce Spring Cloud AliCloud SchedulerX Starter. + +[source,xml] +---- + + org.springframework.cloud + spring-cloud-starter-alicloud-schedulerX + +---- + +=== Start SchedulerX + +After Spring Cloud Alibaba Cloud SchedulerX Starter is introduced into the client, you only need to complete a few simple configurations and you will be able to initialize the SchedulerX service automatically. + +The following is a simple example. + +[source,java] +---- +@SpringBootApplication +public class ScxApplication { + + public static void main(String[] args) { + SpringApplication.run(ScxApplication.class, args); + } + +} +---- + +Add the following configurations in the application.properties file. + +[source,properties] +---- +server.port=18033 +# cn-test is the test region of SchedulerX +spring.cloud.alicloud.scx.group-id=*** +spring.cloud.alicloud.edas.namespace=cn-test +---- + +Before getting the group-id, please https://account.aliyun.com/register/register.htm?spm=5176.8142029.388261.26.e9396d3eEIv28g&oauth_callback=https%3A%2F%2Fwww.aliyun.com%2F[Register an Alibaba Cloud account], and then https://common-buy.aliyun.com/?spm=5176.11451019.0.0.6f5965c0Uq5tue&commodityCode=edaspostpay#/buy[Sign up for EDAS] and https://edas.console.aliyun.com/#/edasTools[Sign up for SchedulerX] as well. + +To get the group-id, refer to the https://help.aliyun.com/document_detail/98784.html[SchedulerX Documentation]. + +NOTE: When you create a group, please select the “test” region. + +=== Compile a simple job + +Simple job is the most commonly used job type. You only need to implement the ScxSimpleJobProcessor interface. + +The following is a sample of a simple single-server job. + +[source,java] +---- +public class SimpleTask implements ScxSimpleJobProcessor { + + @Override + public ProcessResult process(ScxSimpleJobContext context) { + System.out.println("-----------Hello world---------------"); + ProcessResult processResult = new ProcessResult(true); + return processResult; + } + +} +---- + +=== Job Scheduling + +Go to the https://edas.console.aliyun.com/#/edasSchedulerXJob?regionNo=cn-test[SchedulerX Jobs] page, select the “Test” region, and click “Create Job” on the upper-right corner to create a job, as shown below. + +[source,text] +---- +Job Group: Test——***-*-*-**** +Job process interface:org.springframework.cloud.alibaba.cloud.examples.SimpleTask +Type: Simple Single-Server Job +Quartz Cron Expression: Default Option——0 * * * * ? +Job Description: Empty +Custom Parameters: Empty +---- + +The job above is a “Simple Single-Server Job”, and speficied a Cron expression of "0 * * * * ?" . This means that the job will be executed once and once only in every minute. + +For more job types, refer to https://help.aliyun.com/document_detail/43136.html[SchedulerX Documentation]. + +=== Usage in Production Environment + +The previous examples shows how to use SchedulerX in the “Test” region, which is mainly used for local testing. + +At the production level, you need to complete some other configurations in addition to the group-id and namespace as mentioned above. See examples below: + +[source,properties] +---- +server.port=18033 +# cn-test is the test region of SchedulerX +spring.cloud.alicloud.scx.group-id=*** +spring.cloud.alicloud.edas.namespace=*** +# If your application runs on EDAS, you do not need to configure the following. +spring.cloud.alicloud.access-key=*** +spring.cloud.alicloud.secret-key=*** +# The following configurations are not mandatory. You can refer to the SchedulerX documentation for details. +spring.cloud.alicloud.scx.domain-name=*** +---- + +The way to get the group-id is the same as described in the previous examples, and you can get the namespace by clicking “Namespaces” in the left-side navigation pane of the EDAS console. + +NOTE: Group-id must be created within a namespace. + +Access-key and secret-key are the AK/SK of your Alibaba Cloud account. If you deploy you applications on EDAS, then you do not need to fill in this information. Otherwise please go to https://usercenter.console.aliyun.com/#/manage/ak[Security Information] to get your AccessKeys. + +Domain-name is not mandatory. You can refer to https://help.aliyun.com/document_detail/35359.html[SchedulerX Documentation] for details. diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc/sentinel.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc/sentinel.adoc index 59eaa589..40478e4b 100644 --- a/spring-cloud-alibaba-docs/src/main/asciidoc/sentinel.adoc +++ b/spring-cloud-alibaba-docs/src/main/asciidoc/sentinel.adoc @@ -1 +1,337 @@ == Spring Cloud Alibaba Sentinel + +### Introduction of Sentinel + +As microservices become popular, the stability of service calls is becoming increasingly important. https://github.com/alibaba/Sentinel[Sentinel] takes "flow" as the breakthrough point, and works on multiple fields including flow control, circuit breaking and load protection to protect service reliability. + +https://github.com/alibaba/Sentinel[Sentinel] has the following features: + + +* *Rich Scenarios*: Sentinel has supported the key scenarios of Alibaba’s Double 11 Shopping Festivals for over 10 years, such as second kill(i.e., controlling sudden bursts of traffic flow so that it’s within the acceptable range of the system capacity), message load shifting, circuit breaking of unreliable downstream applications. +* *Comprehensive Real-Time Monitoring*: Sentinel provides real-time monitoring capability. You can see the monitoring data of your servers at the accuracy of seconds, and even the overall runtime status of a cluster with less than 500 nodes. +* *Extensive Open-Source Ecosystem*: Sentinel provides out-of-box modules that can be easily integrated with other open-source frameworks/libraries, such as Spring Cloud, Dubbo, and gRPC. To use Sentinel, you only need to introduce the related dependency and make a few simple configurations. +* *Sound SPI Extensions*: Sentinel provides easy-to-use and sound SPI extension interfaces. You can customize logics with the SPI extensions quickly, for example, you can define your own rule management, or adapt to specific data sources. + +### How to Use Sentinel + +If you want to use Sentinel in your project, please use the starter with the group ID as `org.springframework.cloud` and the artifact ID as `spring-cloud-starter-alibaba-sentinel`. + +```xml + + org.springframework.cloud + spring-cloud-starter-alibaba-sentinel + +``` + +The following is a simple example of how to use Sentinel: + +```java +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(ServiceApplication.class, args); + } + +} + +@RestController +public class TestController { + + @GetMapping(value = "/hello") + @SentinelResource("hello") + public String hello() { + return "Hello Sentinel"; + } + +} +``` + +The @SentinelResource annotation is used to identify if a resource is rate limited or degraded. In the above sample, the 'hello' attribute of the annotation refers to the resource name. + +@SentinelResource also provides attributes such as `blockHandler`, `blockHandlerClass`, and `fallback` to identify rate limiting or degradation operations. For more details, refer to https://github.com/alibaba/Sentinel/wiki/%E6%B3%A8%E8%A7%A3%E6%94%AF%E6%8C%81[Sentinel Annotation Support]. + +##### Sentinel Dashboard + +Sentinel dashboard is a lightweight console that provides functions such as machine discovery, single-server resource monitoring, overview of cluster resource data, as well as rule management. To use these features, you only need to complete a few steps. + +*Note*: The statistics overview for clusters only supports clusters with less than 500 nodes, and has a latency of about 1 to 2 seconds. + +.Sentinel Dashboard +image::https://github.com/alibaba/Sentinel/wiki/image/dashboard.png[] + +To use the Sentinel dashboard, simply complete the following 3 steps. + +###### Get the Dashboard + +You can download the latest dashboard JAR file from the https://github.com/alibaba/Sentinel/releases[Release Page]. + +You can also get the latest source code to build your own Sentinel dashboard: + +* Download the https://github.com/alibaba/Sentinel/tree/master/sentinel-dashboard[Dashboard] project. +* Run the following command to package the code into a FatJar: `mvn clean package` + + +###### Start the Dashboard + +Sentinel dashboard is a standard SpringBoot application, and you can run the JAR file in the Spring Boot mode. + +```shell +java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar +``` + +If there is conflict with the 8080 port, you can use `-Dserver.port=new port` to define a new port. + +#### Configure the Dashboard + +.application.yml +---- +spring: + cloud: + sentinel: + transport: + port: 8719 + dashboard: localhost:8080 +---- + +The port number specified in `spring.cloud.sentinel.transport.port` will start an HTTP Server on the corresponding server of the application, and this server will interact with the Sentinel dashboard. For example, if a rate limiting rule is added in the Sentinel dashboard, the the rule data will be pushed to and recieved by the HTTP Server, which in turn registers the rule to Sentinel. + +For more information about Sentinel dashboard, please refer to https://github.com/alibaba/Sentinel/wiki/%E6%8E%A7%E5%88%B6%E5%8F%B0[Sentinel Dashboard]. + +### Feign Support + +Sentinel is compatible with the https://github.com/OpenFeign/feign[Feign] component. To use it, in addition to introducing the `sentinel-starter` dependency, complete the following 3 steps: + +* Enable the Sentinel support for feign in the properties file. `feign.sentinel.enabled=true` +* Add the `feign starter` dependency to trigger and enable `sentinel starter`: +```xml + + org.springframework.cloud + spring-cloud-starter-openfeign + +``` +* The `loadbalance` in `feign` is dependent on the `ribbon` module of Netflix (You do not need to introduce it if `loadbalance` is not used): +```xml + + org.springframework.cloud + spring-cloud-starter-netflix-ribbon + +``` + +This is an interface for `FeignClient`: + +```java +@FeignClient(name = "service-provider", fallback = EchoServiceFallback.class, configuration = FeignConfiguration.class) +public interface EchoService { + @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET) + String echo(@PathVariable("str") String str); +} + +class FeignConfiguration { + @Bean + public EchoServiceFallback echoServiceFallback() { + return new EchoServiceFallback(); + } +} + +class EchoServiceFallback implements EchoService { + @Override + public String echo(@PathVariable("str") String str) { + return "echo fallback"; + } +} +``` + +NOTE: The resource name policy in the corresponding interface of Feign is:httpmethod:protocol://requesturl + +The corresponding resource name of the `echo` method in the `EchoService` interface is `GET:http://service-provider/echo/{str}`. + +Note: All the attributes in the `@FeignClient` annotation is supported by Sentinel. + +### RestTemplate Support + +Spring Cloud Alibaba Sentinel supports the protection of `RestTemplate` service calls using Sentinel. To do this, you need to add the `@SentinelRestTemplate` annotation when constructing the `RestTemplate` bean. + +```java +@Bean +@SentinelRestTemplate(blockHandler = "handleException", blockHandlerClass = ExceptionUtil.class) +public RestTemplate restTemplate() { + return new RestTemplate(); +} +``` + +The parameter of the `@SentinelRestTemplate` annotation support flow control(`blockHandler`, `blockHandlerClass`) and circuit breaking(`fallback`, `fallbackClass`). + +== + +The `blockHandler` or `fallback` is the static method of `blockHandlerClass` or `fallbackClass`. + +The parameter of method in `@SentinelRestTemplate` is same as `ClientHttpRequestInterceptor`, but it has one more parameter `BlockException` and its value of return type should be `ClientHttpResponse`. +If you do not continue to execute the following Http request interceptor after being restricted, you can use the instance of `SentinelClientHttpResponse` to customize the return value result after the current limit. + +The method signature of `handleException` in `ExceptionUtil` above should be like this: + +```java +public class ExceptionUtil { + public static ClientHttpResponse handleException(HttpRequest request, byte[] body, ClientHttpRequestExecution execution, BlockException exception) { + ... + } +} +``` + +It will return `RestTemplate request block by sentinel` when you do not write any flow control(`blockHandler`, `blockHandlerClass`) configuration or circuit breaking configuration(`fallback`, `fallbackClass`), you can override it by using your own method. + +Sentinel RestTemplate provides two granularities for resource rate limiting: + +* `schema://host:port/path`: Protocol, host, port and path + +* `schema://host:port`: Protocol, host and port + +NOTE: Take `https://www.taobao.com/test` as an example. The corresponding resource names have two levels of granularities, `https://www.taobao.com` and `https://www.taobao.com/test`. + +### Dynamic Data Source Support + +#### The usage of our first version 0.2.0.RELEASE or 0.1.0.RELEASE + +you need to complete the following 3 steps to configure your data source. + +* Define the data source information in your properties file. For example, you can use: + +.application.properties +---- +spring.cloud.sentinel.datasource.type=file +spring.cloud.sentinel.datasource.recommendRefreshMs=3000 +spring.cloud.sentinel.datasource.bufSize=4056196 +spring.cloud.sentinel.datasource.charset=utf-8 +spring.cloud.sentinel.datasource.converter=flowConverter +spring.cloud.sentinel.datasource.file=/Users/you/yourrule.json +---- + +* Create a Converter class to implement the `com.alibaba.csp.sentinel.datasource.Converter` interface, and you need to have a bean of this class in `ApplicationContext`. + +```java +@Component("flowConverter") +public class JsonFlowRuleListParser implements Converter> { + @Override + public List convert(String source) { + return JSON.parseObject(source, new TypeReference>() { + }); + } +} +``` + +The bean name of this Converter needs to be the same with the converter in the `application.properties` file. + +* Define a `ReadableDataSource` attribute that is modified by the `@SentinelDataSource` annotation in any Spring Bean `@SentinelDataSource`. + +```java +@SentinelDataSource("spring.cloud.sentinel.datasource") +private ReadableDataSource dataSource; +``` + +The value attribute of `@SentinelDataSource` means that the data source is in the prefix of the `application.properties` file. In this example, the prefix is `spring.cloud.sentinel.datasource`. + +If there are over 1 `ReadableDataSource` beans in `ApplicationContext`, then none of the data sources in `ReadableDataSource` will be loaded. It takes effect only when there is only 1 `ReadableDataSource` in `ApplicationContext`. + +If the data source takes effect and is loaded successfully, the dashboard will print information as shown below: + +``` +[Sentinel Starter] load 3 flow rules +``` + +#### The usage after first version + +You only need to complete 1 step to configure your data source: + +* Configure your data source in the `application.properties` file directly. + +For example, 4 data sources are configures: + +``` +spring.cloud.sentinel.datasource.ds1.file.file=classpath: degraderule.json + +spring.cloud.sentinel.datasource.ds2.nacos.server-addr=localhost:8848 +spring.cloud.sentinel.datasource.ds2.nacos.dataId=sentinel +spring.cloud.sentinel.datasource.ds2.nacos.groupId=DEFAULT_GROUP +spring.cloud.sentinel.datasource.ds2.nacos.data-type=json + +spring.cloud.sentinel.datasource.ds3.zk.path = /Sentinel-Demo/SYSTEM-CODE-DEMO-FLOW +spring.cloud.sentinel.datasource.ds3.zk.server-addr = localhost:2181 + +spring.cloud.sentinel.datasource.ds4.apollo.namespace-name = application +spring.cloud.sentinel.datasource.ds4.apollo.flow-rules-key = sentinel +spring.cloud.sentinel.datasource.ds4.apollo.default-flow-rule-value = test + +``` + +This method follows the configuration of Spring Cloud Stream Binder. `TreeMap` is used for storage internally, and comparator is `String.CASE_INSENSITIVE_ORDER`. + +NOTE: d1, ds2, ds3, ds4 are the names of `ReadableDataSource`, and can be coded as you like. The `file`, `zk`, `nacos` , `apollo` refer to the specific data sources. The configurations following them are the specific configurations of these data sources respecitively. + +Every data source has two common configuration items: `data-type` and `converter-class`. + +`data-type` refers to `Converter`. Spring Cloud Alibaba Sentinel provides two embedded values by defaul: `json` and `xml` (the default is json if not specified). If you do not want to use the embedded `json` or `xml` `Converter`, you can also fill in `custom` to indicate that you will define your own `Converter`, and then configure the `converter-class`. You need to specify the full path of the class for this configuration. + +The two embedded `Converter` only supports parsing the Json array or XML array. Sentinel will determine automatically which of the 4 Sentinel rules that the Json or XML objext belongs to(`FlowRule`, `DegradeRule`, `SystemRule`, `AuthorityRule`, `ParamFlowRule`). + +For example, if 5 rate limiting rules and 5 degradation rules in the 10 rule arrays, then the data source will not be registered, and there will be warnings in the logs. + +If there are 9 rate limiting rules among the 10 arrays, and 1 error parsing the 10th array, then there will be warning in the log indicating that there is error in one of the rules, while the other 9 rules will be registered. + +Here `jackson` is used to parse the Json or XML arrays. `ObjectMapper` or `XmlMapper` uses the default configuration and will report error if there are unrecognized fields. The reason behind this logic is that without this strict parsing, Json will also be parsed as system rules(all system rules have default values). + +In another case, the Json or XML object might match all of the 4 rules(for example, if only the `resource` field is configured in the Json object, then it will match all 4 rules). In this case, there will be warnings in the log, and this object will be filtered. + +When you use this configuration, you only need to fill in the Json or XML correctly, and any of the unreasonable information will be printed in the log. + +If the data source takes effect and is loaded successfully, the dashboard will print information as shown below: + +``` +[Sentinel Starter] DataSource ds1-sentinel-file-datasource load 3 DegradeRule +[Sentinel Starter] DataSource ds2-sentinel-nacos-datasource load 2 FlowRule +``` + +NOTE: XML format is not supported by default. To make it effective, you need to add the `jackson-dataformat-xml` dependency. + +To learn more about how dynamic data sources work in Sentinel, refer to https://github.com/alibaba/Sentinel/wiki/%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%99%E6%89%A9%E5%B1%95[Dynamic Rule Extension]. + +### Endpoint Support + +Before you use the Endpoint feature, please add the `spring-boot-starter-actuator` dependency in Maven, and enable access of Endpoints in your configuration. + +* Add `management.security.enabled=false` in Spring Boot 1.x. The exposed endpoint path is `/sentinel`. +* Add `management.endpoints.web.exposure.include=*` in Spring Boot 2.x. The exposed endpoint path is `/actuator/sentinel`. + +### More + +The following table shows that when there are corresponding bean types in `ApplicationContext`, some actions will be taken: + +:frame: topbot +[width="60%",options="header"] +|==== +^|Existing Bean Type ^|Action ^|Function +|`UrlCleaner`|`WebCallbackManager.setUrlCleaner(urlCleaner)`|Resource cleaning(resource(for example, classify all URLs of /foo/:id to the /foo/* resource)) +|`UrlBlockHandler`|`WebCallbackManager.setUrlBlockHandler(urlBlockHandler)`|Customize rate limiting logic +|==== + +The following table shows all the configurations of Spring Cloud Alibaba Sentinel: + +:frame: topbot +[width="60%",options="header"] +|==== +^|Configuration ^|Description ^|Default Value +|`spring.cloud.sentinel.enabled`|Whether Sentinel automatic configuration takes effect|true +|`spring.cloud.sentinel.eager`|Cancel Sentinel dashboard lazy load|false +|`spring.cloud.sentinel.transport.port`|Port for the application to interact with Sentinel dashboard. An HTTP Server which uses this port will be started in the application|8721 +|`spring.cloud.sentinel.transport.dashboard`|Sentinel dashboard address| +|`spring.cloud.sentinel.transport.heartbeatIntervalMs`|Hearbeat interval between the application and Sentinel dashboard| +|`spring.cloud.sentinel.filter.order`|Loading order of Servlet Filter. The filter will be constructed in the Starter|Integer.MIN_VALUE +|`spring.cloud.sentinel.filter.spring.url-patterns`|Data type is array. Refers to the collection of Servlet Filter ULR patterns|/* +|`spring.cloud.sentinel.metric.charset`|metric file character set|UTF-8 +|`spring.cloud.sentinel.metric.fileSingleSize`|Sentinel metric single file size| +|`spring.cloud.sentinel.metric.fileTotalCount`|Sentinel metric total file number| +|`spring.cloud.sentinel.log.dir`|Directory of Sentinel log files| +|`spring.cloud.sentinel.log.switch-pid`|If PID is required for Sentinel log file names|false +|`spring.cloud.sentinel.servlet.blockPage`| Customized redirection URL. When rate limited, the request will be redirected to the pre-defined URL | +|`spring.cloud.sentinel.flow.coldFactor`| https://github.com/alibaba/Sentinel/wiki/%E9%99%90%E6%B5%81---%E5%86%B7%E5%90%AF%E5%8A%A8[冷启动因子] |3 +|==== diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc/spring-cloud-alibaba.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc/spring-cloud-alibaba.adoc index 294e493b..a47adb74 100644 --- a/spring-cloud-alibaba-docs/src/main/asciidoc/spring-cloud-alibaba.adoc +++ b/spring-cloud-alibaba-docs/src/main/asciidoc/spring-cloud-alibaba.adoc @@ -1,6 +1,6 @@ [[spring-cloud-alibaba-reference]] = Spring Cloud Alibaba Reference Documentation -xiaojing; xiaolongzuo; jim fang; bingting peng +xiaojing; xiaolongzuo; jim fang; bingting peng; wangyuxin :doctype: book :toc: :toclevels: 4 @@ -9,9 +9,9 @@ xiaojing; xiaolongzuo; jim fang; bingting peng == Introduction -Spring Cloud Alibaba provides a one-stop solution for distributed application development. It contains all the components required to develop distributed applications, making it easy for you to develop your applications using Spring Cloud. +Spring Cloud Alibaba aims to provide a one-stop solution for microservices development. This prjoect includes the required components for developing distributed applications and services, so that developers can develop distributed applications easily with the Spring Cloud programming models. -With Spring Cloud Alibaba, you only need to add some annotations and a small amount of configurations to connect Spring Cloud applications to the distributed solutions of Alibaba, and build a distributed application system with Alibaba middleware. +With Spring Cloud Alibaba, you only need to add a few annotations and configurations, and you will be able to use the distributed solutions of Alibaba for your applications, and build a distributed system of your own with Alibaba middleware. include::dependency-management.adoc[] diff --git a/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/config/AbstractDataSourceProperties.java b/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/config/AbstractDataSourceProperties.java index 3aa05be6..6e879b21 100644 --- a/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/config/AbstractDataSourceProperties.java +++ b/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/config/AbstractDataSourceProperties.java @@ -1,5 +1,7 @@ package org.springframework.cloud.alibaba.sentinel.datasource.config; +import com.fasterxml.jackson.annotation.JsonIgnore; + /** * Abstract class Using by {@link DataSourcePropertiesConfiguration} * @@ -9,6 +11,7 @@ public class AbstractDataSourceProperties { private String dataType = "json"; private String converterClass; + @JsonIgnore private final String factoryBeanName; public AbstractDataSourceProperties(String factoryBeanName) { diff --git a/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/config/DataSourcePropertiesConfiguration.java b/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/config/DataSourcePropertiesConfiguration.java index e6bf51bd..3d410cd7 100644 --- a/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/config/DataSourcePropertiesConfiguration.java +++ b/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/config/DataSourcePropertiesConfiguration.java @@ -6,6 +6,8 @@ import java.util.List; import org.springframework.util.ObjectUtils; +import com.fasterxml.jackson.annotation.JsonIgnore; + /** * Using By ConfigurationProperties. * @@ -57,6 +59,7 @@ public class DataSourcePropertiesConfiguration { this.apollo = apollo; } + @JsonIgnore public List getInvalidField() { List fieldList = new ArrayList<>(); for (Field field : this.getClass().getDeclaredFields()) {