In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "Java how to customize the DNS parser". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Foreword:
Recently, we have finally used a high-performance testing machine (54C96G * 3). Compared with the previous single machine, the performance of the single machine has been improved by three times, and the number has increased by three times. The more critical broadband bill of lading machine has increased by more than 30 times, and the overall increase has been more than 100 times. Now you no longer have to worry about the bottleneck of the stand-alone press and just take off in place.
However, not happy for 5 minutes, I found that the request for the interface was blocked. After a while, I finally found the reason: the domain name could not be resolved and IP could not be accessed directly.
Naturally, the solution is on the horizon: a custom Java DNS parser.
After the guidance of colleagues, data search and exploration practice. Finally locked two core classes: org.apache.http.impl.conn.InMemoryDnsResolver and org.apache.http.impl.conn.SystemDefaultDnsResolver, I will demonstrate the practice of using these two classes, the main difference is in the implementation of load balancer, this is free to share.
1.InMemoryDnsResolver
This class is relatively easy to use, first write a Demo to achieve a simple domain name resolution.
/ * rewrite Java custom DNS parser, non-load balancer * * @ return * / private static DnsResolver getDnsResolver2 () {InMemoryDnsResolver dnsResolver = new InMemoryDnsResolver (); try {dnsResolver.add ("fun.tester", InetAddress.getByName ("127.0.0.1"));} catch (Exception e) {e.printStackTrace () } return dnsResolver;}
So we can parse the fun.tester to 127.0.0.1, and I'll do a simple test later.
2.SystemDefaultDnsResolver
This name is the system default DNS parser, but I don't see where the default is. The only reference that can be found is the use of the asynchronous thread pool manager.
Org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager#PoolingNHttpClientConnectionManager (org.apache.http.nio.reactor.ConnectingIOReactor, org.apache.http.nio.conn.NHttpConnectionFactory, org.apache.http.config.Registry, org.apache.http.conn.SchemePortResolver, org.apache.http.conn.DnsResolver, long, java.util.concurrent.TimeUnit)
Next, let's look at this Demo.
/ * override Java custom DNS parser Load balancer * * @ return * / private static DnsResolver getDnsResolver () {return new SystemDefaultDnsResolver () {@ Override public InetAddress [] resolve (final String host) throws UnknownHostException {if (host.equalsIgnoreCase ("fun.tester")) {return new InetAddress [] {InetAddress.getByName ("127.0.0.1")} } else {return super.resolve (host);};} 3. Custom DnsResolver
As you can see from the source code, both implementation classes implement the org.apache.http.conn.DnsResolver#resolve method in the interface org.apache.http.conn.DnsResolver. We can do it all by ourselves.
/ * Custom native DNS parser implementation * * @ return * / private static DnsResolver getDnsResolver3 () {return new DnsResolver () {@ Override public InetAddress [] resolve (final String host) throws UnknownHostException {if (host.equalsIgnoreCase ("fun.tester")) {return new InetAddress [] { InetAddress.getByName ("127.0.0.1")} } else {return InetAddress.getAllByName (host);};}
If you look closely, it is not difficult to find that it is actually a code stitching monster.
4. Connection Pool Manager
Let's share how to use a custom org.apache.http.conn.DnsResolver, which can be set up when you create a connection pool manager.
5. test
First of all, I set up a HTTP service locally, port 12345, which is very simple. The code is as follows:
Static void main (String [] args) {def util = new ArgsUtil (args) def server = getServerNoLog (util.getIntOrdefault (0, 12345)) server.response ("Have Fun ~ Tester!") Def run = run (server) waitForKey ("fan") run.stop ()}
Then I prepare a test script:
Public static void main (String [] args) {String url = "http://fun.tester:12345/" def get = getHttpGet (url) def funtester = {fun {getHttpResponse (get)}} 10.times {funtester ()}}
Console log output:
INFO- > 27.214 Fmur1 request uri: http://fun.tester:12345/, time: 304ms, HTTPcode: 27.214
INFO- > 27.214 Fmur4 request uri: http://fun.tester:12345/, time: 304ms, HTTPcode: 27.214
INFO- > 27.214 Fmur10 request uri: http://fun.tester:12345/, time: 305ms, HTTPcode: 27.214
INFO- > 27.214 Fmur5 request uri: http://fun.tester:12345/, time: 305ms, HTTPcode: 27.214
INFO- > 27.214 Fmur2 request uri: http://fun.tester:12345/, time: 305ms, HTTPcode: 27.214
INFO- > 27.214 Fmur8 request uri: http://fun.tester:12345/, time: 305ms, HTTPcode: 27.214
INFO- > 27.214 Fmur3 request uri: http://fun.tester:12345/, time: 305ms, HTTPcode: 27.214
INFO- > 27.214 Fmur7 request uri: http://fun.tester:12345/, time: 305ms, HTTPcode: 27.214
INFO- > 27.214 Fmur6 request uri: http://fun.tester:12345/, time: 305ms, HTTPcode: 27.214
INFO- > 27.214 Fmur9 request uri: http://fun.tester:12345/, time: 305ms, HTTPcode: 27.214
That's all for "how Java customizes the DNS parser". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.