解决nacos问题的参考文章: https://www.jianshu.com/p/64e1f7d50c6e
https://github.com/alibaba/nacos/releases/tag/1.4.1
https://github.com/alibaba/spring-cloud-alibaba
https://github.com/alibaba/Nacos
https://developer.aliyun.com/mvn/guide
https://docs.spring.io/spring-cloud-gateway/docs/2.2.8.RELEASE/reference/html/
mvn clean install -U
nacos下载
git clone https://github.com/alibaba/nacos.git
启动服务器
Linux/Unix/Mac
启动命令(standalone代表着单机模式运行,非集群模式):
sh startup.sh -m standalone
如果使用的是ubuntu系统,或者运行脚本报错提示[[符号找不到,可尝试如下运行:
bash startup.sh -m standalone
Windows
startup.cmd -m standalone
cubemall-common
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.1.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!--服务注册/发现-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--配置中心-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
cubemall-product
application.yml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/cube_goods?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username: root
password: root
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
application:
name: cubemall-product
server:
port: 8081
mybatis-plus:
mapper-locations: classpath:/mapper/**/*.xml
global-config:
db-config:
id-type: auto
解决nacos问题
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/cube_goods?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username: root
password: root
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
ip: 127.0.0.1
application:
name: cubemall-product
server:
port: 8081
mybatis-plus:
mapper-locations: classpath:/mapper/**/*.xml
global-config:
db-config:
id-type: auto
logic-delete-value: 1 #逻辑删除值(默认为1)
logic-not-delete-value: 0 #逻辑未删除值(默认为0)
logging:
level:
com.xd.cubemall: debug
CubemallProductApplication.java
package com.xd.cubemall.product;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class CubemallProductApplication {
public static void main(String[] args) {
SpringApplication.run(CubemallProductApplication.class, args);
}
}
cubemall-gateway
Spring Cloud Routing->Gateway
spring.cloud.nacos.discovery.server-addr 来指定 Nacos 的服务发现地址。然而,对于配置中心,应该使用 spring.cloud.nacos.config.server-addr。这两个配置项的作用是不同的:
spring.cloud.nacos.discovery.server-addr 用于服务注册与发现。
spring.cloud.nacos.config.server-addr 用于配置中心,即从中获取应用的配置信息。
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
如果不解决这个问题,应用将无法从 Nacos 配置中心获取配置信息,这可能导致应用启动失败或运行时缺少必要的配置。
既然在 Nacos 的服务管理界面看到了 cubemall-gateway,这意味着服务注册部分是没有问题的,问题主要出在配置获取上。添加或修正上述配置项后,应用应该能够正确地从 Nacos 配置中心获取配置,并解决当前的启动错误