springboot zuul實(shí)現(xiàn)網(wǎng)關(guān)的代碼
網(wǎng)關(guān)在微服務(wù)里的角色
在微服務(wù)架構(gòu)體系里,網(wǎng)關(guān)是非常重要的一個(gè)環(huán)節(jié),它主要實(shí)現(xiàn)了一些功能的統(tǒng)一處理,包括了:
- 統(tǒng)一授權(quán)
- 統(tǒng)一異常處理
- 路由導(dǎo)向
- 跨域處理
- 限流
實(shí)踐一下
1 添加依賴
dependencies {
implementation('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')
implementation('org.springframework.cloud:spring-cloud-starter-netflix-zuul')
testImplementation('org.springframework.boot:spring-boot-starter-test')
implementation('com.marcosbarbero.cloud:spring-cloud-zuul-ratelimit:1.3.2.RELEASE')
}
2 添加yml
server:
port: 8300
spring:
application:
name: microservice-gateway-zuul
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:6761/eureka
instance:
ip-address: true
zuul:
routes:
users:
path: /lind/** #以lind開頭的路徑被重定向到lind服務(wù)
serviceId: lind
add-host-header: true #顯示真實(shí)的http頭
retryable: false #關(guān)閉Hystrix的重試功能
ratelimit:
enabled: true
# repository: REDIS
behind-proxy: true
policies:
users:
limit: 5 #限流,每分鐘請(qǐng)求5次
refresh-interval: 60
type:
- user
- origin
- url
# url類型的限流就是通過請(qǐng)求路徑區(qū)分
# origin是通過客戶端IP地址區(qū)分
# user是通過授權(quán)用戶進(jìn)行區(qū)分,也包括匿名用戶
3 添加實(shí)現(xiàn)代碼
http攔截器,獲取用戶ID,為子服務(wù)進(jìn)行傳遞
public class PreRequestLogFilter extends ZuulFilter {
private static final Logger logger = LoggerFactory.getLogger(PreRequestLogFilter.class);
private final RateLimiter rateLimiter = RateLimiter.create(1000.0);
@Override
public Object run() {
try {
RequestContext currentContext = RequestContext.getCurrentContext();
HttpServletResponse response = currentContext.getResponse();
HttpServletRequest reqeust = currentContext.getRequest();
currentContext.addZuulRequestHeader("userId","123");//向子系統(tǒng)http頭寫數(shù)據(jù)
currentContext.addZuulRequestHeader("userName","test");
PreRequestLogFilter.logger.info(
String.format("send %s request to %s",
reqeust.getMethod(),
reqeust.getRequestURL().toString()));
if (!rateLimiter.tryAcquire()) {
HttpStatus httpStatus = HttpStatus.TOO_MANY_REQUESTS;
response.setContentType(MediaType.TEXT_PLAIN_VALUE);
response.setStatus(httpStatus.value());
response.getWriter().append(httpStatus.getReasonPhrase());
currentContext.setSendZuulResponse(false);
throw new ZuulException(
httpStatus.getReasonPhrase(),
httpStatus.value(),
httpStatus.getReasonPhrase()
);
}
} catch (java.lang.Exception e) {
ReflectionUtils.rethrowRuntimeException(e);
}
return null;
}
@Override
public boolean shouldFilter() {
// 判斷是否需要過濾
return true;
}
@Override
public String filterType() {
return FilterConstants.PRE_TYPE;
}
@Override
public int filterOrder() {
return Ordered.HIGHEST_PRECEDENCE;
}
}
在主程中注入這個(gè)過濾器
@Bean
public PreRequestLogFilter preRequestLogFilter() {
return new PreRequestLogFilter();
}
4 使用它
在URL上通過localhost:8300/users/home 將進(jìn)行l(wèi)ind服務(wù)里的home控制器下,并在http頭上寫入了userid和username這個(gè)鍵值對(duì)!
總結(jié)
以上所述是小編給大家介紹的springboot zuul實(shí)現(xiàn)網(wǎng)關(guān),希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
后端報(bào)TypeError:Cannot?read?properties?of?null?(reading?‘
這篇文章主要給大家介紹了關(guān)于后端報(bào)TypeError:Cannot?read?properties?of?null?(reading?‘xxx‘)錯(cuò)誤的解決辦法,這個(gè)錯(cuò)誤是開發(fā)中常見的錯(cuò)誤之一,需要的朋友可以參考下2023-05-05
DOM解析XML報(bào)錯(cuò)Content is not allowed in prolog解決方案詳解
這篇文章主要介紹了DOM解析XML報(bào)錯(cuò)解決方案詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
SpringBoot異步任務(wù)實(shí)現(xiàn)下單校驗(yàn)庫存的項(xiàng)目實(shí)踐
在開發(fā)中,異步任務(wù)應(yīng)用的場(chǎng)景非常的廣泛,本文主要介紹了SpringBoot異步任務(wù)實(shí)現(xiàn)下單校驗(yàn)庫存的項(xiàng)目實(shí)踐,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09
Maven打包SpringBoot工程的實(shí)現(xiàn)示例
在使用Spring Boot和Maven的項(xiàng)目中,你可以使用Maven來打包你的項(xiàng)目,本文主要介紹了Maven打包SpringBoot工程的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-05-05
mybatis關(guān)系映射之一對(duì)多和多對(duì)一
今天小編就為大家分享一篇關(guān)于mybatis關(guān)系映射之一對(duì)多和多對(duì)一,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-01-01
基于Java實(shí)現(xiàn)獲取本地IP地址和主機(jī)名
這篇文章主要介紹了基于Java實(shí)現(xiàn)獲取本地IP地址和主機(jī)名,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
kafka啟動(dòng)報(bào)錯(cuò)(Cluster ID)不匹配問題以及解決
這篇文章主要介紹了kafka啟動(dòng)報(bào)錯(cuò)(Cluster ID)不匹配問題以及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
一文帶你掌握J(rèn)ava?LinkedBlockingQueue
LinkedBlockingQueue?是一個(gè)可選有界阻塞隊(duì)列,這篇文章主要為大家詳細(xì)介紹了Java中LinkedBlockingQueue的實(shí)現(xiàn)原理與適用場(chǎng)景,感興趣的可以了解一下2023-04-04

