A little question about kafka connection



Recently, a project used the code of java api to connect to kafka. In the original test, the value of bootstrap.servers was always written as ip, and there was no problem in producing and consuming data, but it cooperated with the operation and maintenance during the pre-release test. If you need to change the ip to a domain name to access, there will be a problem with the startup, and it will not start up. The exception thrown is as follows:

DNS resolution failed for url in .......


The version of our kafka is apache 0.9.0.0, and then I searched the Internet to see if there were related examples, but I didn't find a particularly clear solution to the problem. Most of the domestic ones said that the server configuration of kafka needs to be changed. Most of the documents in foreign countries say that among the three domain names, one of the domain names is wrong and cannot be connected. Although it is very similar to my case, it is not the same thing, because I am sure that my three domain names are correct. For details, please refer to this kafka issue:

https://issues.apache.org/jira/browse/KAFKA-2657



In order to rule out environmental problems, I used a virtual machine on my computer to build a three-node kafka cluster , and then use the domain name to access, the result can be accessed, even if the domain name is no problem, then I compared the domain name on my own machine with the domain name of our pre-release environment, and found that my pre-release domain name contains capital letters There are also symbols - and . operators, and my own domain name is completely lowercase English letters and numbers. Then I speculated whether it was the reason for adding special characters as separators, and then I tested it separately and jointly. The domain name of the combination of . and - found that it had nothing to do with this, then finally there was a possibility that the domain name contained uppercase letters, and sure enough , After I tested it, I found that the domain name that contains uppercase letters was intercepted by kafka when the domain name was connected, so the connection could not be connected, so there was the problem of DNS resolution failure. Finally, after changing the domain name to a combination of lowercase letters, you can access it normally.


Finally, I checked the source code of kafka's client and found that the following code method is used to extract the domain name rule:

    private static final Pattern HOST_PORT_PATTERN = Pattern.compile(".*?\\[?([0-9a-z\\-.:]*)\\]?:([0-9]+)");

    public static String getHost(String address) {
        Matches matches = HOST_PORT_PATTERN.matches (address);
        return matcher.matches() ? matcher.group(1) : null;
    }


    public static void main(String[] args) {

        System.out.println(getHost("DEV-KAFKA-03"));
        //result is null
    }



Everything is clear here. The version 0.9.0.0 does not support uppercase domain name access. Finally, I checked the source code of kafka of 0.10.0.0 and found that this bug has been fixed, so everyone can pay attention to this when using it. small issue.
If you have any questions, you can scan the code and follow the WeChat public account: I am the siege division (woshigcs), leave a message in the background for consultation. Technical debts cannot be owed, and health debts cannot be owed. On the road of seeking the Tao, walk with you.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326491725&siteId=291194637