springcloud -服务注册治理之nacos

任务

查询: CPU密集型和IO密集型对 CPU内核之间的关系。---线程池

什么是Nacos

**服务(Service)**是 Nacos 世界的一等公民。Nacos 支持几乎所有主流类型的"服务"的发现、配置和管理:

Kubernetes Service

gRPC & Dubbo RPC Service

Spring Cloud RESTful Service

nacos问题修复

问题描述: 读取nacos远程,多级数据出错。

yml 复制代码
user:
  username: admin
yml 复制代码
spring:
  cloud:
    nacos:
      config:
        server-addr: 192.168.201.107:8848
        namespace: a3f1db25-a200-47bc-b6f7-13aad8cc9ba8
        name: ssc-config
        group: DEV
        #配置文件格式
        file-extension: yaml
  application:
    name: ssc-alibaba-nacos

自动读取新配置

复制代码
@RefreshScope

Nacos的版本更新

下载安装

下载

上传到linux

解压三个

shell 复制代码
[root@localhost software]# tar -zxvf nacos-server-2.2.3.tar.gz 
nacos/LICENSE
nacos/NOTICE
nacos/target/nacos-server.jar
nacos/conf/
nacos/conf/derby-schema.sql
nacos/conf/1.4.0-ipv6_support-update.sql
nacos/conf/application.properties.example
nacos/conf/nacos-logback.xml
nacos/conf/announcement.conf
nacos/conf/mysql-schema.sql
nacos/conf/cluster.conf.example
nacos/conf/application.properties
nacos/bin/startup.sh
nacos/bin/startup.cmd
nacos/bin/shutdown.sh
nacos/bin/shutdown.cmd
shell 复制代码
drwxr-xr-x. 5 root root 72 12月  7 09:57 nacos_8848
drwxr-xr-x. 5 root root 72 12月  7 09:58 nacos_8858
drwxr-xr-x. 5 root root 72 12月  7 09:59 nacos_8868
[root@localhost nacos]# pwd
/usr/local/software/nacos

配置nacos(单8848)

修改applicaiton.properties.example文件, 修改文件名application.properties

复制代码
 33 #*************** Config Module Related Configurations ***************#
 34 ### If use MySQL as datasource:
 35 spring.datasource.platform=mysql
 36 
 37 ### Count of DB:
 38 db.num=1
 39 
 40 ### Connect URL of DB:
 41 db.url.0=jdbc:mysql://192.168.201.81:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=30    00&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai
 42 db.user.0=root
 43 # db.password.0=123
 44 
 45 ### Connection pool configuration: hikariCP
 46 db.pool.config.connectionTimeout=30000
 47 db.pool.config.validationTimeout=10000
 48 db.pool.config.maximumPoolSize=20
 49 db.pool.config.minimumIdle=2

创建数据库

nacos作为配置中心

命名空间

用于进行租户粒度的配置隔离。不同的命名空间下,可以存在相同的 Group 或 Data ID 的配置。Namespace 的常用场景之一是不同环境的配置的区分隔离,例如开发测试环境和生产环境的资源(如配置、服务)隔离等。

配置集 ID

Nacos 中的某个配置集的 ID。配置集 ID 是组织划分配置的维度之一。Data ID 通常用于组织划分系统的配置集。一个系统或者应用可以包含多个配置集,每个配置集都可以被一个有意义的名称标识。Data ID 通常采用类 Java 包(如 com.taobao.tc.refund.log.level)的命名规则保证全局唯一性。此命名规则非强制。

配置分组

Nacos 中的一组配置集,是组织配置的维度之一。通过一个有意义的字符串(如 Buy 或 Trade )对配置集进行分组,从而区分 Data ID 相同的配置集。当您在 Nacos 上创建一个配置时,如果未填写配置分组的名称,则配置分组的名称默认采用 DEFAULT_GROUP 。配置分组的常见场景:不同的应用或组件使用了相同的配置类型,如 database_url 配置和 MQ_topic 配置。

