1. 程式人生 > >SpringCloud -- gateway 閘道器 配置

SpringCloud -- gateway 閘道器 配置

Spring Cloud Gateway

       使用IntelliJIdea建立一個消費者工程, New Project ---> 選中Spring Initializr ---> 設定包名/工程名 ---> 勾選Web、Eureka Discovery、gateway等 ---> 設定儲存路徑。

 在 入口類中增加@EnableZuulProxy

package com.springcloud.gateway;

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;


@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class GatewayApplication {

	public static void main(String[] args) {
		SpringApplication.run(GatewayApplication.class, args);
	}
}
修改配置檔案,轉發到server1客戶端  /resources/public/** 路徑下
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8080/eureka/
spring:
  application:
    name: gateway
server:
  port: 8083
zuul:
  routes:
    server1: /resources/public/**

在server1   /resources/public/ 路徑下 新建 html,結構如下:

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    我是 server1 來自閘道器轉發
</body>
</html>

啟動 Eureka註冊中心、server1客戶端、gateway閘道器

可見 訪問server1埠時,閘道器進行了轉發!