typesafe的配置库工具类Config试用

  1. 云栖社区>
  2. 博客>
  3. 正文

typesafe的配置库工具类Config试用

技术小牛人 2017-11-22 10:31:00 浏览1276
展开阅读全文
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import com.typesafe.config.ConfigValue;
import org.junit.Test;
 
import java.util.Map;
 
/**
 * @author by lei zhou on 2017/11/10 15:10.
 */
public class ConfigTest {
    @Test
    public void test() {
 
        Config conf = ConfigFactory.load();
 
        for (Config config : conf.getConfigList("battleserver")) {
            System.out.println(config.getInt("id"));
            System.out.println(config.getString("ip"));
            System.out.println(config.getInt("port"));
        }
 
        System.out.println(conf.getString("login.db.driver"));
        System.out.println(conf.getString("login.db.url"));
        System.out.println(conf.getString("login.db.activeConn"));
 
        for (Map.Entry<String, ConfigValue> entry : conf.getConfig("res.android").entrySet()) {
            System.out.println(entry.getKey() + " " + entry.getValue().valueType() + " " + entry.getValue().unwrapped());
 
        }
 
        for (Map.Entry<String, ConfigValue> entry : conf.getConfig("whitelist.member").entrySet()) {
            System.out.println(entry.getKey() + " " + entry.getValue().valueType() + " " + entry.getValue().unwrapped());
 
        }
 
        System.out.println(conf.getString("company_ip"));
 
        System.out.println(conf.getString("lobbyserver.ip"));
        System.out.println(conf.getString("lobbyserver.port"));
        System.out.println(conf.getString("lobbyserver.rpc"));
 
    }
}


Maven依赖:

1
2
3
4
5
6
7
8
<dependencies>
        <!-- https://mvnrepository.com/artifact/com.typesafe/config -->
        <dependency>
            <groupId>com.typesafe</groupId>
            <artifactId>config</artifactId>
            <version>1.3.2</version>
        </dependency>
    </dependencies>


配置文件:

application.conf

1
2
3
4
5
6
7
globalIP=127.0.0.1
 
include "logindb"
 
include "battleserver"
 
include "common"

logindb.conf

1
2
3
4
5
6
# 账号服数据库
login.db.driver= com.mysql.jdbc.Driver
login.db.url= "jdbc:mysql://"${globalIP}":3306/test?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8"
login.db.username= root
login.db.password= test
login.db.activeConn= 100

battleserver.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 战斗服信息
battleserver=
[
    {
         id = 1
         ip = ${globalIP}
         port = 9010
         rpc = 9011
    }
 
    {
         id = 2
         ip = ${globalIP}
         port = 9010
         rpc = 9011
    }
 
    {
         id = 3
         ip = ${globalIP}
         port = 9010
         rpc = 9011
    }
]

common.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
company_ip=    ${globalIP}
 
lan_res_url= "http://"${globalIP}":80/Resource/AssetBundle/"
wan_res_url= "http://"${globalIP}":18649/Resource/AssetBundle/"
 
lobbyserver= {
    ip= ${globalIP}
    port= 9900
    rpc= 9901
}
 
res {
    android {
        12250: 750
        13363: 906
        14774: 961
        14820: 961
        14824: 961
    }
 
    ios {
        12250: 719
        12275: 727
        13363: 876
        14774: 930
        14820: 930
        14824: 930
    }
 
    pc {
        111: 1120
    }
}
 
whitelist= {
 
    active= true
 
    member{
        test1: A40FE952-6DFB-489D-B5F3-846D7B570754
        test2: 04ed7c30a5c5686a2d7c3ec67a34f46c
        test3: 118FC61E-18FC-4DCA-B28E-A9AAF44430DA
    }
}


输出结果
:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
1
127.0.0.1
9010
2
127.0.0.1
9010
3
127.0.0.1
9010
com.mysql.jdbc.Driver
jdbc:mysql://127.0.0.1:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8
100
14820 NUMBER 961
14774 NUMBER 961
12250 NUMBER 750
14824 NUMBER 961
13363 NUMBER 906
test2 STRING 04ed7c30a5c5686a2d7c3ec67a34f46c
test3 STRING 118FC61E-18FC-4DCA-B28E-A9AAF44430DA
test1 STRING A40FE952-6DFB-489D-B5F3-846D7B570754
127.0.0.1
127.0.0.1
9900
9901
本文转自   zl1030   51CTO博客,原文链接:http://blog.51cto.com/zl1030/1980846

网友评论

登录后评论
0/500
评论
技术小牛人
+ 关注