typesafe的配置库工具类Config试用

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,高可用系列 2核4GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介:
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
相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
9月前
|
算法 安全 Java
微服务(四)-config配置中心的配置加解密
微服务(四)-config配置中心的配置加解密
|
9月前
|
JavaScript
Vue3基础(19)___vite.config.js中配置路径别名
本文介绍了如何在Vue 3的Vite配置文件`vite.config.js`中设置路径别名,以及如何在页面中使用这些别名导入模块。
319 0
Vue3基础(19)___vite.config.js中配置路径别名
|
10月前
|
移动开发 JavaScript 前端开发
UniApp H5 跨域代理配置并使用(配置manifest.json、vue.config.js)
这篇文章介绍了在UniApp H5项目中处理跨域问题的两种方法:通过修改manifest.json文件配置h5设置,或在项目根目录创建vue.config.js文件进行代理配置,并提供了具体的配置代码示例。
UniApp H5 跨域代理配置并使用(配置manifest.json、vue.config.js)
|
10月前
|
JSON 前端开发 JavaScript
vue.config.js配置详解
【8月更文挑战第16天】vue.config.js配置详解
495 1
vue.config.js配置详解
|
8月前
|
JavaScript 前端开发 应用服务中间件
vue前端开发中,通过vue.config.js配置和nginx配置,实现多个入口文件的实现方法
vue前端开发中,通过vue.config.js配置和nginx配置,实现多个入口文件的实现方法
462 0
|
8月前
|
前端开发 JavaScript
vite vue3 config配置
【10月更文挑战第5天】
326 0
|
10月前
|
Web App开发 安全 JavaScript
【Azure 应用服务】App Service 通过配置web.config来添加请求返回的响应头(Response Header)
【Azure 应用服务】App Service 通过配置web.config来添加请求返回的响应头(Response Header)
153 0
|
10月前
|
缓存 NoSQL 网络协议
【Azure Redis 缓存】如何使得Azure Redis可以仅从内网访问? Config 及 Timeout参数配置
【Azure Redis 缓存】如何使得Azure Redis可以仅从内网访问? Config 及 Timeout参数配置
115 0
|
10月前
|
JavaScript Java Python
【Azure 应用服务】在Azure App Service for Windows 中部署Java/NodeJS/Python项目时,web.config的配置模板内容
【Azure 应用服务】在Azure App Service for Windows 中部署Java/NodeJS/Python项目时,web.config的配置模板内容
|
10月前
|
测试技术
Profile Config 多环境不同配置
Profile Config 多环境不同配置
63 0