开源分布式配置组件conf使用教程

本文主要用于说明开源分布式配置组件conf的优势和用法。
相关文章:本地配置注解读取——开源组件Conf
conf项目地址:https://github.com/zsunny6658/conf

优势

  1. 该组件主要用于获取项目本地配置(后期还会对远程配置进行支持)。
  2. 支持配置文件格式类型有:properties、xml、yml(yaml)、json。
  3. 支持动态配置,无需重启应用即可生效配置。
  4. 支持对类配置的支持。
  5. 支持事前事后监听器。
  6. 支持自定义配置文件名。

用法

第一步

github上下载该项目的release或者将项目源码导入。

第二步

使用命令将conf加入本地仓库,此处以1.1版本为例,文件名为conf-1.1.jar,:

mvn install:install-file -Dfile=conf-1.1.jar -DgroupId=com.sunny -DartifactId=conf -Dversion=1.1 -Dpackaging=jar
第三步

在项目的pom.xml中加入依赖:

<dependency>
  <groupId>com.sunny</groupId>
  <artifactId>conf</artifactId>
  <version>1.1</version>
</dependency>
第四步

在项目中使用,具体使用方法可参考项目的readme。
本文仅作展示一些简单用法,因为使用的是conf1.1版本,所以还不支持@Dynamic注解和动态配置。
本文中使用两个类作为测试类,分别命名为Example、ExampleClass,前者用于测试普通配置项,后者用于测试类配置项。
Example的主要内容为:

@ConfSource("classpath: configer.properties")
public class Example {
    @ConfPath("other.file.configer")
    private static String other;
    @ConfPath("server.port")
    private static String port;
    @SystemConfPath("system.conf.active")
    private static String active;
    public static void printPort(){
        System.out.println("other:" + other);
        System.out.println("prop-port:" + port);
        System.out.println("prop-active:" + active);
    }
}

ExampleClass的主要内容为:

@ConfClass
@ConfClassPrefix("test.")
public class ExampleClass {
    private static String a;
    private static String b = "2";
    @ConfClassIgnore
    private static String c;
    @ConfClassDefault("ddddd")
    private static String d;
    @ConfClassAlias("d")
    private static String e;
    public static void print(){
        System.out.println("class-a:" + a);
        System.out.println("class-b:" + b);
        System.out.println("class-c:" + c);
        System.out.println("class-d:" + d);
        System.out.println("class-e:" + e);
    }
}

测试内容非常简单,主要是声明了一些静态变量,和一个print方法用于打印类中变量的值。

第五步

添加配置文件,为了体现对各种配置文件的支持,使用五个配置文件,application.properties、application.xml、application.yml、application-prod.xml和configer.properties。前三者为系统默认配置支持;第四个为系统active配置,需要在配置中指明;第五个为自定义配置文件。在application.properties、application.xml和application-prod.xml中分别设置server.port配置。
application.properties的配置内容为:

server.port=222

application.xml的配置内容为:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
    <comment>系统配置</comment>
    <entry key="server.port"><![CDATA[123456]]></entry>
</properties>

application.yml的配置内容为:

server:
  port: 111
system:
  conf:
    active: prod
test:
  a: 1
  b: 2
  c: 3
  d: 5

application-prod.xml的配置内容为:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
    <comment>系统配置</comment>
    <entry key="server.port"><![CDATA[789]]></entry>
</properties>

configer.properties的配置内容为:

other.file.configer=this is other file configer

其中,各配置文件的默认优先级为:
active >> customize = xml > yml > yaml > properties
此处为:
application-prod.xml > configer.properties = application.xml > application.yml > application.properties

第六步

运行测试,使用测试类Test,其主要内容为:

public class Test {
    public static void main(String[] args) {
        ConfStarter.start();
        Example.printPort();
        ExampleClass.print();
    }
}

最终得到运行结果为:

other:this is other file configer
prop-port:789
prop-active:prod
class-a:1
class-b:2
class-c:null
class-d:ddddd
class-e:5

联系与交流

欢迎小伙伴与我讨论哦~

邮箱:zsunny@yeah.net

本文欢迎转载,请注明本文地址:https://blog.csdn.net/m0_37595562/article/details/89763667

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值