SpringCloud学习笔记(20)----Spring Cloud Netflix之服务网关Zuul的各种姿势

1. 禁用过滤器

# zuul.<SimpleClassName>.<filterType>.disable=true
# 例如禁用 自定义的过滤器
zuul.MyFilter.pre.disable=true

2. 路由的映射

  方法1:

zuul.routes.spring-cloud-provider=/provider/**
zuul.routes.spring-cloud-consumer=/consumer/**

  说明:spring-cloud-provider服务名称,对其进行路由,一个*代表匹配一层,二个**代表匹配所有

  测试:http://localhost:9876/consumer/user/1

  测试:http://localhost:9876/provider/api/user/1

  方法2: 

zuul.routes.provider1.path=/provider1/**
zuul.routes.provider1.service-id=spring-cloud-provider
zuul.routes.consumer1.path=/consumer1/**
zuul.routes.consumer1.service-id=spring-cloud-consumer

  说明:provider1和consumer1代表id,自己定,只要不重复即可。

  测试:http://localhost:9876/consumer/user/1

  测试:http://localhost:9876/provider/api/user/1

3. 禁用服务名进行访问

zuul.ignored-services=/spring-cloud-provider
# zuul.ignored-services=/* 禁用所有

4. strip-prefix(代理前缀)的灵活使用

#strip-prefix 的配置()
# 默认为true,(代理前缀默认会从请求路径中移除)
zuul.routes.provider1.strip-prefix=false
zuul.routes.provider1.path=/api/**
zuul.routes.provider1.service-id=spring-cloud-provider
zuul.routes.consumer1.strip-prefix=false
zuul.routes.consumer1.path=/consumer1/**
zuul.routes.consumer1.service-id=spring-cloud-consumer1

  测试:http://localhost:9876/provider/api/user/1

5. sensitive-headers属性(不传递给下游的服务应用)

zuul.routes.provider1.sensitive-headers=token
# 默认值 Cookie, Set-Cookie,Authorization
# 注意:如果不禁用服务名的情况下,通过服务名,这个设置是无效的

6. 脱离注册中心,不使用注册中心

# 禁用注册中心
ribbon.eureka.enabled=false

# 不实现负载均衡
#zuul.routes.wangx.path=/**
#zuul.routes.wangx.url=http://baidu.com

#实现负载均衡
zuul.routes.wangx.path=/**
zuul.routes.wangx.service-id=wangx-x
wangx-x.ribbon.listOfServers=http://baidu.com,http://spring.io

7. 自定义路由策略

  使用正则表达式,自定义bean

  @Bean
    public PatternServiceRouteMapper serviceRouteMapper() {
        return new PatternServiceRouteMapper("(?<name1>^.*)-(?<name2>c.*)-(?<name3>c.+$)", "${name1}-${name2}-provider") {
            @Override
            public String apply(final String serviceId) {
                String route = super.apply(serviceId);
                System.out.println(route + " -> " + serviceId);
                return route;
            }
        };
    }

  打印:

  spring-cloud-provider -> spring-cloud-consumer

  spring-cloud-zuul -> spring-cloud-zuul

  spring-cloud-provider -> spring-cloud-provider

  说明:将provider转发到consumer

8. ignored-parrterns 说明

  zuul.ignored-patterns=/**/api/**

  忽略含有api的路径的url

9. legacy说明

  zuul.routers.legacy.path=/**

  zuul.routers.legacy.url=http://baidu.com

  #若前面的路由匹配不到,那么就使用这个

10. 上传大文件

  提供者代码

 @RequestMapping("/upload")
    public String upload(@RequestParam("file")MultipartFile file) throws IOException {
        File file1 = new File("F:/" + file.getOriginalFilename());
        System.out.println("开始上传文件......");
        FileCopyUtils.copy(file.getBytes(),file1);
        return "SUCCESS";
    }

  设置上传文件最大值

# 上传文件最大值
spring.servlet.multipart.max-file-size=1000MB
spring.servlet.multipart.max-request-size=1000MB

  测试:curl -F "file=@eclipse.zip" http://localhost:8976/api/user/upload

 使用隐藏连接/zuul/

  curl -F "file=@eclipse.zip" http://localhost:8976/zuul/api/user/upload

  设置

  

hystrix.command.defautl.execution.isolation.thread.timeoutInMilliseconds=6000
#全局设置
ribbon.ConnectTimeout=6000
ribbon.ReadTimeout=6000
# 单独设置
provider1.ribbon.ConnectTimeout=300

11. 异常处理

  

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.springframework.cloud.netflix.zuul.filters.route.ZuulFallbackProvider;
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;

@Component
public class ProviderFallbackProvider implements ZuulFallbackProvider {
    
    @Override
    public String getRoute() {
        return "spring-cloud-provider";
    }

    @Override
    public ClientHttpResponse fallbackResponse() {
        return new ClientHttpResponse() {
            
            @Override
            public HttpStatus getStatusCode() throws IOException {
                return HttpStatus.OK;
            }

            @Override
            public int getRawStatusCode() throws IOException {
                return 200;
            }

            @Override
            public String getStatusText() throws IOException {
                return "OK";
            }

            @Override
            public void close() {

            }

            @Override
            public InputStream getBody() throws IOException {
                return new ByteArrayInputStream((getRoute()+ " is fallback").getBytes());
            }

            @Override
            public HttpHeaders getHeaders() {
                HttpHeaders headers = new HttpHeaders();
                headers.setContentType(MediaType.APPLICATION_JSON);
                return headers;
            }
        };
    }
}

 

posted @ 2018-10-29 16:26  Eternally_dream  阅读(548)  评论(0编辑  收藏  举报