場景SpringCloud-使用熔斷器儀表盤監(jiān)控熔斷: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102673599 SpringCloud -創(chuàng)建統(tǒng)一的依賴管理: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102530574 上面在實(shí)現(xiàn)了使用熔斷儀表盤進(jìn)行監(jiān)控熔斷后,使用路由網(wǎng)關(guān)統(tǒng)一訪問接口。 API網(wǎng)關(guān)API網(wǎng)關(guān)負(fù)責(zé)請(qǐng)求路由、組合和協(xié)議轉(zhuǎn)發(fā)。所有的客戶端請(qǐng)求首先要通過API網(wǎng)關(guān),之后請(qǐng)求被路由到適當(dāng)?shù)姆?wù)。 API網(wǎng)關(guān)通常會(huì)調(diào)用多個(gè)微服務(wù)和聚合結(jié)果來處理一個(gè)請(qǐng)求。它可以在Web協(xié)議(如HTTP和WebSocket)和利用于內(nèi)部的非Web友好協(xié)議之間進(jìn)行轉(zhuǎn)換。 負(fù)載均衡方式在Spring Cloud微服務(wù)系統(tǒng)中,一種常見的負(fù)載均衡方式是:客戶端的請(qǐng)求先經(jīng)過負(fù)載均衡(Zuul、Nginx),再到達(dá)服務(wù)網(wǎng)關(guān)(Zuul集群),然后再到具體的服務(wù)。服務(wù)統(tǒng)一注冊(cè)到高可用的服務(wù)注冊(cè)中心集群,服務(wù)的所有的配置文件放在Git倉庫,方便開發(fā)人員隨時(shí)改配置。 Zuul簡介Zuul的主要功能是路由轉(zhuǎn)發(fā)和過濾器。路由功能是微服務(wù)的一部分。 Zuul默認(rèn)和Ribbon結(jié)合實(shí)現(xiàn)了負(fù)載均衡的功能。 注: 博客: 實(shí)現(xiàn)參考上面構(gòu)建項(xiàng)目的方式,依次建立目錄hello-spring-cloud-zuul目錄以及在 目錄下新建pom.xml,并將其托管。然后新建src/main/java目錄和src/main/resources目錄并分別進(jìn)行目錄設(shè)置。 然后在java下新建包,包下新建啟動(dòng)類,在resources下新建配置文件application.yml。 完成后的目錄為:
pom.xml代碼 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven./POM/4.0.0" xmlns:xsi="http://www./2001/XMLSchema-instance" xsi:schemaLocation="http://maven./POM/4.0.0 http://maven./xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.badao</groupId> <artifactId>hello-spring-cloud-dependencies</artifactId> <version>1.0.0-SNAPSHOT</version> <relativePath>../hello-spring-cloud-dependencies/pom.xml</relativePath> </parent> <artifactId>hello-spring-cloud-zuul</artifactId> <packaging>jar</packaging> <name>hello-spring-cloud-zuul</name> <url>https://blog.csdn.net/badao_liumang_qizhi</url> <inceptionYear>2019-Now</inceptionYear> <dependencies> <!-- Spring Boot Begin --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- Spring Boot End --> <!-- Spring Cloud Begin --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> <!-- Spring Cloud End --> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.badao.hello.spring.cloud.zuul.ZuulApplication</mainClass> </configuration> </plugin> </plugins> </build> </project>
配置文件application.yml代碼 spring: application: name: hello-spring-cloud-zuul server: port: 8769 eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ zuul: routes: api-a: path: /api/a/** serviceId: hello-spring-cloud-web-admin-ribbon api-b: path: /api/b/** serviceId: hello-spring-cloud-web-admin-feign
注: 以 /api/a 開頭的請(qǐng)求都轉(zhuǎn)發(fā)給 hello-spring-cloud-web-admin-ribbon
服務(wù) 其中routes是固定的,但是api-a與api-b是自己定義的。 在自定義的路由中,path代表請(qǐng)求的路徑,serviceId代表真正映射的服務(wù)的Id。 在path中**代表通配符,即以/api/a開頭的都會(huì)請(qǐng)求同組內(nèi)的serviceId所對(duì)應(yīng)的服務(wù)。 然后在包下新建com.badao.hello.spring.cloud.zuul包,并在包下新建ZuulApplication啟動(dòng)類 package com.badao.hello.spring.cloud.zuul; 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; @SpringBootApplication @EnableEurekaClient @EnableZuulProxy public class ZuulApplication { public static void main(String[] args) { SpringApplication.run(ZuulApplication.class, args); } }
至此整個(gè)服務(wù)體系的結(jié)構(gòu)如下
效果我們依次啟動(dòng)Eureka服務(wù)、服務(wù)提供者、兩個(gè)服務(wù)消費(fèi)者、zuul的Application啟動(dòng)類
然后打開瀏覽器,輸入: 打開Eureka的服務(wù)與注冊(cè)發(fā)現(xiàn)中心,發(fā)現(xiàn)全部被注冊(cè)并發(fā)現(xiàn)
然后再打開瀏覽器輸入: http://localhost:8769/api/a/hi?message=HelloZuulFromBadao
再打開瀏覽器輸入: http://localhost:8769/api/b/hi?message=HelloZuulFromBadao
則Zuul的路由配置功能配置成功。 配置網(wǎng)關(guān)路由失敗時(shí)的回調(diào)有時(shí)如果因?yàn)榫W(wǎng)絡(luò)、內(nèi)存等原因?qū)е戮W(wǎng)關(guān)路由失敗,則需要配置失敗時(shí)的回調(diào)。 再新建fallback包,包下新建WebAdminFeignFallbackProvider package com.badao.hello.spring.cloud.zuul.fallback; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.client.ClientHttpResponse; import org.springframework.stereotype.Component; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.Map; @Component public class WebAdminFeignFallbackProvider implements FallbackProvider { @Override public String getRoute() { // ServiceId,如果需要所有調(diào)用都支持回退,則 return "*" 或 return null return "hello-spring-cloud-web-admin-feign"; } /** * 如果請(qǐng)求服務(wù)失敗,則返回指定的信息給調(diào)用者 * @param route * @param cause * @return */ @Override public ClientHttpResponse fallbackResponse(String route, Throwable cause) { return new ClientHttpResponse() { /** * 網(wǎng)關(guān)向 api 服務(wù)請(qǐng)求失敗了,但是消費(fèi)者客戶端向網(wǎng)關(guān)發(fā)起的請(qǐng)求是成功的, * 不應(yīng)該把 api 的 404,500 等問題拋給客戶端 * 網(wǎng)關(guān)和 api 服務(wù)集群對(duì)于客戶端來說是黑盒 * @return * @throws IOException */ @Override public HttpStatus getStatusCode() throws IOException { return HttpStatus.OK; } @Override public int getRawStatusCode() throws IOException { return HttpStatus.OK.value(); } @Override public String getStatusText() throws IOException { return HttpStatus.OK.getReasonPhrase(); } @Override public void close() { } @Override public InputStream getBody() throws IOException { ObjectMapper objectMapper = new ObjectMapper(); Map<String, Object> map = new HashMap<>(); map.put("status", 200); map.put("message", "無法連接,請(qǐng)檢查您的網(wǎng)絡(luò)"); return new ByteArrayInputStream(objectMapper.writeValueAsString(map).getBytes("UTF-8")); } @Override public HttpHeaders getHeaders() { HttpHeaders headers = new HttpHeaders(); // 和 getBody 中的內(nèi)容編碼一致 headers.setContentType(MediaType.APPLICATION_JSON_UTF8); return headers; } }; } }
代碼下載https://download.csdn.net/download/badao_liumang_qizhi/11908425 |
|