Nacos入门到精通

发布时间:2024年01月18日

Nacos概述

? ? ? ? 全称Dynamic Naming and Configuration Service,是一个Nacos,全称Dynamic Naming and Configuration Service,是一个易于构建云原生应用的动态服务发现、配置管理和服务管理平台。其核心功能主要包括服务发现、健康检查、动态配置服务和动态DNS服务等。这些特性使得微服务的发现、管理、共享和组合更加容易。

? ? ? ?具体来说,Nacos支持基于DNS和RPC的服务发现,可以实时检查服务的健康状态,实现动态配置服务,并提供了动态DNS服务等特性。此外,Nacos还与Spring Cloud Alibaba紧密集成,可以作为注册中心,实现服务的注册与发现。

官网Releases · alibaba/nacos (github.com)

第一步:安装Nacos

这里,我就用Windows版本。

解压缩即可。

当然nacos也是用java写的,我们也可以直接运行这个jar包

是不是特别像springboot的配置文件。

第二步:启动nacos

startup.cmd -m standalone

当然,我这个是改了端口号的,我改回去即可。

正常运行(没改的情况下)的是8848端口号。

启动成功。访问Nacosicon-default.png?t=N7T8http://localhost:8848/nacos/#/login

默认用户名和密码都是nacos

第三步:配置管理

新建配置

然后发布即可

JavaSdk拉取配置

第一步:新建项目

pom文件

 <dependencies>
        <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
            <version>2.2.1</version>
        </dependency>
        <dependency>
            <groupId>com.google.sitebricks</groupId>
            <artifactId>slf4j</artifactId>
            <version>0.8.3</version>
        </dependency>
    </dependencies>

第二步:编写代码

package org.cyl;

import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;

import java.util.Properties;

public class Main {
    public static void main(String[] args) {
        try {
            String serverAddr="localhost:8848";
            String dataId="test";
            String group="DEFAULT_GROUP";
            Properties properties=new Properties();
            properties.put("serverAddr",serverAddr);
            ConfigService configService = NacosFactory.createConfigService(properties);
            String config = configService.getConfig(dataId, group, 5000);
            System.out.println(config);
        } catch (NacosException e) {
            throw new RuntimeException(e);
        }
    }
}

第三步:运行结果

JavaSdk监听配置

第一步:编写代码

package org.cyl;

import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;

import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.Executor;

public class Main {
    public static void main(String[] args) {
        try {
            String serverAddr="localhost:8848";
            String dataId="test";
            String group="DEFAULT_GROUP";
            Properties properties=new Properties();
            properties.put("serverAddr",serverAddr);
            ConfigService configService = NacosFactory.createConfigService(properties);
            String config = configService.getConfig(dataId, group, 5000);
            System.out.println(config);


            configService.addListener(dataId, group, new Listener() {
                @Override
                public Executor getExecutor() {
                    return null;
                }

                @Override
                public void receiveConfigInfo(String s) {
                    System.out.println(s);
                }
            });
            System.in.read();
        } catch (NacosException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

第二步:运行项目并修改nacos配置文件

之后看结果,已经监测到了:

Spring拉取配置

第一步:创建项目

pom文件

<?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>org.cyl</groupId>
    <artifactId>test07</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.6.13</version>
        </dependency>

            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
                <version>2022.0.0.0-RC2</version>
            </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.6.13</version>
        </dependency>
        <dependency>
            <groupId>com.google.sitebricks</groupId>
            <artifactId>slf4j</artifactId>
            <version>0.8.3</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-spring-context</artifactId>
            <version>1.1.1</version>
        </dependency>
    </dependencies>

</project>
MyApplication类
package org.cyl;

import com.alibaba.nacos.api.annotation.NacosProperties;
import com.alibaba.nacos.spring.context.annotation.config.EnableNacosConfig;
import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
import org.cyl.service.MyService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@ComponentScan
@Configuration
@EnableNacosConfig(globalProperties = @NacosProperties(serverAddr = "127.0.0.1:8848"))
@NacosPropertySource(dataId = "test",autoRefreshed = true)
public class MyApplication {


    public static void main(String[] args) {
        AnnotationConfigApplicationContext annotationConfigApplicationContext=new AnnotationConfigApplicationContext(MyApplication.class);
        MyService myService = annotationConfigApplicationContext.getBean(MyService.class);
        myService.test();
    }
}
MyService类
package org.cyl.service;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class MyService {
    @Value("${password}")
    public String username;
    public void test(){
        System.out.println(username);
    }
}

第二步:查看运行结果

Springboot拉取配置

pom文件

<?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>org.cyl</groupId>
    <artifactId>test07</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.6.13</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.6.13</version>
        </dependency>
        <dependency>
            <groupId>com.google.sitebricks</groupId>
            <artifactId>slf4j</artifactId>
            <version>0.8.3</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>nacos-config-spring-boot-starter</artifactId>
            <version>0.2.12</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.15.3</version>
        </dependency>
    </dependencies>

</project>

MyApplication类

package org.cyl;
import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
@NacosPropertySource(dataId = "test",autoRefreshed = true)
public class MyApplication {


    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class,args);
    }
}

application.properties

nacos.config.server-addr=127.0.0.1:8848

注意:如果想要全部写在properties里面应该这样写:

nacos.config.server-addr=127.0.0.1:8848
nacos.config.data-id=test
nacos.config.auto-refresh=true
nacos.config.bootstrap.enable=true

Controller

package org.cyl.controller;

import com.alibaba.nacos.api.config.annotation.NacosValue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @NacosValue(value = "${password}",autoRefreshed = true)
    public String password;

    @GetMapping("/")
    public String test(){
        return password;
    }
}

第二步:运行结果,查看

SpringCloud拉取项目

注意:有的版本是会直接读取bootstrap.properties里面

算了,我还是直接写一个微服务系列吧

文章来源:https://blog.csdn.net/m0_63251896/article/details/135632311
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。