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);
        }
    }
  • 相关阅读:
    rac启动维护笔记
    cache-fusion笔记
    RAC配置笔记
    记一次异机rman还原后的操作
    索引小结
    DBlink的创建与删除
    小说经典语录
    SQL通配符
    ArrayList集合详解
    Oracle数据库四种数据完整性约束
  • 原文地址:https://www.cnblogs.com/May-day/p/14872098.html
Copyright © 2011-2022 走看看