zuul(路由網關)與 Hystrix Dasboard 監控平台搭建


基於  eureka 服務於發現 (集群模式)  添加內容

1..... zuul   路由網關   

  zuul  核心就是過濾器  通過過濾器 實現請求過濾  身份校驗等

  過濾:         對請求的處理過程進行干預

  請求路由: 將外部請求轉發到具體微服務實例上

2.....Zuul 過濾展示   -----------自定義過濾器   ----


​    繼承ZuulFilter ZuulFilter是一個抽象類 需要覆蓋它的四個方法

1.... pom.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <parent>
 6         <artifactId>spring-cloud-parent</artifactId>
 7         <groupId>com.wsc</groupId>
 8         <version>1.0-SNAPSHOT</version>
 9         <relativePath>../springCloud/spring-cloud-parent/pom.xml</relativePath>
10     </parent>
11     <modelVersion>4.0.0</modelVersion>
12 
13     <artifactId>springCloud-zuul</artifactId>
14 
15 
16     <properties>
17         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18         <maven.compiler.source>1.8</maven.compiler.source>
19         <maven.compiler.target>1.8</maven.compiler.target>
20     </properties>
21     <dependencies>
22         <!--服務提供者注冊進服務中心-->
23         <dependency>
24             <groupId>org.springframework.cloud</groupId>
25             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
26         </dependency>
27         <!--服務提供者注冊進服務中心-->
28         <dependency>
29             <groupId>org.springframework.boot</groupId>
30             <artifactId>spring-boot-starter-web</artifactId>
31         </dependency>
32         <!--路由網關-->
33         <dependency>
34             <groupId>org.springframework.cloud</groupId>
35             <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
36         </dependency>
37     </dependencies>
38 </project>
pom.xml

2..... loginFilter 類  實現過濾

 1 package com.wsc.core.login;
 2 
 3 import com.netflix.zuul.ZuulFilter;
 4 import com.netflix.zuul.context.RequestContext;
 5 import com.netflix.zuul.exception.ZuulException;
 6 import org.slf4j.Logger;
 7 import org.slf4j.LoggerFactory;
 8 import org.springframework.stereotype.Component;
 9 
10 import javax.servlet.http.HttpServletRequest;
11 import java.io.IOException;
12 
13 /**
14  * @version 1.0
15  * @ClassName LoginFilter
16  * @Description TODO
17  * @Author WSC
18  * @Date 2019/9/2 10:16
19  * 
20  * filterType:  返回字符串代表過濾器的類型    返回值有以下四個
21  *
22  * ​            pre        在請求路由之前執行
23  *
24  * ​            route:     在請求路由的時候調用
25  *
26  * ​            post :     請求路由之后進行調用  
27  *
28  * ​            eroor :    處理請求發生錯誤時調用
29  *
30  * ​       filterOrder :   返回整數值
31  *
32  * ​       shouldOrder :  返回boolean 值  判斷過濾器是否執行  true 要執行此過濾器 
33  *
34  * ​         run   :      過濾器的業務邏輯
35  *
36  **/
37 @Component
38 public class LoginFilter extends ZuulFilter {
39 
40     Logger logger = LoggerFactory.getLogger(getClass());
41     @Override
42     public String filterType() {
43         return "pre"; //請求路由前調用
44     }
45 
46     @Override
47     public int filterOrder() {
48         return 1; // int 值來定義  過濾器的執行的順序  數據越小優先級越高
49     }
50 
51     @Override
52     public boolean shouldFilter() {
53         return true;  //  該過濾器是否執行  true代表執行
54     }
55 
56     @Override
57     public Object run() throws ZuulException {
58         RequestContext currentContext = RequestContext.getCurrentContext();
59         HttpServletRequest request = currentContext.getRequest();
60         // 請求參數token的值
61         String token = request.getParameter("token");
62         if(token==null){
63             logger.warn("此操作需要先登錄系統.............");
64             currentContext.setSendZuulResponse(false);// 拒絕訪問
65             currentContext.setResponseStatusCode(200); //  設置響應狀態碼
66           try {
67               currentContext.getResponse().getWriter().write("token is empty");
68           }catch(IOException e){
69             e.printStackTrace();
70             }
71             return null;
72         }
73         logger.info("ok");
74         return null;
75     }
76 }
loginFilter

3....啟動類

 1 package com.wsc.core;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
 6 
 7 /**
 8  * @version 1.0
 9  * @ClassName Start_zuul_7001
10  * @Description TODO
11  * @Author WSC
12  * @Date 2019/8/30 17:14
13  **/
14 @EnableZuulProxy   // 開啟zuul功能
15 @SpringBootApplication
16 public class Start_zuul_7001 {
17     public static void main(String[] args) {
18         SpringApplication.run(Start_zuul_7001.class,args);
19     }
20 }
啟動類

