鏂规璁捐锛氬熀浜嶪DEA鎻掍欢寮€鍙戝拰瀛楄妭鐮佹彃妗╂妧鏈紝瀹炵幇鐮斿彂浜や粯璐ㄩ噺鑷姩鍒嗘瀽

Posted 灏忓倕鍝?,

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鏂规璁捐锛氬熀浜嶪DEA鎻掍欢寮€鍙戝拰瀛楄妭鐮佹彃妗╂妧鏈紝瀹炵幇鐮斿彂浜や粯璐ㄩ噺鑷姩鍒嗘瀽相关的知识,希望对你有一定的参考价值。

浣滆€咃細灏忓倕鍝?/p>

娌夋穩銆佸垎浜€佹垚闀匡紝璁╄嚜宸卞拰浠栦汉閮借兘鏈夋墍鏀惰幏锛侌煒?/blockquote>

涓€銆佸墠瑷€

濡備綍淇濊瘉浠g爜璐ㄩ噺锛?/code>

涓氬姟鎻愰渶姹傦紝浜у搧瀹氭柟妗堬紝鐮斿彂鍋氬疄鐜帮紝娴嬭瘯楠屾祦绋嬨€傚洓绉嶈鑹茬殑鐩镐簰閰嶅悎鏄‘淇濅竴涓渶姹備笂绾跨殑蹇呭鏉′欢銆傚湪鏁翠釜闇€姹傜殑浜や粯璐ㄩ噺绾у埆鍒掑垎涓紝鐮斿彂涓庢祴璇曟槸闈炲父閲嶇殑涓€鐜紝濡傛灉鐮斿彂鎻愭祴鐨勪唬鐮佽川閲忎笉楂橈紝灏变細鍑虹幇涓嶅悓绾у埆鐨勪慨BUG銆佽繑宸ョ敋鑷抽噸鍋氱殑椋庨櫓銆?/p>

閭d箞锛屾€庝箞鏉ユ彁楂樹唬鐮佽川閲忓憿锛熶竴鑸垜浠兘浼氳姹傜爺鍙戝湪寮€鍙戜唬鐮佺殑杩囩▼涓紪鍐欏崟鍏冩祴璇曪紝楠岃瘉鑷繁鐨勪唬鐮侀€昏緫銆傚鏋滄渶缁堝崟鍏冩祴璇曡鐩栧害涓嶈冻锛屽彲浠ョ敱娴嬭瘯鎷掔粷鐮斿彂鎻愭祴銆?/p>

浣嗘槸锛屾暣涓渶姹傚疄鐜扮殑浠g爜鏄湪鍏ㄩ儴寮€鍙戝畬鎴愬悗鎻愭祴鐨勶紝涔熷氨鏄复杩戜笂绾跨殑鏈€鍚庝竴鐜紝澶у鎵嶇煡閬撴煇涓爺鍙戠殑鏌愪釜鍔熻兘鍩熺殑瀹炵幇鏄惁鍏峰鎻愭祴鏉′欢銆傚鏋滆繖涓椂鍊欎唬鐮佽川閲忎笉楂橈紝閭d箞鎺ヤ笅鏉ュ氨鏄」鐩闄╃殑鏃跺€欍€?code>鍘嬫祴璇曟椂闂?/code>銆?code>璋冧笂绾挎椂闂?/code>锛屾€讳箣鏈夌梾鎷栫潃鏈€鍚庢垚澶х梾浜嗭紒

褰撶劧锛屼綘鍙互鍦ㄩ」鐩紑鍙戞湡闂村畾鏈熸帓鏌ヤ唬鐮侊紝鎴栬€呭湪鏃ヤ細杩涘害鍙嶉绛夌瓑鎵嬫銆傚彲杩欐牱闇€瑕佽€楄垂澶ч噺鏃堕棿1鎷?鐨勫紑鍙戞帓鏌ユ柟寮忓緢闅炬弧瓒冲鏉傛祦绋嬬殑杈冨ぇ鍨嬮」鐩紑鍙戯紝鑰屼笖瀵逛簬椤圭洰椋庨櫓鎶婃帶涔熸槸涓嶅彲棰勪及鐨勩€?/p>