配置管理 (Configuration Management)

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

配置案例

nacos配置

springcloud

bootstrap.yml
  • fileExtension
  • shared-configs

yml 复制代码
spring:
  application:
    name: ssc-cloud-alibaba-nacos
  cloud:
    nacos:
      config:
        #配置nacos服务器ip:port
        server-addr: 192.168.201.107:8848
        #与nacos命名空间对应
        namespace: ssc-cloud-id
        #文件配置格式yaml/properties(默认)
        file-extension: yaml
        shared-configs:
          #读取db.yaml配置文件
          - data-id: db.yaml
            #组名
            group: DEV
            #自动读取发布更新
            refresh: true
          - data-id: es.yaml
            group: DEV
            refresh: true
启动
shell 复制代码
2023-12-07 11:14:09.437  INFO 10328 --- [           main] c.a.c.n.refresh.NacosContextRefresher    : [Nacos Config] Listening config: dataId=db.yaml, group=DEV
2023-12-07 11:14:09.440  INFO 10328 --- [           main] c.a.c.n.refresh.NacosContextRefresher    : [Nacos Config] Listening config: dataId=es.yaml, group=DEV

nacos作为服务治理中心

引入服务发现(discover)依赖

bootstrap.yml

yml 复制代码
spring:
  application:
    name: ssc-cloud-alibaba-nacos
  cloud:
    nacos:
      discovery:
        register-enabled: true
        server-addr: ${spring.cloud.nacos.config.server-addr}
      config:
        #配置nacos服务器ip:port
        server-addr: 192.168.201.107:8848
        #与nacos命名空间对应
        namespace: ssc-cloud-id
        #文件配置格式yaml/properties(默认)
        file-extension: yaml
        shared-configs:
          #读取db.yaml配置文件
          - data-id: db.yaml
            #组名
            group: DEV
            #自动读取发布更新
            refresh: true
          - data-id: es.yaml
            group: DEV
            refresh: true

启动类

java 复制代码
@SpringBootApplication
@EnableDiscoveryClient
public class NacosApp {
    public static void main(String[] args) {
        SpringApplication.run(NacosApp.class);
    }
}

测试注册

  1. 生成可运行jar包

    xml 复制代码
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
  2. 部署jar包

    复制代码
    java -jar xxxx.jar

nacos集群

为什么要使用集群

CAP,

Nacos: AP

nacos集群方案

配置nacos集群

nacos的conf文件夹

在nacos_8848,nacos_8858,nacos_8868中的clustor.conf文件中添加服务ip:port

shell 复制代码
#it is ip
#example
192.168.201.107:8848
192.168.201.107:8858
#192.168.201.107:8868
~                         

开放端口号

序号 服务器 端口号 Rpc端口
1 nacos_8848 8848 9848
2 nacos_8858 8858 9858
3 nacos_8868 8868 9868
shell 复制代码
[root@localhost conf]# firewall-cmd --add-port=9858/tcp --permanent 
success

[root@localhost conf]# firewall-cmd --list-ports 
8848/tcp 9848/tcp 9859/tcp 8858/tcp 9858/tcp

配置8858

复制代码
 cp application.properties ../../nacos_8858/conf

启动集群

./startup.sh

复制代码
         ,--.
       ,--.'|
   ,--,:  : |                                           Nacos 2.2.3
,`--.'`|  ' :                       ,---.               Running in cluster mode, All function modules
|   :  :  | |                      '   ,'\   .--.--.    Port: 8858
:   |   \ | :  ,--.--.     ,---.  /   /   | /  /    '   Pid: 30829
|   : '  '; | /       \   /     \.   ; ,. :|  :  /`./   Console: http://192.168.201.107:8858/nacos/index.html
'   ' ;.    ;.--.  .-. | /    / ''   | |: :|  :  ;_
|   | | \   | \__\/: . ..    ' / '   | .; : \  \    `.      https://nacos.io
'   : |  ; .' ," .--.; |'   ; :__|   :    |  `----.   \
|   | '`--'  /  /  ,.  |'   | '.'|\   \  /  /  /`--'  /
'   : |     ;  :   .'   \   :    : `----'  '--'.     /
;   |.'     |  ,     .-./\   \  /            `--'---'
'---'        `--`---'     `----'

