首页 > 图灵资讯 > 技术篇>正文

springboot集成nacos

2023-08-21 17:17:37

1. 安装nacos

以widows为例的nacos安装包image.png解压后进入nacos\conf目录,修改 application.properties 配置

# 默认识别插件用于生成用户登录临时accestoken使用的密钥,使用默认值有安全风险 (2.2.0.1后无默认值)nacos.core.auth.plugin.nacos.token.secret.key=NDU2Nzg=VGhpc0lzTXLDDXN0b21TZWNyZXRLZXKW=注意:2.2.0.1版本后,社区发布版本需要自己填充nacoss.core.auth.plugin.nacos.token.secret.key值,否则节点无法启动

配置参考:权利鉴定image.png

修改启动模式:配置完成后进入 nacos\bin 目录image.png

双击 startup.cmd 启动nacosimage.png启动成功

2. 新建工程 nacos-demo

引入依赖

<!-- nacos服务注册及发现 --><dependency>    <groupId>com.alibaba.cloud</groupId>    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency>

完整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 http://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupId>open.source.test</groupId>    <artifactId>nacos-discovery-test</artifactId>    <version>1.0-SNAPSHOT</version>    <name>nacos-discovery-test</name>    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>${spring.boot.version}</version>        <relativePath/>    </parent>    <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>        <java.version>1.8</java.version>    </properties>    <dependencyManagement>        <dependencies>            <dependency>                <groupId>org.springframework.cloud</groupId>                <artifactId>spring-cloud-dependencies</artifactId>                <version>${spring.cloud.version}</version>                <type>pom</type>                <scope>import</scope>            </dependency>            <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>    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-actuator</artifactId>        </dependency>        <dependency>            <groupId>com.alibaba.cloud</groupId>            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>        </plugins>    </build></project>

Spring Cloud Alibaba版本选择参考:版本说明

application配置.properties

# 端口server.port=18080# Spring服务名称.application.name=mall-user# nacos注册中心地址spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848# nacos注册中心地址spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

@EnableDiscoveryClient注释添加到启动服务启动类中,可以省略此注释

启动后登录 http://localhost:8848/nacos 检查服务是否成功注册image.png

上一篇 ubuntu java11 环境变量
下一篇 (一)Dubbo源码解析:增强SPI

文章素材均来源于网络,如有侵权,请联系管理员删除。