1,新建一個SpringBoot項目,這裏命名gateway,然後導入相關依賴:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
2,新建GatewayApplication類;加上兩個註解 @EnableZuulProxy是開啓網關功能
package com.hcmony;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@EnableZuulProxy
@SpringBootApplication
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
3,配置文件
path: 我們要轉發路徑規則
serviceId:是我們要轉發路由的服務
zuul.routes.customer.path=/**
zuul.routes.customer.serviceId=springcloud-customer
eureka.client.service-url.defaultZone: http://localhost:8888/eureka/
server.port=8101
spring.application.name=springcloud-gateway
4,查看eureka中心,查看服務註冊情況
5,請求http://localhost:8101/test (這個服務其實是customer的,它的端口號是8001)
當我們請求時,他會轉發到8001端口的服務上。
這樣就完成 了一個api網關服務。
idea創建maven,spring,springmvc,mybatis,項目(二)
idea創建maven,spring,springmvc,mybatis,項目(三)
idea創建springboot項目圖文教程(四)
idea創建springboot項目圖文教程(配置文件)(五)
idea創建springcloud項目圖文教程(EurekaServer註冊中心)(六)
idea創建springcloud項目圖文教程(創建服務提供者)(七)