项目文件:下载 264KB,可以用于 对比文件 及 目录结构,提前 查看效果,上文传送地址:Eureka 服务注册中心 。

Zuul

  • 一、为什么用网关。
  • 二、创建子工程 linze-zuul-service 网关微服务。
    • 2.1 工程创建。
    • 2.2 文件配置。
      • 2.2.0 编辑 pom.xml。
      • 2.2.1 编辑 application.yml。
      • 2.2.2 编辑 ZuulServiceApplication。

一、为什么用网关。

我们现在有两种微服务,分别是 数据微服务 和 视图微服务。
他们有可能放在不同的 ip 地址上,有可能是不同的端口。
为了访问他们,就需要记录这些地址和端口。 而地址和端口都可能会变化,这就增加了访问者的负担。
所以这个时候,我们就可以用网关来解决这个问题。
如图所示,我们只需要记住网关的地址和端口号就行了。
如果要访问数据服务,访问地址 http://ip:port/api-data/xxx 即可。
如果要访问视图服务,访问地址 http://ip:port/api-view/xxx 即可。

二、创建子工程 linze-zuul-service 网关微服务。

2.1 工程创建。


2.2 文件配置。

2.2.0 编辑 pom.xml。

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>cn.lz</groupId><artifactId>cloud</artifactId><version>0.0.1-SNAPSHOT</version></parent><artifactId>zuul-service</artifactId><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><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></dependencies></project>

2.2.1 编辑 application.yml。

server:port: 8026
eureka:client:serviceUrl:defaultZone: http://localhost:8761/eureka/
spring:application:name: linze-zuul-service
# 路由映射
zuul:routes:api-a:# 访问路径path: /api-data/**# 服务名serviceId: LINZE-DATA-SERVICEapi-b:path: /api-view/**serviceId: LINZE-VIEW-SERVICE-FEIGN

2.2.2 编辑 ZuulServiceApplication。

@EnableZuulProxy - 启用 zuul 代理。

package cn.lz.cloud.zuulservice;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
@EnableDiscoveryClient
public class ZuulServiceApplication {public static void main(String[] args) {SpringApplication.run(ZuulServiceApplication.class, args);}}

我们挨个启动,
EurekaServerApplication 服务注册中心
DataServiceApplication8001、DataServiceApplication8002 数据服务
ViewServiceFeignApplication8011、ViewServiceFeignApplication8012 视图服务
ZuulServiceApplication 网关服务

启动完成后:访问 http://localhost:8761/ 可以看到我们所有注册的服务。


数据微服务:http://localhost:8026/api-data/bugs
视图微服务:http://localhost:8026/api-view/bugs

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ↓ 无错可忽略,路径问题 start ↓ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

如果在访问视图微服务的时候出了 404 路径错误 引起的 500 代码错误,就要检查 BugsClientFeign 中的映射路径啦。

错误信息如下:
html

