Learn how to use Java to modify DNS resolution records and use the properties file instead of the hosts file.
In different run environments access different will the data source server, in order to facilitate switching whether to use domain name instead of IP?
If you use a domain name, you need to configure the hosts file in the development environment
Does the other team member checkout code need documentation to tell him how to configure the Hosts file?
What about the projects you are responsible for? Would you like to use software such as Win Hosts Manager in order to easily modify the hots file?
Do you hate writing documents? Want the other members of the team to run the program directly without modifying the HOSTS file checkout code?
Javahost (JVM virtual DNS) to help you solve these problems, let's take a look at how to use Javahost to omit the Hosts file configuration.
1. Configure MAVEN Dependencies
Join in Pom.xml
<dependencies> [...] <dependency> <groupId>io.leopard</groupId> <artifactid>javahost</artifactid > <version>0.0.5-SNAPSHOT</version> </dependency> [...] </dependencies><repositories> <repository> <id>leopard-snapshots</id> <name>leopard snapshots</name> <url>http://leopard.io/nexus/content/repositories/ Snapshots/</url> </repository></repositories>
If you are a non-MAVEN user, you can download the jar package from the following link.
Io.leopard:javahost:0.0.5-snapshot
2, Write Vdns.properties
src/main/resources/vdns.properties
#MySQL User Databaseuser.mysql.guides.leopard.io=127.0.0.1#redis User Database user.redis.guides.leopard.io= 127.0.0.2#redis Session Database
You can put these domain name resolution configuration and other placeholders in the same file, value is legitimate IP will set the virtual DNS
However, we recommend a separate file configuration, which will be clearer
3. Set up virtual DNS
src/test/java/io/leopard/guides/JettyTest.java
Package Io.leopard.guides;import Io.leopard.javahost.javahost;import Io.leopard.jetty.jettyserver;import Java.io.ioexception;import Java.net.inetaddress;import Java.util.properties;import Org.springframework.core.io.classpathresource;import Org.springframework.core.io.resource;import Org.springframework.core.io.support.propertiesloaderutils;public class Jettytest {private static void Loaddns () Throws IOException {Resource Resource = new Classpathresource ("/vdns.properties"); Properties props = propertiesloaderutils.loadproperties (Resource); Javahost.updatevirtualdns (props);} public static void Main (string[] args) throws Exception {Loaddns (); Javahost.printallvirtualdns ();//print all virtual DNS records SYSTEM.OUT.PRINTLN ("IP:" + inetaddress.getbyname (" User.mysql.guides.leopard.io "). Gethostaddress ());//Verify that the resolution is correct jettyserver.start ();//Start jetty Server}}
Unit test code using virtual DNS, refer to Jettytest to
4. Output log
Javahost [Host=user.redis.guides.leopard.io, Ip=127.0.0.2]javahost [Host=user.mysql.guides.leopard.io, ip= 127.0.0.1]javahost [Host=session.redis.guides.leopard.io, ip=127.0.0.3]ip:127.0.0.1
Now that the other members of your team checkout the code, you can run the program without modifying the Hosts file.
To learn more about Leopard, please visit http://leopard.io/
Summarize
Congratulations to you! You can already learn to use Javahost to configure virtual DNS, no need to modify the annoying Hosts file, I wish you good luck.
Javahost: Using virtual DNS to dispense with the development environment configuration Hosts file