ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、视频、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
从前面对网关作用的介绍的图中可以看到,网关其实是另外一个抽象层。因此,对应到 Spring Cloud 微服务架构中,就是需要额外的启动一个服务。 * 首先将前面实验四中的三个微服务拷贝过来,并新建一个网关微服务。 ~~~bash mvn archetype:generate -DgroupId=com.shiyanlou -DartifactId=microservice-gateway-zuul -DarchetypeArtifactId=maven-archetype-quickstart ~~~ 我们先来创建一个 Zuul 网关服务。如何创建项目我相信通过前面的实验你也已经学会,这里只是着重讲解一下关键点。 * 在 Maven 的 pom.xml 中引入 Zuul 和 Eureka 的依赖: ~~~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.0.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.shiyanlou</groupId> <artifactId>microservice-gateway-zuul</artifactId> <version>0.0.1-SNAPSHOT</version> <name>microservice-gateway-zuul</name> <description>microservice-gateway-zuul</description> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> <!-- 引入spring cloud的依赖,不能少,主要用来管理Spring Cloud生态各组件的版本 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.SR2</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <!-- 添加spring-boot的maven插件,不能少,打jar包时得用 --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> ~~~ 这里为什么要引入 Eureka 依赖呢?大家思考一下哦~~ * 在启动类`App.java`上面添加注解`@EnableZuulProxy`: ~~~java package com.shiyanlou; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; @SpringBootApplication @EnableDiscoveryClient @EnableZuulProxy public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } } ~~~ * 在`application.yml`中添加相关配置: ~~~yml server: port: 9000 spring: application: name: microservice-gateway-zuul eureka: client: service-url: defaultZone: http://localhost:8080/eureka/ ~~~ 到这里,我们的网关服务就写好了,是不是很简单。接下来我们开始测试。