Table of Contents
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 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.
The features of Spring Cloud Alibaba:
Spring Cloud Alibaba also provide rich examples.
If you’re a Maven Central user, add our BOM to your pom.xml <dependencyManagement> section. This will allow you to omit versions for any of the Maven dependencies and instead delegate versioning to the BOM.
<dependencyManagement> <dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>1.5.0.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
In the following sections, it will be assumed you are using the Spring Cloud Alibaba BOM and the dependency snippets will not contain versions.
Nacos is an easy-to-use dynamic service discovery, configuration and service management platform for building cloud native applications.
With Spring Cloud Alibaba Nacos Discovery, you can quickly access the Nacos service registration feature based on Spring Cloud’s programming model.
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 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 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 Nacos Website.
please use the starter with the group ID as com.alibaba.cloud
and the artifact ID as spring-cloud-starter-alibaba-nacos-discovery
.
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency>
Nacos Discovery integrate with the Netflix Ribbon, RestTemplate or OpenFeign can be used for service-to-service calls.
For details about how to download and start Nacos, refer to the Nacos Website.
After Nacos Server starts, go to http://ip:8848 to view the console (default account name/password is nacos/nacos):
For more Nacos Server versions, you can download the latest version from release page.
The following sample illustrates how to register a service to Nacos.
<?xml version="1.0" encoding="UTF-8"? > <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>open.source.test</groupId> <artifactId>nacos-discovery-test</artifactId> <version>1.0-SNAPSHOT</version> <name>nacos-discovery-test</name> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>${spring.boot.version}</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring.cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${spring.cloud.alibaba.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
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 |
@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 Naco Website for more details. |
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. |
@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 path = String.format("http://%s:%s/echo/%s",serviceInstance.getHost(),serviceInstance.getPort(),appName); System.out.println("request path:" +path); return restTemplate.getForObject(path,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 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
Nacos Discovery provides an Endpoint internally with a corresponding endpoint id of nacos-discovery
.
Endpoint exposed json contains two properties:
The followings shows how a service instance accesses the Endpoint:
{ "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": "" } }
The following shows the other configurations of the starter of Nacos Discovery:
Configuration | Key | Default Value | Description |
---|---|---|---|
Server address |
| IP and port of the Nacos Server listener | |
Service name |
|
| Name the current service |
Weight |
|
| Value range: 1 to 100. The bigger the value, the greater the weight |
Network card name |
| 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 |
| Highest priority | |
Registered port |
|
| Will be detected automatically by default. Do not need to be configured. |
Namespace |
| 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 |
| Alibaba Cloud account accesskey | |
SecretKey |
| Alibaba Cloud account secretkey | |
Metadata |
| You can define some of the metadata for your services in the Map format | |
Log file name |
| ||
Endpoint |
| 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 |
|
| Set to true in most cases |
Enable Nacos Watch |
|
| set to false to close watch |
Nacos is an easy-to-use dynamic service discovery, configuration and service management platform for building cloud native applications.
Use Spring Cloud Alibaba Nacos Config to quickly access Nacos configuration management capabilities based on Spring Cloud’s programming model.
please use the starter with the group ID as com.alibaba.cloud
and the artifact ID as spring-cloud-starter-alibaba-nacos-config
.
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency>
Nacos Config uses DataId and GROUP to determine a configuration.
The following figure shows that the DataId uses myDataid
, GROUP uses DEFAULT_GROUP
, and configures a configuration item of the format Properties:
For specific startup methods, refer to the "Nacos Server Startup" section of the Spring Cloud Alibaba Nacos Discovery section.
After the Nacos Server is started, add how to configure it:
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. |
If you want to use Nacos to manage externalized configurations for your applications, please use the starter with the group ID as com.alibaba.cloud
and the artifact ID as spring-cloud-starter-alibaba-nacos-config
.
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency>
Now we can create a standard Spring Boot application.
@SpringBootApplication public class NacosConfigApplication { public static void main(String[] args) { ConfigurableApplicationContext applicationContext = SpringApplication.run(NacosConfigApplication.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.
# DataId By default, the `spring.application.name` configuration is combined with the file extension (the configuration format uses properties by default), and the GROUP is not configured to use DEFAULT_GROUP by default. Therefore, the Nacos Config configuration corresponding to the configuration file has a DataId of nacos-config.properties and a GROUP of DEFAULT_GROUP 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 |
Run this example and you can see the following output:
2018-11-02 14:24:51.638 INFO 32700 --- [main] c.a.demo.provider.NacosConfigApplication : Started NacosConfigApplication 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
Nacos Config supports yaml format as well. You only need to complete the following 2 steps.
bootstrap.properties.
spring.cloud.nacos.config.file-extension=yaml
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.
2018-11-02 14:59:00.484 INFO 32928 --- [main] c.a.demo.provider.NacosConfigApplication:Started NacosConfigApplication 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
Nacos Config also supports dynamic configuration updates. The code for starting Spring Boot application testing is as follows:
@SpringBootApplication public class NacosConfigApplication { public static void main(String[] args) { ConfigurableApplicationContext applicationContext = SpringApplication.run(NacosConfigApplication.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:
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`. |
When configurations are loaded by 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.
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:
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:
@SpringBootApplication public class NacosConfigApplication { public static void main(String[] args) { ConfigurableApplicationContext applicationContext = SpringApplication.run(NacosConfigApplication.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:
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:
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:
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:
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 |
For details about namespaces in Nacos, refer to Nacos Concepts
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:
spring.cloud.nacos.config.namespace=b3404bc0-d7dc-4855-b519-570ed34b62d7
Note | |
---|---|
This configuration must be in the bootstrap.properties file. The value of |
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:
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 |
As of Spring Cloud Alibaba Nacos Config, data id can be self-defined. For detailed design of this part, refer to Github issue. The following is a complete sample:
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:
spring.cloud.nacos.config.ext-config[n].data-id
.spring.cloud.nacos.config.ext-config[n].group
. If not specified, DEFAULT_GROUP is used.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 |
Note | |
---|---|
The value of |
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:
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:
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 |
Note | |
---|---|
When using |
Note | |
---|---|
When |
Nacos Config provides an Endpoint internally with a corresponding endpoint id of nacos-config
.
Endpoint exposed json contains three properties:
The followings shows how a service instance accesses the Endpoint:
{ "NacosConfigProperties": { "serverAddr": "127.0.0.1:8848", "encode": null, "group": "DEFAULT_GROUP", "prefix": null, "fileExtension": "properties", "timeout": 3000, "endpoint": null, "namespace": null, "accessKey": null, "secretKey": null, "contextPath": null, "clusterName": null, "name": null, "sharedDataids": "base-common.properties,common.properties", "refreshableDataids": "common.properties", "extConfig": null }, "RefreshHistory": [{ "timestamp": "2019-07-29 11:20:04", "dataId": "nacos-config-example.properties", "md5": "7d5d7f1051ff6571e2ec9f90887d9d91" }], "Sources": [{ "lastSynced": "2019-07-29 11:19:04", "dataId": "common.properties" }, { "lastSynced": "2019-07-29 11:19:04", "dataId": "base-common.properties" }, { "lastSynced": "2019-07-29 11:19:04", "dataId": "nacos-config-example.properties" }] }
set spring.cloud.nacos.config.enabled = false to disable Spring Cloud Nacos Config AutoConfiguration.
The following shows the other configurations of the starter of Nacos Config:
Configuration | Key | Default Value | Description |
---|---|---|---|
Server address |
| IP and port of the Nacos Server listener | |
Dataid from nacos config |
| First take the prefix, then go to the name, and finally take spring.application.name | |
Dataid from nacos config |
| First take the prefix, then go to the name, and finally take spring.application.name | |
Encode for nacos config content |
| Encode for nacos config content | |
GROUP for nacos config |
|
| GROUP for nacos config |
The suffix of nacos config dataId, also the file extension of config content. |
|
| The suffix of nacos config dataId, also the file extension of config content(now support properties or yaml(yml)) |
Timeout for get config from nacos |
|
| Timeout for get config from nacos |
Endpoint |
| Endpoint | |
Namespace |
| Namespace | |
AccessKey |
| Alibaba Cloud account accesskey | |
SecretKey |
| Alibaba Cloud account secretkey | |
The context path of Nacos Server |
| The context path of Nacos Server | |
Cluster name |
| Cluster name | |
Dataid for Shared Configuration |
| Dataid for Shared Configuration, split by "," | |
Dynamic refresh dataid for Shared Configuration |
| Dynamic refresh dataid for Shared Configuration, split by "," | |
custom dataid |
| It’s a List,build up by |
As microservices become popular, the stability of service calls is becoming increasingly important. Sentinel takes "flow" as the breakthrough point, and works on multiple fields including flow control, circuit breaking and load protection to protect service reliability.
Sentinel has the following features:
If you want to use Sentinel in your project, please use the starter with the group ID as com.alibaba.cloud
and the artifact ID as spring-cloud-starter-alibaba-sentinel
.
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency>
The following is a simple example of how to use Sentinel:
@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 Sentinel Annotation Support.
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.
To use the Sentinel dashboard, simply complete the following 3 steps.
You can download the latest dashboard JAR file from the Release Page.
You can also get the latest source code to build your own Sentinel dashboard:
mvn clean package
Sentinel dashboard is a standard SpringBoot application, and you can run the JAR file in the Spring Boot mode.
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.
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 Sentinel Dashboard.
Sentinel is compatible with the OpenFeign component. To use it, in addition to introducing the sentinel-starter
dependency, complete the following 2 steps:
feign.sentinel.enabled=true
openfeign starter
dependency to trigger and enable sentinel starter
:<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
This is a simple usage of FeignClient
:
@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. All the attributes in the |
The corresponding resource name of the echo
method in the EchoService
interface is GET:http://service-provider/echo/{str}
.
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.
@Bean @SentinelRestTemplate(blockHandler = "handleException", blockHandlerClass = ExceptionUtil.class) public RestTemplate restTemplate() { return new RestTemplate(); }
The attribute 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 and return value of method in @SentinelRestTemplate
is same as org.springframework.http.client.ClientHttpRequestInterceptor#interceptor
, but it has one more parameter BlockException
to catch the exception by Sentinel.
The method signature of handleException
in ExceptionUtil
above should be like this:
public class ExceptionUtil { public static ClientHttpResponse handleException(HttpRequest request, byte[] body, ClientHttpRequestExecution execution, BlockException exception) { ... } }
Note | |
---|---|
When the application starts, it will check if the |
The attribute of the @SentinelRestTemplate
annotation is optional.
It will return RestTemplate request block by sentinel
when you using RestTemplate
blocked by Sentinel. You can override it by your own logic. We provide SentinelClientHttpResponse
to handle the response.
Sentinel RestTemplate provides two granularities for resource rate limiting:
httpmethod:schema://host:port/path
: Protocol, host, port and pathhttpmethod:schema://host:port
: Protocol, host and portNote | |
---|---|
Take Http GET |
SentinelProperties
provide datasource
attribute to configure datasource.
For example, 4 data sources are configures:
spring.cloud.sentinel.datasource.ds1.file.file=classpath: degraderule.json spring.cloud.sentinel.datasource.ds1.file.rule-type=flow #spring.cloud.sentinel.datasource.ds1.file.file=classpath: flowrule.json #spring.cloud.sentinel.datasource.ds1.file.data-type=custom #spring.cloud.sentinel.datasource.ds1.file.converter-class=JsonFlowRuleListConverter #spring.cloud.sentinel.datasource.ds1.file.rule-type=flow spring.cloud.sentinel.datasource.ds2.nacos.server-addr=localhost:8848 spring.cloud.sentinel.datasource.ds2.nacos.data-id=sentinel spring.cloud.sentinel.datasource.ds2.nacos.group-id=DEFAULT_GROUP spring.cloud.sentinel.datasource.ds2.nacos.data-type=json spring.cloud.sentinel.datasource.ds2.nacos.rule-type=degrade 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.ds3.zk.rule-type=authority 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.sentinel.datasource.ds4.apollo.rule-type=param-flow
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 |
Every data source has 3 common configuration items: data-type
, converter-class
and rule-type
.
data-type
refers to Converter
. Spring Cloud Alibaba Sentinel provides two embedded values by default: 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.
rule-type
refers to the rule type in datasource(flow
,degrade
,authority
,system
, param-flow
, gw-flow
, gw-api-group
).
Note | |
---|---|
XML format is not supported by default. To make it effective, you need to add the |
To learn more about how dynamic data sources work in Sentinel, refer to Dynamic Rule Extension.
If you want to use Sentinel Starter with Zuul, you need to add the spring-cloud-alibaba-sentinel-gateway
dependency, and you need to add the spring-cloud-starter-netflix-zuul
dependency to let Zuul AutoConfiguration class in the gateway module takes effect:
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency>
Sentinel provides an Endpoint internally with a corresponding endpoint id of sentinel
.
Endpoint exposed json contains multi properties:
The followings shows how a service instance accesses the Endpoint:
{ "blockPage": null, "appName": "sentinel-example", "consoleServer": "localhost:8080", "coldFactor": "3", "rules": { "flowRules": [{ "resource": "GET:http://www.taobao.com", "limitApp": "default", "grade": 1, "count": 0.0, "strategy": 0, "refResource": null, "controlBehavior": 0, "warmUpPeriodSec": 10, "maxQueueingTimeMs": 500, "clusterMode": false, "clusterConfig": null }, { "resource": "/test", "limitApp": "default", "grade": 1, "count": 0.0, "strategy": 0, "refResource": null, "controlBehavior": 0, "warmUpPeriodSec": 10, "maxQueueingTimeMs": 500, "clusterMode": false, "clusterConfig": null }, { "resource": "/hello", "limitApp": "default", "grade": 1, "count": 1.0, "strategy": 0, "refResource": null, "controlBehavior": 0, "warmUpPeriodSec": 10, "maxQueueingTimeMs": 500, "clusterMode": false, "clusterConfig": null }] }, "metricsFileCharset": "UTF-8", "filter": { "order": -2147483648, "urlPatterns": ["/*"], "enabled": true }, "totalMetricsFileCount": 6, "datasource": { "ds1": { "file": { "dataType": "json", "ruleType": "FLOW", "converterClass": null, "file": "...", "charset": "utf-8", "recommendRefreshMs": 3000, "bufSize": 1048576 }, "nacos": null, "zk": null, "apollo": null, "redis": null } }, "clientIp": "30.5.121.91", "clientPort": "8719", "logUsePid": false, "metricsFileSize": 52428800, "logDir": "...", "heartbeatIntervalMs": 10000 }
The following table shows that when there are corresponding bean types in ApplicationContext
, some actions will be taken:
Existing Bean Type | Action | Function |
---|---|---|
|
| Resource cleaning(resource(for example, classify all URLs of /foo/:id to the /foo/* resource)) |
|
| Customize rate limiting logic |
|
| Setting the origin |
The following table shows all the configurations of Spring Cloud Alibaba Sentinel:
Configuration | Description | Default Value |
---|---|---|
| Project Name Of Sentinel | |
| Whether Sentinel automatic configuration takes effect | true |
| Whether to trigger Sentinel initialization in advance | false |
| Port for the application to interact with Sentinel dashboard. An HTTP Server which uses this port will be started in the application | 8719 |
| Sentinel dashboard address | |
| Hearbeat interval between the application and Sentinel dashboard | |
| The client IP of this configuration will be registered to the Sentinel Server side. | |
| Loading order of Servlet Filter. The filter will be constructed in the Starter | Integer.MIN_VALUE |
| Data type is array. Refers to the collection of Servlet Filter ULR patterns | /* |
| Enable to instance CommonFilter | true |
| metric file character set | UTF-8 |
| Sentinel metric single file size | |
| Sentinel metric total file number | |
| Directory of Sentinel log files | |
| If PID is required for Sentinel log file names | false |
| Customized redirection URL. When rate limited, the request will be redirected to the pre-defined URL | |
| 3 | |
| The order of SentinelZuulPreFilter | 10000 |
| The order of SentinelZuulPostFilter | 1000 |
| The order of SentinelZuulErrorFilter | -1 |
Note | |
---|---|
These configurations will only take effect in servlet environment. RestTemplate and Feign will not take effect for these configurations. |
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:
Download Latest Binary File of RocketMQ, and decompress it.
The decompressed directory is as follows:
apache-rocketmq ├── LICENSE ├── NOTICE ├── README.md ├── benchmark ├── bin ├── conf └── lib
nohup sh bin/mqnamesrv & tail -f ~/logs/rocketmqlogs/namesrv.log
nohup sh bin/mqbroker -n localhost:9876 &
tail -f ~/logs/rocketmqlogs/broker.log
Send messages:
sh bin/tools.sh org.apache.rocketmq.example.quickstart.Producer
Output when the message is successfuly sent: SendResult [sendStatus=SEND_OK, msgId= …
Receive messages:
sh bin/tools.sh org.apache.rocketmq.example.quickstart.Consumer
Output when the message is successfully received: ConsumeMessageThread_%d Receive New Messages: [MessageExt…
sh bin/mqshutdown broker sh bin/mqshutdown namesrv
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
For example, Kafka
uses KafkaMessageChannelBinder
, RabbitMQ
uses RabbitMessageChannelBinder
, while RocketMQ
uses RocketMQMessageChannelBinder
.
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.
Now let’s use Spring Cloud Stream to write a simple code for sending and receiving messages:
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.
For using the Spring Cloud Alibaba RocketMQ Binder, you just need to add it to your Spring Cloud Stream application, using the following Maven coordinates:
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-stream-binder-rocketmq</artifactId> </dependency>
Alternatively, you can also use the Spring Cloud Stream RocketMQ Starter:
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-stream-rocketmq</artifactId> </dependency>
This is the implementation architecture of Spring Cloud Stream RocketMQ Binder:
The implementation of RocketMQ Binder depend on the RocketMQ-Spring framework.
RocketMQ Spring framework is an integration of RocketMQ and Spring Boot. It provides three main features:
RocketMQTemplate
: Sending messages, including synchronous, asynchronous, and transactional messages.@RocketMQTransactionListener
: Listen and check for transaction messages.@RocketMQMessageListener
: Consume messages.RocketMQMessageChannelBinder
is a standard implementation of Binder, it will build RocketMQInboundChannelAdapter
and RocketMQMessageHandler
internally.
RocketMQMessageHandler
will construct RocketMQTemplate
based on the Binding configuration. RocketMQTemplate
will convert the org.springframework.messaging.Message
message class of spring-messaging
module to the RocketMQ message class org.apache.rocketmq.common .message.Message
internally, then send it out.
RocketMQInboundChannelAdapter
will also construct RocketMQListenerBindingContainer
based on the Binding configuration, and RocketMQListenerBindingContainer
will start the RocketMQ Consumer
to receive the messages.
Note | |
---|---|
RocketMQ Binder Application can also be used to configure rocketmq.** to trigger RocketMQ Spring related AutoConfiguration |
Currently Binder supports setting the relevant key in Header
to set the properties of the RocketMQ message.
For example, TAGS
, DELAY
, TRANSACTIONAL_ARG
, KEYS
, WAIT_STORE_MSG_OK
, FLAG
represent the labels corresponding to the RocketMQ message.
MessageBuilder builder = MessageBuilder.withPayload(msg) .setHeader(RocketMQHeaders.TAGS, "binder") .setHeader(RocketMQHeaders.KEYS, "my-key") .setHeader("DELAY", "1"); Message message = builder.build(); output().send(message);
The name server of RocketMQ Server(Older versions use the namesrv-addr configuration item).
Default: 127.0.0.1:9876
.
The AccessKey of Alibaba Cloud Account.
Default: null.
The SecretKey of Alibaba Cloud Account.
Default: null.
Enable Message Trace feature for all producers and consumers.
Default: true
.
The trace topic for message trace.
Default: RMQ_SYS_TRACE_TOPIC
.
The following properties are available for RocketMQ producers only and must be prefixed with spring.cloud.stream.rocketmq.bindings.<channelName>.consumer.
.
Enable Consumer Binding.
Default: true
.
Consumer subscription tags expression, tags split by ||
.
Default: empty.
Consumer subscription sql expression.
Default: empty.
Control message mode, if you want all subscribers receive message all message, broadcasting is a good choice.
Default: false
.
Receiving message concurrently or orderly.
Default: false
.
Message consume retry strategy for concurrently consume:
>0,client control retry frequency
Default: 0
.
Time interval of message consume retry for orderly consume.
Default: 1000
.
The following properties are available for RocketMQ producers only and must be prefixed with spring.cloud.stream.rocketmq.bindings.<channelName>.producer.
.
Enable Producer Binding.
Default: true
.
Producer group name.
Default: empty.
Maximum allowed message size in bytes.
Default: 8249344
.
Send Transactional Message.
Default: false
.
Send message in synchronous mode.
Default: false
.
Send message with vip channel.
Default: true
.
Millis of send message timeout.
Default: 3000
.
Compress message body threshold, namely, message body larger than 4k will be compressed on default.
Default: 4096
.
Maximum number of retry to perform internally before claiming sending failure in synchronous mode.
Default: 2
.
Maximum number of retry to perform internally before claiming sending failure in asynchronous mode.
Default: 2
.
Indicate whether to retry another broker on sending failure internally.
Default: false
.
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 |
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
.
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alicloud-ans</artifactId> </dependency>
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.
@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.
# 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. |
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.
Refer to the 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.
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.
# 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 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 Sign up for EDAS first. You will not be charged under the pay-as-you-go mode. Then log on to the 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. |
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. |
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
.
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alicloud-acm</artifactId> </dependency>
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.
@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.
# 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. |
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.
Refer to the 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). |
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 ACM Console
# 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. |
Group: DEFAULT_GROOUP DataId: acm-config.properties Content: user.name=james user.age=18
Note | |
---|---|
The format of dataId is |
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. |
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 |
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.
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.
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.
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:
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 |
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.
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. |
Note | |
---|---|
If you configured |
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.
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.
We’ve released Spring Cloud Alibaba version 0.2.1. You will need to add dependency management POM first.
<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.
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alicloud-oss</artifactId> </dependency>
Before you start to use Spring Cloud Alibaba Cloud OSS, please add the following configurations in application.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 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 Documentation and get the endpoint for your region.
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.
@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 Register an Alibaba Cloud Account. If you already have one, please Sign up for OSS.
Log on to the 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 OSS Console.
For more instructions on OSS APIs, please refer to OSS SDK Documentation.
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.
@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. |
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.
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 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 STS Documentation.
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.
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 OSSClient Configurations.
Note | |
---|---|
In most cases, you need to connect the parameter names with “-” for the parameters in the table of OSSClient Configurations with “-”, and all letters should be in lowercase. For example, ConnectionTimeout should be changed to connection-timeout. |
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.
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
.
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alicloud-schedulerX</artifactId> </dependency>
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.
@SpringBootApplication public class ScxApplication { public static void main(String[] args) { SpringApplication.run(ScxApplication.class, args); } }
Add the following configurations in the application.properties file.
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 Register an Alibaba Cloud account, and then Sign up for EDAS and Sign up for SchedulerX as well.
To get the group-id, refer to the SchedulerX Documentation.
Note | |
---|---|
When you create a group, please select the “test” region. |
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.
public class SimpleTask implements ScxSimpleJobProcessor { @Override public ProcessResult process(ScxSimpleJobContext context) { System.out.println("-----------Hello world---------------"); ProcessResult processResult = new ProcessResult(true); return processResult; } }
Go to the SchedulerX Jobs page, select the “Test” region, and click “Create Job” on the upper-right corner to create a job, as shown below.
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 SchedulerX Documentation.
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:
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 Security Information to get your AccessKeys.
Domain-name is not mandatory. You can refer to SchedulerX Documentation for details.
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.
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
.
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alicloud-sms</artifactId> </dependency>
Before you start to use Spring Cloud Alibaba Cloud SMS, please add the following configurations in application.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 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.
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:
@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 Register an Alibaba Cloud Account. If you already have one, please Turn on SMS Service.
For more information about SMS , please refer to the SMS official 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. |
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.
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:
@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 reference here
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:
@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 reference here
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.
@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 reference here.
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.
spring.cloud.alicloud.sms.up-queue-name=Alicom-Queue-********-SmsUp
2、Implement the SmsUpMessageListener interface and initialize a 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; } }
More message body format for Message can be reference here.