1、在新的SpringBoot项目中的pom.xml引入如下依赖:
<!-- 引入Zuul starter --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter</artifactId> </dependency> <!-- 连接Eureka --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
2、在pom.xml的project中加入SpringCloud的版本管理配置:
<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>
3、编辑配置文件application.propertites:
spring.application.name = zero4j-zuul-gateway eureka.client.serviceUrl.defaultZone = http://admin:123456@localhost:8761/eureka/ eureka.instance.instance-id = ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port} eureka.instance.prefer-ip-address = true #只要访问以/api/v1/areas开头的多层目录都可以路由到服务名为zero4j-provider-area的服务上. zuul.routes.zero4j-provider-area.path = /api/v1/areas zuul.routes.zero4j-provider-area.service-id = zero4j-provider-area zuul.routes.zero4j-provider-area.stripPrefix = false server.port = 4000
4、最后在application启动类中加入@EnableDiscoveryClient和@EnableZuulProxy注解:
package com.zero4j; 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; @EnableDiscoveryClient @EnableZuulProxy @SpringBootApplication public class Zero4jZuulGatewayApplication { public static void main(String[] args) { SpringApplication.run(Zero4jZuulGatewayApplication.class, args); } }