springboot集成nacos 注册中心

 

  • 接上一篇集成配置中心,本文介绍注册中心,目录结构如下

  • 在nacosregister的pom.xml文件中添加引用
 <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.2.3.RELEASE</version>
        </dependency>
  • 在application.yml文件中,添加配置
server:
  port: 9090   #启动端口号  命令行注入
spring:
  application:
    name: mytask-register
  cloud:
    nacos: # nacos服务地址
      discovery:
        server-addr: 127.0.0.1:8848
  • 启动类,main函数如下
package org.howdy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * Hello world!
 *
 */
@SpringBootApplication
@EnableDiscoveryClient //可以省略,不添加
public class SpringbootNacosRegisterApplication
{
    public static void main( String[] args )
    {
        System.out.println( "Hello SpringbootNacosRegisterApplication!" );
        SpringApplication.run(SpringbootNacosRegisterApplication.class,args);
    }
}
  • 操作完成后,启动项目,在nacos服务管理界面,显示注册的服务

 

posted @ 2023-01-16 10:03  低调码农哥!  阅读(716)  评论(0编辑  收藏  举报