Spring Cloud Alibaba Basic Course -Nacos (a)

The end of 2019, but also a long time did not write the blog, and today we talk about Nacos , if it helps, upper left corner of the trouble spots a concern to you, thank you! Hee hee today to write the first chapter

Why use Nacos

Eureka closed source

I believe that now, Eureka 2.0 closed source is not new. In 2017-2018, almost in the country set off a burst SpringCloud boom, almost a large part of the population with the focus on SpringBoot, we have started to pay attention up SpringCloud. And due to the ease of integration Eureka registry, etc., it is most preferred to use SpringCloud registry. But with the announcement of the official website of Eureka, as follows.

Here Insert Picture Description
View on the simple point that is open-source projects have stopped maintaining that kind of thing, not be introduced here, may be interested in Eureka on Github address https://github.com/Netflix/eureka/wiki

Nacos is also an excellent registry, and by the Alibaba open source, and the recent high heat, has been updated to version 1.14, and basically updated very frequently, Eureka is also a good choice after closed source.
Here Insert Picture Description
This can be seen from the following advantages Nacos of
Here Insert Picture Description
the configuration of the center point of view, the highest performance read and write performance of Nacos, Apollo followed, Spring Cloud Config rely Git scenes not suitable for open
discharge operation and maintenance of large-scale automation API. Apollo function most perfect, nacos Apollo has most of the configuration management functions, while Cloud the Spring
Config without operation and maintenance management interface, you need to develop their own. One advantage is the integration of Nacos registration center, distribution center function, deploy and operate compared to
Apollo must be intuitive and simple, so it simplifies the complexity of the architecture, and reduce operation and maintenance and deployment.
On the whole, Nacos features and advantages quite obvious, following us into the world of Nacos.

Nacos advantage

What is Nacos

Nacos supports DNS and RPC-based service discovery (as the registry springcloud), dynamic configuration services (distribution center can do), dynamic DNS service.

The official description is as follows:

Nacos committed to helping you discover, configure, and micro-management services. Nacos provides a set of simple-to-use set of features to help you achieve dynamic service discovery, service configuration management, service and traffic management.
Nacos help you more quickly and easily build, deliver and manage micro-service platform. Nacos is to build a "service" as the center of modern application architectures (such as micro-service paradigm, cloud native paradigm) service infrastructure.

Official website address: https://nacos.io/zh-cn/docs/what-is-nacos.html

一张图看到Nacos优势
Here Insert Picture Description
Here Insert Picture Description
包括虎牙在微服务改造方面 采用Nacos的好处
文章地址 : https://nacos.io/en-us/blog/huya-practice.html
全景图 :

Here Insert Picture Description

架构及概念的理解

Here Insert Picture Description

  • 服务 (Service)

服务是指一个或一组软件功能(例如特定信息的检索或一组操作的执行),其目的是不同的客户端可以为不同的目的重用(例如通过跨进程的网络调用)。Nacos 支持主流的服务生态,如 Kubernetes Service、gRPC|Dubbo RPC Service 或者 Spring Cloud RESTful Service.

  • 服务注册中心 (Service Registry)

服务注册中心,它是服务,其实例及元数据的数据库。服务实例在启动时注册到服务注册表,并在关闭时注销。服务和路由器的客户端查询服务注册表以查找服务的可用实例。服务注册中心可能会调用服务实例的健康检查 API 来验证它是否能够处理请求。

  • 服务元数据 (Service Metadata)

服务元数据是指包括服务端点(endpoints)、服务标签、服务版本号、服务实例权重、路由规则、安全策略等描述服务的数据

  • 服务提供方 (Service Provider)

是指提供可复用和可调用服务的应用方

  • 服务消费方 (Service Consumer)

是指会发起对某个服务调用的应用方

  • 配置 (Configuration)

