zoukankan      html  css  js  c++  java
  • SpringCloud Alibaba系列(二) Nacos使用

    愿你生命中有够多的云翳,造就一个美好的黄昏 

     

    欢迎关注公众号【渣男小四】,一个喜欢技术更喜欢艺术的青年



    一.环境准备

       1.64 bit OS,支持 Linux/Unix/Mac/Windows,推荐选用 Linux/Unix/Mac。

      2.64 bit JDK 1.8+

      3.Maven 3.2.x+


    二.安装

      1.源码安装

    git clone https://github.com/alibaba/nacos.git
    cd nacos/
    mvn -Prelease-nacos -Dmaven.test.skip=true clean install -U  
    ls -al distribution/target/
    
    // change the $version to your actual path
    cd distribution/target/nacos-server-$version/nacos/bin

       启动服务

       启动命令(standalone代表着单机模式运行,非集群模式):

      sh startup.sh -m standalone

       如果您使用的是ubuntu系统,或者运行脚本报错提示[[符号找不到,可尝试如下运行:

      bash startup.sh -m standalone

      

       2.源码安装

      从GitHub下载好tar安装包

      tar -xvf nacos-server-$version.tar.gz cd nacos/bin

      启动同上

       3.Docker安装

      查找镜像
      docker search nacos   录取镜像   docker pull nacos
    /nacos-server
      运行镜像(单机版)
      
    docker run -d -p 8848:8848 -e MODE=standalone -v /opt/nacos/init.d/
    custom.properties:/home/nacos/init.d/custom.properties -v /opt/nacos/logs:/home/nacos/logs
    --restart always --name nacos nacos/nacos-server

       

      安装启动成功后浏览器输入http://ip:8848/nacos/ , 账号密码默认为nacos

       服务注册

       1.pom文件

            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
           <version>${latest.version}</version>  </dependency>

       2.yml文件

    spring:
      application:
        name: dream-gateway
      cloud:
        nacos:
          discovery:
            server-addr: 127.0.0.1:8848

        3.主启动类上加上注解

    @EnableDiscoveryClient

        

        服务注册到nacos成功


       Nacos Config(配置中心)

          Nacos 提供用于存储配置和其他元数据的 key/value 存储,为分布式系统中的外部化配置提供服务器端和客户端支持。使用 Spring Cloud Alibaba Nacos Config,您可以在 Nacos Server 集中管理你 Spring Cloud 应用的外部属性配置。

       Spring Cloud Alibaba Nacos Config 是 Config Server 和 Client 的替代方案,客户端和服务器上的概念与 Spring Environment 和 PropertySource 有着一致的抽象,在特殊的 bootstrap 阶段,配置被加载到 Spring 环境中。当应用程序通过部署管道从开发到测试再到生产时,您可以管理这些环境之间的配置,并确保应用程序具有迁移时需要运行的所有内容。

      在nacos可视化界面配置

        Data ID格式为: ${prefix}-${spring.profiles.active}.${file-extension}

        prefix 默认为 spring.application.name 的值,也可以通过配置项 spring.cloud.nacos.config.prefix来配置。

        spring.profiles.active 即为当前环境对应的 profile, 注意:当 spring.profiles.active 为空时,对应的连接符 - 也将不存在,dataId 的拼接格式变成 ${prefix}.${file-extension}  

        file-exetension 为配置内容的数据格式,可以通过配置项 spring.cloud.nacos.config.file-extension 来配置。目前只支持 properties 和 yaml 类型。


       代码:

        pom.xml

    <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    </dependency>

       bootstrapp.yml

    spring:
      application:
        name: dream-test
      cloud:
        nacos:
          config:
            file-extension: yaml  #不能为yml,不然识别不出来
            server-addr: 127.0.0.1:8848
          discovery:
            server-addr:
    127.0.0.1
    :8848

      application.yml

    spring:
      profiles:
        active: dev  #开发环境

      获取配置中心key-value

    
    
    @RefreshScope 注解动态刷新


    @RestController @RefreshScope
    public class NacosController { @Value("${user.name}") private String name; @Value("${user.age}") private int age; @GetMapping("/getProperties") public R nacosConfig(){ HashMap<String,Object> hashMap = new HashMap<>(); hashMap.put("name",name); hashMap.put("age",age); return R.success(hashMap); } }

      

      结果

    生命不止,折腾不息
  • 相关阅读:
    vux 数据模拟mockjs的使用
    vux 配置颜色问题
    vue-router 学习
    vue 学习笔记
    点击加载更多
    table td 固定宽度
    js scroll 滚动连续多次触发事件只执行一次
    Merge into的注意点之ORA-30926: 无法在源表中获得一组稳定的行?
    js页面中取值的注意点
    insert into的方式
  • 原文地址:https://www.cnblogs.com/steakliu/p/13756977.html
Copyright © 2011-2022 走看看