SpringCLoud搭建Zuul網(wǎng)關(guān)集群過程解析
1.使用技術(shù)
Springboot,SpringCloud,Zuul,Nignx
2.目的
使用Zuul搭建微服務(wù)高可用的網(wǎng)關(guān)
3.項(xiàng)目創(chuàng)建
3.1 創(chuàng)建注冊中心(略)
3.2 創(chuàng)建一個(gè)hello-service的服務(wù)工程
3.3 創(chuàng)建springcloud-zuul-ha網(wǎng)關(guān)服務(wù)
3.3.1 創(chuàng)建工程(略)
3.3.2 pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>qinfeng.zheng</groupId> <artifactId>springcloud-zuul-ha</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springcloud-zuul-ha</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <!--Dalston.RC1這個(gè)高版本的zuul依賴有問題--> <version>Brixton.SR7</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
3.3.3 application.yml
###服務(wù)注冊地址 eureka: client: serviceUrl: defaultZone: http://localhost:8763/eureka/ ###api網(wǎng)關(guān)端口號(hào) server: port: 82 ###網(wǎng)關(guān)名稱 spring: application: name: service-zuul zuul: routes: ###定義轉(zhuǎn)發(fā)服務(wù)規(guī)則 api-a: path: /api-hello/** #請求路徑中含有api-hello,都會(huì)轉(zhuǎn)發(fā)到hello-service服務(wù) ###服務(wù)別名 zuul網(wǎng)關(guān)默認(rèn)整合ribbon 自動(dòng)實(shí)現(xiàn)負(fù)載均衡輪訓(xùn)效果 serviceId: hello-service
3.3.4 定義一個(gè)過濾器
qinfeng.zheng.filter.AccessFilter
import com.netflix.zuul.ZuulFilter; import com.netflix.zuul.context.RequestContext; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest; /** * 創(chuàng)建時(shí)間: 16:07 2018/7/16 * 修改時(shí)間: * 編碼人員: ZhengQf * 版 本: 0.0.1 * 功能描述: 自定義一個(gè)Zuul Filter,它在請求路由之前進(jìn)行過濾 * * 補(bǔ): zuul兩大功能: 1.路由請求 * 2.過濾 */ @Component public class AccessFilter extends ZuulFilter { @Value("${server.port}") private String serverPort; /** * 過濾器的類型,它決定過濾器在請求的哪個(gè)生命周期中執(zhí)行, * pre:請求被路由之前做一些前置工作 ,比如請求和校驗(yàn) * routing : 在路由請求時(shí)被調(diào)用,路由請求轉(zhuǎn)發(fā),即是將請求轉(zhuǎn)發(fā)到具體的服務(wù)實(shí)例上去. * post : 在routing 和 error過濾器之后被調(diào)用..所以post類型的過濾器可以對請求結(jié)果進(jìn)行一些加工 * error :處理請求發(fā)生錯(cuò)誤時(shí)調(diào)用 */ @Override public String filterType() { return "pre"; // } /** *過濾器的執(zhí)行順序. *在一個(gè)階段有多個(gè)過濾器時(shí),需要用此指定過濾順序 * 數(shù)值越小優(yōu)先級(jí)越高 */ @Override public int filterOrder() { return 0; } /** * 判斷過濾器是否執(zhí)行,直接返回true,代表對所有請求過濾 * 此方法指定過濾范圍 * @return */ @Override public boolean shouldFilter() { return true; } /** * 過濾的具體邏輯 * @return */ @Override public Object run() { // 1.獲取上下文 RequestContext currentContext = RequestContext.getCurrentContext(); // 2.獲取 Request HttpServletRequest request = currentContext.getRequest(); // 3.獲取token 的時(shí)候 從請求頭中獲取 String token = request.getParameter("token"); request.setAttribute("serverPort", serverPort); if (StringUtils.isEmpty(token)) { // 不會(huì)繼續(xù)執(zhí)行... 不會(huì)去調(diào)用服務(wù)接口,網(wǎng)關(guān)服務(wù)直接響應(yīng)給客戶端 currentContext.setSendZuulResponse(false); currentContext.setResponseBody("token is null"); currentContext.setResponseStatusCode(401); return null; // 返回一個(gè)錯(cuò)誤提示 } // 正常執(zhí)行調(diào)用其他服務(wù)接口... System.out.println("網(wǎng)關(guān)執(zhí)行端口號(hào):" + serverPort); return null; } }
3.3.5 啟動(dòng)類
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; @EnableZuulProxy @EnableEurekaClient @SpringBootApplication public class SpringcloudZuulHaApplication { public static void main(String[] args) { SpringApplication.run(SpringcloudZuulHaApplication.class, args); } }
3.3.6 啟動(dòng)網(wǎng)關(guān)項(xiàng)目
使用server.port模擬兩個(gè)網(wǎng)關(guān)項(xiàng)目,端口號(hào)分別為81,82 -------> 測試網(wǎng)關(guān)高可用
使用server.port模擬兩個(gè)hello-service項(xiàng)目,端口號(hào)分別為8080,8081 ---->測試zuul的路由時(shí),自動(dòng)負(fù)載均衡
查看注冊中心,一共有四個(gè)服務(wù):
3.4 nginx配置負(fù)載均衡,然后 cmd ,start nginx.exe啟動(dòng)nginx服務(wù)
upstream backServer{ server 127.0.0.1:81; server 127.0.0.1:82; } server { listen 80; server_name qinfeng.zheng.com; location / { ### 指定上游服務(wù)器負(fù)載均衡服務(wù)器 proxy_pass http://backServer/; index index.html index.htm; } }
3.5 在本地host文件中配置qinfeng.zheng.com 的域名
4.測試
第一次請求: http://qinfeng.zheng.com/api-hello/hello/index?token=123
第二次請求:http://qinfeng.zheng.com/api-hello/hello/index?token=123
5.總結(jié)
1.使用nignx負(fù)載均衡和反向代理技術(shù)可以實(shí)現(xiàn)網(wǎng)關(guān)的高可用
2.zuul網(wǎng)關(guān)自動(dòng)集成ribbon客戶端,實(shí)現(xiàn)路由的負(fù)載均衡
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot項(xiàng)目使用validated實(shí)現(xiàn)參數(shù)校驗(yàn)框架
當(dāng)談到Spring的參數(shù)校驗(yàn)功能時(shí),@Validated注解無疑是一個(gè)重要的利器,它為我們提供了一種簡單而又強(qiáng)大的方式來驗(yàn)證請求參數(shù)的合法性,保證了系統(tǒng)的穩(wěn)定性和安全性,本文將介紹Spring Validated的基本用法以及在實(shí)際項(xiàng)目中的應(yīng)用,需要的朋友可以參考下2024-05-05Java Session驗(yàn)證碼案例代碼實(shí)例解析
這篇文章主要介紹了Java Session驗(yàn)證碼案例代碼實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06SpringBoot整合Echarts實(shí)現(xiàn)用戶人數(shù)和性別展示功能(詳細(xì)步驟)
這篇文章主要介紹了SpringBoot整合Echarts實(shí)現(xiàn)用戶人數(shù)和性別展示,通過數(shù)據(jù)庫設(shè)計(jì)、實(shí)現(xiàn)數(shù)據(jù)訪問層、業(yè)務(wù)邏輯層和控制層的代碼編寫,以及前端頁面的開發(fā),本文詳細(xì)地介紹了SpringBoot整合Echarts的實(shí)現(xiàn)步驟和代碼,需要的朋友可以參考下2023-05-05基于SpringBoot實(shí)現(xiàn)上傳2種方法工程代碼實(shí)例
這篇文章主要介紹了基于SpringBoot實(shí)現(xiàn)上傳工程代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08java 中 System.out.println()和System.out.write()的區(qū)別
這篇文章主要介紹了 java 中 System.out.println()和System.out.write()的區(qū)別.的相關(guān)資料,需要的朋友可以參考下2017-04-04Java線程池隊(duì)列LinkedBlockingDeque
這篇文章主要為大家介紹了Java線程池隊(duì)列LinkedBlockingDeque示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12java Arrays快速打印數(shù)組的數(shù)據(jù)元素列表案例
這篇文章主要介紹了java Arrays快速打印數(shù)組的數(shù)據(jù)元素列表案例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09