Javahost: Using virtual DNS to dispense with the development environment configuration Hosts file

Source: Internet
Author: User

Javahost: Using virtual DNS to dispense with the development environment configuration Hosts fileLearn 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, the value of the legitimate IP to set the virtual DNS but we recommend a separate file configuration, it will be more clear

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();// 打印所有虚拟DNS记录System.out.println("IP:" + InetAddress.getByName("user.mysql.guides.leopard.io").getHostAddress());// 验证一下解析是否正确JettyServer.start();// 启动Jetty服务器}}

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.