鎵€浠?/strong>锛屾垜浠笇鏈涢噰闆嗙爺鍙戝湪寮€鍙戣繃绋嬩腑鐨勬墽琛屽姩浣滐紝鎶婇闄╁垽鏂彁鍓嶃€傚疄闄呮搷浣滀妇渚嬪氨鏄紝褰撲綘寮€鍙戝畬鎴愪竴涓帴鍙o紝寮€濮嬫祴璇曡繍琛屾椂锛屾垜浠殑鎻掍欢灏卞彲浠ラ噰闆嗗埌杩欎釜鎺ュ彛鐨勫叏閮ㄤ俊鎭紝鍖呮嫭锛氭帴鍙e悕绉般€佸叆鍙傜被鍨嬪拰鍐呭銆佸嚭鍙傜被鍨嬪拰鍐呭銆佸紓甯镐俊鎭€佽皟鐢ㄥ叧绯婚摼绛夈€傝€屽啀鎶婅繖浜涗俊鎭眹鎬绘彁浜ゅ埌鏈嶅姟绔紝鐢熸垚鏈闇€姹備唬鐮佸垎鏀笅鐨勫叏閮ㄦ帴鍙e姩浣滐紝浠ュ強鍚勭郴缁熼棿鐨勫叧绯婚摼璺紝骞堕檮甯﹂殢鏃剁敓鎴愭渶鏂扮殑鎺ュ彛鏂囨。鍜屼竴閿祴璇曢獙璇佸姛鑳姐€傚悗鏈熸祴璇曚汉鍛樹粙鍏ユ椂灏卞彲浠ュ弬鑰冪爺鍙戝湪缂栫爜杩囩▼涓殑鍏ㄩ儴娴嬭瘯鐢ㄤ緥锛屼篃鍙互鏌ョ湅鏁翠釜鍔熻兘鐨勮鐩栫▼搴︼紝姝ゅ娴嬭瘯浜哄憳娴嬭瘯杩囩▼涓殑鏁版嵁涔熶細琚繚鐣欎笅銆傜幇鍦ㄦ嫢鏈夎繖浜涙暟鎹俊鎭互鍚庯紝灏卞彲浠ュ畬鏁寸殑鐢熸垚涓€濂楃爺鍙戞祴璇曡川閲忎氦浠樺叏瑙堝浘锛岃鏁翠釜宸ョ▼寮€鍙戜氦浠樿川閲忚瘎浼伴€忔槑鍖栥€?/p>

鎺ヤ笅鏉ユ垜浠氨鎸夌収浠ヤ笂鐨勬弿杩版€у唴瀹癸紝瀹炶返寮€鍙戜竴涓渚嬩綋浼氫笅銆?em>璧拌捣锛?/em>

浜屻€佹妧鏈疄鐜板噯澶?/h2>
  1. 瀛楄妭鐮佹彃妗╋紝鍥犱负鎴戜滑闇€瑕侀噰闆嗗埌鎺ュ彛鎵ц淇℃伅锛岄偅涔堝氨闇€瑕佷娇鐢ㄥ瓧鑺傜爜鎻掓々缁勪欢缁欐帴鍙f柟娉曞寮恒€傝繖涓疄鐜版湁鐐圭被浼艰胺姝岀殑Dapper锛屽ぇ瑙勬ā鍒嗗竷寮忔灦鏋勭殑闈炲叆渚电洃鎺с€傚彧涓嶈繃鎴戜滑闇€瑕侀噰闆嗙殑鎻忚堪鎬т俊鎭洿澶氥€傚叧浜庡瓧鑺傜爜鎻掓々锛屽彲浠ヤ簡瑙SM銆丣avassist銆丅yte-Buddy锛屽畠浠兘鍙互鍋氭椤瑰伐浣溿€?/li>
  2. IDEA 鎻掍欢寮€鍙戯紝鍥犱负鎴戜滑闇€瑕佸湪鐮斿彂浜哄憳寮€鍙戣繃绋嬩腑杩涜閲囬泦锛屼篃涓嶇牬鍧忕爺鍙戠殑鎿嶄綔涔犳儻銆傞偅涔堟渶濂界殑鏂瑰紡灏辨槸宓屽叆鍒?code>鍚姩杩愯涓紝鍙鍦ㄥ紑鍙戣繃绋嬩腑鏈夎繍琛屼唬鐮佺殑鍔ㄤ綔锛屽氨閲囬泦鐩稿簲鐨勬帴鍙d俊鎭€?/li>
  3. 鏈€鍚庡氨鏄暟鎹殑浼犺緭鍜屽鐞嗭紝浼犺緭鍙互浣跨敤MQ鎴栬€呯洿鎺ョ敤Netty銆傝€屽鐞嗘暟鎹殑杩囩▼浼氱浉瀵规瘮杈冨鏉傦紝鍦ㄨ繖涓繃绋嬮渶瑕佸垎鏋愬嚭鏈変环鍊肩殑鏁版嵁锛屽悓绫荤殑鏁版嵁锛屽悎骞朵竴鏉℃墽琛岄摼璺殑鏁版嵁锛屼互鍙婄敓鎴愮浉鍏崇殑鎺ュ彛鏂囨。鍜屽伐绋嬫湇鍔″湴鍥俱€?/li>

