Alibaba Nacos quick application [dry information]

Alibaba Nacos quick application [dry information]

Official introduction:

Nacos is dedicated to helping you discover, configure and manage microservices. Nacos provides a simple and easy-to-use feature set to help you quickly implement dynamic service discovery, service configuration, service metadata and traffic management.

Nacos helps you build, deliver and manage microservices platforms more agilely and easily. Nacos is a service infrastructure for building modern application architecture centered on "services" (such as microservice paradigm, cloud native paradigm).

What is Nacos

Service discovery and service health monitoring
Nacos supports DNS-based and RPC-based service discovery. After the service provider uses the native SDK, OpenAPI, or an independent Agent TODO to register the Service, the service consumer can use DNS TODO or HTTP&API to find and discover the service.

Nacos provides real-time health checks on services and prevents requests from unhealthy hosts or service instances. Nacos supports health checks at the transport layer (PING or TCP) and application layer (such as HTTP, MySQL, user-defined). For health check of services in complex cloud environments and network topology environments (such as VPC, edge networks, etc.), Nacos provides two health check modes: agent reporting mode and server-side active detection. Nacos also provides a unified health check dashboard to help you manage service availability and traffic based on health status.

Dynamic Configuration Service
Dynamic Configuration Service allows you to manage the application configuration and service configuration of all environments in a centralized, external and dynamic manner.

Dynamic configuration eliminates the need to redeploy applications and services when configuration changes, making configuration management more efficient and agile.

Centralized management of configuration makes it easier to implement stateless services and elastically expand services on demand.

Nacos provides a simple and easy-to-use UI (console sample Demo) to help you manage the configuration of all services and applications. Nacos also provides a series of out-of-the-box configuration management features including configuration version tracking, canary release, one-click rollback of configuration, and client configuration update status tracking to help you manage configurations in a production environment more securely. Change and reduce the risk associated with configuration changes.

Dynamic DNS service
Dynamic DNS service supports weighted routing, allowing you to more easily implement middle-tier load balancing, more flexible routing policies, traffic control, and simple DNS resolution services for data center intranets. Dynamic DNS services also make it easier to implement service discovery based on the DNS protocol, helping you eliminate the risk of coupling to vendor-proprietary service discovery APIs.

Nacos provides some simple DNS APIs TODO to help you manage the service's associated domain names and available IP:PORT lists.

Service and metadata management
Nacos allows you to manage all services and metadata in the data center from the perspective of microservice platform construction, including management service descriptions, life cycles, static dependency analysis of services, service health status, and service traffic. Management, routing and security policies, service SLAs, and most importantly, metrics statistics.
The above text is reproduced from: Nacos Chinese documentation

quick start

Operating system: windows10
Development tools: idea 2018.3
spring-boot v2.2.1.RELEASE
spring-cloud v2.1.1.RELEASE
Nacos v1.1.4 Download address: https://github.com/alibaba/nacos/releases

Insert image description here
The download in this issue is nacos-server-1.1.4.zip

1. Unzip the compressed package

Unzip the downloaded unzipped package

2. Initialize the database required by Nacos

Insert image description here
Create Nacos database in mysql and set database character set
Insert image description here

3. Add mysql database connection for Nacos

Insert image description here
edit
Insert image description here
Insert image description here

4. Start Nacos

Insert image description here
Note: If the window flashes away after clicking, or there is no response after clicking. You can use the cmd command to start. There will be an error message when cmd is started
. Solution https://blog.csdn.net/weixin_30906425/article/details/97639117

Nacos started successfully

Insert image description here
Home:http://localhost:8848/nacos/index.html#/login
user:nacos password:nacos

Nacos service discovery

Create the parent project nacos-demo

Insert image description here
Insert image description here
Add maven dependency to parent project 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.nacos</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>nacos-demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <!--spirng-cloud版本配置-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.1.1.RELEASE</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

Create subproject nacos-server

Insert image description here

Create startup class
package cn.nacos;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableDiscoveryClient //开启服务发现
@RefreshScope //配置文件自动刷新
@RestController
public class NacosServerApplication {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(NacosServerApplication.class);
    }
    @Value("${nacos.name}")
    private String value;

    @RequestMapping("/getValue")
    public String getValue() {
    
    
        return value;
    }
}

Create the configuration file bootstrap.yml
server:
  port: 8081 #设置启动的端口号
spring:
  application:
    name: nacos-server #服务发现的名称
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848 #服务发现注册的地址
      config:
        server-addr: 127.0.0.1:8848 #配置中心的地址
        file-extension: yml #配置文件类型
nacos: #配置中心测试属性
  name:
    test
pom.xml file of nacos-server
<?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>demo</artifactId>
        <groupId>com.nacos</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.nacos</groupId>
    <artifactId>nacos-server</artifactId>
    <dependencies>
        <!--服务注册maven支持-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.1.1.RELEASE</version>
        </dependency>
        <!--配置中心maven支持-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            <version>2.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.10</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>
starting program

http://localhost:8848/nacos/index.html#/login Enter the nacos client.
Insert image description here
At this time, our service registration is successful.

Nacos configuration center

Testing http://localhost:8081/getValue
Insert image description here
found that the initial value of nacos:name is test. We need to modify the configuration file of the nacos-server service through the nacos configuration center without starting the server.

Create new configuration file

Insert image description here
Click the + sign in the upper right corner to create a new configuration
Insert image description here

Click Publish in the lower right corner

Insert image description here

test

Visit: http://localhost:8081/getValue.
Insert image description here
The nacos:name in the configuration file has become the value of the new configuration file we created in the nacos console, indicating that our configuration center has taken effect. It means that the nacos configuration center has been configured successfully! !

Follow WX official account: Jiawa.
More exciting and like-minded people are waiting for you.

Guess you like

Origin blog.csdn.net/weixin_45116026/article/details/103754260