With the popularity of the concept of micro-services, more and more companies are adopting Spring Cloud
family barrel build micro-services systems, achieve rapid iteration of business. Spring Cloud
It provides services to quickly build distributed micro commonly used components, including Spring Cloud Eureka
, Spring Cloud Ribbon
, Spring Cloud Hystrix
, Spring Cloud Zuul
and so on. Dependent on Spring
a strong ecological environment, it has become Java
developers to build micro-service systems the preferred solution.
This article series, we meet another micro-service solutions Spring Cloud Alibaba
. Relying on Alibaba
strong technical support, and Eureka2.x
no longer maintained and other factors, we believe that the near future Spring Cloud Alibaba
will be the better choice for most companies to micro-services practice.
Next we introduce Nacos service registration discovery and configuration management capabilities.
Nacos committed to helping you discover, configure, and micro-management services. Nacos provides a set of simple-to-use set of features that help you quickly realize dynamic service discovery, service configuration, service metadata and traffic management.
Nacos help you more quickly and easily build, deliver and manage micro-service platform. Nacos is to build a "service" as the center of modern application architectures (such as micro-service paradigm, cloud native paradigm) service infrastructure.
First, find registration
First you need to download Nacos , directly extract the download is complete, the next startNacos
cd nacos/bin
// Linux/Unix/Mac
sh startup.sh -m standalone
// Windows
cmd startup.cmd -m standalone
Above that the use of independent way to start Nacos
, start after the completion of access Nacos Home , the default user name and password are nacos
After a successful login, you can see in the following figure
Next, create a service registry toNacos
- First create a dependent parent project management,
pom.xml
as follows
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<modules>
<module>service-provider</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>spring-cloud-alibaba-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>alibaba-demo</name>
<packaging>pom</packaging>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
<spring-cloud-alibaba.version>2.1.1.RELEASE</spring-cloud-alibaba.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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- Then create sub-module
service-provider
,pom.xml
as follows
<?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">
<parent>
<artifactId>spring-cloud-alibaba-demo</artifactId>
<groupId>com.example</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>service-provider</artifactId>
<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>
- In
service-provider
adding the modulebootstrap.yml
configuration file, as follows:
spring:
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
application:
name: service-provider
main:
allow-bean-definition-overriding: true
server:
port: 8080
The above configuration specifies service registration address discovery
- Start Class Code
@EnableDiscoveryClient
@SpringBootApplication
public class ServiceProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceProviderApplication.class, args);
}
}
Then start service-provider
, to the Nacos
list of services registered already observed up service
By Nacos
providing the Open-API you can see the list of services registered
➜ ~ curl -X GET '127.0.0.1:8848/nacos/v1/ns/service/list?pageNo=1&pageSize=10'
{"count":1,"doms":["service-provider"]}%
➜ ~
The basic use of this service has been completed registration, followed by introduction of the use of Configuration Management
Second, configuration management
For convenience, I directly on the service-provider
project to transform the presentation
- Modify
pom.xml
file
<?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">
<parent>
<artifactId>spring-cloud-alibaba-demo</artifactId>
<groupId>com.example</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>service-provider</artifactId>
<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>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Added spring-cloud-starter-alibaba-nacos-config
dependence
- modify
bootstrap.yml
spring:
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
config:
server-addr: ${spring.cloud.nacos.discovery.server-addr}
application:
name: service-provider
main:
allow-bean-definition-overriding: true
server:
port: 8080
The above configuration specifies configuration management service address
- Modify the startup class code
@RefreshScope
@RestController
@EnableDiscoveryClient
@SpringBootApplication
public class ServiceProviderApplication {
@Value("${config.test}")
private String testConfigValue;
@GetMapping("/config")
public String getConfigValue() {
return testConfigValue;
}
public static void main(String[] args) {
SpringApplication.run(ServiceProviderApplication.class, args);
}
}
Adding @RefreshScope
support dynamic configuration updates
- Add
Nacos
profile
Then start the project observed the console log output
2019-12-01 15:44:39.097 INFO 19295 --- [ main] c.a.c.n.c.NacosPropertySourceBuilder : Loading nacos data, dataId: 'service-provider.properties', group: 'DEFAULT_GROUP', data: config.test=这是一个测试值
You can see the local service has been acquired to Nacos
the data in the configuration management. Attempts to access data interfaces
➜ ~ curl -X GET '127.0.0.1:8080/config'
这是一个测试值%
➜ ~
The right to acquire the configuration data, then try to modify the configuration data
View the console log
2019-12-01 15:47:25.694 INFO 19295 --- [-127.0.0.1_8848] o.s.c.e.event.RefreshEventListener : Refresh keys changed: [config.test]
Description has been changed successfully, the service is synchronized to the latest configuration. Attempts to access data interfaces
➜ ~ curl -X GET '127.0.0.1:8080/config'
这是一个测试值-修改后%
➜ ~
Configure dynamic updates are already in force. Source Project
This article from the blog article multiple platforms OpenWrite release!