1. 部署到远程服务器
-
官方文档
-
部署
-
Clone 项目
# 克隆项目 git clone https://github.com/nacos-group/nacos-docker.git
-
进入
nacos-docker/
# 进入 nacos-docker/ 文件夹 cd nacos-docker/
-
单机模式
Derby
# 后台运行启动 nacos Derby单机模式 docker-compose -f example/standalone-derby.yaml up -d
-
查看容器是否正常启动
# 查看容器 docker ps -a
-
登录
nacos
-
浏览器输入地址:ip:8848/nacos/
-
初始
账号/密码
:nacos
-
-
可在
权限控制
→用户列表
修改密码、创建用户等
-
2. 使用
-
依赖
-
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.sheng.cloud</groupId> <artifactId>nacos-demo</artifactId> <version>1.0.0-SNAPSHOT</version> <name>nacos-demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version> </properties> <dependencies> <!-- web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- nacos 注册中心 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!-- 测试 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${spring-cloud-alibaba.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
-
说明
-
主要添加
nacos
的starter
依赖<dependencies> <!-- nacos --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> </dependencies>
-
spring-cloud-alibaba-nacos-discoery
的父依赖<properties> <spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version> </properties> <dependencyManagement> <dependencies> <!-- spring-cloud-alibaba 父依赖 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${spring-cloud-alibaba.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
-
-
-
配置文件及启动类
-
application.yml
主配置文件server: port: 8080 spring: application: # 注册到服务中心的服务名 name: nacos-demo cloud: nacos: discovery: # 必须配置,nacos安装的服务器的ip地址 server-addr: 120.25.207.44:8848 cluster-name: ${spring.application.name}
-
nacos安装的服务器的
ip
地址如果有多个,用,
隔开,例如spring: cloud: nacos: discovery: server-addr: 120.25.207.44:8848,120.25.207.44:8849,120.25.207.44:8850
-
对于
server-addr:
,如果报以下错误(nacos地址拼写错误
),ip:端口
加上双引号
即可解决,或者写上完整路径http://120.25.207.44:8848
。解决之后再去掉双引号
或者改回原来的ip:端口
即可Caused by: com.alibaba.nacos.api.exception.NacosException: failed to req API:/api//nacos/v1/ns/instance after all servers...
-
-
启动类用
@EnableDiscoveryClient
注解标记import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient @SpringBootApplication public class NacosDemoApplication { public static void main(String[] args) { SpringApplication.run(NacosDemoApplication.class, args); } }
-
-
测试
-
书写测试
controller
@RestController @RequestMapping("/hello") public class HelloNacosController { @GetMapping("/hello") public String hello() { return "Hello nacos!"; } }
-
目录结构
-
启动并在
nacos
网站查看是否注册成功-
服务管理
→服务列表
-
服务名
是程序在主配置文件application.yml
中设置的spring.application.name
spring: application: name:
-
-