在系统开发过程中通常会将一些需要变更的参数、变量等从代码中分离出来独立管理,以独立的配置文件的形式存在。目的是让静态的系统工件或者交付物(如 WAR,JAR 包等)更好地和实际的物理运行环境进行适配。配置管理一般包含在系统部署的过程中,由系统管理员或者运维人员完成这个步骤。配置变更是调整系统运行时的行为的有效手段之一。

  • 配置管理 (Configuration Management)

在数据中心中,系统中所有配置的编辑、存储、分发、变更管理、历史版本管理、变更审计等所有与配置相关的活动统称为配置管理。

  • 名字服务 (Naming Service)

提供分布式系统中所有对象(Object)、实体(Entity)的“名字”到关联的元数据之间的映射管理服务,例如 ServiceName -> Endpoints Info, Distributed Lock Name -> Lock Owner/Status Info, DNS Domain Name -> IP List, 服务发现和 DNS 就是名字服务的2大场景。

  • 配置服务 (Configuration Service)

在服务或者应用运行过程中,提供动态配置或者元数据以及配置管理的服务提供者

总结一下优势

相对于 Spring Cloud Eureka来说,Nacos 更强大。

为什么呢 ?

Nacos 相当于 一挑 2

  • Nacos == Spring Cloud Eureka + Spring Cloud Config
  • Nacos 可以与 Spring, Spring Boot, Spring Cloud 集成,并能代替 Spring Cloud Eureka, Spring Cloud Config。甚至提供图形化界面管理

通过 Nacos Server 和 spring-cloud-starter-alibaba-nacos-config 实现配置的动态变更。
通过 Nacos Server 和 spring-cloud-starter-alibaba-nacos-discovery 实现服务的注册与发现。

Nacos 快速入门 - Quick Start

1.1 安装Nacos Server

1.1.1 预备环境准备

Nacos 依赖 Java 环境来运行。如果您是从代码开始构建并运行Nacos,还需要为此配置 Maven环境,请确保是在
以下版本环境中安装使用:

  1. 64 bit OS,支持 Linux/Unix/Mac/Windows,推荐选用 Linux/Unix/Mac。
  2. 64 bit JDK 1.8+;下载 & 配置。
  3. Maven 3.2.x+;下载 & 配置。

1.1.2 下载源码或者安装包

你可以通过源码和发行包两种方式来获取 Nacos。

从 Github 上下载源码方式

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

下载编译后压缩包方式

您可以从 最新稳定版本 下载 nacos-server-$version.zip 包,本教程使用nacos-server-1.1.3版本。
下载地址:https://github.com/alibaba/nacos/releases

下载后解压:

unzip nacos-server-$version.zip 或者 tar -xvf nacos-server-$version.tar.gz
  cd nacos/bin

1.1.3启动服务器

nacos的默认端口是8848,需要保证8848默认端口没有被其他进程占用。
进入安装程序的bin目录:

Linux/Unix/Mac

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

sh startup.sh -m standalone

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

bash startup.sh -m standalone

Windows启动方式:

启动命令:

cmd startup.cmd

或者双击startup.cmd运行文件。

启动成功,可通过浏览器访问 http://127.0.0.1:8848/nacos ,打开如下nacos控制台登录页面:

Here Insert Picture Description

使用默认用户名:nacos,默认密码:nacos 登录即可打开主页面。Here Insert Picture Description

1.1.4OPEN API 配置管理测试

启动nacos成功后,可通过nacos提供的http api验证nacos服务运行是否正常。
下边我们通过 curl工具来测试nacos的open api:
curl 是开发中常用的命令行工具,可以用作HTTP协议测试。

本教程下载curl的windows版本:curl-7.66.0_2-win64-mingw,下载地址:https://curl.haxx.se/windows/
下载完成进入curl-7.66.0_2-win64-mingw的bin目录,进行下边的测试,通过测试可判断nacos是否正常工作:

发布配置

curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos.cfg.dataId&group=test&content=HelloWorld"

上边的命令表示向nacos发布一个配置:Here Insert Picture Description
点击详情
Here Insert Picture Description

