springcloud學習之路: (三) springcloud集成Zuul網關

網關就是作一下過濾或攔截操做 讓咱們的服務更加安全 用戶訪問咱們服務的時候就要先經過網關 而後再由網關轉發到咱們的微服務

1. 新建一個網關服務Modulejava

 2. 依然選擇springboot工程spring

 3. 老規矩起個名字api

 4. 勾選註冊中心客戶端安全

 5. 勾選zuul網關模塊springboot

 6. 編寫配置文件服務器

server:
  # 服務端口號
  port: 8085
spring:
  application:
    # 服務名稱 - 服務之間使用名稱進行通信
    name: service-zuul
eureka:
  client:
    service-url:
      # 填寫註冊中心服務器地址
      defaultZone: http://localhost:8081/eureka
zuul:
  routes:
    # 設置服務a 路徑名稱 隨便起
    service-a:
      path: /service-a/**
      # 這裏寫a服務的註冊名字
      serviceId: service-objcat-a
    # 設置服務b 路徑名稱 隨便起
    service-b:
      path: /service-b/**
      # 這裏寫b服務的註冊名字
      serviceId: service-objcat-b

 7. 建立一個包 名字是com.objcat.filter, 建立一個類TokenFilter繼承ZuulFilter 用來實現過濾規則app

  8. 重寫ZuulFilter中的方法,在run()方法中編寫過濾邏輯ide

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

public class TokenFilter extends ZuulFilter {
    /**
     * 過濾器類型 pre表示在請求以前進行邏輯操做
     */
    @Override
    public String filterType() {
        return "pre";
    }

    /**
     * 過濾器執行順序
     * 當一個請求在同一個階段存在多個過濾器的時候 過濾器的執行順序
     */
    @Override
    public int filterOrder() {
        return 0;
    }

    /**
     * 是否開啓過濾
     */
    @Override
    public boolean shouldFilter() {
        return true;
    }

    /**
     * 編寫過濾器攔截業務邏輯代碼
     */
    @Override
    public Object run() {
        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletRequest request = currentContext.getRequest();
        String token = request.getParameter("token");
        if (token == null) {
            currentContext.setSendZuulResponse(false);
            currentContext.setResponseBody("token is null");
            currentContext.setResponseStatusCode(401);
        }
        return null;
    }
}

邏輯很簡單 就是校驗客戶端發來的請求token是否爲空 若是爲空就不能經過 返回 token is null微服務

9. 配置網關入口文件, 這個地方千萬不要忘記實例化出來filter不然不生效url

import com.example.servicezuul.filter.TokenFilter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
// 添加註解聲明是註冊中心客戶端
@EnableEurekaClient
// 開啓網管
@EnableZuulProxy
public class ServiceZuulApplication {

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

    // 實例化tokenfilter,不然網管不生效
    @Bean
    TokenFilter tokenFilter(){
        return new TokenFilter();
    }

}

10. 經過網關訪問a服務

只須要使用 網關的地址 + 網關的端口號 + 服務的別名路徑(配置文件中配置) + api名稱 就能夠訪問了

http://localhost:8085/service-a/testA

http://localhost:8085/service-a/testA?token=123

 

 當沒有token的時候返回就是 token is null,當token有值的時候就能夠正常進行訪問了

這種網關轉發以後的請求 就叫作反向代理你能夠隱藏你本地的服務器的真實地址
只暴露給外界網關的地址 而後由網關轉發給服務器 從而作到安全性更高
相關文章
相關標籤/搜索