1、nacos brief introduction
Nacos Is to build in “ service ” Modern application architecture centered ( For example, the microservices paradigm 、 Cloud native paradigm ) Service infrastructure .
-
Service management : Implementation services CRUD, domain name CRUD, Service health check , Service weight management and other functions
-
Configuration Management : Implement configuration management CRUD, version management , Gray management , Monitoring management , Push trajectory , Aggregate data and other functions
-
Metadata management : Provide metadata CURD And marking ability
-
Plug-in mechanism : Realize the ability of three modules to be separated and combined , Implementation extension point SPI Mechanism
-
Event mechanism : Implement asynchronous event notification ,sdk Data change, asynchronous notification, etc
-
Log module : Manage log classification , The level of logging , Log portability ( Especially avoiding conflict ), Log format , Abnormal code + Help document
-
Callback mechanism :sdk Notification data , Call back the user through a unified mode . Interfaces and data structures need to be scalable
-
Addressing mode : solve ip, domain name ,nameserver、 Broadcast and other addressing modes , Need to be scalable
-
Push channel : solve server With storage 、server between 、server And sdk The problem of inter push performance
-
Capacity management : Manage every tenant , The capacity under the packet , Prevent the storage from being written explosion , Affect service availability
-
Traffic management : According to the tenant , Grouping and other dimensions on the request frequency , Number of long links , Message size , Request flow control to control
-
Caching mechanisms : Disaster recovery Directory , Local cache ,server Caching mechanisms . Disaster recovery directory needs tools
-
Boot mode : In stand-alone mode , Configuration mode , Service mode ,dns Pattern , perhaps all Pattern , Start different programs +UI
-
Agreement of conformity : Solve different data , In the case of different consistency requirements , Different consistency mechanisms
-
Storage module : Solve data persistence 、 Non persistent storage , Solve the problem of data fragmentation
-
Nameserver: solve namespace To clusterid The routing problem of , Solve the problem of user environment and nacos Physical environment mapping problem
-
CMDB: Solve metadata storage , With the three parties cmdb System docking problem , Solution application , people , Resource relations
-
Metrics: Exposure criteria metrics data , It is convenient to connect with the three party monitoring system
-
Trace: Exposure criteria trace, Convenience and SLA Get the system through , The journal is white and flat , Push trajectory, etc , And it can connect with the metering and billing system
-
Access management : It's equivalent to Alibaba cloud opening service , Assign identities 、 Capacity 、 Permission process
-
User management : Solve user management , Sign in ,sso Other questions
-
Rights management : Solve identification , Access control , Role management and so on
-
Audit system : Expand the interface to facilitate access to audit systems of different companies
-
Notification system : Core data changes , Or operation , Convenient passage SMS Get the system through , Inform the corresponding person of the data change
-
OpenAPI: Exposure criteria Rest style HTTP Interface , Simple and easy to use , Facilitate multilingual Integration
-
Console: Easy to use console , Do service management 、 Configuration management and other operations
-
SDK: Multilingual sdk
-
Agent:dns-f Similar patterns , Or with mesh And so on
-
CLI: Command line lightweight management of products
2、nacos Download start
Download installation package
https://github.com/alibaba/nacos/releases
When the download is complete , Need configuration mysql Connection information , stay nacos Of /conf/application.properties Configuration in file
Then execute... In the database nacos Database script for /conf/nacos-mysql.sql
Then you can start , When starting, use stand-alone mode
startup.cmd -m standalone
3、 Build a service delivery project
3.1、pom.xml
<?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>
<artifactId>provider</artifactId>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.2.2.RELEASE</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.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
<optional>true</optional>
</dependency>
</dependencies>
</project>
3.2、 The configuration file
Specify the project name and in the configuration file nacos The address of
server.port=8081
spring.application.name=nacos-provider
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
management.endpoints.web.exposure.include=*
3.3、 Interface
Build an interface , Enter who called this interface
@SpringBootApplication
@EnableDiscoveryClient
public class NacosProviderDemoApplication {
public static void main(String[] args) {
SpringApplication.run(NacosProviderDemoApplication.class, args);
}
@RestController
public class EchoController {
@GetMapping(value = "/echo/{string}")
public String echo(@PathVariable String string) {
return "Hello Nacos Discovery " + string;
}
}
}
4、 Functions of consumer services
4.1、pom.xml
<?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>cn.com.wenyl.alibaba</groupId>
<artifactId>consumer</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.2.2.RELEASE</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.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
<optional>true</optional>
</dependency>
</dependencies>
</project>
4.2、 The configuration file
server.port=8080
spring.application.name=nacos-consumer
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
management.endpoints.web.exposure.include=*
4.3、 Interface
Build an interface , The interface of the service provider is invoked in this interface
/**
* @author Mr.Wen
* @version 1.0
* @date 2021-08-24 16:54
*/
@SpringBootApplication
@EnableDiscoveryClient
public class NacosConsumerDemoApplication {
public static void main(String[] args) {
SpringApplication.run(NacosConsumerDemoApplication.class, args);
}
@RestController
public class NacosController{
@Resource
private LoadBalancerClient loadBalancerClient;
@Resource
private RestTemplate restTemplate;
@Value("${spring.application.name}")
private String appName;
@GetMapping("/echo/app-name")
public String echoAppName(){
//Access through the combination of LoadBalanceClient and RestTemplate
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();
}
}
5、 transfer
Call the consumption interface of the service consumer , Then the load balancing client selects a service to invoke (loadBanceClient It's the load balancing client , Call the service according to the service name ), The results are as follows
title :spring cloud Integrate nacos
author : wenyl
Address : http://www.wenyoulong.com/articles/2021/09/08/1631080254271.html