springcloudAlibaba之nacos(2.X版本) 注册中心和配置中心使用实战流程

一、下载nacos服务端:

访问:https://nacos.io/  

进入下载源码和服务端程序的地方:

往下翻: 

点击:

往下看翻,找到服务 下载的地方,这是最新的  版本,后面还有其他版本:

下载后解压配置,连接mysql 执行初始化脚本:nacos\conf\nacos-mysql.sql  数据库脚本文件。

客户端执行:

生成表:

配置数据库信息  \nacos\conf\application.properties  目录 文件下添加:

spring.datasource.platform=mysql
db.num=1 
db.url.0=jdbc:mysql://10.130.16.246:3019/ZIPKIN?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true 
db.user.0=mysql
db.password.0=mysql

如图: 

 

也可,亲测OK:

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

#*************** Spring Boot Related Configurations ***************#
### Default web context path:
#server.servlet.contextPath=/nacos
### Default web server port:
#server.port=8848

#*************** Network Related Configurations ***************#
### If prefer hostname over ip for Nacos server addresses in cluster.conf:
# nacos.inetutils.prefer-hostname-over-ip=false

### Specify local server's IP:
nacos.inetutils.ip-address=


#*************** Config Module Related Configurations ***************#
### If use MySQL as datasource:
# spring.datasource.platform=mysql

### Count of DB:
# db.num=1

### Connect URL of DB:
# 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=nacos
# db.password=nacos


#*************** Naming Module Related Configurations ***************#
### Data dispatch task execution period in milliseconds:
# nacos.naming.distro.taskDispatchPeriod=200

### Data count of batch sync task:
# nacos.naming.distro.batchSyncKeyCount=1000

### Retry delay in milliseconds if sync task failed:
# nacos.naming.distro.syncRetryDelay=5000

### If enable data warmup. If set to false, the server would accept request without local data preparation:
# nacos.naming.data.warmup=true

### If enable the instance auto expiration, kind like of health check of instance:
# nacos.naming.expireInstance=true

nacos.naming.empty-service.auto-clean=true
nacos.naming.empty-service.clean.initial-delay-ms=50000
nacos.naming.empty-service.clean.period-time-ms=30000


#*************** CMDB Module Related Configurations ***************#
### The interval to dump external CMDB in seconds:
# nacos.cmdb.dumpTaskInterval=3600

### The interval of polling data change event in seconds:
# nacos.cmdb.eventTaskInterval=10

### The interval of loading labels in seconds:
# nacos.cmdb.labelTaskInterval=300

### If turn on data loading task:
# nacos.cmdb.loadDataAtStart=false


#*************** Metrics Related Configurations ***************#
### Metrics for prometheus
#management.endpoints.web.exposure.include=*

### Metrics for elastic search
management.metrics.export.elastic.enabled=false
#management.metrics.export.elastic.host=http://localhost:9200

### Metrics for influx
management.metrics.export.influx.enabled=false
#management.metrics.export.influx.db=springboot
#management.metrics.export.influx.uri=http://localhost:8086
#management.metrics.export.influx.auto-create-db=true
#management.metrics.export.influx.consistency=one
#management.metrics.export.influx.compressed=true


#*************** Access Log Related Configurations ***************#
### If turn on the access log:
server.tomcat.accesslog.enabled=true

### The access log pattern:
server.tomcat.accesslog.pattern=%h %l %u %t "%r" %s %b %D %{User-Agent}i %{Request-Source}i

### The directory of access log:
server.tomcat.basedir=


#*************** Access Control Related Configurations ***************#
### If enable spring security, this option is deprecated in 1.2.0:
#spring.security.enabled=false

