自学内容网 自学内容网

Spring Cloud Alibaba 之 “Nacos配置中心”

创建一个SpringBoot项目,pom.xml文件如下: 4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.5.RELEASE com.coco article-center 0.0.1-SNAPSHOT article-center Demo project for Spring Boot

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-alibaba-nacos-discovery</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <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>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
          </plugin>
        </plugins>
    </build>
</project>

修改配置文件application.yml

server:
  port: 8081
spring:
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
  application:
    name: article-center  //这个配置一定要写,不然nacos控制台不会显示
    

在启动类上加上 @EnableDiscoveryClient 注解

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

@SpringBootApplication
@EnableDiscoveryClient
public class ArticleCenterApplication {

    public static void main(String[] args) {
        SpringApplication.run(ArticleCenterApplication.class, args);
    }

}

在本地启动nacos,我使用的是Nacos1.1.4版本的,cd /bin目录下 执行 sh startup.sh -m standalone

在浏览器上访问 localhost:8848/nacos就可以看到我们的服务注册上去了


原文地址:https://blog.csdn.net/mf97532/article/details/144249936

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!