当前位置: 首页>后端>正文

NACOS 练习笔记

0.下载并解压:

         wget https://github.com/alibaba/nacos/releases/download/2.0.4/nacos-server-2.0.4.tar.gz
         tar -xzvf nacos-server-2.0.4.tar.gz
         cd ./nacos 

1.建库建用户,分配权限,导入表

    CREATE USER nacos@%  IDENTIFIED BY 'pwd123';
    grant all privilegs on nacos.* to nacos ;
    修改密码:alter user "nacos"@"%" identified by "xeeeeexqqq";
    flush PRIVILEGES;

2. 改配置文件 application.properties ,开启数据库相关配置

#*************** 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=nacos
db.password.0=xxxxx

3. 启动 (单机)

     sh startup.sh -m standalone

4. 访问地址: http://Ip:8848/nacos

NACOS 练习笔记,第1张

5. 配置nacos相关

spring:
  application:
    name: nacos-client-a
  cloud:
    nacos:
      username: nacos
      password: nacos
      discovery:
        server-addr: 192.168.1.155:8848


server:
  port: 8080

问题1:nacos-client-a 用openfeign调用 nacos-client-b

  用feign调用的时候,开始没加loadbalancer依赖,报错
  
  Did you forget to include spring-cloud-starter-loadbalancer

解决方案: 调用方加上如下依赖

  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-loadbalancer</artifactId>
    
</dependency>

问题2: nacos集成gateway 报错 503

错误信息:
{
    "timestamp": "2023-04-10T06:43:23.788+00:00",
    "path": "/nacos-client-a/info",
    "status": 503,
    "error": "Service Unavailable",
    "requestId": "1397b4f7-1"
}

解决方案: 加spring-cloud-starter-loadbalancer依赖



netstat -ano | findstr 8849

问题3.nacosConfig 集成启动报错 No spring.config.import set

解决方案:
添加依赖:
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-bootstrap</artifactId>
        <version>3.0.4</version>
    </dependency>

bootstrap.yml

server:
  port: 81

spring:
  application:
    name: nacos-config-a

  cloud:
    nacos:
      config:
        prefix: nacos-config-a
        server-addr: 192.168.1.155:8848
        username: nacos
        password: nacos
        file-extension: yml
  profiles:
    active: dev

DataId命名规范

Nacos Spring Cloud 快速开始

dataId:  ${prefix}-${spring.profiles.active}.${file-extension}

依赖版本

版本说明 · alibaba/spring-cloud-alibaba Wiki · GitHub

 <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.6.11</spring-boot.version>
        <spring-cloud-alibaba.version>2021.0.4.0</spring-cloud-alibaba.version>
        <spring-cloud.version>2021.0.4</spring-cloud.version>
    </properties>

https://www.xamrdz.com/backend/3l51938095.html

相关文章: