mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
remove unsupported adoc
This commit is contained in:
parent
45fe83f444
commit
ed4ece8d8a
@ -1,176 +0,0 @@
|
||||
== Spring Cloud AliCloud ACM
|
||||
|
||||
Spring Cloud AliCloud ACM 是阿里云提供的商业版应用配置管理(Application Configuration Management) 产品 在 Spring Cloud 应用侧的客户端实现,且目前完全免费。
|
||||
|
||||
使用 Spring Cloud AliCloud ACM,可基于 Spring Cloud 的编程模型快速接入 ACM 配置管理功能。
|
||||
|
||||
NOTE: 目前 EDAS 已经支持直接部署 Nacos Config 应用
|
||||
|
||||
=== 如何引入 Spring Cloud AliCloud ACM
|
||||
|
||||
如果要在您的项目中引入 ACM,使用 group ID 为 `com.alibaba.cloud` 和 artifact ID 为 `spring-cloud-starter-alicloud-acm` 的 starter。
|
||||
|
||||
[source,xml]
|
||||
----
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alicloud-acm</artifactId>
|
||||
</dependency>
|
||||
----
|
||||
|
||||
=== 使用 ACM 进行配置管理
|
||||
|
||||
当客户端引入了 Spring Cloud AliCloud ACM Starter 以后,应用启动时会自动从配置管理的服务端获取配置信息,并注入到 Spring 的 Environment 中。
|
||||
|
||||
以下是一个简单的应用示例。
|
||||
|
||||
[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);
|
||||
}
|
||||
}
|
||||
----
|
||||
|
||||
在从配置中心服务端获取配置信息之前,还需要配置服务端的地址,在 bootstrap.properties 中,还需要配置以下信息。
|
||||
|
||||
[source,properties]
|
||||
----
|
||||
# 必选,应用名会被作为从服务端获取配置 key 的关键词组成部分
|
||||
spring.application.name=acm-config
|
||||
server.port=18081
|
||||
# 以下就是配置中心服务端的IP和端口配置
|
||||
spring.cloud.alicloud.acm.server-list=127.0.0.1
|
||||
spring.cloud.alicloud.acm.server-port=8080
|
||||
----
|
||||
|
||||
NOTE: 此时没有启动配置中心,启动应用会报错,因此在应用启动之前,应当首先启动配置中心。
|
||||
|
||||
|
||||
==== 启动配置中心
|
||||
|
||||
ACM 使用的配置中心有两种,一种是本地运行的轻量版配置中心,主要用于开发和本地调试,一种是阿里云产品 ACM。通常情况下,可以使用轻量版配置中心作为开发和测试环境,使用云上的 ACM 作为灰度和生产环境。
|
||||
|
||||
===== 使用轻量版配置中心
|
||||
|
||||
轻量版配置中心的下载和启动方式可参考 https://help.aliyun.com/document_detail/44163.html[这里]
|
||||
|
||||
NOTE: 只需要执行文档中的第1步 (下载轻量配置中心) 和第2步 (启动轻量配置中心)。
|
||||
|
||||
|
||||
===== 使用阿里云配置中心
|
||||
|
||||
使用云上 ACM ,可以省去服务端的维护工作,同时稳定性也会更有保障。当使用云上配置中心时,代码部分和使用轻量配置中心并没有区别,但是配置上会有一些区别。
|
||||
|
||||
以下是一个简单的使用云上配置中心的配置示例,配置详情需要在 https://acm.console.aliyun.com[ACM控制台查询]
|
||||
|
||||
[source,properties]
|
||||
----
|
||||
# 应用名会被作为从服务端获取配置 key 的关键词组成部分,因此是必选
|
||||
spring.application.name=acm-config
|
||||
# 端口配置自由配置即可
|
||||
server.port=18081
|
||||
# 以下就是配置中心的IP和端口配置
|
||||
spring.cloud.alicloud.acm.server-mode=EDAS
|
||||
spring.cloud.alicloud.access-key=你的阿里云AK
|
||||
spring.cloud.alicloud.secret-key=你的阿里云SK
|
||||
spring.cloud.alicloud.acm.endpoint=acm.aliyun.com
|
||||
spring.cloud.alicloud.acm.namespace=你的 ACM namespace,需要在 ACM 控制台查询
|
||||
----
|
||||
|
||||
NOTE: EDAS 提供应用托管服务,如果你将应用托管到 EDAS,那么 EDAS 将会自动为你填充所有与业务无关的配置。
|
||||
|
||||
==== 在配置中心添加配置
|
||||
|
||||
1. 启动好轻量版配置中心之后,在控制台中添加如下的配置。
|
||||
|
||||
[source,subs="normal"]
|
||||
----
|
||||
Group: DEFAULT_GROOUP
|
||||
|
||||
DataId: acm-config.properties
|
||||
|
||||
Content: user.name=james
|
||||
user.age=18
|
||||
----
|
||||
|
||||
NOTE: DataId 的格式为 `{prefix}.{file-extension}`,prefix 默认从配置 spring.application.name 中取值,file-extension 默认的值为 "properties"。
|
||||
|
||||
==== 启动应用验证
|
||||
|
||||
启动这个Example,可以在控制台看到打印出的值正是我们在轻量版配置中心上预先配置的值。
|
||||
|
||||
[source,subs="normal"]
|
||||
----
|
||||
user name :james; age: 18
|
||||
----
|
||||
|
||||
=== 更改配置文件扩展名
|
||||
|
||||
spring-cloud-starter-alicloud-acm 中 DataId 默认的文件扩展名是 properties。除去 properties 格式之外,也支持 yaml 格式。
|
||||
支持通过 spring.cloud.alicloud.acm.file-extension 来配置文件的扩展名,yaml 格式可以配置成 `yaml` 或 `yml`。
|
||||
|
||||
NOTE: 修改文件扩展名后,在配置中心中的 DataID 以及 Content 的格式都必须做相应的修改。
|
||||
|
||||
=== 动态更新
|
||||
|
||||
spring-cloud-starter-alicloud-acm 默认支持配置的动态更新,当您在配置中心修改配置的内容时,会发布 Spring 中的 RefreshEvent 事件。
|
||||
带有 @RefreshScope 和 @ConfigurationProperties 注解的类会自动刷新。
|
||||
|
||||
NOTE: 你可以通过配置 spring.cloud.alicloud.acm.refresh.enabled=false 来关闭动态刷新。
|
||||
|
||||
=== Profile 粒度的配置
|
||||
|
||||
spring-cloud-starter-alicloud-acm 在加载配置的时候,首先会加载 DataId 为{spring.application.name}.{file-extension}的配置,当 spring.profiles.active 中配置有内容时,还会依次去加载 spring.profile 对应的内容, DataId 的格式为{spring.application.name}-{profile}.{file-extension}的配置,且后者的优先级高于前者。
|
||||
|
||||
spring.profiles.active 属于配置的元数据,所以也必须配置在 bootstrap.properties 或 bootstrap.yaml 中。比如可以在 bootstrap.properties 中增加如下内容。
|
||||
|
||||
[sources,properties]
|
||||
----
|
||||
spring.profiles.active={profile-name}
|
||||
----
|
||||
|
||||
Note: 也可以通过 JVM 参数 -Dspring.profiles.active=develop 或者 --spring.profiles.active=develop 这类优先级更高的方式来配置,只需遵循 Spring Boot 规范即可。
|
||||
|
||||
|
||||
=== 自定义配置中心超时时间
|
||||
|
||||
ACM Client 与 Server 通信的超时时间默认是 3000ms,可以通过 `spring.cloud.alicloud.acm.timeout` 来修改超时时间,单位为 ms 。
|
||||
|
||||
=== 自定义 Group 的配置
|
||||
|
||||
在没有明确指定 `{spring.cloud.alicloud.acm.group}` 配置的情况下, 默认使用的是 DEFAULT_GROUP 。如果需要自定义自己的 Group,可以通过以下配置来实现:
|
||||
|
||||
[source,properties]
|
||||
----
|
||||
spring.cloud.alicloud.acm.group=DEVELOP_GROUP
|
||||
----
|
||||
|
||||
NOTE: 该配置必须放在 bootstrap.properties 文件中。并且在添加配置时 Group 的值要和 `spring.cloud.alicloud.acm.group` 的配置值一致。
|
||||
|
||||
=== 共享配置
|
||||
|
||||
ACM 提供了一种多个应用之间共享配置中心的同一个配置的推荐方式,供多个应用共享一些配置时使用,您在使用的时候需要添加在 bootstrap 中添加一个配置项 `spring.application.group`。
|
||||
|
||||
[source,properties]
|
||||
----
|
||||
spring.application.group=company.department.team
|
||||
----
|
||||
|
||||
这时应用在获取上文提到的自身所独有的配置之前,会先依次从这些 DataId 去获取,分别是 company:application.properties, company.department:application.properties, company.department.team:application.properties。
|
||||
然后,还会从 {spring.application.group}:{spring.application.name}.{file-extension} 中获取,越往后优先级越高,最高的仍然是应用自身所独有的配置。
|
||||
|
||||
|
||||
NOTE: 共享配置中 DataId 默认后缀为 properties,可以通过 spring.cloud.alicloud.acm.file-extension 配置. `{spring.application.group}:{spring.application.name}.{file-extension}` 。
|
||||
|
||||
NOTE: 如果设置了 `spring.profiles.active` ,DataId 的格式还支持 `{spring.application.group}:{spring.application.name}-{spring.profiles.active}.{file-extension}`。优先级高于 `{spring.application.group}:{spring.application.name}.{file-extension}`
|
||||
|
||||
=== Actuator 监控
|
||||
|
||||
ACM 对应的 Actuator 监控地址为 `/acm`,其中 `config` 代表了 ACM 元数据配置的信息,`runtime.sources` 对应的是从 ACM 服务端获取的配置的信息及最后刷新时间, `runtime.refreshHistory` 对应的是动态刷新的历史记录。
|
@ -1,96 +0,0 @@
|
||||
== Spring Cloud AliCloud ANS
|
||||
|
||||
ANS(Application Naming Service) 是隶属于阿里云 EDAS 产品的组件, Spring Cloud AliCloud ANS 提供了 Spring Cloud 规范下商业版的服务注册与发现,可以让用户方便的在本地开发,同时也可以运行在云环境里。
|
||||
|
||||
NOTE: 目前 EDAS 已经支持直接部署 Nacos Discovery 应用
|
||||
|
||||
=== 如何引入 Spring Cloud AliCloud ANS
|
||||
|
||||
如果要在您的项目中引入 ANS,使用 group ID 为 `com.alibaba.cloud` 和 artifact ID 为 `spring-cloud-starter-alicloud-ans` 的 starter。
|
||||
|
||||
[source,xml]
|
||||
----
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alicloud-ans</artifactId>
|
||||
</dependency>
|
||||
----
|
||||
|
||||
=== 使用ANS进行服务注册
|
||||
|
||||
当客户端引入了 Spring Cloud AliCloud ANS Starter 以后,服务的元数据会被自动注册到注册中心,比如IP、端口、权重等信息。客户端会与服务端保持心跳,来证明自己可以正常提供服务。
|
||||
|
||||
以下是一个简单的应用示例。
|
||||
|
||||
[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);
|
||||
}
|
||||
|
||||
}
|
||||
----
|
||||
|
||||
既然服务会被注册到注册中心,那么肯定需要配置注册中心的地址,在 application.properties 中,还需要配置上以下地址。
|
||||
|
||||
[source,properties]
|
||||
----
|
||||
# 应用名会被作为服务名称使用,因此会必选
|
||||
spring.application.name=ans-provider
|
||||
server.port=18081
|
||||
# 以下就是注册中心的IP和端口配置
|
||||
spring.cloud.alicloud.ans.server-list=127.0.0.1
|
||||
spring.cloud.alicloud.ans.server-port=8080
|
||||
----
|
||||
|
||||
NOTE: 此时没有启动注册中心,启动应用会报错,因此在应用启动之前,应当首先启动注册中心。
|
||||
|
||||
=== 启动注册中心
|
||||
|
||||
ANS 使用的注册中心有两种,一种是完全免费的轻量版配置中心,主要用于开发和本地调试,一种是云上注册中心,ANS 依托于阿里云 EDAS 产品提供服务注册的功能。通常情况下,可以使用轻量版配置中心作为开发和测试环境,使用云上的 EDAS 作为灰度和生产环境。
|
||||
|
||||
==== 启动轻量版配置中心
|
||||
|
||||
轻量版配置中心的下载和启动方式可参考 https://help.aliyun.com/document_detail/44163.html?spm=a2c4g.11186623.6.677.5f206b82Z2mTCF[这里]
|
||||
|
||||
NOTE: 只需要进行第1步(下载轻量配置中心)和第2步(启动轻量配置中心)即可,第3步(配置hosts)在与 ANS 结合使用时,不需要操作。
|
||||
|
||||
启动完轻量版配置中心以后,直接启动 ProviderApplication ,即可将服务注册到轻量版配置中心,由于轻量版配置中心的默认端口是8080,因此你可以打开 http://127.0.0.1:8080 ,点击左侧"服务列表",查看注册上来的服务。
|
||||
|
||||
==== 使用云上注册中心
|
||||
|
||||
使用云上注册中心,可以省去服务端的维护工作,同时稳定性也会更有保障。当使用云上注册中心时,代码部分和使用轻量配置中心并没有区别,但是配置上会有一些区别。
|
||||
|
||||
以下是一个简单的使用云上配置中心的配置示例。
|
||||
|
||||
[source,properties]
|
||||
----
|
||||
# 应用名会被作为服务名称使用,因此是必选
|
||||
spring.application.name=ans-provider
|
||||
# 端口配置自由配置即可
|
||||
server.port=18081
|
||||
# 以下就是注册中心的IP和端口配置,因为默认就是127.0.0.1和8080,因此以下两行配置也可以省略
|
||||
spring.cloud.alicloud.ans.server-mode=EDAS
|
||||
spring.cloud.alicloud.access-key=你的阿里云AK
|
||||
spring.cloud.alicloud.secret-key=你的阿里云SK
|
||||
spring.cloud.alicloud.edas.namespace=cn-xxxxx
|
||||
----
|
||||
|
||||
server-mode 的默认值为 LOCAL ,如果要使用云上注册中心,则需要更改为 EDAS 。
|
||||
|
||||
access-key 和 secret-key 则是阿里云账号的 AK/SK,需要首先注册阿里云账号,然后登陆 https://usercenter.console.aliyun.com/#/manage/ak[阿里云AK/SK管理页面] ,即可看到 AccessKey ID 和 Access Key Secret ,如果没有的话,需要点击"创建 AccessKey"按钮创建。
|
||||
|
||||
namespace 是阿里云 EDAS 产品的概念,用于隔离不同的环境,比如测试环境和生产环境。要获取 namespace 需要 https://common-buy.aliyun.com/?spm=5176.11451019.0.0.6f5965c0Uq5tue&commodityCode=edaspostpay#/buy[开通 EDAS 服务],按量计费模式下开通是免费的,开通以后进入 https://edas.console.aliyun.com/#/namespaces?regionNo=cn-hangzhou[EDAS控制台],即可看到对应的 namespace,比如 cn-hangzhou。
|
||||
|
||||
NOTE: EDAS 提供应用托管服务,如果你将应用托管到 EDAS,那么 EDAS 将会自动为你填充所有配置。
|
||||
|
@ -1,140 +0,0 @@
|
||||
== Spring Cloud AliCloud OSS
|
||||
|
||||
OSS(Object Storage Service)是阿里云的一款对象存储服务产品, Spring Cloud AliCloud OSS 提供了Spring Cloud规范下商业版的对象存储服务,提供简单易用的API,并且支持与 Spring 框架中 Resource 的整合。
|
||||
|
||||
=== 如何引入 Spring Cloud AliCloud OSS
|
||||
|
||||
如果要在您的项目中引入 OSS,使用 group ID 为 `org.springframework.cloud` 和 artifact ID 为 `spring-cloud-starter-alicloud-oss` 的 starter。
|
||||
|
||||
[source,xml]
|
||||
----
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alicloud-oss</artifactId>
|
||||
</dependency>
|
||||
----
|
||||
|
||||
=== 如何使用 OSS API
|
||||
|
||||
==== 配置 OSS
|
||||
|
||||
使用 Spring Cloud AliCloud OSS 之前,需要在 application.properties 中加入以下配置。
|
||||
|
||||
[source,properties]
|
||||
----
|
||||
spring.cloud.alicloud.access-key=你的阿里云AK
|
||||
spring.cloud.alicloud.secret-key=你的阿里云SK
|
||||
spring.cloud.alicloud.oss.endpoint=***.aliyuncs.com
|
||||
----
|
||||
|
||||
access-key 和 secret-key 是阿里云账号的AK/SK,需要首先注册阿里云账号,然后登陆 https://usercenter.console.aliyun.com/#/manage/ak[阿里云AK/SK管理页面] ,即可看到 AccessKey ID 和 Access Key Secret ,如果没有的话,需要点击"创建AccessKey"按钮创建。
|
||||
|
||||
endpoint可以到 OSS 的 https://help.aliyun.com/document_detail/31837.html?spm=a2c4g.11186623.2.9.7dc72841Z2hGqa#concept-zt4-cvy-5db[官方文档]中查看,根据所在的 region ,填写对应的 endpoint 即可。
|
||||
|
||||
|
||||
==== 引入 OSS API
|
||||
|
||||
Spring Cloud Alicloud OSS 中的 OSS API 基于阿里云官方OSS SDK提供,具备上传、下载、查看等所有对象存储类操作API。
|
||||
|
||||
一个简单的使用 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);
|
||||
}
|
||||
|
||||
}
|
||||
----
|
||||
|
||||
在上传文件之前,首先需要 https://account.aliyun.com/register/register.htm?spm=5176.8142029.388261.26.e9396d3eaYK2sG&oauth_callback=https%3A%2F%2Fwww.aliyun.com%2F[注册阿里云账号] ,如果已经有的话,请 https://common-buy.aliyun.com/?spm=5176.8465980.unusable.dopen.4cdf1450rg8Ujb&commodityCode=oss#/open[开通OSS服务]。
|
||||
|
||||
进入 https://oss.console.aliyun.com/overview[OSS控制台],点击左侧"新建Bucket",按照提示创建一个Bucket,然后将bucket名称替换掉上面代码中的"bucketName",而"fileName"取任意文件名,"/your/local/file/path"取任意本地文件路径,然后 curl http://127.0.0.1:端口/ 即可上传文件,可以到 https://oss.console.aliyun.com/overview[OSS控制台]查看效果。
|
||||
|
||||
更多关于 OSS API 的操作,可以参考 https://help.aliyun.com/document_detail/32008.html[OSS官方SDK文档]。
|
||||
|
||||
=== 与 Spring 框架的 Resource 结合
|
||||
|
||||
Spring Cloud AliCloud OSS 整合了 Spring 框架的 Resource 规范,可以让用户很方便的引用 OSS 的资源。
|
||||
|
||||
一个简单的使用 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: 以上示例运行的前提是,在 OSS 上需要有名为"bucketName"的Bucket,同时在该Bucket下,存在名为"fileName"的文件。
|
||||
|
||||
=== 采用 STS 授权
|
||||
|
||||
Spring Cloud AliCloud OSS 除了 AccessKey/SecretKey 的授权方式以外,还支持 STS 授权方式。 STS 是临时访问令牌的方式,一般用于授权第三方,临时访问自己的资源。
|
||||
|
||||
作为第三方,也就是被授权者,只需要配置以下内容,就可以访问临时被授权的资源。
|
||||
|
||||
[source,properties]
|
||||
----
|
||||
spring.cloud.alicloud.oss.authorization-mode=STS
|
||||
spring.cloud.alicloud.oss.endpoint=***.aliyuncs.com
|
||||
spring.cloud.alicloud.oss.sts.access-key=你被授权的AK
|
||||
spring.cloud.alicloud.oss.sts.secret-key=你被授权的SK
|
||||
spring.cloud.alicloud.oss.sts.security-token=你被授权的ST
|
||||
----
|
||||
|
||||
其中 spring.cloud.alicloud.oss.authorization-mode 是枚举类型,此时填写 STS ,代表采用 STS 的方式授权。 endpoint可以到 OSS 的 https://help.aliyun.com/document_detail/31837.html?spm=a2c4g.11186623.2.9.7dc72841Z2hGqa#concept-zt4-cvy-5db[官方文档]中查看,根据所在的 region ,填写对应的 endpoint 即可。
|
||||
|
||||
access-key、secret-key和security-token需要由授权方颁发,如果对 STS 不了解的话,可以参考 https://help.aliyun.com/document_detail/31867.html[STS官方文档]。
|
||||
|
||||
=== 更多客户端配置
|
||||
|
||||
除了基本的配置项以外, Spring Cloud AliCloud OSS 还支持很多额外的配置,也是在 application.properties 文件中。
|
||||
|
||||
以下是一些简单的示例。
|
||||
|
||||
[source,properties]
|
||||
----
|
||||
spring.cloud.alicloud.oss.authorization-mode=STS
|
||||
spring.cloud.alicloud.oss.endpoint=***.aliyuncs.com
|
||||
spring.cloud.alicloud.oss.sts.access-key=你被授权的AK
|
||||
spring.cloud.alicloud.oss.sts.secret-key=你被授权的SK
|
||||
spring.cloud.alicloud.oss.sts.security-token=你被授权的ST
|
||||
|
||||
spring.cloud.alicloud.oss.config.connection-timeout=3000
|
||||
spring.cloud.alicloud.oss.config.max-connections=1000
|
||||
----
|
||||
|
||||
如果想了解更多的配置项,可以参考 https://help.aliyun.com/document_detail/32010.html?spm=a2c4g.11186623.6.703.50b25413nGsYHc[OSSClient配置项] 的末尾表格。
|
||||
|
||||
NOTE: 通常情况下,都需要将 https://help.aliyun.com/document_detail/32010.html?spm=a2c4g.11186623.6.703.50b25413nGsYHc[OSSClient配置项] 末尾表格中的参数名更换成"-"连接,且所有字母小写。例如 ConnectionTimeout,对应 connection-timeout。
|
@ -1,114 +0,0 @@
|
||||
== Spring Cloud AliCloud SchedulerX
|
||||
|
||||
SchedulerX(分布式任务调度) 是隶属于阿里云EDAS产品的组件, Spring Cloud AliCloud SchedulerX 提供了在Spring Cloud的配置规范下,分布式任务调度的功能支持。SchedulerX可提供秒级、精准、高可靠、高可用的定时任务调度服务,并支持多种类型的任务调度,如简单单机任务、简单多机任务、脚本任务以及网格任务。
|
||||
|
||||
=== 如何引入 Spring Cloud AliCloud SchedulerX
|
||||
|
||||
如果要在您的项目中引入 SchedulerX,使用 group ID 为 `com.alibaba.cloud` 和 artifact ID 为 `spring-cloud-starter-alicloud-schedulerX` 的 starter。
|
||||
|
||||
[source,xml]
|
||||
----
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alicloud-schedulerX</artifactId>
|
||||
</dependency>
|
||||
----
|
||||
|
||||
=== 启动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官方文档]。
|
@ -1,253 +0,0 @@
|
||||
== Spring Cloud AliCloud SMS
|
||||
|
||||
短信服务(Short Message Service)是阿里云为用户提供的一种通信服务的能力。 Spring Cloud AliCloud SMS 实现了与 SMS 的简单集成,提供更为简单易用的 API,可以基于 Spring Cloud Alibaba SMS 来快速的接入阿里云的 SMS 服务。
|
||||
|
||||
=== 如何引入 Spring Cloud AliCloud SMS
|
||||
|
||||
如果要在您的项目中引入 SMS,使用 group ID 为 `com.alibaba.cloud` 和 artifact ID 为 `spring-cloud-starter-alicloud-sms` 的 starter。
|
||||
|
||||
[source,xml]
|
||||
----
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alicloud-sms</artifactId>
|
||||
</dependency>
|
||||
----
|
||||
|
||||
=== 如何使用 SMS API
|
||||
|
||||
==== 配置 SMS
|
||||
|
||||
使用 Spring Cloud AliCloud SMS 之前,需要在 application.properties 中加入以下配置。
|
||||
|
||||
[source,properties]
|
||||
----
|
||||
spring.cloud.alicloud.access-key=你的阿里云 AK
|
||||
spring.cloud.alicloud.secret-key=你的阿里云 SK
|
||||
----
|
||||
|
||||
access-key 和 secret-key 是阿里云账号的 AK/SK,需要首先注册阿里云账号,然后登陆 https://usercenter.console.aliyun.com/#/manage/ak[阿里云AK/SK管理页面] ,即可看到 AccessKey ID 和 Access Key Secret ,如果没有的话,需要点击"创建AccessKey"按钮创建。
|
||||
|
||||
|
||||
==== 引入 SMS API
|
||||
|
||||
Spring Cloud Alicloud SMS 中的 SMS API 基于阿里云官方 SMS SDK ,提供具备单个短信发送、多个短信批量发送、短信查询、短信消息(`短信回执消息` 和 `上行短信消息`) 类型操作API。
|
||||
|
||||
一个简单的使用 SMS API 发送短信的应用如下。
|
||||
|
||||
[source,java]
|
||||
----
|
||||
@SpringBootApplication
|
||||
public class SmsApplication {
|
||||
|
||||
@Autowired
|
||||
private ISmsService smsService;
|
||||
|
||||
/**
|
||||
* 短信发送 Example
|
||||
* @param code
|
||||
* @return
|
||||
*/
|
||||
@RequestMapping("/batch-sms-send.do")
|
||||
|
||||
public SendBatchSmsResponse batchsendCheckCode(
|
||||
@RequestParam(name = "code") String code) {
|
||||
|
||||
// 组装请求对象-具体描述见控制台-文档部分内容
|
||||
SendSmsRequest request = new SendSmsRequest();
|
||||
// 必填:待发送手机号
|
||||
request.setPhoneNumbers("152******");
|
||||
// 必填:短信签名-可在短信控制台中找到
|
||||
request.setSignName("******");
|
||||
// 必填:短信模板-可在短信控制台中找到
|
||||
request.setTemplateCode("******");
|
||||
// 可选:模板中的变量替换JSON串,如模板内容为"【企业级分布式应用服务】,您的验证码为${code}"时,此处的值为
|
||||
request.setTemplateParam("{\"code\":\"" + code + "\"}");
|
||||
SendSmsResponse sendSmsResponse ;
|
||||
try {
|
||||
sendSmsResponse = smsService.sendSmsRequest(request);
|
||||
}
|
||||
catch (ClientException e) {
|
||||
e.printStackTrace();
|
||||
sendSmsResponse = new SendSmsResponse();
|
||||
}
|
||||
return sendSmsResponse ;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws URISyntaxException {
|
||||
|
||||
SpringApplication.run(SmsApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
----
|
||||
|
||||
在发送短信之前,首先需要 https://account.aliyun.com/register/register.htm?spm=5176.8142029.388261.26.e9396d3eaYK2sG&oauth_callback=https%3A%2F%2Fwww.aliyun.com%2F[注册阿里云账号] ,如果已经有的话,请 https://dysms.console.aliyun.com/dysms.htm?spm=5176.8195934.1283918..18924183bHPct2&accounttraceid=c8cb4243-3080-4eb1-96b0-1f2316584269#/[开通 SMS 服务]。
|
||||
|
||||
更多关于 SMS 发送短信的步骤,可以参考 SMS 官方 https://help.aliyun.com/document_detail/55284.html?spm=a2c4g.11186623.6.568.715e4f30ZiVkbI[短信发送API(SendSms)---JAVA] 文档。
|
||||
|
||||
NOTE: 由于早期的 SMS sdk 版本的问题,如果短信发送失败,请将代码中含有明确指定 MethodType 为 POST 的这行代码给删除掉。如果还有问题,请第一时间联系我们。
|
||||
|
||||
|
||||
=== SMS Api 的高级功能
|
||||
|
||||
Spring Cloud Alicloud SMS 封装的 API 接口为了降低学习的成本,尽量保持和官网提供的 API 以及 Example 保持一致。
|
||||
|
||||
* 批量短信发送
|
||||
|
||||
参考以下的 Example ,来快速开发一个具有批量短信发送的功能。在 Controller 中或者新建一个 Controler 新增如下代码:
|
||||
|
||||
[source,java]
|
||||
----
|
||||
/**
|
||||
* 批量短信发送 Example
|
||||
* @param code
|
||||
* @return
|
||||
*/
|
||||
@RequestMapping("/batch-sms-send.do")
|
||||
public SendBatchSmsResponse batchsendCheckCode(
|
||||
@RequestParam(name = "code") String code) {
|
||||
// 组装请求对象
|
||||
SendBatchSmsRequest request = new SendBatchSmsRequest();
|
||||
// 使用 GET 提交
|
||||
request.setMethod(MethodType.GET);
|
||||
// 必填:待发送手机号。支持JSON格式的批量调用,批量上限为100个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式
|
||||
request.setPhoneNumberJson("[\"177********\",\"130********\"]");
|
||||
// 必填:短信签名-支持不同的号码发送不同的短信签名
|
||||
request.setSignNameJson("[\"*******\",\"*******\"]");
|
||||
// 必填:短信模板-可在短信控制台中找到
|
||||
request.setTemplateCode("******");
|
||||
// 必填:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
|
||||
// 友情提示:如果JSON中需要带换行符,请参照标准的JSON协议对换行符的要求,比如短信内容中包含\r\n的情况在JSON中需要表示成\\r\\n,否则会导致JSON在服务端解析失败
|
||||
request.setTemplateParamJson(
|
||||
"[{\"code\":\"" + code + "\"},{\"code\":\"" + code + "\"}]");
|
||||
SendBatchSmsResponse sendSmsResponse ;
|
||||
try {
|
||||
sendSmsResponse = smsService
|
||||
.sendSmsBatchRequest(request);
|
||||
return sendSmsResponse;
|
||||
}
|
||||
catch (ClientException e) {
|
||||
e.printStackTrace();
|
||||
sendSmsResponse = new SendBatchSmsResponse();
|
||||
}
|
||||
return sendSmsResponse ;
|
||||
}
|
||||
----
|
||||
|
||||
NOTE: 这里设置请求的 MethodType 为 GET,和官网给出的例子还有些不一样。这是因为依赖的阿里云 POP API 版本不一致导致不兼容的问题,设置为 GET 即可。
|
||||
|
||||
更多的参数说明可 https://help.aliyun.com/document_detail/66041.html?spm=a2c4g.11186623.6.571.631315e8AauJhP[参考这里]
|
||||
|
||||
* 短信查询
|
||||
|
||||
参考以下的 Example ,可以快速开发根据某个指定的号码查询短信历史发送状态。在 Controller 中或者新建一个 Controler 新增如下代码:
|
||||
|
||||
[source,java]
|
||||
----
|
||||
/**
|
||||
*
|
||||
* 短信查询 Example
|
||||
* @param telephone
|
||||
* @return
|
||||
*/
|
||||
@RequestMapping("/query.do")
|
||||
public QuerySendDetailsResponse querySendDetailsResponse(
|
||||
@RequestParam(name = "tel") String telephone) {
|
||||
// 组装请求对象
|
||||
QuerySendDetailsRequest request = new QuerySendDetailsRequest();
|
||||
// 必填-号码
|
||||
request.setPhoneNumber(telephone);
|
||||
// 必填-短信发送的日期 支持30天内记录查询(可查其中一天的发送数据),格式yyyyMMdd
|
||||
request.setSendDate("20190103");
|
||||
// 必填-页大小
|
||||
request.setPageSize(10L);
|
||||
// 必填-当前页码从1开始计数
|
||||
request.setCurrentPage(1L);
|
||||
try {
|
||||
QuerySendDetailsResponse response = smsService.querySendDetails(request);
|
||||
return response;
|
||||
}
|
||||
catch (ClientException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return new QuerySendDetailsResponse();
|
||||
}
|
||||
|
||||
----
|
||||
|
||||
更多的参数说明,可 https://help.aliyun.com/document_detail/55289.html?spm=a2c4g.11186623.6.569.4f852c78mugEfx[参考这里]
|
||||
|
||||
* 短信回执消息
|
||||
|
||||
通过订阅 SmsReport 短信状态报告,可以获知每条短信的发送情况,了解短信是否达到终端用户的状态与相关信息。这些工作已经都被 Spring Cloud AliCloud SMS 封装在内部了。你只需要完成以下两步即可。
|
||||
|
||||
1、在 `application.properties` 配置文件中(也可以是 application.yaml)配置 SmsReport 的队列名称。
|
||||
|
||||
.application.properties
|
||||
[source,properties]
|
||||
----
|
||||
spring.cloud.alicloud.sms.report-queue-name=Alicom-Queue-********-SmsReport
|
||||
----
|
||||
|
||||
2、 实现 SmsReportMessageListener 接口,并初始化一个 Spring Bean 。
|
||||
|
||||
[source,java]
|
||||
----
|
||||
/**
|
||||
* 如果需要监听短信是否被对方成功接收,只需实现这个接口并初始化一个 Spring Bean 即可。
|
||||
*/
|
||||
@Component
|
||||
public class SmsReportMessageListener
|
||||
implements org.springframework.cloud.alicloud.sms.SmsReportMessageListener {
|
||||
|
||||
@Override
|
||||
public boolean dealMessage(Message message) {
|
||||
// 在这里添加你的处理逻辑
|
||||
|
||||
//do something
|
||||
|
||||
System.err.println(this.getClass().getName() + "; " + message.toString());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
----
|
||||
|
||||
更多关于 Message 的消息体格式可 https://help.aliyun.com/document_detail/55496.html?spm=a2c4g.11186623.6.570.7f792c78rOiWXO[参考这里]。
|
||||
|
||||
* 上行短信消息
|
||||
|
||||
通过订阅SmsUp上行短信消息,可以获知终端用户回复短信的内容。这些工作也已经被 Spring Cloud AliCloud SMS 封装好了。你只需要完成以下两步即可。
|
||||
|
||||
1、 在 `application.properties` 配置文件中(也可以是 application.yaml)配置 SmsReport 的队列名称。
|
||||
|
||||
.application.properties
|
||||
----
|
||||
spring.cloud.alicloud.sms.up-queue-name=Alicom-Queue-********-SmsUp
|
||||
----
|
||||
|
||||
2、实现 SmsUpMessageListener 接口,并初始化一个 Spring Bean 。
|
||||
|
||||
[source,java]
|
||||
----
|
||||
/**
|
||||
* 如果发送的短信需要接收对方回复的状态消息,只需实现该接口并初始化一个 Spring Bean 即可。
|
||||
*/
|
||||
@Component
|
||||
public class SmsUpMessageListener
|
||||
implements org.springframework.cloud.alicloud.sms.SmsUpMessageListener {
|
||||
|
||||
@Override
|
||||
public boolean dealMessage(Message message) {
|
||||
// 在这里添加你的处理逻辑
|
||||
|
||||
//do something
|
||||
|
||||
System.err.println(this.getClass().getName() + "; " + message.toString());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
----
|
||||
|
||||
更多关于 Message 的消息体格式可 https://help.aliyun.com/document_detail/55496.html?spm=a2c4g.11186623.6.570.7f792c78rOiWXO[参考这里]。
|
@ -1,178 +0,0 @@
|
||||
== Spring Cloud Alibaba Cloud ACM
|
||||
|
||||
Spring Cloud AliCloud ACM is an implementation of the commercial product Application Configuration Management(ACM) in the client side of Spring Cloud, and is free of charge.
|
||||
|
||||
Use Spring Cloud AliCloud ACM to quickly access ACM configuration management capabilities based on Spring Cloud's programming model.
|
||||
|
||||
NOTE: Currently EDAS already supports direct deployment of the Nacos Config app.
|
||||
|
||||
=== How to Introduce Spring Cloud Alibaba Cloud ACM
|
||||
|
||||
If you want to use ACM in your project, please use the starter with the group ID as `com.alibaba.cloud` and the artifact ID as `spring-cloud-starter-alicloud-acm`.
|
||||
|
||||
[source,xml]
|
||||
----
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alicloud-acm</artifactId>
|
||||
</dependency>
|
||||
----
|
||||
|
||||
=== 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 lightweight configuration center, 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.
|
||||
|
||||
===== Use 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).
|
||||
|
||||
|
||||
===== Use ACM on the Alibaba 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=acm-config
|
||||
# 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 about ACM 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.alicloud.acm.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 Refresh
|
||||
|
||||
spring-cloud-starter-alicloud-acm supports dynamic configuration updates. RefreshEvent in Spring is published when you update configuration in the configuration center.
|
||||
All classes with @RefreshScope and @ConfigurationProperties annotations will be refreshed automatically.
|
||||
|
||||
NOTE: You can disable automatic refresh by this setting: spring.cloud.alicloud.acm.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 ACM Timeout
|
||||
|
||||
the default timeout of ACM client get config from sever is 3000 ms . If you need to define a timeout, set configuration `spring.cloud.alicloud.acm.timeout`,the unit is millisecond.
|
||||
|
||||
|
||||
=== Support Custom Group Configurations
|
||||
|
||||
DEFAULT_GROUP is used by default when no `{spring.cloud.alicloud.acm.group}` configuration is defined. If you need to define your own group, you can use the following method:
|
||||
|
||||
[source,properties]
|
||||
----
|
||||
spring.cloud.alicloud.acm.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.alicloud.acm.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.alicloud.acm.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}`
|
||||
|
||||
=== Actuator Endpoint
|
||||
|
||||
the Actuator endpoint of ACM is `/acm`, `config` represents the ACM metadata configuration information, `runtime.sources` corresponds to the configuration information obtained from the ACM server and the last refresh time, `runtime.refreshHistory` corresponds to the dynamic refresh history.
|
@ -1,96 +0,0 @@
|
||||
== 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.
|
||||
|
||||
NOTE: EDAS currently supports direct deployment of Nacos Discovery applications
|
||||
|
||||
=== How to Introduce Spring Cloud Alibaba Cloud ANS
|
||||
|
||||
If you want to use ANS in your project, please use the starter with the group ID as `com.alibaba.cloud` and the artifact ID as `spring-cloud-starter-alicloud-ans`.
|
||||
|
||||
[source,xml]
|
||||
----
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alicloud-ans</artifactId>
|
||||
</dependency>
|
||||
----
|
||||
|
||||
=== 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.
|
||||
|
@ -1,73 +0,0 @@
|
||||
=== Circuit Breaker: Spring Cloud Circuit Breaker With Sentinel & Configuring Sentinel Circuit Breakers
|
||||
|
||||
==== Default Configuration
|
||||
|
||||
To provide a default configuration for all of your circuit breakers create a `Customizer` bean that is passed a
|
||||
`SentinelCircuitBreakerFactory` or `ReactiveSentinelCircuitBreakerFactory`.
|
||||
The `configureDefault` method can be used to provide a default configuration.
|
||||
|
||||
====
|
||||
[source,java]
|
||||
----
|
||||
@Bean
|
||||
public Customizer<SentinelCircuitBreakerFactory> defaultCustomizer() {
|
||||
return factory -> factory.configureDefault(id -> new SentinelConfigBuilder(id)
|
||||
.build());
|
||||
}
|
||||
----
|
||||
====
|
||||
|
||||
You can choose to provide default circuit breaking rules via `SentinelConfigBuilder#rules(rules)`.
|
||||
You can also choose to load circuit breaking rules later elsewhere using
|
||||
`DegradeRuleManager.loadRules(rules)` API of Sentinel, or via Sentinel dashboard.
|
||||
|
||||
===== Reactive Example
|
||||
|
||||
====
|
||||
[source,java]
|
||||
----
|
||||
@Bean
|
||||
public Customizer<ReactiveSentinelCircuitBreakerFactory> defaultCustomizer() {
|
||||
return factory -> factory.configureDefault(id -> new SentinelConfigBuilder(id)
|
||||
.build());
|
||||
}
|
||||
----
|
||||
====
|
||||
|
||||
==== Specific Circuit Breaker Configuration
|
||||
|
||||
Similarly to providing a default configuration, you can create a `Customizer` bean this is passed a
|
||||
`SentinelCircuitBreakerFactory`.
|
||||
|
||||
====
|
||||
[source,java]
|
||||
----
|
||||
@Bean
|
||||
public Customizer<SentinelCircuitBreakerFactory> slowCustomizer() {
|
||||
String slowId = "slow";
|
||||
List<DegradeRule> rules = Collections.singletonList(
|
||||
new DegradeRule(slowId).setGrade(RuleConstant.DEGRADE_GRADE_RT)
|
||||
.setCount(100)
|
||||
.setTimeWindow(10)
|
||||
);
|
||||
return factory -> factory.configure(builder -> builder.rules(rules), slowId);
|
||||
}
|
||||
----
|
||||
====
|
||||
|
||||
===== Reactive Example
|
||||
|
||||
====
|
||||
[source,java]
|
||||
----
|
||||
@Bean
|
||||
public Customizer<ReactiveSentinelCircuitBreakerFactory> customizer() {
|
||||
List<DegradeRule> rules = Collections.singletonList(
|
||||
new DegradeRule().setGrade(RuleConstant.DEGRADE_GRADE_RT)
|
||||
.setCount(100)
|
||||
.setTimeWindow(10)
|
||||
);
|
||||
return factory -> factory.configure(builder -> builder.rules(rules), "foo", "bar");
|
||||
}
|
||||
----
|
||||
====
|
@ -1,157 +0,0 @@
|
||||
== 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]
|
||||
----
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
|
||||
<version>0.2.2.BUILD-SNAPSHOT</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
----
|
||||
|
||||
Next we need to introduce Spring Cloud Alibaba Cloud OSS Starter.
|
||||
|
||||
[source,xml]
|
||||
----
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alicloud-oss</artifactId>
|
||||
</dependency>
|
||||
----
|
||||
|
||||
=== 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.
|
@ -1,114 +0,0 @@
|
||||
== 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
|
||||
|
||||
If you want to use SchedulerX in your project, please use the starter with the group ID as `com.alibaba.cloud` and the artifact ID as `spring-cloud-starter-alicloud-schedulerX`.
|
||||
|
||||
[source,xml]
|
||||
----
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alicloud-schedulerX</artifactId>
|
||||
</dependency>
|
||||
----
|
||||
|
||||
=== 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: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.
|
@ -1,210 +0,0 @@
|
||||
== Spring Cloud Alibaba Cloud SMS
|
||||
|
||||
SMS(Short Message Service)is a messaging service that covers the globe, Alibaba SMS provides convenient, efficient, and intelligent communication capabilities that help businesses quickly contact their customers.
|
||||
|
||||
Spring Cloud Alibaba Cloud SMS provide an easier-to-use API for quick access to Alibaba Cloud's SMS service based on Spring Cloud Alibaba SMS.
|
||||
|
||||
=== How to Introduce Spring Cloud Alibaba Cloud SMS
|
||||
|
||||
If you want to use SMS in your project, please use the starter with the group ID as `com.alibaba.cloud` and the artifact ID as `spring-cloud-starter-alicloud-sms`.
|
||||
|
||||
[source,xml]
|
||||
----
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alicloud-sms</artifactId>
|
||||
</dependency>
|
||||
----
|
||||
|
||||
=== How to use SMS API
|
||||
|
||||
==== Configure SMS
|
||||
|
||||
Before you start to use Spring Cloud Alibaba Cloud SMS, please add the following configurations in application.properties.
|
||||
|
||||
[source,properties]
|
||||
----
|
||||
spring.cloud.alicloud.access-key=AK
|
||||
spring.cloud.alicloud.secret-key=SK
|
||||
----
|
||||
|
||||
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.
|
||||
|
||||
==== Introduce SMS API
|
||||
|
||||
The SMS API in Spring Cloud Alicloud SMS is based on Alibaba Cloud SMS SDK. It has a single SMS sending, multiple SMS bulk sending, SMS query, SMS message (SMS receipt message and Upstream SMS message) class operation API.
|
||||
|
||||
The following is a simple example of how to use SMS api to send short message:
|
||||
|
||||
[source,java]
|
||||
----
|
||||
@SpringBootApplication
|
||||
public class SmsApplication {
|
||||
|
||||
@Autowired
|
||||
private ISmsService smsService;
|
||||
|
||||
@RequestMapping("/batch-sms-send.do")
|
||||
public SendBatchSmsResponse batchsendCheckCode(
|
||||
@RequestParam(name = "code") String code) {
|
||||
|
||||
SendSmsRequest request = new SendSmsRequest();
|
||||
// Required:the mobile number
|
||||
request.setPhoneNumbers("152******");
|
||||
// Required:SMS-SignName-could be found in sms console
|
||||
request.setSignName("******");
|
||||
// Required:Template-could be found in sms console
|
||||
request.setTemplateCode("******");
|
||||
// Required:The param of sms template.For exmaple, if the template is "Hello,your verification code is ${code}". The param should be like following value
|
||||
request.setTemplateParam("{\"code\":\"" + code + "\"}");
|
||||
SendSmsResponse sendSmsResponse ;
|
||||
try {
|
||||
sendSmsResponse = smsService.sendSmsRequest(request);
|
||||
}
|
||||
catch (ClientException e) {
|
||||
e.printStackTrace();
|
||||
sendSmsResponse = new SendSmsResponse();
|
||||
}
|
||||
return sendSmsResponse ;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws URISyntaxException {
|
||||
SpringApplication.run(SmsApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
----
|
||||
|
||||
Before you send your messages, 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://dysms.console.aliyun.com/dysms.htm?spm=5176.8195934.1283918..18924183bHPct2&accounttraceid=c8cb4243-3080-4eb1-96b0-1f2316584269#/[Turn on SMS Service].
|
||||
|
||||
For more information about SMS , please refer to the SMS official https://help.aliyun.com/document_detail/55284.html?spm=a2c4g.11186623.6.568.715e4f30ZiVkbI[SMS] (SendSms)---JAVA] docs .
|
||||
|
||||
NOTE: Due to an issue with the earlier SMS sdk version, if the text message fails to be sent, please delete the line of code that contains the explicit MethodType as POST. If you still have problems, please contact us as soon as possible.
|
||||
|
||||
=== The Advanced Features of SMS Api
|
||||
|
||||
In order to reduce the cost of learning, the API interface of the Spring Cloud Alicloud SMS package is kept as consistent as the API and Example provided by the official website.
|
||||
|
||||
* Batch SMS sending
|
||||
|
||||
Refer to the following example to quickly develop a feature with bulk SMS sending. Add the following code in the Controller or create a new Controller:
|
||||
|
||||
[source,java]
|
||||
----
|
||||
@RequestMapping("/batch-sms-send.do")
|
||||
public SendBatchSmsResponse batchsendCheckCode(
|
||||
@RequestParam(name = "code") String code) {
|
||||
SendBatchSmsRequest request = new SendBatchSmsRequest();
|
||||
request.setMethod(MethodType.GET);
|
||||
request.setPhoneNumberJson("[\"177********\",\"130********\"]");
|
||||
request.setSignNameJson("[\"*******\",\"*******\"]");
|
||||
request.setTemplateCode("******");
|
||||
request.setTemplateParamJson(
|
||||
"[{\"code\":\"" + code + "\"},{\"code\":\"" + code + "\"}]");
|
||||
SendBatchSmsResponse sendSmsResponse ;
|
||||
try {
|
||||
sendSmsResponse = smsService
|
||||
.sendSmsBatchRequest(request);
|
||||
return sendSmsResponse;
|
||||
}
|
||||
catch (ClientException e) {
|
||||
e.printStackTrace();
|
||||
sendSmsResponse = new SendBatchSmsResponse();
|
||||
}
|
||||
return sendSmsResponse ;
|
||||
}
|
||||
----
|
||||
|
||||
NOTE: The MethodType of the request is set to GET, which is somewhat different from the example given by the official website. This is because the inconsistent version of the dependent Alibaba Cloud POP API version causes incompatibility issues, set to GET.
|
||||
|
||||
More parameter descriptions can be https://help.aliyun.com/document_detail/66041.html?spm=a2c4g.11186623.6.571.631315e8AauJhP[reference here]
|
||||
|
||||
* SMS Query
|
||||
|
||||
Refer to the following example to quickly develop a history of sending SMS messages based on a specified number. Add the following code in the Controller or create a new Controller:
|
||||
|
||||
[source,java]
|
||||
----
|
||||
@RequestMapping("/query.do")
|
||||
public QuerySendDetailsResponse querySendDetailsResponse(
|
||||
@RequestParam(name = "tel") String telephone) {
|
||||
QuerySendDetailsRequest request = new QuerySendDetailsRequest();
|
||||
request.setPhoneNumber(telephone);
|
||||
request.setSendDate("20190103");
|
||||
request.setPageSize(10L);
|
||||
request.setCurrentPage(1L);
|
||||
try {
|
||||
QuerySendDetailsResponse response = smsService.querySendDetails(request);
|
||||
return response;
|
||||
}
|
||||
catch (ClientException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return new QuerySendDetailsResponse();
|
||||
}
|
||||
|
||||
----
|
||||
|
||||
More parameter descriptions can be found at https://help.aliyun.com/document_detail/55289.html?spm=a2c4g.11186623.6.569.4f852c78mugEfx[reference here]
|
||||
|
||||
* SMS receipt message
|
||||
|
||||
By subscribing to the SmsReport SMS status report, you can know the status of each SMS message and whether it knows the status and related information of the terminal user. These efforts have been encapsulated internally by Spring Cloud AliCloud SMS. You only need to complete the following two steps.
|
||||
|
||||
1、Configure the queue name for SmsReport in the `application.properties` configuration file (which can also be application.yaml).
|
||||
|
||||
.application.properties
|
||||
----
|
||||
spring.cloud.alicloud.sms.report-queue-name=Alicom-Queue-********-SmsReport
|
||||
----
|
||||
|
||||
2、Implement the SmsReportMessageListener interface and initialize a Spring Bean.
|
||||
|
||||
[source,java]
|
||||
----
|
||||
@Component
|
||||
public class SmsReportMessageListener
|
||||
implements SmsReportMessageListener {
|
||||
|
||||
@Override
|
||||
public boolean dealMessage(Message message) {
|
||||
//do something
|
||||
System.err.println(this.getClass().getName() + "; " + message.toString());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
----
|
||||
|
||||
More message body format for Message can be https://help.aliyun.com/document_detail/55496.html?spm=a2c4g.11186623.6.570.7f792c78rOiWXO[reference here].
|
||||
|
||||
* Upstream SMS message
|
||||
|
||||
By subscribing to the SmsUp upstream SMS message, you can know the content of the end user replying to the SMS. These efforts have also been packaged by Spring Cloud AliCloud SMS. You only need to complete the following two steps.
|
||||
|
||||
1、Configure the queue name for SmsReport in the `application.properties` configuration file (which can also be application.yaml).
|
||||
|
||||
.application.properties
|
||||
[source,properties]
|
||||
----
|
||||
spring.cloud.alicloud.sms.up-queue-name=Alicom-Queue-********-SmsUp
|
||||
----
|
||||
|
||||
2、Implement the SmsUpMessageListener interface and initialize a Spring Bean.
|
||||
|
||||
[source,java]
|
||||
----
|
||||
@Component
|
||||
public class SmsUpMessageListener
|
||||
implements org.springframework.cloud.alicloud.sms.SmsUpMessageListener {
|
||||
|
||||
@Override
|
||||
public boolean dealMessage(Message message) {
|
||||
//do something
|
||||
System.err.println(this.getClass().getName() + "; " + message.toString());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
----
|
||||
|
||||
More message body format for Message can be https://help.aliyun.com/document_detail/55496.html?spm=a2c4g.11186623.6.570.7f792c78rOiWXO[reference here].
|
Loading…
x
Reference in New Issue
Block a user