java_配置使用nacos完整示例
参考:49 尚上优选项目-平台管理端-整合ES+MQ实现商品上下架-流程分析_哔哩哔哩_bilibili
1. 下载安装nacos
https://github.com/alibaba/nacos/releases
2. 解压,运行
PS D:\path/to\nacos-server-2.4.3\nacos\bin> .\startup.cmd -m standalone
3. pom中引用:
<!--服务注册 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--服务调用feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
4. yml中配置:
application.yml:
spring:
application:
name: service-sys
profiles:
active: dev
cloud:
nacos:
discovery:
server-addr: localhost:8848 # Nacos地址
5. main启动程序上加注释:
@EnableDiscoveryClient // 启用Nacos服务发现功能class, args
package com.atguigu.ssyx;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient // 启用Nacos服务发现功能class, args
public class ServiceSysApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceSysApplication.class, args);
}
}
6. 启动服务,看是否服务已经注册
启动服务:
本地查看:http://localhost:8848/nacos/ 中: