(一)SpringCloud-Alibaba-Nacos注册中心
使用Nacos作为注册中心用法
-
首先,修改 pom.xml 文件,引入 Nacos Discovery Starter。(服务注册/发现)
com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery -
在应用的 /src/main/resources/application.properties 配置文件中配置 Nacos Server 地址
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
- 使用 @EnableDiscoveryClient 注解开启服务注册与发现功能
@SpringBootApplication @EnableDiscoveryClient public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); } }
应用启动
-
增加配置,在 nacos-discovery-provider-example 项目的 /src/main/resources/application.properties 中添加基本配置信息
spring.application.name=service-provider server.port=18082
-
启动应用,支持 IDE 直接启动和编译打包后启动。
- IDE直接启动:找到 nacos-discovery-provider-example 项目的主类
ProviderApplication
,执行 main 方法启动应用。 - 打包编译后启动:在 nacos-discovery-provider-example 项目中执行
mvn clean package
将工程编译打包,然后执行java -jar nacos-discovery-provider-example.jar
启动应用。
- IDE直接启动:找到 nacos-discovery-provider-example 项目的主类
- 启动成功后浏览器访问:http://localhost:8848/nacos/ ;账号/密码:nacos/nacos