Public address: java paradise
The previous article explained the path URL forwarding method of zuul gateway; this article explained the path serviceid forwarding method. Path serviceid needs to be used in the registration center eureka
1. Create a new project SC zuul consumer, which mainly provides one Controller and two interfaces. The corresponding pom.xml file is as follows
<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> <!-- Description is a eureka client --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> </project>
2. Create a new configuration file application.yml
server: port: 7090 spring: application: name: sc-zuul-consumer eureka: client: registerWithEureka: true #Whether to register yourself in Eureka service, the default is true fetchRegistry: true #Whether to get the registration information from Eureka? true by default serviceUrl: defaultZone: http://localhost:5001/eureka/
3. New Controller
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("gold"); 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("gold"); User u2 = new User(); u2.setId(2); u2.setPosition("cto"); u2.setUsername("silver"); List<User> list = new ArrayList<User>(); list.add(u1); list.add(u2); resp.put("body", list); return resp; } }
4. Create a new springboot startup class ZuulConsumerApplication.java
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); } }
5. Start the registry project SC Eureka server, and then SC zuul consumer
Provider: http://127.0.0.1:7090/user/listUser
Provider: http://127.0.0.1:7090/user/getUser/1
6. Create a new Gateway project SC zuul towway. The corresponding pom.xml file is as follows
<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> <!-- Description is a 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>
Note: spring cloud starter Zul is marked expired after springboot2.x
7. Create a new configuration file application.yml
server: port: 8090 spring: application: name: sc-zuul-towway #Routing rule configuration zuul: routes: user: path: /api/** serviceId: sc-zuul-consumer #API gateway will also be registered as a service on Eureka server eureka: client: registerWithEureka: true #Whether to register yourself in Eureka service, the default is true fetchRegistry: true #Whether to get the registration information from Eureka? true by default serviceUrl: defaultZone: http://localhost:5001/eureka/
8. Create a new startup class ZuulApplication.java
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); } }
9. Startup project ZuulApplication.java
10, validation
Visit the registry
Visit: http://127.0.0.1:8090/api/user/listUser
Visit: http://127.0.0.1:8090/api/user/getUser/1