### The ignore urls of auth, is deprecated in 1.2.0:
nacos.security.ignore.urls=/,/error,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/v1/auth/**,/v1/console/health/**,/actuator/**,/v1/console/server/**

### The auth system to use, currently only 'nacos' is supported:
nacos.core.auth.system.type=nacos

### If turn on auth system:
nacos.core.auth.enabled=false

### The token expiration in seconds:
nacos.core.auth.default.token.expire.seconds=18000

### The default token:
nacos.core.auth.default.token.secret.key=SecretKey012345678901234567890123456789012345678901234567890123456789

### Turn on/off caching of auth information. By turning on this switch, the update of auth information would have a 15 seconds delay.
nacos.core.auth.caching.enabled=true


#*************** Istio Related Configurations ***************#
### If turn on the MCP server:
nacos.istio.mcp.server.enabled=false



###*************** Add from 1.3.0 ***************###


#*************** Core Related Configurations ***************#

### set the WorkerID manually
# nacos.core.snowflake.worker-id=

### Member-MetaData
# nacos.core.member.meta.site=
# nacos.core.member.meta.adweight=
# nacos.core.member.meta.weight=

### MemberLookup
### Addressing pattern category, If set, the priority is highest
# nacos.core.member.lookup.type=[file,address-server]
## Set the cluster list with a configuration file or command-line argument
# nacos.member.list=192.168.16.101:8847?raft_port=8807,192.168.16.101?raft_port=8808,192.168.16.101:8849?raft_port=8809
## for AddressServerMemberLookup
# Maximum number of retries to query the address server upon initialization
# nacos.core.address-server.retry=5
## Server domain name address of [address-server] mode
# address.server.domain=jmenv.tbsite.net
## Server port of [address-server] mode
# address.server.port=8080
## Request address of [address-server] mode
# address.server.url=/nacos/serverlist

#*************** JRaft Related Configurations ***************#

### Sets the Raft cluster election timeout, default value is 5 second
# nacos.core.protocol.raft.data.election_timeout_ms=5000
### Sets the amount of time the Raft snapshot will execute periodically, default is 30 minute
# nacos.core.protocol.raft.data.snapshot_interval_secs=30
### raft internal worker threads
# nacos.core.protocol.raft.data.core_thread_num=8
### Number of threads required for raft business request processing
# nacos.core.protocol.raft.data.cli_service_thread_num=4
### raft linear read strategy. Safe linear reads are used by default, that is, the Leader tenure is confirmed by heartbeat
# nacos.core.protocol.raft.data.read_index_type=ReadOnlySafe
### rpc request timeout, default 5 seconds
# nacos.core.protocol.raft.data.rpc_request_timeout_ms=5000

spring.datasource.platform=mysql
#db.num=1 
#db.url.0=jdbc:mysql://10.130.16.2466:305199/ZIPKIN?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true 
#db.user.0=mysql
#db.password.0=mysql


server.contextPath=/nacos
server.servlet.contextPath=/nacos
server.port=8858


db.num=1
db.url.0=jdbc:mysql://10.130.16.2466:305199/ZIPKIN?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.user=mysql
db.password=mysql

启动nacos服务:

nacos\bin\startup.cmd    双击“startup.cmd”启动nocas服务

这种启动方式同样有弊端,在关闭命令窗口后nacos进程会直接shutdown,导致服务停止。
解决方法:
新建一个记事本,输入如下代码:

Set ws = CreateObject("Wscript.Shell")
ws.run "cmd /c startup.cmd",vhhide

把记录本文件命名:***.vbs 如 runbat.vbs,然后双击运行runbat.vbs文件

启动成功:

如果是1.4版本 还需要设置单机启动,配置文件 nacos\bin\startup.cmd 配置内容

set MODE="cluster" 改成如下图这样 set MODE="standalone":

如果不改,启动报错如下:

 ERROR Startup errors :

org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:156)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:544)
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:744)
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:391)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:312)

或者 用命令窗口,进入到安装路径的bin目录下,执行:

sh startup.sh -m standalone

访问如下地址:

http://172.21.12.11:8848/nacos/index.html

用户名和密码 均为:nacos ,s首次登陆页面:

 创建命名空间:

 成功生成:

配置列表里出现:

创建配置列表:这是配置中心  配置内容具体写:!!!! 

点击发布按钮后,返回后显示列表:

此时可以在对应的客户端项目中 配置:bootstrap.yml

spring:
  profiles:
    #Data Id 的后缀 即此处  nandao-demo-dev.yml的 dev(最后一个横杆和点之前的字符串)
    active: dev
  application:
    #微服务名称
    name: nandao-oms
  cloud:
    nacos:
      config:
        #自定义配置
        #nacos服务器的地址
        server-addr: 172.21.12.11:8848
        #nacos服务器的命名空间
        namespace: cee09d5e-e6ad-4c68-847c-733ed2246bbb
        #组别
        group: nandao
        #配置文件类型
        file-extension: yml
        #Data Id 的前缀 即此处  nandao-demo-dev.yml的前缀
        prefix: nandao-demo
      #注册中心配置
      discovery:
        server-addr: 172.21.12.11:8848
        namespace: cee09d5e-e6ad-4c68-847c-733ed2246bbb

或者在这里配置

服务列表看是否注册成功:

 并且可以创建多个命名空间:同时可以从一个 克隆的另一个:举例

 克隆成功:

导出功能: 

 这就是web端简单的使用

二、客户端流程:

1、jar引入:

<dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            <version>2.1.1.RELEASE</version>
        </dependency>

2、启动类加注解:@EnableDiscoveryClient

3、 配置文件创建(上面也有详细的介绍):bootstrap.yml

spring:
  profiles:
#    active: dev
    active: local
  application:
    name: admin-oms
  cloud:
    nacos:
      config:
        #自定义配置
#        server-addr: 10.130.64.189:8848
        server-addr: 10.130.64.189:8848
        namespace: bab5e416-0b7d-4cef-b421-d06b6d0f4c67
        group: nmp-oms
        file-extension: yml
        prefix: nmp-oms

      discovery:
        server-addr: 10.130.64.189:8848
        namespace: bab5e416-0b7d-4cef-b421-d06b6d0f4c67
#        server-addr: 10.130.64.189:8848
#        namespace: a1ed9d55-3917-46e4-93fd-30232ff50545

启动后:

springcloud相关版本对应介绍

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

寅灯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值