本机IP为 192.168.1.102
1. 新建 Maven 项目 zuul
2. pom.xml
4.0.0 com.java zuul 1.0.0-SNAPSHOT ${project.artifactId} 1.8 2.0.0.RELEASE org.springframework.boot spring-boot-starter-parent 2.0.5.RELEASE org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-netflix-zuul ${spring.cloud.version} org.springframework.cloud spring-cloud-starter-netflix-eureka-client ${spring.cloud.version} org.springframework.boot spring-boot-starter-actuator org.springframework springloaded 1.2.8.RELEASE org.springframework.boot spring-boot-devtools ${project.artifactId} org.apache.maven.plugins maven-compiler-plugin org.springframework.boot spring-boot-maven-plugin repackage org.apache.maven.plugins maven-resources-plugin $
3. application.yml
server: port: 10000 spring: application: name: zuul eureka: client: service-url: defaultZone: http://192.168.1.102:8080/eureka instance: instance-id: zuul.java.com prefer-ip-address: true zuul: #prefix: /java ignored-services: "*" #ignored-services: microservice routes: microservice.serviceId: microservice microservice.path: /service/** info: app.name: zuul app.update: 2018-11-10 build.groupId: $project.groupId$ build.artifactId: $project.artifactId$ build.version: $project.version$
4. ZuulStarter.java
package com.java.zuul;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.builder.SpringApplicationBuilder;import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;import org.springframework.cloud.netflix.zuul.EnableZuulProxy;@EnableZuulProxy@SpringBootApplicationpublic class ZuulStarter extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(ZuulStarter.class, args); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(ZuulStarter.class); }}
5. 运行测试
启动 Eureka 服务注册中心,参考
启动 MicroService 微服务, 参考
启动 Zuul服务,运行 ZuulStarter.java
浏览器输入URL
http://192.168.1.102:10000/service/getHostMessage/hello
返回数据如下:
{"hostname":"F6RK2EXYAFARPPS","hostAddress":"192.168.1.102","id":"hello"}
截图如下:
搭建成功!
查看Eureka 服务注册情况,输入Eureka服务URL
http://192.168.1.102:8080/
截图如下:
.