优秀如你,代码世界因你绽放光彩!!!

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

 

posted @ 2019-09-02 21:18  Hi~Hi  阅读(1310)  评论(0编辑  收藏  举报
学习不分先后,知识不分多少;事无巨细,当以虚心求教;三人行,必有我师焉!!!wished for you successed !!!