java

},Server stats: [[Server:SKY20180425NCD:8001;    Zone:defaultZone;   Total Requests:0;   Successive connection failure:0;    Total blackout seconds:0;   Last connection made:Thu Jan 01 08:00:00 CST 1970;  First connection made: Thu Jan 01 08:00:00 CST 1970;    Active Connections:0;   total failure count in last (1000) msecs:0; average resp time:0.0;  90 percentile resp time:0.0;    95 percentile resp time:0.0;    min resp time:0.0;  max resp time:0.0;  stddev resp time:0.0]
, [Server:SKY20180425NCD:8002;  Zone:defaultZone;   Total Requests:0;   Successive connection failure:0;    Total blackout seconds:0;   Last connection made:Thu Jan 01 08:00:00 CST 1970;  First connection made: Thu Jan 01 08:00:00 CST 1970;    Active Connections:0;   total failure count in last (1000) msecs:0; average resp time:0.0;  90 percentile resp time:0.0;    95 percentile resp time:0.0;    min resp time:0.0;  max resp time:0.0;  stddev resp time:0.0]
]}ServerList:org.springframework.cloud.netflix.ribbon.eureka.DomainExtractingServerList@b6c437e
2019-11-19 13:38:14.296 ERROR 544316 --- [nio-8011-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is feign.FeignException$NotFound: status 404 reading BugsClientFeign#listBugs()] with root causefeign.FeignException$NotFound: status 404 reading BugsClientFeign#listBugs()at feign.FeignException.clientErrorStatus(FeignException.java:165) ~[feign-core-10.4.0.jar:na]......at java.base/java.lang.Thread.run(Thread.java:835) ~[na:na]

解决方式如下:
注:本文错误是因为上章的 /bc 忘记去掉啦。如下图:去掉 /bc 即可。
原文件

@RequestMapping("bc")
@FeignClient("LINZE-DATA-SERVICE")
public interface BugsClientFeign {@GetMapping("bc/bugs")List<Bugs> listBugs();}

新文件

@FeignClient("LINZE-DATA-SERVICE")
public interface BugsClientFeign {@GetMapping("bugs")List<Bugs> listBugs();}

修改完文件后,重启 ViewServiceFeignApplication8011、ViewServiceFeignApplication8012 即可。

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ↑ 无错可忽略,路径问题 end ↑ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

数据微服务:http://localhost:8026/api-data/bugs
视图微服务:http://localhost:8026/api-view/bugs

这样就可以访问 数据微服务 和 视微服务 集群 了,并且无需去记住那么多 ip地址 和 端口号 啦。

本文到此结束,2019.11.19,下一篇 [ Zipkin 链路追踪 ] ,待续 。。。。。。

SpringCloud 之 Zuul 网关(二)相关推荐

  1. SpringCloud 之 Zuul 网关搭建及配置

    点击上方蓝色"方志朋",选择"设为星标" 回复"666"获取独家整理的学习资料! 作者:Anakki blog.csdn.net/qq_29 ...

  2. properties 配置回车_非常全面的讲解SpringCloud中Zuul网关原理及其配置,看它就够了!...

    本文同步Java知音社区,专注于Java 作者:kosamino http://www.cnblogs.com/jing99/p/11696192.html Zuul是spring cloud中的微服 ...

  3. 非常全面的讲解SpringCloud中Zuul网关原理及其配置,看它就够了!

    作者:kosamino www.cnblogs.com/jing99/p/11696192.html Zuul是spring cloud中的微服务网关.网关:是一个网络整体系统中的前置门户入口.请求首 ...

  4. properties 配置回车_非常全面的讲解SpringCloud中Zuul网关原理及其配置,看它就够了! - 风平浪静如码

    Zuul是spring cloud中的微服务网关.网关:是一个网络整体系统中的前置门户入口.请求首先通过网关,进行路径的路由,定位到具体的服务节点上. Zuul是一个微服务网关,首先是一个微服务.也是 ...

  5. 全面的讲解SpringCloud中Zuul网关原理及其配置,看它就够了

    Zuul是spring cloud中的微服务网关.网关:是一个网络整体系统中的前置门户入口.请求首先通过网关,进行路径的路由,定位到具体的服务节点上. Zuul是一个微服务网关,首先是一个微服务.也是 ...

  6. SpringCloud之Zuul网关服务

    Zuul是spring cloud中的微服务网关.网关: 是一个网络整体系统中的前置门户入口.请求首先通过网关,进行路径的路由,定位到具体的服务节点上. Zuul是一个微服务网关,首先是一个微服务.也 ...

  7. 【SpringCloud】zuul:网关

    我们使用Spring Cloud Netflix中的Eureka实现了服务注册中心以及服务注册与发现:而服务间通过Ribbon或Feign实现服务的消费以及均衡负载.为了使得服务集群更为健壮,使用Hy ...

  8. SpringCloud之Zuul网关

    周末订了四本书, 将微服务,SPRINGCLOUD,架构师知识点重新整理一下. zuul作为反向代码网关,总入口,可以串起很多知识的. package MasterSpringBoot;import ...

  9. SpringCloud之Zuul网关控制(Finchley版本)

    Zuul 的主要功能是路由转发和过滤器.路由功能是微服务的一部分,比如 /api/user 转发到到 User 服务,/api/shop 转发到到 Shop 服务.Zuul 默认和 Ribbon 结合 ...

最新文章

  1. 转载-致IT同仁 —— IT人士常犯的17个职场错误
  2. PHP安装加载yaf扩展
  3. ios审核被拒3.2.1问题总结
  4. 完全禁用Microsoft Compatibility Telemetry
  5. 网络协议及IP地址分类
  6. c++ map底层_深入浅出Redisredis底层数据结构(上)
  7. 什么是SQL Server数据字典?为什么要创建一个?
  8. Spring Cloud 异常处理
  9. 从底层分析python中深拷贝和浅拷贝区别
  10. 漫谈TCP-AIMD/BBR的公平性以及buffer bloat
  11. 在线书签 php源码_PHP在线书签系统分享
  12. 学而思css动画使用
  13. 寄给J.Keisler教授的一份电子生日贺卡(修正版)
  14. pdf转换器免费版下载
  15. 1、ipywidgets
  16. stm32配置wifi
  17. frequency bins解释
  18. 用python爬取实时基金估值
  19. 大学的python选修课好学吗_中国大学MOOC(慕课)_用Python玩转数据_章节考试选修课答案...
  20. 功能测试中遇到不可重现软件缺陷的解决策略

热门文章

  1. 【角蜂鸟学习笔记1】(在MacOS系统安装的艰难过程)
  2. android 录屏 github,GitHub - mabeijianxi/ScreenRecordPushStream: Android 录屏推流demo
  3. 推荐一款好用的手机思维导图app
  4. 事件触发航天器编队调研
  5. Windows 快捷键 Windows Keyboard Shortcuts
  6. CMOS 图像传感器——Color Filter Array
  7. Omni Recover 1.3.5 Ios数据恢复工具 http://www.macxiazai.cn/downloads/omni-recover-1-3-5/
  8. 如何全自动下载知乎上的视频到本地
  9. python alpha策略_从零开始学量化:04阿尔法策略
  10. 编程对小学生重要吗?看完小学生机器人编程学习线路图就知道了