上一篇講解了網關zuul的path-url這種轉發方式;這一篇講解一下path-serviceId這種轉發方式。path-serviceId這種方式須要使用到註冊中心eurekajava
一、新建項目sc-zuul-consumer,該項目主要提供一個Controller,兩個接口,對應的pom.xml文件以下web
<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> <groupId>spring-cloud</groupId> <artifactId>sc-zuul-consumer</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>sc-zuul-consumer</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 說明是一個 eureka client --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> </project>
二、新建配置文件application.ymlspring
server: port: 7090 spring: application: name: sc-zuul-consumer eureka: client: registerWithEureka: true #是否將本身註冊到Eureka服務中,默認爲true fetchRegistry: true #是否從Eureka中獲取註冊信息,默認爲true serviceUrl: defaultZone: http://localhost:5001/eureka/
三、新建Controllerapache
package sc.zuul.consumer.controller; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import sc.zuul.consumer.model.User; @RestController public class UserController { @RequestMapping("/user/getUser/{id}") public Map<String, Object> getUser(@PathVariable Integer id) { System.out.println("id = " + id); Map<String, Object> resp = new HashMap<String, Object>(); resp.put("code", "000000"); resp.put("msg", "success"); User u = new User(); u.setId(1); u.setPosition("cto"); u.setUsername("黃金"); resp.put("body", u); return resp; } @RequestMapping("/user/listUser") public Map<String, Object> listUser() { Map<String, Object> resp = new HashMap<String, Object>(); resp.put("code", "000000"); resp.put("msg", "success"); User u1 = new User(); u1.setId(1); u1.setPosition("cto"); u1.setUsername("黃金"); User u2 = new User(); u2.setId(2); u2.setPosition("cto"); u2.setUsername("白銀"); List<User> list = new ArrayList<User>(); list.add(u1); list.add(u2); resp.put("body", list); return resp; } }
四、新建springboot啓動類ZuulConsumerApplication.javaapi
package sc.zuul.consumer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class ZuulConsumerApplication { public static void main(String[] args) { SpringApplication.run(ZuulConsumerApplication.class, args); } }
五、先啓動註冊中心項目sc-eureka-server,再啓動sc-zuul-consumer
訪問接口:http://127.0.0.1:7090/user/listUser
訪問接口:http://127.0.0.1:7090/user/getUser/1
六、新建網關項目sc-zuul-towway,對應的pom.xml文件以下springboot
<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> <groupId>spring-cloud</groupId> <artifactId>sc-zuul-towway</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>sc-zuul-towway</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <!-- 說明是一個 eureka client --> <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.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId> <version>1.4.6.RELEASE</version> </dependency> --> </dependencies> </project>
備註:springboot2.x後spring-cloud-starter-zuul被標誌爲過時app
七、新建配置文件application.ymlmaven
server: port: 8090 spring: application: name: sc-zuul-towway # 路由規則配置 zuul: routes: user: path: /api/** serviceId: sc-zuul-consumer # API網關也將做爲一個服務註冊到eureka-server上 eureka: client: registerWithEureka: true #是否將本身註冊到Eureka服務中,默認爲true fetchRegistry: true #是否從Eureka中獲取註冊信息,默認爲true serviceUrl: defaultZone: http://localhost:5001/eureka/
八、新建啓動類ZuulApplication.javaspring-boot
package sc.zuul.towway; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; @SpringBootApplication @EnableZuulProxy @EnableEurekaClient public class ZuulApplication { public static void main(String[] args) { SpringApplication.run(ZuulApplication.class, args); } }
九、啓動項目ZuulApplication.javafetch
十、驗證
訪問註冊中心
訪問:http://127.0.0.1:8090/api/user/listUser
訪問:http://127.0.0.1:8090/api/user/getUser/1