• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Nacos使用教程

武飞扬头像
JackieGGu
帮助1

1. Nacos简介

一个更易于构建云原生应用的动态服务发现、配置管理和服务管理的平台;简单说就是一个注册中心 配置中心

2. 下载与安装

下载地址:https://github.com/alibaba/nacos/tags

选择对应版本并进入下载页面;

学新通

选择任意一个压缩包进行下载;

学新通

将下载的压缩包进行解压,如下:

学新通

运行bin目录下的startup脚本即可,注意:1.4.1版本开始该脚本默认是以集群方式运行,若需要单节点运行需要添加-m standalone参数运行脚本;

学新通

当出现以下界面与信息表示启动成功;

学新通

访问测试,默认端口8848,用户名:nacos,密码:nacos;

学新通

3. 数据持久化到MySQL

修改nacos的conf目录下的application.properties文件;

......
#*************** Config Module Related Configurations ***************#
### If use MySQL as datasource:
# 启用外部MySQL数据库
spring.datasource.platform=mysql

### Count of DB:
# 数据库数量
db.num=1

### Connect URL of DB:
# 数据库配置,0表示第一个数据库
db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
db.user.0=nacos
db.password.0=nacos
......
学新通

将nacos的conf目录下的nacos-mysql.sql数据库脚本导入上面配置的数据库中;

学新通

重启服务,进行查看;

学新通

4. 集群搭建

将所有nacos节点的conf目录下的cluster.conf.example文件拷贝一份并重命名为cluster.conf,然后在里面配置上所有集群节点的地址,注意:nacos集群必须全部使用外置MySQL数据库才能成功启动;

学新通

#
# Copyright 1999-2018 Alibaba Group Holding Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

#it is ip
#example
# 所有nacos服务地址,包括自己
192.168.1.20:8844
192.168.1.20:8846
192.168.1.20:8848
学新通

重新启动所有的nacos节点,注意1.4.1之前的版本(不包括1.4.1)startup启动脚本默认是单机运行的,需要添加-m cluster参数运行才可以,若部署在同一台服务器上注意修改不同节点的端口;

学新通

访问任意一个节点的管理平台,查看集群状态,出现如下显示表示集群搭建成功;

学新通

5. 微服务整合

5.1 注册中心整合

  1. 引入alibaba nacos服务发现依赖坐标,注意spring boot、spring cloud、spring cloud alibaba之间的版本兼容性,可通过https://github.com/alibaba/spring-cloud-alibaba/wiki/版本说明地址查看;

    <!-- 采用版本 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.3.2.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR9</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
    
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.2.6.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
    <dependencies>
        <!-- spring cloud alibaba dependency -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
    </dependencies>
    
    学新通
  2. 配置nacos服务地址;

    spring:
      application:
        # 微服务名称
        name: provider
      cloud:
        nacos:
          discovery:
            # nacos服务发现地址, 多个地址使用逗号隔开, 注意: 不用添加http(s)://
            server-addr: 192.168.1.20:8848
    

5.2 配置中心整合

  1. 引入alibaba nacos配置依赖坐标,同上需要注意版本之间的兼容性;

    <!-- spring boot、spring cloud、spring cloud alibaba采用版本同上 -->
    <!-- spring cloud alibaba dependency -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    </dependency>
    
  2. 删除微服务中的application.yml或者application.properties文件,新建bootstrap.yml文件,并在该文件中配置nacos配置地址;

    spring:
      application:
        # 微服务名称
        name: consumer
      cloud:
        nacos:
          config:
            # nacos服务配置地址, 多个地址使用逗号隔开, 注意: 不用添加http(s)://
            server-addr: 192.168.1.20:8846
            # 配置文件类型
            file-extension: yml
    
  3. 在nacos管理平台中新建该服务配置;

学新通

学新通

最后点击发布即可;

5.3 配置动态更新

使用@RefreshScope注解,如下:

@Service
// 刷新作用域:在运行时动态刷新对象属性
@RefreshScope
public class UserServiceImpl implements UserService {

    // 注入配置文件属性
    @Value("${user.sex}")
    private String userSex;

    @Autowired
    private UserDao userDao;

    @Override
    public UserEntity get(Long id) {
        UserEntity user = userDao.findById(id);
        user.setName(user.getName()   ":"   userSex);
        return user;
    }
}
学新通

5.4 多环境配置

在配置文件后面添加环境后缀,如下:

学新通

在启动jar包时,可通过参数--spring.profiles.active=[ev]或者-Dspring.profiles.active=[ev]来控制使用哪个配置文件,如:

java -jar test.jar --spring.profiles.active=dev

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgaachh
系列文章
更多 icon
同类精品
更多 icon
继续加载