2023-12-07 15:47:47,151 INFO The server IP list of Nacos is [192.168.201.107:8848, 192.168.201.107:8858]

shell脚本控制集群环境启动

启动脚本

shell 复制代码
                                                                                                  21,0-1       全部
#!/bin/bash

# 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.

echo "我们准备开启多个nacos服务集群..."

#调用启动nacos的脚本
/usr/local/software/nacos/nacos_8848/bin/startup.sh
/usr/local/software/nacos/nacos_8858/bin/startup.sh

关闭脚本

shell 复制代码
#!/bin/bash

# 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.
cd `dirname $0`/../target
target_dir=`pwd`

pid=`ps ax | grep -i 'nacos.nacos' | grep ${target_dir} | grep java | grep -v grep | awk '{print $1}'`
if [ -z "$pid" ] ; then
        echo "No nacosServer running."
        exit -1;
fi

echo "The nacosServer(${pid}) is running..."

kill ${pid}

echo "Send shutdown request to nacosServer(${pid}) OK"

nginx+nacos集群

序号 服务器 端口号 Rpc端口 nginx(http) nginx(rpc)
1 nacos_8848 8848 9848 7777 8777
2 nacos_8858 8858 9858 7777 8777
3 nacos_8868 8868 9868

8848/8858 ---->nginx监听7777

9848/9858----->nginx监听8777

shell 复制代码
docker run -it \
--name nginx \
-p 8080:8080 \
-p 8075:80 \
-p 7777:7777 \
-p 8777:8777 \
--privileged=true \
--network wn_docker_net \
--ip 172.18.12.90 \
-v /etc/localtime:/etc/localtime \
-v /usr/local/software/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /usr/local/software/nginx/conf/conf.d:/etc/nginx/conf.d \
-v /usr/local/software/nginx/html:/usr/share/nginx/html \
-v /usr/local/software/nginx/logs:/var/log/nginx \
-d nginx 
properties 复制代码
user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}
stream {
   #loadBlance is here
    upstream nacos-tcp{
      server 192.168.201.107:9848;
      server 192.168.201.107:9858;
    }
    #server  code is here....
    server {
      listen 8777;
      proxy_pass     nacos-tcp;
    }
}
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;
    #gzip  on;

    #loadBlance is here
    upstream nacos-http{
      server 192.168.201.107:8848;
      server 192.168.201.107:8858;
    }

    #server  code is here....
    server {
      keepalive_requests 120;
      listen 7777;
      location /{
         proxy_pass             http://nacos-http;
         proxy_redirect         off;
      }
    }

    include /etc/nginx/conf.d/*.conf;
}
相关推荐
先睡2 小时前
Redis的缓存击穿和缓存雪崩
redis·spring·缓存
Piper蛋窝6 小时前
深入 Go 语言垃圾回收:从原理到内建类型 Slice、Map 的陷阱以及为何需要 strings.Builder
后端·go
Bug退退退1237 小时前
RabbitMQ 高级特性之死信队列
java·分布式·spring·rabbitmq
六毛的毛8 小时前
Springboot开发常见注解一览
java·spring boot·后端
AntBlack8 小时前
拖了五个月 ,不当韭菜体验版算是正式发布了
前端·后端·python
31535669138 小时前
一个简单的脚本,让pdf开启夜间模式
前端·后端
uzong9 小时前
curl案例讲解
后端
一只叫煤球的猫9 小时前
真实事故复盘:Redis分布式锁居然失效了?公司十年老程序员踩的坑
java·redis·后端
大鸡腿同学10 小时前
身弱武修法:玄之又玄,奇妙之门
后端
轻语呢喃12 小时前
JavaScript :字符串模板——优雅编程的基石
前端·javascript·后端