• springCloud zuul网关服务


    第一步:编写application.properties文件

      spring.application.name=api-gateway
      server.port=5555

      zuul.routes.users.path=/users/**
      zuul.routes.users.stripPrefix=true
      zuul.routes.users.serviceId=RENREN-JINKONG-KYLIN-USER-SERVER
      eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
      #ribbon.ConnectTimeout=5000
      hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=60000

      # 开启敏感头信息传递
      zuul.routes.renren-jinkong-kylin-user-server.customSensitiveHeaders=true
      # 将敏感头信息设置为空
      #zuul.routes.compute-service.sensitiveHeaders=

      zuul.add-host-header=true

    第二步:编写启动类

    import org.springframework.boot.SpringApplication;
    import org.springframework.cloud.client.SpringCloudApplication;
    import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
    import org.springframework.cloud.netflix.zuul.filters.discovery.PatternServiceRouteMapper;
    import org.springframework.context.annotation.Bean;

    import com.renren.kylin.filter.AccessFilter;

    @SpringCloudApplication
    @EnableZuulProxy
    public class ZuulApplication {

        @Bean
        public AccessFilter accessFilter(){
            return new AccessFilter();
        }

        /**
         * 将  /service-v1 格式转换成 /v1/service,没有匹配的服务还是使用原来的名称
         * @return
         */
        @Bean
        public PatternServiceRouteMapper serviceRouteMapper(){
            return new PatternServiceRouteMapper("(?<name>^.+)-(?<version>v.+$)" , "${version}/${name}");
        }
        public static void main(String[] args) {
            SpringApplication.run(ZuulApplication.class, args);
        }
    }

    第三步:编写Filter

    import com.netflix.zuul.ZuulFilter;
    import com.netflix.zuul.context.RequestContext;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;

    import javax.servlet.http.HttpServletRequest;

    public class AccessFilter extends ZuulFilter {
        Logger logger = LoggerFactory.getLogger(AccessFilter.class);
        @Override
        public String filterType() {
            return "pre";
        }

        @Override
        public int filterOrder() {
            return 0;
        }

        @Override
        public boolean shouldFilter() {
            return true;
        }

        @Override
        public Object run() {
            RequestContext ctx = RequestContext.getCurrentContext();
            HttpServletRequest request = ctx.getRequest();
            logger.info("send {} request to {}" , request.getMethod() , request.getRequestURL().toString());
              Object accessToken = request.getParameter("accessToken");
              if(accessToken == null){
                 logger.warn("accessToken is empty");
                 ctx.setSendZuulResponse(false);
                 ctx.setResponseStatusCode(401);
                 return null;
              }
            logger.info("access token ok");
            return null;
        }
    }

    第四步:编写ServletInitializer类

    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.boot.web.support.SpringBootServletInitializer;

    public class ServletInitializer extends SpringBootServletInitializer {

        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(ZuulApplication.class);
        }

    }

  • 相关阅读:
    序列
    笔算开方法
    笔算开方法
    【AFO】闷声发大财
    P1092 虫食算[搜索]
    数据结构总结
    P1486 [NOI2004]郁闷的出纳员[权值线段树]
    P1850 换教室[dp+期望]
    P4281 [AHOI2008]紧急集合 / 聚会[LCA]
    P5021 赛道修建[贪心+二分]
  • 原文地址:https://www.cnblogs.com/gslblog/p/7147764.html
Copyright © 2020-2023  润新知