SpringBoot中實(shí)現(xiàn)代理方式
SpringBoot實(shí)現(xiàn)代理
功能
定義一個(gè)功能,使指定的方法執(zhí)行前后輸出日志信息。
1. 定義一個(gè)注解,添加的方法上具有該功能,或者添加到類上,類下的所有方法都具有該功能
@Target( {ElementType.TYPE, ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) public @interface LogAround { }
2. 實(shí)現(xiàn)PointCut
接口,找出標(biāo)注注解的方法或類
// 這里是直接繼承了一個(gè)便捷基類,實(shí)現(xiàn) matches 方法 public class LogAroundPointcut extends StaticMethodMatcherPointcut { @Override public boolean matches(Method method, Class<?> targetClass) { return AnnotatedElementUtils.hasAnnotation(targetClass, LogAround.class) || AnnotatedElementUtils.hasAnnotation(method, LogAround.class); } }
3. 實(shí)現(xiàn) Advice
,定義增強(qiáng)行為
// 這里繼承了 MethodInterceptor,方法攔截。前后打印日志 public class LogAroundInterceptor implements MethodInterceptor { @Nullable @Override public Object invoke(@Nonnull MethodInvocation invocation) throws Throwable { System.out.println("before method invoke log...."); Method method = invocation.getMethod(); Object[] args = invocation.getArguments(); Object target = invocation.getThis(); Object invoke = method.invoke(target, args); System.out.println("after method invoke log...."); return invoke; } }
4. 定義一個(gè) Advisor
,把這兩個(gè)組合起來(lái),并添加到 Spring 中
@Component public class LogAroundPointcutAdvisor extends AbstractPointcutAdvisor { @Override public Pointcut getPointcut() { return new LogAroundPointcut(); } @Override public Advice getAdvice() { return new LogAroundInterceptor(); } @Override public boolean isPerInstance() { return false; } }
5. 測(cè)試bean
@Component public class AopDemo { @LogAround public static int sum(int i1, int i2) { return i1 + i2; } }
6. 測(cè)試結(jié)果
前后有打印日志,代理成功
SpringBoot實(shí)現(xiàn)反向代理
背景:在前后端分離的項(xiàng)目中,有一天后端項(xiàng)目因?yàn)槟承┰虿荒鼙┞对诠W(wǎng)地址,此時(shí)為了修改最少,利用反向代理技術(shù)進(jìn)行實(shí)現(xiàn)。
這種不是解決方案,曲線救國(guó)。
1. 引入依賴
? <dependency> ?? ? <groupId>org.mitre.dsmiley.httpproxy</groupId> ? ?? ? <artifactId>smiley-http-proxy-servlet</artifactId> ? ??? ? <version>1.12</version> </dependency>
2. 配置文件
將本地項(xiàng)目的 /baidu/* 下的請(qǐng)求轉(zhuǎn)發(fā)至https://baidu.com
/baidu/* 前面一定要加 /
proxy: ? baidu: ? ? url: /baidu/* ? ? target_url: https://baidu.com
3. 配置代理
package athena.gateway.app.banshi; import org.mitre.dsmiley.httpproxy.ProxyServlet; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class SolrProxyServletConfiguration { ? ? @Value("${proxy.baidu.url}") ? ? private String url; ? ? @Value("${proxy.baidu.target_url}") ? ? private String targetUrl; ? ? @Bean ? ? public ServletRegistrationBean servletRegistrationBean() { ? ? ? ? ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new ProxyServlet(), url); ? ? ? ? servletRegistrationBean.setName("百度"); ? ? ? ? servletRegistrationBean.addInitParameter("targetUri", targetUrl); ? ? ? ? servletRegistrationBean.addInitParameter(ProxyServlet.P_LOG, String.valueOf(true)); ? ? ? ? return servletRegistrationBean; ? ? } }
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java面試常見(jiàn)問(wèn)題---ConcurrentHashMap
ConcurrentHashMap是由Segment數(shù)組結(jié)構(gòu)和HashEntry數(shù)組結(jié)構(gòu)組成。Segment的結(jié)構(gòu)和HashMap類似,是一種數(shù)組和鏈表結(jié)構(gòu),今天給大家普及java面試常見(jiàn)問(wèn)題---ConcurrentHashMap知識(shí),一起看看吧2021-06-06MyBatis是如何實(shí)現(xiàn)日志模塊的詳解
這篇文章主要給大家介紹了關(guān)于MyBatis是如何實(shí)現(xiàn)日志模塊的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用MyBatis具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10Java函數(shù)接口和Lambda表達(dá)式深入分析
這篇文章主要介紹了Java函數(shù)接口和Lambda表達(dá)式,函數(shù)接口是一個(gè)具有單個(gè)抽象方法的接口,接口設(shè)計(jì)主要是為了支持Lambda表達(dá)式和方法引用,使得Java能更方便地實(shí)現(xiàn)函數(shù)式編程風(fēng)格,需要的朋友可以參考下2025-04-04一文帶你入門(mén)JDK8新特性——Lambda表達(dá)式
這篇文章主要介紹了JDK8新特性——Lambda表達(dá)式的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)JAVA開(kāi)發(fā),感興趣的朋友可以了解下2020-08-08IDEA 2020.1 搜索不到Chinese (Simplified) Language
小編在安裝中文插件時(shí)遇到IDEA 2020.1 搜索不到Chinese ​(Simplified)​ Language Pack EAP,無(wú)法安裝的問(wèn)題,本文給大家分享我的解決方法,感興趣的朋友一起看看吧2020-04-04java Socket實(shí)現(xiàn)簡(jiǎn)單模擬HTTP服務(wù)器
這篇文章主要介紹了java Socket實(shí)現(xiàn)簡(jiǎn)單模擬HTTP服務(wù)器,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05Java Spring開(kāi)發(fā)環(huán)境搭建及簡(jiǎn)單入門(mén)示例教程
這篇文章主要介紹了Java Spring開(kāi)發(fā)環(huán)境搭建及簡(jiǎn)單入門(mén)示例,結(jié)合實(shí)例形式分析了spring環(huán)境搭建、配置、使用方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-11-11