28、网关zuul入门二

简介: 这一篇讲解一下path-serviceId这种转发方式。path-serviceId这种方式需要使用到注册中心eureka

上一篇讲解了网关zuulpath-url这种转发方式;这一篇讲解一下path-serviceId这种转发方式。path-serviceId这种方式需要使用到注册中心eureka

 

1、 新建项目sc-zuul-consumer,该项目主要提供一个Controller,两个接口,对应的pom.xml文件如下


<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>


2、 新建配置文件application.yml


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/


3、 新建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("黄金");
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;
}
}


4、 新建springboot启动类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、 先启动注册中心项目sc-eureka-server,再启动sc-zuul-consumer


微信图片_20220501221144.png


访问接口:http://127.0.0.1:7090/user/listUser


微信图片_20220501221148.png


访问接口:http://127.0.0.1:7090/user/getUser/1


微信图片_20220501221152.png


6、 新建网关项目sc-zuul-towway,对应的pom.xml文件如下


<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.xspring-cloud-starter-zuul被标志为过期


微信图片_20220501221200.png


7、 新建配置文件application.yml


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/


8、 新建启动类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、 启动项目ZuulApplication.java


微信图片_20220501221211.png


10、 验证


访问注册中心


微信图片_20220501221215.png


访问:http://127.0.0.1:8090/api/user/listUser


微信图片_20220501221218.png


访问:http://127.0.0.1:8090/api/user/getUser/1

 

微信图片_20220501221221.png

 

相关文章
|
安全 前端开发 应用服务中间件
每个后端都应该了解的OpenResty入门以及网关安全实战(2)
泛型 for 循环通过一个迭代器函数来遍历所有值,类似 java 中的 foreach 语句。 Lua 编程语言中泛型 for 循环语法格式:
179 0
|
负载均衡 应用服务中间件 API
微服务技术系列教程(25) - SpringCloud- 接口网关服务Zuul
微服务技术系列教程(25) - SpringCloud- 接口网关服务Zuul
203 0
|
2月前
|
监控 负载均衡 安全
微服务(五)-服务网关zuul(一)
微服务(五)-服务网关zuul(一)
|
2月前
|
负载均衡 Java 网络架构
实现微服务网关:Zuul与Spring Cloud Gateway的比较分析
实现微服务网关:Zuul与Spring Cloud Gateway的比较分析
102 5
|
2月前
|
测试技术 微服务
微服务(八)-服务网关zuul(四)
微服务(八)-服务网关zuul(四)
|
2月前
|
监控 前端开发 Java
微服务(七)-服务网关zuul(三)
微服务(七)-服务网关zuul(三)
|
2月前
|
负载均衡 前端开发 安全
微服务(六)-服务网关zuul(二)
微服务(六)-服务网关zuul(二)
|
6月前
|
监控 Java API
springcloud5-服务网关zuul及gateway
springcloud5-服务网关zuul及gateway
157 1
springcloud5-服务网关zuul及gateway
|
6月前
|
负载均衡 Java API
|
6月前
|
负载均衡 Java API
SpringCloud - Zuul路由网关使用详解
SpringCloud - Zuul路由网关使用详解
415 0