开源Java诊断工具Arthas推荐

Arthas 是Alibaba开源的Java诊断工具,初步试用了一下,甚是方便,在生产线上找问题应该比较方便。


Arthas可以帮助你解决什么问题?

  1. 这个类从哪个 jar 包加载的?为什么会报各种类相关的 Exception?
  2. 我改的代码为什么没有执行到?难道是我没 commit?分支搞错了?
  3. 遇到问题无法在线上 debug,难道只能通过加日志再重新发布吗?
  4. 线上遇到某个用户的数据处理有问题,但线上同样无法 debug,线下无法重现!
  5. 是否有一个全局视角来查看系统的运行状况?
  6. 有什么办法可以监控到JVM的实时运行状态?

Arthas支持JDK 6+,支持Linux/Mac/Winodws,采用命令行交互模式,同时提供丰富的 Tab 自动补全功能,进一步方便进行问题的定位和诊断。

快速安装

远程到生产的Linux主机上,一行代码即可下载工具包。

1
wget https://alibaba.github.io/arthas/arthas-boot.jar


上传本地jar包到Nexus

一些第三方提供的SDK,往往在Maven中央仓库是下载不到的,这个时候如果搭建了私服,就可以往上面丢

相关命令如下:

1
mvn deploy:deploy-file -DgroupId=org.budo -DartifactId=mipush_sdk_server -Dversion=2_2_21 -Dpackaging=jar -Dfile=MiPush_SDK_Server_2_2_21.jar -Durl=http://nexus-server:8081/nexus/content/repositories/thirdparty/ -DrepositoryId=snapshots.nexus.ewei.ren

settings.xml 关键配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<servers>
<server>
<id>snapshots.nexus.ewei.ren</id>
<username>deployment</username>
<password>12369.</password>
</server>
</servers>

<mirrors>
<mirror>
<id>snapshots.nexus.ewei.ren</id>
<name>nexus-ewei</name>
<url>http://nexus-server:8081/nexus/content/groups/public</url>
<mirrorOf>*</mirrorOf>
</mirror>
</mirrors>

关闭macOS所有过渡动画

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# opening and closing windows and popovers
defaults write -g NSAutomaticWindowAnimationsEnabled -bool false

# smooth scrolling
defaults write -g NSScrollAnimationEnabled -bool false

# showing and hiding sheets, resizing preference windows, zooming windows
# float 0 doesn't work
defaults write -g NSWindowResizeTime -float 0.001

# opening and closing Quick Look windows
defaults write -g QLPanelAnimationDuration -float 0

# rubberband scrolling (doesn't affect web views)
defaults write -g NSScrollViewRubberbanding -bool false

# resizing windows before and after showing the version browser
# also disabled by NSWindowResizeTime -float 0.001
defaults write -g NSDocumentRevisionsWindowTransformAnimation -bool false

# showing a toolbar or menu bar in full screen
defaults write -g NSToolbarFullScreenAnimationDuration -float 0

# scrolling column views
defaults write -g NSBrowserColumnAnimationSpeedMultiplier -float 0

# showing the Dock
defaults write com.apple.dock autohide-time-modifier -float 0
defaults write com.apple.dock autohide-delay -float 0

# showing and hiding Mission Control, command+numbers
defaults write com.apple.dock expose-animation-duration -float 0

# showing and hiding Launchpad
defaults write com.apple.dock springboard-show-duration -float 0
defaults write com.apple.dock springboard-hide-duration -float 0

# changing pages in Launchpad
defaults write com.apple.dock springboard-page-duration -float 0

# at least AnimateInfoPanes
defaults write com.apple.finder DisableAllAnimations -bool true

# sending messages and opening windows for replies
defaults write com.apple.Mail DisableSendAnimations -bool true
defaults write com.apple.Mail DisableReplyAnimations -bool true

简单轮询算法实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
fun main(args: Array<String>) {
val list = arrayListOf(0, 1, 2)

var prevIndex = -1
for (value in (1..7)) { // 执行7次测试
val currentIndex = (prevIndex + 1) % list.size

print("${list[currentIndex]} \t")

prevIndex = currentIndex
}
}

// 0 1 2 0 1 2 0
// TODO 加权

RestTemplate 跳过SSL证书验证

在使用RestTemplate请求接口的过程中,遇到HTTPS请求又没有证书的情况,只能通过配置来忽略证书验证了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.ewei.custom.yto.config

import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.http.client.ClientHttpRequestFactory
import org.springframework.http.client.SimpleClientHttpRequestFactory
import org.springframework.web.client.RestTemplate
import java.net.HttpURLConnection
import java.security.SecureRandom
import java.security.cert.X509Certificate
import javax.net.ssl.HttpsURLConnection
import javax.net.ssl.SSLContext
import javax.net.ssl.SSLSocketFactory
import javax.net.ssl.X509TrustManager


/**
* @author wuwenze
* @date 2019-06-21
*/
@Configuration
class RestTemplateClientConfig {

@Bean
fun restTemplate(factory: ClientHttpRequestFactory): RestTemplate {
return RestTemplate(factory)
}

@Bean
fun simpleClientHttpRequestFactory(): ClientHttpRequestFactory {
val factory = SkipSSLSimpleClientHttpRequestFactory()
factory.setReadTimeout(30000)
factory.setConnectTimeout(30000)
return factory
}

class SkipSSLSimpleClientHttpRequestFactory : SimpleClientHttpRequestFactory() {
override fun prepareConnection(connection: HttpURLConnection, httpMethod: String) {
if (connection is HttpsURLConnection) {
try {
connection.setHostnameVerifier { _, _ -> true }
connection.sslSocketFactory = createSslSocketFactory()
} catch (e: Throwable) {
// ignore
}
}
super.prepareConnection(connection, httpMethod)
}

private fun createSslSocketFactory(): SSLSocketFactory {
val context: SSLContext = SSLContext.getInstance("TLS")
context.init(null, arrayOf(SkipX509TrustManager()), SecureRandom())
return context.socketFactory
}

class SkipX509TrustManager : X509TrustManager {
override fun getAcceptedIssuers(): Array<X509Certificate> = arrayOf()
override fun checkClientTrusted(chain: Array<out X509Certificate>?, authType: String?) {}
override fun checkServerTrusted(chain: Array<out X509Certificate>?, authType: String?) {}
}
}
}

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×