4.....application.yml配置

 1 server:
 2   port: 7001
 3 spring:
 4   application:
 5     name: microserver-zuul-geteway
 6 eureka:
 7   client:
 8     register-with-eureka: true             #服務注冊開關
 9     fetch-registry: true                  #服務發現開關
10     service-url:
11       defaultZone: http://eureka6001.com:6001/eureka/, http://eureka6002.com:6002/eureka/
12   instance:
13     instanceId: ${spring.application.name}:${server.port}   #  2   指定實例ID 不顯示主機名
14     preferipAddress: true  # 訪問路徑可以顯示ip 地址
15 
16 zuul:
17   routes:
18     povider-product:  # 路由的名稱、名稱任意  保持所有路由名稱唯一
19         path: /product/**   #訪問路徑
20         service-id: microserver-product  
21         strip-prefix: false    # 代理轉發時 去掉前綴
application.yml

過濾效果

 

通過驗證

 

 

 

3......   Fegin  客戶端服務熔斷

    hystrix-8001  在  eureka 服務於發現 (集群模式) 上

1......pom.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <parent>
 6         <artifactId>spring-cloud-parent</artifactId>
 7         <groupId>com.wsc</groupId>
 8         <version>1.0-SNAPSHOT</version>
 9         <relativePath>../springCloud/spring-cloud-parent/pom.xml</relativePath>
10     </parent>
11     <modelVersion>4.0.0</modelVersion>
12 
13     <artifactId>comsumer-consumer</artifactId>
14 
15 
16     <properties>
17         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18         <maven.compiler.source>1.8</maven.compiler.source>
19         <maven.compiler.target>1.8</maven.compiler.target>
20     </properties>
21     <dependencies>
22         <dependency>
23             <groupId>com.wsc</groupId>
24             <artifactId>common</artifactId>
25             <version>1.0-SNAPSHOT</version>
26         </dependency>
27         <!--springboot web啟動器-->
28         <dependency>
29             <groupId>org.springframework.boot</groupId>
30             <artifactId>spring-boot-starter-web</artifactId>
31         </dependency>
32         <!--Ribbon 相關的依賴-->
33         <!--spring-cloud-starter-netflix-eureka-client  會自動添加spring-cloud-starter-netflix-ribbon-->
34         <dependency>
35             <groupId>org.springframework.cloud</groupId>
36             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
37         </dependency>
38         <!--feign的依賴-->
39         <dependency>
40             <groupId>org.springframework.cloud</groupId>
41             <artifactId>spring-cloud-starter-openfeign</artifactId>
42         </dependency>
43     </dependencies>
44 </project>
pom.xml

2.....application.yml

 1 server:
 2   port: 80
 3 eureka:
 4   client:
 5     register-with-eureka: true             #服務注冊開關
 6     fetch-registry: true                  #服務發現開關
 7     service-url:
 8       defaultZone: http://eureka6001.com:6001/eureka/,http://eureka6002.com:6002/eureka/    # http://localhost:6001/eureka # 1 顯示主機名
 9 #    decoder-name:
10 #      instance:
11 #        instanceId: ${spring.application.name}:${server.port}   #  2   指定實例ID 不顯示主機名
12 #        preferipAddress: true
13 
14 #  在fegin中需要開啟Hystrix
15 feign:
16   hystrix:
17     enabled: true
View Code

3.....FeignService

 1 package com.wsc.core.service;
 2 
 3 import com.wsc.core.pojo.Product;
 4 import org.springframework.cloud.openfeign.FeignClient;
 5 import org.springframework.web.bind.annotation.PathVariable;
 6 import org.springframework.web.bind.annotation.RequestMapping;
 7 import org.springframework.web.bind.annotation.RequestMethod;
 8 
 9 import java.util.List;
10 
11 @FeignClient(value = "microserver-product",fallback = ProductClientServiceFallBack.class)
12 public interface FeignService {
13     @RequestMapping(value = "/product/get/{id}",method = RequestMethod.GET)
14     Product get(@PathVariable Long id);
15     @RequestMapping(value = "/product/list",method = RequestMethod.GET)
16     List<Product> list();
17     @RequestMapping(value = "/product/add",method = RequestMethod.POST)
18     boolean add(Product product);
19 }
View Code

4.....ProductClientServiceFallBack

 1 package com.wsc.core.service;
 2 
 3 import com.wsc.core.pojo.Product;
 4 import org.springframework.stereotype.Component;
 5 
 6 import java.util.List;
 7 
 8 /**
 9  * @version 1.0
10  * @ClassName ProductClientServiceFallBack
11  * @Description TODO
12  * @Author WSC
13  * @Date 2019/9/2 14:41
14  **/
15 @Component
16 public class ProductClientServiceFallBack implements FeignService{
17     @Override
18     public Product get(Long id) {
19         return new Product(id,"id="+id+"無數據-fegin&hystrix","無效的數據庫");
20     }
21 
22     @Override
23     public List<Product> list() {
24         return null;
25     }
26 
27     @Override
28     public boolean add(Product product) {
29         return false;
30     }
31 }
View Code

