Springboot: 如何将服务注册到Nacos

Nacos

Nacos致力于帮助您发现、配置和管理微服务。Nacos提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据及流量管理。
下面是将Springboot服务注册到nacos的配置过程:

增加依赖


    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>Greenwich.SR2version>
                <type>pomtype>
                <scope>importscope>
            dependency>
            <dependency>
                <groupId>com.alibaba.cloudgroupId>
                <artifactId>spring-cloud-alibaba-dependenciesartifactId>
                <version>2.1.0.RELEASEversion>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>
    
        <dependency>
            <groupId>com.alibaba.bootgroupId>
            <artifactId>nacos-config-spring-boot-starterartifactId>
            <version>0.2.4version>
            <exclusions>
                <exclusion>
                    <groupId>com.alibabagroupId>
                    <artifactId>fastjsonartifactId>
                exclusion>
            exclusions>
        dependency>
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
            <exclusions>
                <exclusion>
                    <groupId>com.google.guavagroupId>
                    <artifactId>guavaartifactId>
                exclusion>
                <exclusion>
                    <groupId>com.alibabagroupId>
                    <artifactId>fastjsonartifactId>
                exclusion>
            exclusions>
        dependency>

增加配置

在application.properties中增加如下配置

  spring:    
         cloud:
          nacos:
              discovery:
                server-addr: ${NACOS_SERVER:nacos-center.v-base:30848}
                namespace: a85a3xxx-5bec-478c-a60f-0b11f10bxxxx
        
        spring:
            application:
                name: xxx-console

环境配置

有两种方式:

  • 配置NACOS_SERVER环境变量
  • 将nacos-center.v-base增加到系统的/etc/hosts文件中

参考

https://nacos.io/zh-cn/
https://nacos.io/zh-cn/docs/quick-start-spring-boot.html

你可能感兴趣的:(微服务架构,springboot,服务注册,nacos)