美文网首页
nacos单机部署

nacos单机部署

作者: 匿名用户xx | 来源:发表于2019-12-02 10:34 被阅读0次

    注册中心类似于通讯录,可以查找到服务和服务地址的映射关系
    一、安装jdk1.8

    yum install java-1.8.0-openjdk.x86_64 -y
    #yum方式安装没有配置JAVA_HOME环境变量
    cat>>/etc/profile<<EOF
    export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.232.b09-0.el7_7.x86_64/jre
    export PATH=$PATH:$JAVA_HOME/bin
    export CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
    EOF
    source /etc/profile
    

    二、安装参考官方文档
    nacos下载稳定版:
    https://github.com/alibaba/nacos/releases

    #解压
    unzip nacos-server-1.1.0.zip -d/usr/local/ && cd /usr/local/nacos/bin
    #启动单机模式
    sh startup.sh -m standalone
    

    [root@nacos bin]# sh startup.sh -m standalone
    /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.232.b09-0.el7_7.x86_64/jre/bin/java -Xms512m -Xmx512m -Xmn256m -Dnacos.standalone=true -Djava.ext.dirs=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.232.b09-0.el7_7.x86_64/jre/jre/lib/ext:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.232.b09-0.el7_7.x86_64/jre/lib/ext:/usr/local/nacos/plugins/cmdb:/usr/local/nacos/plugins/mysql -Xloggc:/usr/local/nacos/logs/nacos_gc.log -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=100M -Dnacos.home=/usr/local/nacos -Dloader.path=/usr/local/nacos/plugins/health -jar /usr/local/nacos/target/nacos-server.jar --spring.config.location=classpath:/,classpath:/config/,file:./,file:./config/,file:/usr/local/nacos/conf/ --logging.config=/usr/local/nacos/conf/nacos-logback.xml --server.max-http-header-size=524288
    nacos is starting with standalone
    nacos is starting,you can check the /usr/local/nacos/logs/start.out

    服务注册&发现和配置管理

    1、服务注册
    curl -X POST 'http://127.0.0.1:8848/nacos/v1/ns/instance?serviceName=nacos.naming.serviceName&ip=20.18.7.10&port=8080'
    返回ok
    2、服务发现
    curl -X GET 'http://127.0.0.1:8848/nacos/v1/ns/instance/list?serviceName=nacos.naming.serviceName'
    返回{"metadata":{},"dom":"nacos.naming.serviceName","cacheMillis":3000,"useSpecifiedURL":false,"hosts":[],"name":"DEFAULT_GROUP@@nacos.naming.serviceName","checksum":"a07d4655e7caf8a6aa987662a6179ad2","lastRefTime":1575104879006,"env":"","clusters":""}
    3、发布配置
    curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos.cfg.dataId&group=test&content=HelloWorld"
    返回true
    4、获取配置
    curl -X GET "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos.cfg.dataId&group=test"
    返回HelloWorld
    

    关闭服务器
    sh /usr/local/nacos/bin/shutdown.sh
    三、访问nacos图形界面(默认安装)
    http://ip:8848/nacos/
    默认账号密码nacos/nacos

    image.png
    image.png

    java示例代码

    /*
    * Demo for Nacos
    * pom.xml
        <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
            <version>${version}</version>
        </dependency>
    */
    package com.alibaba.nacos.example;
    
    import java.util.Properties;
    import java.util.concurrent.Executor;
    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;
    
    /**
     * Config service example
     * 
     * @author Nacos
     *
     */
    public class ConfigExample {
    
        public static void main(String[] args) throws NacosException, InterruptedException {
            String serverAddr = "localhost";
            String dataId = "nacos.cfg.dataId";
            String group = "test";
            Properties properties = new Properties();
            properties.put(PropertyKeyConst.SERVER_ADDR, serverAddr);
            ConfigService configService = NacosFactory.createConfigService(properties);
            String content = configService.getConfig(dataId, group, 5000);
            System.out.println(content);
            configService.addListener(dataId, group, new Listener() {
                @Override
                public void receiveConfigInfo(String configInfo) {
                    System.out.println("recieve:" + configInfo);
                }
    
                @Override
                public Executor getExecutor() {
                    return null;
                }
            });
            
            boolean isPublishOk = configService.publishConfig(dataId, group, "content");
            System.out.println(isPublishOk);
            
            Thread.sleep(3000);
            content = configService.getConfig(dataId, group, 5000);
            System.out.println(content);
    
            boolean isRemoveOk = configService.removeConfig(dataId, group);
            System.out.println(isRemoveOk);
            Thread.sleep(3000);
    
            content = configService.getConfig(dataId, group, 5000);
            System.out.println(content);
            Thread.sleep(300000);
    
        }
    }
    
    

    相关文章

      网友评论

          本文标题:nacos单机部署

          本文链接:https://www.haomeiwen.com/subject/ofgowctx.html