Java:使用Toxiproxy模拟各种连接问题
生活随笔
收集整理的這篇文章主要介紹了
Java:使用Toxiproxy模拟各种连接问题
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
用Toxiproxy和Java的HttpURLConnection模擬各種連接問題,以查看產(chǎn)生了什么樣的錯誤:連接超時vs.讀取超時vs.連接被拒絕…。
結(jié)果:
系統(tǒng):openjdk 11.0.1 2018-10-16
(我還無法模擬(但)是“連接中斷/斷開”。)
設(shè)置
先決條件
要在/etc/hosts添加:
127.0.0.1 proxied.google.com毒物替代品設(shè)置
開始使用抗氧化劑:
docker pull shopify/toxiproxy # BEFORE we `run` it: case #3 docker run --rm -p 5555:5555 -p 6666:6666 -p 8474:8474 --name toxiproxy -it shopify/toxiproxy配置它(我們可以只發(fā)布到:8474但是使用CLI更容易):
$ docker exec -it toxiproxy /bin/sh / # cd /go/bin/ # ./toxiproxy-cli create google -l 0.0.0.0:6666 -u www.google.com:443 # BEFORE this is run: case #4 # ./toxiproxy-cli toxic add google -t latency -a latency=5000 # case #2 Added downstream latency toxic 'latency_downstream' on proxy 'google # ./toxiproxy-cli toxic remove google -n latency_downstream Removed toxic 'latency_downstream' on proxy 'google'# ./toxiproxy-cli toxic add google -t timeout -a timeout=2000 # case #2 Added downstream timeout toxic 'timeout_downstream' on proxy 'google' # ./toxiproxy-cli toxic remove google -n timeout_downstream Removed toxic 'timeout_downstream' on proxy 'google'# ./toxiproxy-cli toxic add google -t limit_data -a bytes=5000 # case #5 Added downstream limit_data toxic 'limit_data_downstream' on proxy 'google'測試代碼
(import '[java.net URL HttpURLConnection]) (->(doto ^HttpURLConnection (.openConnection (URL. "https://proxied.google.com:6666/"));; BEWARE: JVM *must* be started with `-Dsun.net.http.allowRestrictedHeaders=true` to allow setting the Host:(.setRequestProperty "Host" "www.google.com")(.setConnectTimeout 1000)(.setReadTimeout 1000))(.getInputStream)slurp)巴克特
閱讀我的toxiproxy模擬網(wǎng)絡(luò)超時以了解為什么我們需要打擾/etc/hosts和Host標(biāo)頭。
翻譯自: https://www.javacodegeeks.com/2018/11/java-simulating-connection-problems.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的Java:使用Toxiproxy模拟各种连接问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux默认目录的名称及作用(linu
- 下一篇: 罗盘安卓版(罗盘安卓)