多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
**1. 构建路由模块:cloud-zuul-gateway-9528** **2. 在当前模块的`pom.xml`中添加 eureka-client 和 zuul 依赖** ```xml <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> ``` **3. 当前模块的`resources/application.yml`** ```yml server: port: 9528 spring: application: name: cloud-zuul-gateway eureka: client: service-url: #将当前模块注册到Eureka中 defaultZone: http://www.eureka7001.com:7001/eureka/ instance: instance-id: ${project.artifactId} prefer-ip-address: true ``` **4. 在当前模块的启动类上添加注解 `@EnableZuulProxy`** ```java @SpringBootApplication @EnableZuulProxy public class ZuulAppMain9528 { public static void main(String[] args) { SpringApplication.run(ZuulAppMain9528.class, args); } } ``` **5. 测试** 启动 Eureka 注册中心 cloud-eureka-server7001、cloud-provider-payment8001 服务端、 cloud-zuul-gateway-9528 网关。 <br/> (1)访问 Eureka 注册中心 http://www.eureka7001.com:7001/ ,可以看到 8001 服务端、网关模块已经注册了。 ![](https://img.kancloud.cn/dc/0b/dc0b33f1f4d4504799b52d8eca7428cd_1533x223.jpg) (2)不通过网关访问 8001 服务端 http://localhost:8001/payment/idport ,得到的响应如下。 ```json instanceId:cloud-provider-payment8001,serverPort:8001 ``` (3)通过网关访问 8001 服务端 http://localhost:9528/cloud-payment-service/payment/idport 也得到如下响应。可见也访问成功! ```json instanceId:cloud-provider-payment8001,serverPort:8001 ``` url地址写法: ``` http://zuul-hostname:zuul-port/application-name/要访问的url application-name:为服务端的 spring.application.name 的配置 ```