涓夈€佸瀛楄妭鐮佹彃妗?/h2>

杩欓噷鎴戜滑浣跨敤鐨勫瓧鑺傜爜鎻掓々缁勪欢鏄?Byte-buddy锛屽畠鏄竴涓唬鐮佺敓鎴愬拰鎿嶄綔搴擄紝鐢ㄤ簬鍦?Java 搴旂敤绋嬪簭杩愯鏃跺垱寤哄拰淇敼 Java 绫伙紝鑰屾棤闇€缂栬瘧鍣ㄧ殑甯姪銆傞櫎浜?Java 绫诲簱闄勫甫鐨勪唬鐮佺敓鎴愬疄鐢ㄧ▼搴忓锛?code>Byte Buddy 杩樺厑璁稿垱寤轰换鎰忕被锛屽苟涓斾笉闄愪簬瀹炵幇鐢ㄤ簬鍒涘缓杩愯鏃朵唬鐞嗙殑鎺ュ彛銆傛澶栵紝Byte Buddy 鎻愪緵浜嗕竴绉嶆柟渚跨殑 API锛屽彲浠ヤ娇鐢?Java 浠g悊鎴栧湪鏋勫缓杩囩▼涓墜鍔ㄦ洿鏀圭被銆?/p>

  • 鏃犻渶鐞嗚В瀛楄妭鐮佹寚浠わ紝鍗冲彲浣跨敤绠€鍗曠殑 API 灏辫兘寰堝鏄撴搷浣滃瓧鑺傜爜锛屾帶鍒剁被鍜屾柟娉曘€?/li>
  • 宸叉敮鎸丣ava 11锛屽簱杞婚噺锛屼粎鍙栧喅浜嶫ava瀛楄妭浠g爜瑙f瀽鍣ㄥ簱ASM鐨勮闂€匒PI锛屽畠鏈韩涓嶉渶瑕佷换浣曞叾浠栦緷璧栭」銆?/li>
  • 姣旇捣JDK鍔ㄦ€佷唬鐞嗐€乧glib銆丣avassist锛孊yte Buddy鍦ㄦ€ц兘涓婂叿鏈変竴瀹氱殑浼樺娍銆?/li>

1. 鏂规硶鍏ュ彛

public static void premain(String agentArgs, Instrumentation inst) {
    AgentBuilder.Transformer transformer = (builder, typeDescription, classLoader, javaModule) -> {
        return builder
                .method(ElementMatchers.any()) // 鎷︽埅浠绘剰鏂规硶
                .intercept(MethodDelegation.to(MonitorMethod.class));
    };
    new AgentBuilder
            .Default()
            .type(ElementMatchers.nameStartsWith(agentArgs)) 
            .transform(transformer)
            .installOn(inst);
}

濡傛灉浣犳帴瑙﹁繃 Javaagent 寮€鍙戯紝閭d箞瀵逛簬 premain 浼氭瘮杈冪啛鎮夈€傚鏋滀笉娓呮浣犲彲浠ユ妸瀹冪悊瑙d负锛屽畠鏄▼搴忓惎鍔ㄧ殑鏃剁殑鏂规硶鍏ュ彛锛屼綘鍙互浠庤繖涓叆鍙d腑鎷︽埅鍒颁綘闇€瑕佺殑鏂规硶锛屼箣鍚庡瀹冭繘琛屽瓧鑺傜爜澧炲己銆傚叾瀹炰篃灏辨槸鍔ㄦ€佸啓浠g爜锛屽湪鏂规硶涓坊鍔犱綘鐨勪唬鐮侊紝鏉ユ敹闆嗘柟娉曚俊鎭€?/p>

2. 閲囬泦淇℃伅

