“This is the sixth day of my participation in the August More Text Challenge. For more details, see August More Text Challenge
Why Spring Cloud Alibaba?
Spring Cloud website:
Netflix was also officially recommended in the Spring Cloud in the past, but with multiple Netflix components in maintenance state, it can be said that Netflix components are no longer available due to potential risks. The most important components for microservices are service discovery, circuit breakers, gateways, and load balancing. However, these components are in maintenance state. So the Ailibaba component became our first choice.
The main components of Spring Cloud Alibaba
- Sentinel: Traffic as the entry point, from the flow control, circuit breaker degradation, system load protection and other dimensions to protect the stability of services.
- Nacos: A dynamic service discovery, configuration management, and service management platform that makes it easier to build cloud-native applications.
- RabbitMQ: An open source distributed messaging system based on highly available distributed clustering technology that provides low latency, highly reliable messaging publishing and subscription services.
- Dubbo: High-performance Java RPC framework.
- Seata: Alibaba’s open source product, an easy-to-use, high-performance microservice distributed transaction solution.
- Alibaba Cloud ACM: An application configuration center that centrally manages and pushes application configurations in a distributed architecture environment.
- Alibaba Cloud SchedulerX: distributed task scheduling product, providing second-level, accurate, highly reliable and highly available scheduled task scheduling service.
- Alibaba Cloud SMS: global coverage of SMS services, friendly, efficient, only Internet communication capabilities
- Alibaba Cloud OSS: object storage service.
Such as OSS SMS, SchedulerX, ACM, these are the charged products of Ali Cloud, these are not the necessities of building microservices, we can do without.
What is Nacos?
Service is a first-class citizen of the Nacos world. Nacos supports the discovery, configuration, and management of almost all major types of “services” :
Kubernetes Service
gRPC & Dubbo RPC Service
Spring Cloud RESTful Service
Use Nacos Server and register the application
1. Download Nacos. Download at github.com/alibaba/nac…
2. Decompress the Nacos package.
3. Start Nacos.
Under Windows startup
CMD startup. CMD or double-click startup. CMD
Linux/Unix/Mac started
sh startup.sh -m standalone
4. The startup is complete
Nacos has a complete Web interface with a lot of nice features. Open http://localhost:8848/nacos in a browser. The default user name and password are nacos
Nacos web interface
5. Write applications and register them with Nacos
Create a project:
pom.xml
<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>com.msr</groupId>
<artifactId>cloud-v2</artifactId>
<version>1.0 the SNAPSHOT</version>
<modules>
<module>cloud-user-center-nacos-7003</module>
<module>cloud-user-center-nacos-7005</module>
</modules>
<packaging>pom</packaging>
<! -- Unified management of JAR package versions -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.version>4.12</junit.version>
<log4j.version>1.2.17</log4j.version>
<lombok.version>1.16.18</lombok.version>
<mysql.version>8.0.18</mysql.version>
<druid.version>1.1.16</druid.version>
<druid.spring.boot.starter.version>1.1.10</druid.spring.boot.starter.version>
<spring.boot.version>2.2.2. RELEASE</spring.boot.version>
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
<spring.cloud.alibaba.version>2.1.0. RELEASE</spring.cloud.alibaba.version>
<mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version>
<mybatis-spring-boot-starter.version>2.1.1</mybatis-spring-boot-starter.version>
<hutool-all.version>5.1.0</hutool-all.version>
</properties>
<! After submodule inheritance, provide function: lock version + submodule don't mention groupId and version -->
<dependencyManagement>
<dependencies>
<! - spring boot 2.2.2 -- -- >
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.2. RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<! --spring cloud Hoxton.SR1-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<! - Spring cloud alibaba 2.1.0. RELEASE -- -- >
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.1.0. RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.spring.boot.starter.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis-spring-boot-starter.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<addResources>true</addResources>
</configuration>
</plugin>
</plugins>
</build>
</project>
Copy the code
Create module (cloud-user-center- NACOS-7003)
Pom. XML:
<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>cloud-v2</artifactId>
<groupId>com.msr</groupId>
<version>1.0 the SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-user-center-nacos-7003</artifactId>
<dependencies>
<! -- SpringCloud ailibaba nacos-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
Copy the code
UserCenter7003Application start classes: * * * *
package com.msr.cloudv2.nacos;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class UserCenter7003Application {
public static void main(String[] args) { SpringApplication.run(UserCenter7003Application.class, args); }}Copy the code
Configuration file:
server:
port: 7003
spring:
application:
name: cloud-user-center-provider-service
cloud:
nacos:
discovery:
server-addr: localhost:8848
management:
endpoints:
web:
exposure:
include: The '*'
Copy the code
Create module (cloud-user-center- NACOS-7005)
pom.xml
<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>cloud-v2</artifactId>
<groupId>com.msr</groupId>
<version>1.0 the SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-user-center-nacos-7005</artifactId>
<dependencies>
<! -- SpringCloud ailibaba nacos-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
Copy the code
Start the class:
package com.msr.cloudv2.nacos;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class Nacos7005Application {
public static void main(String[] args) { SpringApplication.run(Nacos7005Application.class, args); }}Copy the code
Configuration file:
server:
port: 7003
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
url: jdbc:mysql://localhost:3306/test? serverTimezone=Asia/Shanghai&characterEncoding=utf8
application:
name: cloud-user-center-provider-service
cloud:
nacos:
discovery:
server-addr: localhost:8848
management:
endpoints:
web:
exposure:
include: The '*'
Copy the code
Run the startup class to start and you can see that the program is successfully registered with the Nacos registry.
Log on to the Nacos Web interface to see: two real numbers have been successfully registered.
Mutual perception between services
It is clear that the user-center columns have been successfully registered, and that services in a registry are mutually aware of each other. The code is as follows:
Create a UserCenterController in the service on port 7003, and start both services. In the browser to http://localhost:7003/nacos/getServiceInfo
package com.msr.cloudv2.nacos.conttoller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("nacos")
public class UserCenterController {
@Autowired
private DiscoveryClient discoveryClient;
@Autowired
private RestTemplate restTemplate;
@GetMapping("getServiceInfo")
public Object testNacos(a) {
List<String> services = discoveryClient.getServices();
List<Object> list = new ArrayList<>();
services.stream().distinct().forEach(e -> list.add(discoveryClient.getInstances(e)));
return list;
}
@GetMapping("toCall7005")
public Object call(a) {
List<ServiceInstance> instances = discoveryClient.getInstances("cloud-user-center-provider-service");
List<ServiceInstance> serviceInstances = instances.stream().filter(e -> e.getPort() == 7005).collect(Collectors.toList());
ServiceInstance serviceInstance = serviceInstances.get(0);
URI uri = serviceInstance.getUri();
return restTemplate.getForObject(uri.toString()+"nacos/test", String.class);
}
@Bean
public RestTemplate restTemplate(a) {
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setReadTimeout(5000);
requestFactory.setConnectTimeout(5000);
return newRestTemplate(); }}Copy the code
Obviously, information about services registered with Nacos can be obtained through DiscoveryClient. The RestTemplate can then be used to invoke the service on port 7005.
First create a UserCenterController for the service on port 7005 as well
package com.msr.cloudv2.nacos.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("nacos")
public class UserCenterController {
@GetMapping("test")
public String testNacos(a) {
return "Welcome to use nacos port:"+7005; }}Copy the code
In the browser to http://localhost:7003/nacos/toCall7005
To sum up, services registered in Nacos can be perceived by each other, and can be invoked by some Http clients.
Domain model for Nacos service discovery
- NameSpace: NameSpace. The default NameSpace of Nacso is public. For example, in development, you can create a NameSpace for the development environment and test environment. In this way, you can isolate the environment by specifying a NameSpace for the service.
- Group: indicates a Group. Used when Nacos is used as a configuration center.
- **Service: ** micro Service
- Cluster: a virtual division of a specified microservice. DEFAULT is DEFAULT.
- **Instance: ** microservice Instance.
- Metadata: as long as it is used for versioning. For example, we may have multiple versions in development.
Configured to use
Create a NameSpace named dev under Nacos, and then create another NameSpace under that NameSpace
cloud-user-center-nacos-7003
You only need to configure namespace and cluster-name in the configuration file.
server:
port: 7003
spring:
application:
name: cloud-user-center-provider-service
cloud:
nacos:
discovery:
server-addr: localhost:8848
# Develop NameSpace with value UUID
namespace:
# list such as: Guangzhou cluster
cluster-name: GZ
management:
endpoints:
web:
exposure:
include: The '*'
Copy the code
cloud-user-center-nacos-7005
The configuration file
server:
port: 7005
spring:
application:
name: cloud-user-center-provider-service
cloud:
nacos:
discovery:
server-addr: localhost:8848
# Develop NameSpace with value UUID
namespace: fa47d71f-9bc5-45ae-96c1-a6b9f7c55700
# Beijing Cluster
cluster-name: BJ
management:
endpoints:
web:
exposure:
include: The '*'
Copy the code
The results
Registry: list of services
Click “Details” to see two clusters: GZ and BJ
Nacos meta information
Nacos Data (such as configuration and service) description information, such as service version, weight, disaster recovery policy, load balancing policy, authentication configuration, and various user-defined labels. In terms of scope, it can be classified into service level meta information, cluster meta information, and instance meta information.
Setting method: 1. Set on the console of the Nacos Web interface. 2. Set parameters in the configuration file
Settings in the configuration file
server:
port: 7003
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root
url: jdbc:mysql://localhost:3306/test? serverTimezone=Asia/Shanghai&characterEncoding=utf8
application:
name: cloud-user-center-provider-service
cloud:
nacos:
discovery:
server-addr: localhost:8848
# Develop NameSpace with value UUID
namespace: fa47d71f-9bc5-45ae-96c1-a6b9f7c55700
# Guangzhou Cluster
cluster-name: GZ
metadata:
version: v1
info: user-center
management:
endpoints:
web:
exposure:
include: The '*'
Copy the code
conclusion
Short step without a thousand miles, not small flow into rivers and seas