SpringCLoud搭建Zuul網(wǎng)關(guān)集群過程解析
1.使用技術(shù)
Springboot,SpringCloud,Zuul,Nignx
2.目的
使用Zuul搭建微服務(wù)高可用的網(wǎng)關(guān)
3.項目創(chuàng)建
3.1 創(chuàng)建注冊中心(略)
3.2 創(chuàng)建一個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這個高版本的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)端口號 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,都會轉(zhuǎn)發(fā)到hello-service服務(wù) ###服務(wù)別名 zuul網(wǎng)關(guān)默認整合ribbon 自動實現(xiàn)負載均衡輪訓(xùn)效果 serviceId: hello-service
3.3.4 定義一個過濾器
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)建時間: 16:07 2018/7/16 * 修改時間: * 編碼人員: ZhengQf * 版 本: 0.0.1 * 功能描述: 自定義一個Zuul Filter,它在請求路由之前進行過濾 * * 補: zuul兩大功能: 1.路由請求 * 2.過濾 */ @Component public class AccessFilter extends ZuulFilter { @Value("${server.port}") private String serverPort; /** * 過濾器的類型,它決定過濾器在請求的哪個生命周期中執(zhí)行, * pre:請求被路由之前做一些前置工作 ,比如請求和校驗 * routing : 在路由請求時被調(diào)用,路由請求轉(zhuǎn)發(fā),即是將請求轉(zhuǎn)發(fā)到具體的服務(wù)實例上去. * post : 在routing 和 error過濾器之后被調(diào)用..所以post類型的過濾器可以對請求結(jié)果進行一些加工 * error :處理請求發(fā)生錯誤時調(diào)用 */ @Override public String filterType() { return "pre"; // } /** *過濾器的執(zhí)行順序. *在一個階段有多個過濾器時,需要用此指定過濾順序 * 數(shù)值越小優(yōu)先級越高 */ @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 的時候 從請求頭中獲取 String token = request.getParameter("token"); request.setAttribute("serverPort", serverPort); if (StringUtils.isEmpty(token)) { // 不會繼續(xù)執(zhí)行... 不會去調(diào)用服務(wù)接口,網(wǎng)關(guān)服務(wù)直接響應(yīng)給客戶端 currentContext.setSendZuulResponse(false); currentContext.setResponseBody("token is null"); currentContext.setResponseStatusCode(401); return null; // 返回一個錯誤提示 } // 正常執(zhí)行調(diào)用其他服務(wù)接口... System.out.println("網(wǎng)關(guān)執(zhí)行端口號:" + serverPort); return null; } }
3.3.5 啟動類
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 啟動網(wǎng)關(guān)項目
使用server.port模擬兩個網(wǎng)關(guān)項目,端口號分別為81,82 -------> 測試網(wǎng)關(guān)高可用
使用server.port模擬兩個hello-service項目,端口號分別為8080,8081 ---->測試zuul的路由時,自動負載均衡
查看注冊中心,一共有四個服務(wù):
3.4 nginx配置負載均衡,然后 cmd ,start nginx.exe啟動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ù)器負載均衡服務(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負載均衡和反向代理技術(shù)可以實現(xiàn)網(wǎng)關(guān)的高可用
2.zuul網(wǎng)關(guān)自動集成ribbon客戶端,實現(xiàn)路由的負載均衡
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot項目使用validated實現(xiàn)參數(shù)校驗框架
當(dāng)談到Spring的參數(shù)校驗功能時,@Validated注解無疑是一個重要的利器,它為我們提供了一種簡單而又強大的方式來驗證請求參數(shù)的合法性,保證了系統(tǒng)的穩(wěn)定性和安全性,本文將介紹Spring Validated的基本用法以及在實際項目中的應(yīng)用,需要的朋友可以參考下2024-05-05SpringBoot整合Echarts實現(xiàn)用戶人數(shù)和性別展示功能(詳細步驟)
這篇文章主要介紹了SpringBoot整合Echarts實現(xiàn)用戶人數(shù)和性別展示,通過數(shù)據(jù)庫設(shè)計、實現(xiàn)數(shù)據(jù)訪問層、業(yè)務(wù)邏輯層和控制層的代碼編寫,以及前端頁面的開發(fā),本文詳細地介紹了SpringBoot整合Echarts的實現(xiàn)步驟和代碼,需要的朋友可以參考下2023-05-05基于SpringBoot實現(xiàn)上傳2種方法工程代碼實例
這篇文章主要介紹了基于SpringBoot實現(xiàn)上傳工程代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08java 中 System.out.println()和System.out.write()的區(qū)別
這篇文章主要介紹了 java 中 System.out.println()和System.out.write()的區(qū)別.的相關(guān)資料,需要的朋友可以參考下2017-04-04java Arrays快速打印數(shù)組的數(shù)據(jù)元素列表案例
這篇文章主要介紹了java Arrays快速打印數(shù)組的數(shù)據(jù)元素列表案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09