您的位置:首页 > 理论基础 > 计算机网络

zuul网关 在过滤器返回参数中文乱码

2019-05-30 15:43 2955 查看

zuul网关配置文件:

    中文乱码在配置文件中配置

  ##处理中文乱码

spring:
      http:
        encoding:
          charset: UTF-8
          enabled: true
          force: true

###注册 中心
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8100/eureka/
server:
port: 80
###网关名称
spring:
application:
name: service-zuul
##处理中文乱码
http:
encoding:
charset: UTF-8
enabled: true
force: true
### 配置网关反向代理
zuul:
routes:
###自己命名
api-member:
### 以 /api-member/访问转发到会员服务
path: /api-member/**
##会员在注册中心的别名
serviceId: member
api-order:
### 以 /api-order/访问转发到订单服务
path: /api-order/**
##订单在注册中心的别名
serviceId: order

过滤器代码

    filterType返回类型

  • PRE: 这种过滤器在请求被路由之前调用。我们可利用这种过滤器实现身份验证、在集群中选择请求的微服务、记录调试信息等。
  • ROUTING:这种过滤器将请求路由到微服务。这种过滤器用于构建发送给微服务的请求,并使用Apache HttpClient或Netfilx Ribbon请求微服务。
  • POST:这种过滤器在路由到微服务以后执行。这种过滤器可用来为响应添加标准的HTTP Header、收集统计信息和指标、将响应从微服务发送给客户端等。
  • ERROR:在其他阶段发生错误时执行该过滤器。
import com.netflix.discovery.util.StringUtil;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class TokeFilter extends ZuulFilter {
/**
* pre:可以在请求被路由之前调用
* route:在路由请求时候被调用
* post:在route和error过滤器之后被调用
* error:处理请求时发生错误时被调用
*
*/
@Override
public String filterType() {
return "pre";
}
//过滤器执行顺序
@Override
public int filterOrder() {
return 0;
}
//是否开启过滤器
@Override
public boolean shouldFilter() {
return true;
}

@Override
public Object run() throws ZuulException {
//获取上下文
RequestContext context=RequestContext.getCurrentContext();
HttpServletRequest request= context.getRequest();
String username=request.getParameter("userName");

if(StringUtils.isEmpty(username)){
context.setSendZuulResponse(false);
context.setResponseBody("没有登录");
return null;
}
return null;
}
}

启动类

@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class ZuulStar {

public static void main(String[] args) {
SpringApplication.run(ZuulStar.class,args);
}

 

(adsbygoogle = window.adsbygoogle || []).push({});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  HttpClient Ribbon Zuul
相关文章推荐