hooyantsing's Blog

第129次课程_SpringCloud_ZuuI_配置中心

字数统计: 1.2k阅读时长: 5 min
2020/03/21

源辰76班

第129次课程

2020.03.21

内容

SpringCloud_Zuul_配置中心[廖彦]

项目路径:

1.

2.Zuul基本配置

配置Zuul后,直接可以通过服务名进行访问。

例如:http://127.0.0.1/**sc-user**/user/way

1.加入以下依赖

C76-S3-Ply-sc-index/pom.xml

1
2
3
4
           <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
           </dependency>

2.Index启动类内加入注解

899d5eaab47d487249425ec3dbd70e4b.png

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.yc.index;
import org.springframework.boot.SpringApplication;
import  org.springframework.boot.autoconfigure.SpringBootApplication;
import  org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import  org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
//开启声明式远程服务调用
@EnableFeignClients
//服务熔断降级开关
@EnableCircuitBreaker
//开启zuul网络网关服务
@EnableZuulProxy
public class IndexApplication {
     public static void main(String[] args) {
           SpringApplication.run(IndexApplication.class, args);
     }
     
     //负载均衡
     @LoadBalanced
     @Bean
     public RestTemplate getRestTemplate() {
           return new RestTemplate();
     }
}

C76-S3-Ply-sc-index -> application.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
server:
   port: 80
spring:
   application:
      name: sc-index
eureka:
   client:
      serviceUrl:
         # 注册服务器的地址
         defaultZone: http://127.0.0.1:8000/eureka
         
         
# 开启声明式熔断降级开关
feign:
  hystrix:
    enabled: true
    
# zuul
zuul:
  routes:
    #定义路有名称
    test:

3.路由配置

**3.1 ****简单路由 ****跳转路由 **Ribbon路由

C76-S3-Ply-sc-index -> application.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
server:
   port: 80
spring:
   application:
      name: sc-index
eureka:
   client:
      serviceUrl:
         # 注册服务器的地址
         defaultZone: http://127.0.0.1:8000/eureka
         
         
# 开启声明式熔断降级开关
feign:
  hystrix:
    enabled: true
    
# zuul
zuul:
  routes:
    #定义路有名称 ,zuul会默认路由子系统,格式:zuul服务器地址(这里是index的80端口)/服务名/路径
    
    #1.简单路由,响应重定向(可访问服务器内/外部资源)
    #http://127.0.0.1/baidu/index.html
    test1:
      path: /baidu/**
      url: http://www.baidu.com
      
    #2.跳转路由,请求转发(仅访问服务器内部资源)
    #注意:由于/user/way在IndexAction.java内的方法,方法内指定了服务名,因此可以正常跳转
    #http://127.0.0.1/zuser/way
    test2:
      path: /zuser/**
      url: forward:/user
      
    #3.1Ribbon路由
    #http://127.0.0.1/ruser/user/way
    test3:
      path: /ruser/**
      serviceId: sc-user
      
    #3.2以服务名作为路由名称
    #http://127.0.0.1/rruser/user/way
    sc-user:
      path: /rruser/**

**3.2 ****配置忽略行为 ****默认路由 **去敏感头

C76-S3-Ply-sc-index -> application.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
server:
   port: 80
spring:
   application:
      name: sc-index
eureka:
   client:
      serviceUrl:
         # 注册服务器的地址
         defaultZone: http://127.0.0.1:8000/eureka
         
         
# 开启声明式熔断降级开关
feign:
  hystrix:
    enabled: true
    
# zuul
zuul:
  # 配置忽略行为,忽略所有的子系统的默认路由
  ignoredServices: '*'
  
  routes:
    #定义路有名称 ,zuul会默认路由子系统,格式:zuul服务器地址(这里是index的80端口)/服务名/路径
    
    #1.简单路由,响应重定向(可访问服务器内/外部资源)
    #http://127.0.0.1/baidu/index.html
    test1:
      path: /baidu/**
      url: http://www.baidu.com
      
    #2.跳转路由,请求转发(仅访问服务器内部资源)
    #注意:由于/user/way在IndexAction.java内的方法,方法内指定了服务名,因此可以正常跳转
    #http://127.0.0.1/zuser/way
    test2:
      path: /zuser/**
      url: forward:/user
      
    #3.1Ribbon路由
    #http://127.0.0.1/ruser/user/way
    test3:
      path: /ruser/**
      serviceId: sc-user
      #由于zuul有默认的敏感头设定,所以要忽略默认的的敏感头设置,必须将敏感头设置为空
      sensitiveHeaders:
      
    #3.2以服务名作为路由名称
    #http://127.0.0.1/rruser/user/way
    sc-user:
      path: /rruser/**
      
      
    #4.默认路由
    #如果是不存在或者被忽略的访问路径,将会调用这个,相当于switch内的default
    legacy:
      path: /**
      url: http://www.163.com
    
    #去敏感头
    test4:
      # /suser/user/way -> /sc-user/user/way
      path: /suser/**
      serviceId: sc-user
      #不是所有的头域字段都可以过滤
      sensitiveHeaders: cookie,set-cookie  

4.Zuul过滤器

bc587e8c65491a456f2a26e87c08ad2e.png

f28d59482e3d98908b7ff675a34af088.png

(可能)在使用前需要配置一个路由,没有路由配置可能会报错。详见3

1
2
3
4
5
# zuul
zuul:
  routes:
    #定义路有名称
    test:

TokenFilter.java

需要继承ZuulFilter抽象类,实现里面的抽象方法(5个)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.yc.index.web;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Component;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
@Component
public class TokenFilter extends ZuulFilter{
     @Override
     //判断是否执行该过滤器 true开 false关
     public boolean shouldFilter() {
           return true;
     }
     @Override
     //所要执行的具体过滤动作
     public Object run() throws ZuulException {
           //获取zuul上下文对象
           RequestContext requestContext =  RequestContext.getCurrentContext();
           //获取servlet请求
           HttpServletRequest req = requestContext.getRequest();
           //获取令牌
           String token = req.getParameter("token");
           if(token == null) {
                //设置是否发送zull响应
                requestContext.setSendZuulResponse(false);
                //设置结果码
                requestContext.setResponseStatusCode(401);
                //设置响应信息
                requestContext.setResponseBody("{\"result\":\"accessToken is  empty!\"}");
           }
           return null;
     }
     @Override
     //pre前置,post后置,route路由中,error错误
     public String filterType() {
           return "pre";
     }
     @Override
     //拦截顺序,值越小优先级越高 0表示第一个
     public int filterOrder() {
           return 0;
     }
}

测试方式

没有令牌参数

86b6775fd208da22948f4d96f9b09f68.png

有令牌参数

1dc69f3a41e5c491ebe8edd245af7426.png

CATALOG