Nacos是阿里巴巴开源的服务发现和配置中心的中间件,springboot集成nacos比较方便,只需要以下三步:
1、依赖配置(pom文件)
<properties>
<spring-cloud.version>Hoxton.SR1</spring-cloud.version>
<alibaba.version>0.9.0.RELEASE</alibaba.version>
<nacos-config-spring-boot.version>0.2.1</nacos-config-spring-boot.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.9.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- nacos 注册中心依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<exclusions>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- nacos 配置中心依赖 -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-starter</artifactId>
<version>${nacos-config-spring-boot.version}</version>
<exclusions>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
</exclusions>
</dependency>
2、在bootstrap.yml文件声明服务发现的地址和配置中心的地址:
spring:
application:
name: gateway
cloud:
nacos:
config:
server-addr: 127.0.0.1:8848
discovery:
server-addr: 127.0.0.1:8848
3、在springboot 的入口类加上
@SpringBootApplication(scanBasePackages = {"com.test.a"})
//这里开启服务发现
@EnableDiscoveryClient
//@NacosPropertySource(dataId = "custom-routers", autoRefreshed = true)
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication .class, args);
}
}
4、启动服务在nacos的ui运维页面查看服务
如果看到自己声明的服务已经注册上了,即可。