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);
    }

}

上一篇:Hibernate查询之API查询


下一篇:C语言程序第二次作业