一、Nacos 安装
由于我们SpringCloud Alibaba 选择的版本是 2.2.6.RELEASE ,所以对应Nacos 版本就暂定 1.4.2
Docker 安装 Nacos 步骤(记得指定版本):传送门
二、注册中心-客户端搭建
2.1、打开idea,在父工程下创建模块
2.2、选择 Spring Initializr
2.3、修改Artifact,选择 jdk 版本
2.4、不用勾选直接下一步
2.5、无需修改项目地址
2.6、删除多余目录和文件
2.7、pom 文件
<?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>
<groupId>com.wangjing</groupId>
<artifactId>service-nacos</artifactId>
<version>1.0-SNAPSHOT</version>
<name>service-nacos</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.wangjing</groupId>
<artifactId>spring-cloud-alibaba</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
<!-- springweb 启动依赖 -->
<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>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>cn.piesat.wallet.WalletApplication</mainClass>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
2.8、修改 application.yml 配置
# 应用服务 WEB 访问端口
server:
port: 8801
spring:
application:
name: service-nacos # 应用名称
cloud:
# Nacos帮助文档: https://nacos.io/zh-cn/docs/concepts.html
# Nacos认证信息
nacos:
discovery:
username: nacos
password: nacos
# Nacos 服务发现与注册配置,其中子属性 server-addr 指定 Nacos 服务器主机和端口
server-addr: **.**.**.**:8848
namespace: public # 注册到 nacos 的指定 namespace,默认为 public
2.9、启动nacos-service,然后去Nacos 查看是否注册成功
3.0、添加TestController
package com.wangjing.servicenacos.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/test")
public class TestController {
@RequestMapping("/test")
public String test() {
return "测试service-nacos,端口:8801";
}
}
3.1、浏览器访问测试:
http://localhost:8801/test/test
注:以上内容仅提供参考和交流,请勿用于商业用途,如有侵权联系本人删除!