ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

nacos使用说明

2021-10-18 11:31:47  阅读:338  来源: 互联网

标签:8848 nacos 192.168 server 说明 eyecool 使用 ###


nacos使用说明

1、安装/部署

官网下载相应的安装包:https://nacos.io/zh-cn/index.html,例如nacos-server-2.0.2.tar.gz

上传服务器,解压:

eyecool@eyecool-OptiPlex-7060:~/nacos/nacos$ ls -l
总用量 44
drwxrwxr-x 4 eyecool eyecool  4096 7月  14 14:29 bin
drwxr-xr-x 2 eyecool eyecool  4096 7月  14 14:20 conf
drwxrwxr-x 7 eyecool eyecool  4096 7月  14 14:29 data
-rw-r--r-- 1 eyecool eyecool 16583 3月  18 11:36 LICENSE
drwxrwxr-x 2 eyecool eyecool  4096 7月  15 10:47 logs
-rw-r--r-- 1 eyecool eyecool  1305 5月  14  2020 NOTICE
drwxrwxr-x 2 eyecool eyecool  4096 7月  14 14:19 target

nacos是支持mysql数据库的,在conf文件夹下有nacos-mysql.sql 数据库脚本,创建对应的数据库并执行该脚本。

启动:

进入conf文件夹,修改配置文件application.properties,例如端口、数据库配置等

#*************** 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.0=root
db.password.0=eyecool

其中数据库配置,可以配置多个,如果配置多个事,需要将db.num属性打开,例如:

#*************** 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=2

### 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.0=root
db.password.0=eyecool

db.url.1=jdbc:mysql://127.0.0.1:3307/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
db.user.1=root
db.password.1=eyecool

启动

nacos启动分为两种,一种是单机启动,另一种是集群启动,集群在后面会有介绍。

单机启动时,进入bin目录,然后执行命令,在ubuntu下命令是:

bash startup.sh -m standalone

其他linux系统,执行命令为:

sh startup.sh -m standalone

windows系统下,执行命令为:

startup.cmd -m standalone

访问

浏览器中访问:http://192.168.61.95:8848/nacos , 并使用nacos/nacos登录系统,进行相关配置操作。

2、集群部署

机器1:192.168.61.95

机器2:192.168.63.59

分别在两台机器上,修改cluster.conf,添加对应的ip和端口

192.168.61.95:8848
192.168.63.59:8848

分别启动,在bin目录下执行命令,默认以集群方式启动

bash ./startup.sh

某台机器的启动日志:

2021-07-15 17:18:20,739 INFO Started Nacos in 19.706 seconds (JVM running for 20.965)

2021-07-15 17:18:20,739 INFO Nacos started successfully in cluster mode. use external storage

2021-07-15 17:18:20,949 INFO Initializing Spring DispatcherServlet 'dispatcherServlet'

2021-07-15 17:18:20,949 INFO Initializing Servlet 'dispatcherServlet'

2021-07-15 17:18:20,975 INFO Completed initialization in 26 ms

2021-07-15 17:18:21,163 INFO [Cluster-192.168.63.59:8848]RpcClient init label, labels={source=cluster}

2021-07-15 17:18:21,165 INFO [Cluster-192.168.63.59:8848]RpcClient init, ServerListFactory =com.alibaba.nacos.core.cluster.remote.ClusterRpcClientProxy$1

2021-07-15 17:18:21,171 INFO [Cluster-192.168.63.59:8848] Try to connect to server on start up, server: {serverIp='192.168.63.59', server main port=8848}

2021-07-15 17:18:21,491 INFO [Cluster-192.168.63.59:8848] Success to connect to server [192.168.63.59:8848] on start up,connectionId=1626340701266_192.168.61.95_49626

2021-07-15 17:18:21,495 INFO [Cluster-192.168.63.59:8848]Register server push request handler:com.alibaba.nacos.common.remote.client.RpcClient$ConnectResetRequestHandler

2021-07-15 17:18:21,496 INFO [Cluster-192.168.63.59:8848]Register server push request handler:com.alibaba.nacos.common.remote.client.RpcClient$4

springboot项目,服务注册时,可以配置多个nacos服务,以逗号隔开,比如:

spring:    application:        name: nacos-user    cloud:        nacos:            discovery:                server-addr: 192.168.63.59:8848,192.168.61.95:8848     

注意

在局域网中,部署nacos集群,有时在服务注册的时候,会报超时错误。

***************************APPLICATION FAILED TO START***************************Description:server error: such as timeout. failed to req API:/nacos/v1/ns/instance after all servers([192.168.63.59:8848, 192.168.61.95:8848]) tried: ErrCode:400, ErrMsg:<html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Fri Jul 16 09:11:50 CST 2021</div><div>There was an unexpected error (type=Bad Request, status=400).</div><div>receive invalid redirect request from peer 192.168.61.95</div></body></html>Action:please check server status