@RuntimeType
public static Object intercept(@Origin Method method, @SuperCall Callable<?> callable, @AllArguments Object[] args) throws Exception {
    long start = System.currentTimeMillis();
    Object resObj = null;
    try {
        resObj = callable.call();
        return resObj;
    } finally {
        System.out.println("鏂规硶鍚嶇О锛? + method.getName());
        System.out.println("鍏ュ弬涓暟锛? + method.getParameterCount());
        for (int i = 0; i < method.getParameterCount(); i++) {
            System.out.println("鍏ュ弬 Idx锛? + (i + 1) + " 绫诲瀷锛? + method.getParameterTypes()[i].getTypeName() + " 鍐呭锛? + args[i]);
        }
        System.out.println("鍑哄弬绫诲瀷锛? + method.getReturnType().getName());
        System.out.println("鍑哄弬缁撴灉锛? + resObj);
        System.out.println("鏂规硶鑰楁椂锛? + (System.currentTimeMillis() - start) + "ms");
    }
}

杩欎釜灏辨槸浣跨敤 Byte-Buddy 鍙互閲囬泦鐨勪俊鎭紝浣犲彲浠ラ€氳繃娉ㄨВ鍏ュ弬锛岃幏鍙栧埌涓€涓柟娉曠殑鍏ㄩ儴淇℃伅銆傛柟娉曞悕绉般€佸叆鍙備釜鏁般€佸叆鍙傜被鍨嬪拰鍐呭銆佸嚭鍙傜被鍨嬪拰缁撴灉浠ュ強杩樿兘璁$畻鏂规硶鎵ц鑰楁椂銆?/p>

鍥涖€両DEA 鎻掍欢寮€鍙?/h2>

鍏充簬 IDEA 鎻掍欢寮€鍙戠殑鐭ヨ瘑鍐呭杈冨锛屽彲浠ヤ粠GitHub鎼滅储涓€浜涜祫鏂欏拰鏌ラ槄瀹樻柟鏂囨。锛?a href="https://link.segmentfault.com/?url=https%3A%2F%2Fplugins.jetbrains.com%2Fdocs%2Fintellij%2Fgradle-build-system.html%3Ffrom%3Djetbrains.org" rel="nofollow">https://plugins.jetbrains.com/docs/intellij/gradle-build-system.html?from=jetbrains.org

姝ゅ婕旂ず妗堜緥鍏充簬鎻掍欢寮€鍙戠殑鍐呭姣旇緝绠€鍗曪紝涓昏鏄户鎵?com.intellij.execution.impl.DefaultJavaProgramRunner锛孫verride doExecute 鏂规硶锛屾坊鍔犺嚜宸遍渶瑕佺殑鍐呭鍗冲彲銆?/p>

杩欓儴鍒嗘坊鍔犵殑鍐呭鏍稿績灏辨槸鍦ㄧ▼搴忓惎鍔ㄦ椂娣诲姞鎴戜滑鐨勫瓧鑺傜爜鎻掓々绋嬪簭锛屽涓嬶細

@Override
protected RunContentDescriptor doExecute(@NotNull RunProfileState state, @NotNull ExecutionEnvironment env) throws ExecutionException {
    JavaParameters parameters = ((JavaCommandLine) state).getJavaParameters();
    // 淇℃伅鑾峰彇
    PsiFile psiFile = env.getDataContext().getData(LangDataKeys.PSI_FILE);
    String packageName = ((PsiJavaFileImpl) psiFile).getPackageName();
    // 娣诲姞瀛楄妭鐮佹彃瑁?    ParametersList parametersList = parameters.getVMParametersList();
    parametersList.add("-javaagent:" + this.getClass().getResource("/").getPath().substring(1) + "ProjectProbe.jar=" + packageName);
    return super.doExecute(state, env);
}

姝ゅ鏈€鏍稿績鐨勫氨鏄?-javaagent 鎶?ProjectProbe.jar 宸ョ▼鎺㈤拡绋嬪簭鐨凧ar鍖呭姞杞借繘鍘汇€傚叾浠栫殑灏辨槸涓€浜涘叧浜?PsiFile API 鐨勪娇鐢紝鎰熷叴瓒e彲浠ラ槄璇诲畼鏂规枃妗d腑鐨勪粙缁嶃€?/p>

浜斻€佹晥鏋滄紨绀?/h2>

瀹夎鎻掍欢

  • 瀹夎鎻掍欢灏卞拰鎴戜滑姝e父瀹夎涓€鏍凤紝涓嶈繃鐩墠杩欎釜鎻掍欢鍦ㄥ紑鍙戦樁娈碉紝鎵€浠ラ渶瑕佹湰鍦板畨瑁呫€?/li>

杩愯鏁堟灉

  • 涓婂浘灏辨槸杩愯鏁堟灉鐨勬渚嬫紨绀猴紝鎴戜滑鎶婅繍琛屾椂鎺ュ彛鐨勪俊鎭畬鏁寸殑杈撳嚭鍒版帶鍒跺彴銆?/li>
  • 鍦ㄥ疄闄呬娇鐢ㄧ殑杩囩▼涓紝浼氭妸杩欓儴鍒嗕俊鎭紶鍥炴湇鍔$锛岀敱鏈嶅姟绔垎鏋愬鐞嗗悗锛屽睍绀哄湪椤甸潰涓娿€?/li>

鍏€佹€荤粨

  • 鍩轰簬IDEA鎻掍欢鍜屽瓧鑺傜爜鎻掓々鎶€鏈紝鑳藉仛鐨勫姛鑳藉疄鐜拌繕鏈夊緢澶氥€傛湰鏂囦粎浠呮槸鍏朵腑涓€绉嶇爺鍙戝埌娴嬭瘯鐥涚偣鐨勮В鍐虫柟妗堬紝濡傛灉鎰熷叴瓒e彲浠ヤ竴璧锋繁鍏ョ爺绌躲€?/li>
  • 褰撲綘鐪嬪埌杩欐牱鐨勬渚嬩互鍚庯紝甯屾湜鑳界粰浣犵殑鏄苟涓嶄竴瀹氭墍鏈夌殑鎶€鏈偣閮芥槸涓轰簡闈㈣瘯閫犵伀绠璇濈殑銆傚綋浣犵湡鐨勬妸瀹冭惤鍦颁互鍚庯紝鎵嶄細鎳傜殑鑷繁闇€瑕佸緢澶氱煡璇嗐€?/li>
  • 鏈枃娌℃湁澶繃澶氱殑浠嬬粛鎻掍欢寮€鍙戝拰瀛楄妭鐮佹妧鏈紝濡傛灉瀵瑰瓧鑺傜爜缂栫▼鎰熷叴瓒o紝鍙互鍦ㄥ叕浼楀彿锛?code>bugstack铏礊鏍?/code>锛屽洖澶?code>瀛楄妭鐮佺紪绋?/code>銆傚叏涔?1涓?鍗冨瓧锛?0涓珷鑺傛兜鐩栦笁涓瓧鑺傜爜妗嗘灦(ASM銆丣avassist銆丅yte-budy)鍜孞avaAgent浣跨敤骞堕檮甯︽暣濂楁渚嬫簮鐮侊紒

涓冦€佺郴鍒楁帹鑽?/h2>
  • 鏁板锛岀涓€涓▼搴忓憳鏈夊杩戯紵
  • 涓€娆′唬鐮佽瘎瀹★紝宸偣杩囦笉浜嗚瘯鐢ㄦ湡锛?/a>
  • 銆奐ava 闈㈢粡鎵嬪唽銆婸DF锛屽叏涔?绔?9鑺傦紝417椤?1.5涓囧瓧锛屽畬绋?amp;鍙戠増锛?/a>
  • 宸ヤ綔涓ゅ勾绠€鍘嗗啓鎴愯繖鏍凤紝璋佽浣犲憖锛?/a>
  • 宸ヤ綔3骞达紝鐪嬪暐璧勬枡鑳芥湀钖?0K锛?/a>

鍗氬锛?a href="https://link.segmentfault.com/?url=https%3A%2F%2Fbugstack.cn" rel="nofollow">https://bugstack.cn
GitHub锛?a href="https://link.segmentfault.com/?url=https%3A%2F%2Fgithub.com%2Ffuzhengwei%2FCodeGuide%2Fwiki" rel="nofollow">https://github.com/fuzhengwei/CodeGuide/wiki

以上是关于鏂规璁捐锛氬熀浜嶪DEA鎻掍欢寮€鍙戝拰瀛楄妭鐮佹彃妗╂妧鏈紝瀹炵幇鐮斿彂浜や粯璐ㄩ噺鑷姩鍒嗘瀽的主要内容,如果未能解决你的问题,请参考以下文章

QGIS 3.14鎻掍欢寮€鍙戔€斺€擶in10绯荤粺PyCharm寮€鍙戠幆澧冩惌寤哄洓姝ヨ蛋

UE4鎻掍欢

jenkins 鏇存柊鎻掍欢浣跨敤浠g悊

10涓潪甯稿疄鐢ㄧ殑 VS Code 鎻掍欢

鍝嶅簲寮忚璁♀€斺€斾互鍙婂紑鍙戝湪璁捐涓殑浣滅敤

鍦↖ntelliJ IDEA涓紝Lombok娉ㄨВ@Slf4j鎵句笉鍒發og瑙e喅鏂规