zoukankan      html  css  js  c++  java
  • springboot之nacos的配置和服务发现

    一,我们需要引用的包(nacos需对应springboot的2.3.6.RELEASE这个版本

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            <version>2.2.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.2.3.RELEASE</version>
        </dependency>

    二,启动类的配置

    package com.nl;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    
    @SpringBootApplication
    @EnableDiscoveryClient
    public class NacosApplication {
        public static void main(String[] args) {
            System.out.println("test");
            SpringApplication.run(NacosApplication.class, args);
        }
    }

    三,配置文件(需注意的点,配置必须在bootstrap.yml这里添加)

    spring:
      profiles:
        active: dev
      application:
        name: test
      cloud:
        nacos:
          config:
            namespace: dev
            file-extension: yaml
            server-addr: https://nacos.test.com
            username: nacos_devtest
            password: 123456
          discovery:
            namespace: dev
            server-addr: https://nacos.test.com
            username: nacos_devtest
            password: 123456

    四,测试

    import com.nl.NacosApplication;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = NacosApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    public class NaocsTest {
        @Value("${test.name}")
        private String Name;
    
        @Test
        public void NacosTest() {
            System.out.println("Test" + Name);
        }
    }
  • 相关阅读:
    eclipse中集成python开发环境
    取消eclipse英文单词拼写验证
    exe所在路径
    [转]关于Megatops BinCalc RPN计算器的说明
    WinDbg 蓝屏dump分析教程
    Delphi与Windows 7下的用户账户控制(UAC)机制 及 禁用兼容性助手
    【Delphi7】 解决“程序第一次可以正常编译,但再次编译的时候会报错,必须重新打开Delphi”的问题
    解决xftp远程连接中文乱码
    创建用资源管理器打开FTP位置
    收藏夹里的js
  • 原文地址:https://www.cnblogs.com/May-day/p/14872098.html
Copyright © 2011-2022 走看看