mirror of https://github.com/tikv/client-java.git
				
				
				
			Support load configuration from tikv.properties on classpath (#297)
Signed-off-by: marsishandsome <marsishandsome@gmail.com>
This commit is contained in:
		
							parent
							
								
									11bd22234d
								
							
						
					
					
						commit
						f9cb05784d
					
				
							
								
								
									
										15
									
								
								README.md
								
								
								
								
							
							
						
						
									
										15
									
								
								README.md
								
								
								
								
							|  | @ -82,21 +82,6 @@ public class Main { | |||
| 
 | ||||
| The following includes JVM related parameters. | ||||
| 
 | ||||
| #### tikv.configuration_file | ||||
| - tikv java client configuration file | ||||
| - default: null | ||||
| 
 | ||||
| This parameter can be passed by `-Dtikv.configuration_file=/path/to/tikv_client_config.properties`. | ||||
| 
 | ||||
| Here is an example of `tikv_client_config.properties`. | ||||
| 
 | ||||
| ``` | ||||
| tikv.grpc.timeout_in_ms=150ms  | ||||
| tikv.grpc.forward_timeout_in_ms=200ms | ||||
| tikv.metrics.enable=true | ||||
| tikv.metrics.port=3140 | ||||
| ``` | ||||
| 
 | ||||
| #### tikv.pd.addresses | ||||
| - pd addresses, separated by comma | ||||
| - default: 127.0.0.1:2379 | ||||
|  |  | |||
|  | @ -19,7 +19,7 @@ import org.tikv.common.util.BackOffer; | |||
| import org.tikv.kvproto.Kvrpcpb; | ||||
| 
 | ||||
| public class ConfigUtils { | ||||
|   public static final String TIKV_CONFIGURATION_FILE = "tikv.configuration_file"; | ||||
|   public static final String TIKV_CONFIGURATION_FILENAME = "tikv.properties"; | ||||
| 
 | ||||
|   public static final String TIKV_PD_ADDRESSES = "tikv.pd.addresses"; | ||||
|   public static final String TIKV_GRPC_TIMEOUT = "tikv.grpc.timeout_in_ms"; | ||||
|  |  | |||
|  | @ -18,8 +18,8 @@ package org.tikv.common; | |||
| import static org.tikv.common.ConfigUtils.*; | ||||
| 
 | ||||
| import io.grpc.Metadata; | ||||
| import java.io.FileInputStream; | ||||
| import java.io.IOException; | ||||
| import java.io.InputStream; | ||||
| import java.io.Serializable; | ||||
| import java.net.URI; | ||||
| import java.util.*; | ||||
|  | @ -57,20 +57,27 @@ public class TiConfiguration implements Serializable { | |||
|   } | ||||
| 
 | ||||
|   private static void loadFromConfigurationFile() { | ||||
|     Optional<String> file = getOption(TIKV_CONFIGURATION_FILE); | ||||
|     if (file.isPresent()) { | ||||
|     try (InputStream input = | ||||
|         TiConfiguration.class | ||||
|             .getClassLoader() | ||||
|             .getResourceAsStream(ConfigUtils.TIKV_CONFIGURATION_FILENAME)) { | ||||
|       Properties properties = new Properties(); | ||||
|       try { | ||||
|         properties.load(new FileInputStream(file.get())); | ||||
|       } catch (IOException e) { | ||||
|         logger.error("load config file error, path = " + file.get(), e); | ||||
| 
 | ||||
|       if (input == null) { | ||||
|         logger.warn("Unable to find " + ConfigUtils.TIKV_CONFIGURATION_FILENAME); | ||||
|         return; | ||||
|       } | ||||
| 
 | ||||
|       logger.info("loading " + ConfigUtils.TIKV_CONFIGURATION_FILENAME); | ||||
|       properties.load(input); | ||||
|       for (String key : properties.stringPropertyNames()) { | ||||
|         if (key.startsWith("tikv.")) { | ||||
|           String value = properties.getProperty(key); | ||||
|           setIfMissing(key, value); | ||||
|         } | ||||
|       } | ||||
|     } catch (IOException e) { | ||||
|       logger.error("load config file error", e); | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|  | @ -117,7 +124,7 @@ public class TiConfiguration implements Serializable { | |||
|   } | ||||
| 
 | ||||
|   public static void listAll() { | ||||
|     logger.warn("static configurations are:" + new ArrayList<>(settings.entrySet()).toString()); | ||||
|     logger.info("static configurations are:" + new ArrayList<>(settings.entrySet()).toString()); | ||||
|   } | ||||
| 
 | ||||
|   private static void set(String key, String value) { | ||||
|  |  | |||
|  | @ -17,12 +17,11 @@ package org.tikv.common; | |||
| 
 | ||||
| import static org.junit.Assert.assertEquals; | ||||
| 
 | ||||
| import org.junit.Ignore; | ||||
| import org.junit.Test; | ||||
| 
 | ||||
| public class TiConfigurationTest { | ||||
| 
 | ||||
|   // Set `-Dtikv.configuration_file=src/test/resources/tikv_client_config.properties` to test | ||||
|   @Ignore | ||||
|   @Test | ||||
|   public void configFileTest() { | ||||
|     TiConfiguration conf = TiConfiguration.createRawDefault(); | ||||
|     assertEquals("configFileTest", conf.getDBPrefix()); | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue