spring cloud 快速上手系列
系列说明:快速上手,一切从简,搭建一个简单的微服务框架,让新手可以在这个基础框架上做各种学习、研究。
04-网关 Gateway
041-空的工程
1,说明
网关最原始的,都是用nginx来实现的,速度快。但是扩展功能需要lua,很麻烦。KONG是在nginx基础上搭建的工程,一样的快和难用。
我们现在学习spring cloud gateway。这次只是搭建一个空的工程,配置最简单的路由。
说句实话,最新版的spring cloud gateway,照着目前搜出来的文章,一个都搭建不起来,有坑在里面。我的工程是实打实的可用。
2,Gateway
1) 代码目录
2) 代码内容
- pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.3</version>
<relativePath/>
</parent>
<groupId>com.hui.study.cloud</groupId>
<artifactId>StudyEurekaClient01</artifactId>
<version>1.0.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2021.0.4</spring-cloud.version>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!--Spring Cloud 的 eureka-client 起步依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--gateway-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gateway-server</artifactId>
</dependency>
</dependencies>
</project>
少了spring-cloud-gateway-server这个,编译啥都不影响,跑的有模有样的,也没啥异常。但是路由全失败,404坑死。
- application.yml
server:
port: 7201 #网关的端口号
spring:
application:
name: gateway
cloud:
gateway:
routes:
- id: Client01_route #路由的ID,没有固定规则但要求唯一,建议配合服务名
uri:
lb://Client01 #从注册中心获取服务名
predicates:
- Method=GET,POST
eureka:
client:
#表示是否将自己注册进EurekaServer
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,默认为true。
fetchRegistry: true
service-url:
#服务中心地址
defaultZone: http://localhost:7001/eureka
uri的配置,lb:// 是固定的。后面是注册中心里的Application名。我们配置最简单的predicates,全量转发GET和POST。后面再开一章,说详细一些。
- CloudGatewayApplication.java
package com.hui.study.cloud.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
/**
* 网关启动
*/
public class CloudGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(CloudGatewayApplication.class, args);
}
}
感觉这个类就是水字数的。还好文章不像小说,按字数收费。
3) 启动
注册中心正常启动,然后启动我们第一章的StudyEurekaClient01。
执行 CloudGatewayApplication.java
启动成功后,访问 http://localhost:7001
4)调用
用apifox或postman访问:http://localhost:7201/demo/d01