获取配置
向nacos发布配置成功,就可以通过客户端从nacos获取配置信息,执行下边的命令:

获取 :curl -X GET "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos.cfg.dataId&group=test"

Here Insert Picture Description

通过测试发现,可以从nacos获取前边发布的配置:HelloWorld

1.1.5 关闭服务器

关闭nacos服务的方式如下:

Linux/Unix/Mac方式:

sh shutdown.sh

Windows

cmd shutdown.cmd

或者双击shutdown.cmd运行文件。

外部mysql数据库支持
单机模式时nacos默认使用嵌入式数据库实现数据的存储,若想使用外部mysql存储nacos数据,需要进行以下步
骤:

  • 1.安装数据库,版本要求:5.6.5+ ,mysql 8 以下 ,如果没有的话使用docker的数据库也行
  • 2.初始化mysql数据库,新建数据库nacos_config,数据库初始化文件:${nacoshome}/conf/nacosmysql.sql
  • 3.修改${nacoshome}/conf/application.properties文件,增加支持mysql数据源配置(目前只支持
    mysql),添加mysql数据源的url、用户名和密码。
spring.datasource.platform=mysql
db.num=1
db.url.0=jdbc:mysql://192.168.100.128:3306/nacos_config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=false&serverTimezone=UTC
db.user=root
db.password=123456
1.2 Nacos配置入门
1.2.1 发布配置

首先在nacos发布配置。

浏览器访问 http://127.0.0.1:8848/nacos ,打开nacos控制台,并点击菜单配置管理->配置列表:

在Nacos添加如下的配置:

Data ID: spiritmark-nacos-demo.yaml
Group : DEFAULT_GROUP
配置格式: YAML
配置内容:
 common:
config1: some-spiritmark

Note: 注意dataid是以 properties(默认的文件扩展名方式)为扩展名,这里使用yaml。

像下面这样 ,复制过去的配置内容 可以按Tab键一下 因为是yaml格式 注意层级
Here Insert Picture Description

发布配置
在点击“发布”,如下图,点击确定发布成功。
Here Insert Picture Description
查询配置

Here Insert Picture Description

1.2.2 Nacos客户端获取配置

我们需要新增一个名为spiritmark-nacos-demo的项目,坐标如下:


 <groupId>com.spiritmark.nacos</groupId>
    <artifactId>spiritmark-nacos-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

添加group ID 为 com.alibaba.nacos 和 artifact ID 为 nacos-clientstarter。用于实现项目中使用 Nacos 来
实现应用的外部化配置。

        <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
            <version>1.1.3</version>
        </dependency>

(1)完整 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>com.spiritmark.nacos</groupId>
    <artifactId>spiritmark-nacos-demo</artifactId>
    <version>1.0-SNAPSHOT</version>


    <dependencies>

        <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
            <version>1.1.3</version>
        </dependency>

    </dependencies>


</project>

(2)获取外部化配置

新增java执行类,并在执行过程中获取配置信息:

package com.spirimark.nacos;

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 SimpleDemoMain {
    public static void main(String[] args) throws NacosException {
        //nacos 地址
        String serverAddr = "127.0.0.1:8848";
//Data Id
        String dataId = "spiritmark-nacos-demo.yaml";
//Group
        String group = "DEFAULT_GROUP";
        Properties properties = new Properties();
        properties.put("serverAddr", serverAddr);
        ConfigService configService = NacosFactory.createConfigService(properties);
//获取配置,String dataId, String group, long timeoutMs
        String content = configService.getConfig(dataId, group, 5000);
        System.out.println(content);

    }
}

 common:
    config1: some-spiritmark

说明获取配置成功。

好了 第一篇就先写到这 ,马上快过年了,争取在过年之前写完 ,这也是 2019年最后的目标的 2020,在接再厉 !

Here Insert Picture Description

Published 43 original articles · won praise 116 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_42897427/article/details/104042076