nacos注册中心在启动时会通过NetUtils类中InetAddress.getLocalHost()去获取本机的IP地址,但有时候获取的不是本机地址,导致注册中心加载失败。在大多数的linux系统中,都是根据hosts来查找主机名。可以修改hosts文件,添加本机ip和名称的映射。也可以nacos的application.properties文件中,将nacos.inetutils.ip-address 属性放开,并设置本机ip。

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

3、服务注册

以springboot项目为例,如果想让服务注册到nacos上,该做哪些操作?

1、引入jar包,注意版本,0.2.7对应的是nacos服务的2.x版本

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

2、在application类中,增加注解@EnableDiscoveryClient

@SpringBootApplication@EnableDiscoveryClient@NacosPropertySource(dataId = "database-test")@EnableFeignClientspublic class SpringBootApplicationOrder {    public static void main(String[] agrs) {        SpringApplication.run(SpringBootApplicationOrder.class, agrs);    }}

3、在springboot工程的yml文件中,指定nacos服务,并且指定该服务的名称:spring.application.name

spring:    application:        name: order-service    cloud:        nacos:            discovery:                server-addr: 192.168.63.59:8848,192.168.61.95:8848  

启动springboot工程成功后,在nacos管理页面中,可以找到对应的服务:

在这里插入图片描述

4、服务调用

nacos支持的服务调用有多种,比如TestTemplate、WebClient、Feign等。此处使用feign为例进行说明。

1、添加依赖

<dependency>			<groupId>org.springframework.cloud</groupId>			<artifactId>spring-cloud-starter-openfeign</artifactId>			<version>2.2.5.RELEASE</version></dependency>

注意:

注意版本的选择,版本太高的话,会报缺少loadbalance错误。因为高版本将loadbalance从该jar包中剔除了。

2、application类中添加注解@EnableFeignClients

@SpringBootApplication@EnableDiscoveryClient@NacosPropertySource(dataId = "database-test")@EnableFeignClientspublic class SpringBootApplicationOrder {    public static void main(String[] agrs) {        SpringApplication.run(SpringBootApplicationOrder.class, agrs);    }}

3、服务调用

package nacos.demo.mynacos.service;import org.springframework.cloud.openfeign.FeignClient;import org.springframework.web.bind.annotation.GetMapping;@FeignClient(name = "user-service")public interface OrderService {    @GetMapping("/userservice/user/get")    String getUser(Integer id);}

其中,使用FeignClient注解,指定需要调用的服务名称,在我这如果使用value=不起作用,需要使用name=才可以访问。GetMapping注解里的内容,对应着需要访问服务的地址。/userservice/user/get,其中userservice是user-service应用的根路径。

下面是user-service的接口代码:

package nacos.demo.mynacos.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.bind.annotation.RestController;import nacos.demo.mynacos.model.User;import nacos.demo.mynacos.service.UserService;@Controller@RequestMapping("user")public class UserController {    private final UserService userService;    @Autowired    public UserController(UserService userService) {        this.userService = userService;    }    @RequestMapping(value = "/get", method = RequestMethod.POST)    @ResponseBody    public String get(@RequestBody Integer id) {        User user =  userService.findById(id);        if(user!=null) {            return user.toString();        }        return "null";    }}

被调用服务的方法中,需要@RequestBody,否则会报错,不能是@RequestParam。

4、调用结果,访问order-service的order的get方法返回结果。

在这里插入图片描述

5、服务负载均衡

如果使用feign这种方式,在低版本时,是自带负载。复制user-service项目,并启动,此时nacos上应该有两个user-service服务,如下图

在这里插入图片描述

看下详细情况:

在这里插入图片描述

此时再次访问order-service的order的get方法返回结果:

在这里插入图片描述

再次访问一次:

在这里插入图片描述

返回结果变了,说明feign自带负载。

如果想修改负载的算法,请在order-service的application.yml中新增:

#配置ribbon,被调用者的服务名称user-service:  ribbon: #    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule #配置规则 随机#    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule #配置规则 轮询#    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RetryRule #配置规则 重试#    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.WeightedResponseTimeRule #配置规则 响应时间权重    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.BestAvailableRule #配置规则 最空闲连接策略    ConnectTimeout: 500 #请求连接超时时间    ReadTimeout: 1000 #请求处理的超时时间    OkToRetryOnAllOperations: true #对所有请求都进行重试    MaxAutoRetriesNextServer: 2 #切换实例的重试次数    MaxAutoRetries: 1 #对当前实例的重试次数

标签:8848,nacos,192.168,server,说明,eyecool,使用,###
来源: https://blog.csdn.net/sanshizhang/article/details/120823101

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有