1、创建网关模块

image.png
image.png
image.png

2、完成后的pom依赖

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.3.2.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.fcant</groupId>
  12. <artifactId>gateway_zuul</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>gateway_zuul</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. <spring-cloud.version>Hoxton.SR6</spring-cloud.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.cloud</groupId>
  23. <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.cloud</groupId>
  27. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-starter-test</artifactId>
  32. <scope>test</scope>
  33. <exclusions>
  34. <exclusion>
  35. <groupId>org.junit.vintage</groupId>
  36. <artifactId>junit-vintage-engine</artifactId>
  37. </exclusion>
  38. </exclusions>
  39. </dependency>
  40. </dependencies>
  41. <dependencyManagement>
  42. <dependencies>
  43. <dependency>
  44. <groupId>org.springframework.cloud</groupId>
  45. <artifactId>spring-cloud-dependencies</artifactId>
  46. <version>${spring-cloud.version}</version>
  47. <type>pom</type>
  48. <scope>import</scope>
  49. </dependency>
  50. </dependencies>
  51. </dependencyManagement>
  52. <build>
  53. <plugins>
  54. <plugin>
  55. <groupId>org.springframework.boot</groupId>
  56. <artifactId>spring-boot-maven-plugin</artifactId>
  57. </plugin>
  58. </plugins>
  59. </build>
  60. </project>

3、在配置文件配置Zuul的使用

注意-如果在注册中心配置了server.servlet.context-path的路径,此处配置注册中心的端口后面也要添加该路径

  1. server:
  2. port: 8762
  3. spring:
  4. application:
  5. name: GatewayZuul
  6. eureka:
  7. client:
  8. service-url:
  9. # 将网关注册到注册中心
  10. defaultZone: http://127.0.0.1:8761/eureka/

4、在启动类添加启动网关的代理注解 @EnableZuulProxy

  1. package com.fcant.gateway_zuul;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
  5. @EnableZuulProxy
  6. @SpringCloudApplication
  7. public class GatewayZuulApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(GatewayZuulApplication.class, args);
  10. }
  11. }

5、启动网关服务,在注册中心查看已经成功注册至注册中心

image.png