GithubHelp home page GithubHelp logo

chaomc / nacos-config-core Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mx-go/nacos-config-core

0.0 0.0 0.0 76 KB

基于springcloud的spring-cloud-nacos配置中心加强版,支持更多场景

License: Apache License 2.0

Java 100.00%

nacos-config-core's Introduction

alibaba-nacos配置中心加强

优化

  1. 支持动态加载static的属性配置
  2. 自动化配置NacosserverAddrgroupId
  3. 动态更新配置,不需要写繁琐的nacos配置监听
  4. 容器启动时自动从配置中心拉取相关配置
  5. 动态获取不同类型数据,避免在代码中做数据类型转换,并可设置默认值

引入Maven坐标

<dependency>
    <groupId>com.github.mx-go</groupId>
    <artifactId>nacos-config-core</artifactId>
    <version>${latest-version}</version>
</dependency>

使用方式(例)

@RefreshConfig
public class ConfigCenter {

    public static String userName;
    public static int userAge;
    public static String configName;
    public static double configAge;

    public static String content = "content";

    @PostConstruct
    public void init() {
        // 使用方式1
        ConfigFactory.getInstance().addListener("user.properties", iConfig -> {
            userName = iConfig.get("user.name");
            userAge = iConfig.getInt("user.age");
            // 设置默认值
            content = iConfig.get("user.content", content);
            System.out.println(iConfig.getString());
        });

        // 使用方式2
        ConfigFactory.getInstance().addListener("config.properties", "groupId", iConfig -> {
            configName = iConfig.get("config.name");
            // 设置默认值
            configAge = iConfig.getDouble("config.age", 18d);
        });

        // 方式3
        ConfigFactory.getInstance().addListener("route.json", this::updateConfig);
        }
                 
        private void updateConfig(String configInfo) {
            // 更新
        }
    }

建议将所有配置项放在一个类中,如例子中ConfigCenter,方便统一管理和配置。

注意

  • 配置的类需要添加@RefreshConfig注解
  • 由于本组件基于Nacos做二次封装。故需要配置spring.cloud.nacos.server-addrspring.cloud.nacos.config.server-addrspring.cloud.nacos.discovery.server-addr
  • nacos中的groupId默认取spring.application.name

nacos-config-core's People

Contributors

mx-go avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.