网关程序的开发

出现问题

问题1:pom.xml中有标红

网关程序的开发_第1张图片

解决方法:等一下就好了,它之前可能没有下载完成

问题2:Zull无法注册到Eureka Server中

网关程序的开发_第2张图片

解决方法:是因为在application.yml文件中http都写成了https

网关程序的开发_第3张图片

主要工作

1.只使用单节点的Eureka Server

2.新建模块

网关程序的开发_第4张图片

3.配置pom.xml文件



    
        imooc-ad
        com.imooc.ad
        1.0-SNAPSHOT
    
    4.0.0

    ad-gateway
    1.0-SNAPSHOT
    jar

    
    ad-gateway
    ad-gateway

    
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-zuul
        
    

    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    
    
    
        8
        8
    

4.编写网关启动程序

package com.imooc.ad;

@EnableZuulProxy
@SpringCloudApplication
public class ZuulGatewayApplication {

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

5.编写配置文件yml

server:
  port: 9000
spring:
  application:
    name: ad-gateway
eureka:
  client:
    server-url:
      defaultZone: http://server1:8000/eureka

6.自定义网关过滤器,用于日志的操作

(1)新建包

(2)新建过滤器1:PreRequestFilter

PRE:这种过滤器在请求被路由之前调用。我们可利用这种过滤器实现身份验证、在集群中选择请求的微服务、记录调试信息等

package com.imooc.ad.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

@Slf4j//打印日志
@Component//注入Spring容器
public class PreRequestFilter extends ZuulFilter {
    //filter的执行类型
    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    //filter的执行舒徐
    @Override
    public int filterOrder() {
        return 0;
    }

    //是否要执行过滤器
    @Override
    public boolean shouldFilter() {
        return true;
    }

    //具体操作,记录该操作的起始时间戳
    @Override
    public Object run() throws ZuulException {
        RequestContext ctx = RequestContext.getCurrentContext();
        ctx.set("startTime",System.currentTimeMillis());
        return null;
    }
}

(3)新建AccessLogFilter(POST类型的Filter)

POST:这种过滤器在路由到微服务以后执行。这种过滤器可用来为响应添加标准的HTTP Header、收集统计信息和指标、将响应从微服务发送给客户端等。

package com.imooc.ad.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@Component
public class AcessLogFilter extends ZuulFilter {
    @Override
    public String filterType() {
        return FilterConstants.POST_TYPE;
    }

    @Override
    public int filterOrder() {
        return FilterConstants.SEND_RESPONSE_FILTER_ORDER - 1;//系统的最后一个 post 过滤器执行之前
    }

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

    @Override
    public Object run() throws ZuulException {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest  request = ctx.getRequest();
        Long startTime = (Long)ctx.get("startTime");
        String url = request.getRequestURI();
        long duration = System.currentTimeMillis() - startTime;
        log.info("url: " + url + ": duration" + duration / 100 + "ms");
        return null;
    }
}

7.启动Eureka Server

8.启动网关

网关程序的开发_第5张图片

9.服务注册成功

网关程序的开发_第6张图片

你可能感兴趣的:(广告项目)