5....ConfigBeanController

 1 package com.wsc.core.controller;
 2 
 3 import com.wsc.core.pojo.Product;
 4 import com.wsc.core.service.FeignService;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.web.bind.annotation.PathVariable;
 7 import org.springframework.web.bind.annotation.RequestMapping;
 8 import org.springframework.web.bind.annotation.RestController;
 9 
10 import java.util.List;
11 
12 /**
13  * @version 1.0
14  * @ClassName ConfigBeanController
15  * @Description TODO
16  * @Author WSC
17  * @Date 2019/8/27 14:33
18  **/
19 @RestController
20 public class ConfigBeanController {
21     @Autowired
22     private FeignService feignService;
23     @RequestMapping(value ="/product/add")
24     public boolean add(Product product){
25         return feignService.add(product);
26     }
27 
28     @RequestMapping(value = "/consumer/{id}")
29     public Product get(@PathVariable("id") Long id) {
30         return feignService.get(id);
31     }
32     @RequestMapping(value = "/consumer/product/list")
33     public List<Product> list() {
34         return feignService.list();
35     }
36 }
View Code

6.....Start_800_feign

 1 package com.wsc.core;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
 6 import org.springframework.cloud.openfeign.EnableFeignClients;
 7 
 8 /**
 9  * @version 1.0
10  * @ClassName Start_80
11  * @Description TODO
12  * @Author WSC
13  * @Date 2019/8/27 14:31
14  **/
15 @EnableFeignClients(basePackages ="com.wsc.core")  //feign  通過接口+注解    獲得服務的調用
16 @EnableEurekaClient //自動注冊eureka
17 @SpringBootApplication
18 public class Start_800_feign {
19     public static void main(String[] args) {
20         SpringApplication.run(Start_800_feign.class,args);
21     }
22 }
View Code

 

4.....  Hystrix  Dasboard 監控平台搭建

    Hystrix 還提供了准時的調用監控(Hystrix Dashboard) Hystrix會持續的記錄所有的通過Hystrix的請求進行的信息,並以統計報表和圖形的形式展示給用戶看 包含執行了多少次的請求 多少次成功和失敗

​       Netflix 通過Hystrix-metrics-event-stream 項目實現了 對以上指標的監控 springCloud 也提供了Hystrix Dashboard 的整合 對監控內容轉換成可視化界面

1....   啟動類

 1 package com.wsc.core.start;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
 6 
 7 /**
 8  * @version 1.0
 9  * @ClassName Start_9001
10  * @Description TODO
11  * @Author WSC
12  * @Date 2019/9/2 14:58
13  **/
14 @EnableHystrixDashboard   // 開啟服務監控
15 @SpringBootApplication
16 public class Start_9001 {
17     public static void main(String[] args) {
18         SpringApplication.run(Start_9001.class,args);
19     }
20 }
啟動類

2....pom.xml 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <parent>
 6         <artifactId>spring-cloud-parent</artifactId>
 7         <groupId>com.wsc</groupId>
 8         <version>1.0-SNAPSHOT</version>
 9         <relativePath>../springCloud/spring-cloud-parent/pom.xml</relativePath>
10     </parent>
11     <modelVersion>4.0.0</modelVersion>
12 
13     <artifactId>hystrix-dashboard-9001</artifactId>
14 
15 
16     <properties>
17         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18         <maven.compiler.source>1.8</maven.compiler.source>
19         <maven.compiler.target>1.8</maven.compiler.target>
20     </properties>
21     <dependencies>
22         <dependency>
23             <groupId>com.wsc</groupId>
24             <artifactId>common</artifactId>
25             <version>1.0-SNAPSHOT</version>
26         </dependency>
27         <dependency>
28             <groupId>org.springframework.boot</groupId>
29             <artifactId>spring-boot-starter-web</artifactId>
30         </dependency>
31         <dependency>
32 <!--            熔斷器-->
33             <groupId>org.springframework.cloud</groupId>
34             <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
35         </dependency>
36         <dependency>
37 <!--            監控-->
38             <groupId>org.springframework.cloud</groupId>
39             <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
40         </dependency>
41     </dependencies>
42 </project>
pom.xml

3......application.yml

1 server:
2   port: 9001
3 
4   # 進入  hystrix  界面:::  http://localhost:9001/hystrix
5 
6  #  進入 監測 界面   ::::   http://localhost:9001/actuator/hystrix.stream

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM