A problem with using Javahost

To do an HBase project, you need to configure hosts. This hosts actually corresponds to the address of zookeeper. It is very convenient to run locally. You can directly configure the local file hosts by yourself (or the computer permissions are limited and you cannot modify the local hosts file). But the project needs to be released online, other team members need to use it, etc., do I have to tell them how to configure hosts? This is too troublesome, so I try to find another solution.
If you access different data source servers in different operating environments, should you use a domain name instead of an IP for easy switching? If you use a domain name, you need to configure the hosts file in the development environment. I hope that the program can be run directly without modifying the hosts file. Through my efforts, I finally found javahost (JVM virtual DNS) to help me solve these troubles.

Introduce dependencies


<dependency>
    <groupId>io.leopard</groupId>
    <artifactId>javahost</artifactId>
    <version>0.9.12</version>
</dependency>

Because the project is a springcloud project, we also use configserver. Therefore, you can easily change the configuration information of the corresponding service on git. I am here to configure the hosts information in the configuration file on git.

application.properties

# 省略其他配置
dns.resolve.zk.kanyun.zk01.com=10.11.12.32
dns.resolve.zk.kanyun.zk02.com=10.11.12.33
dns.resolve.zk.kanyun.zk03.com=10.11.12.34
# 省略其他配置

You can see that I have configured the ip addresses corresponding to the three domain names above, why does one zk need to configure multiple addresses? Because in fact zookeeper is composed of a cluster. At the same time, the above configuration will eventually become a Map<String, String> structure. You can refer to the link

Next we need to import the configuration.

import io.leopard.javahost.JavaHost;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.InetAddress;
import java.util.Map;
@Slf4j
@Component
@ConfigurationProperties(prefix = "dns.resolve")
public class DnsConfiguration {
    
    /**
     * 这里的zk也要跟配置文件中的配置一致
     * 如 需要配置多个域名 则在配置文件中配置
     * dns.resolve.zk.aabb.com=192.168.1.1
     * dns.resolve.zk.ccdd.com=192.168.1.2
     * dns.resolve.zk.eeff.com=192.168.1.3
    */
    private Map<String, String> zk;

    public Map<String, String> getZk() {
        return zk;
    }

    public void setZk(Map<String, String> zk) {
        this.zk = zk;
    }

    public void handler()  {
//        业务代码......

        if (zk != null && !zk.isEmpty()) {
//            设置域名解析
            JavaHost.updateVirtualDns(zk);
            for (String host : zk.keySet()) {
                try {
//                    ping 命令,设置超时时间为30秒
                    boolean status = InetAddress.getByName(host).isReachable(30);
//                     ping 的结果
                    log.info("Test Ping Host: [{}] result: [{}]", host, status);
                } catch (IOException e) {
                    log.error("Test Ping Host: [{}] exception: ", host, e);
                }
            }
        }
        

//        业务代码....
    }
}

It should be noted that the above code @Component / @ConfigurationProperties annotation and the set/get method of the member variable zk are indispensable.

It should be noted that when configuring domain name resolution, the domain name must be lowercase.

Compared with the above example, dns.resolve.zk.kanyun.zk01.com is lowercase, if the uppercase is configured here, the resolution will fail . Report UnknownHostException or Name does not resolve when accessing the domain name.

Therefore, it must be configured in lowercase. When configured in lowercase, access to uppercase domain names can also be resolved!

おすすめ

転載: blog.csdn.net/kanyun123/article/details/118735772