项目文件:下载 264KB,可以用于 对比文件 及 目录结构,提前 查看效果,上文传送地址:Eureka 服务注册中心 。
Zuul
一、为什么用网关。
我们现在有两种微服务,分别是 数据微服务 和 视图微服务。
他们有可能放在不同的 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-SERVICE
api-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
如果在访问视图微服务的时候出了 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 cause
feign.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 即可。
数据微服务:http://localhost:8026/api-data/bugs
视图微服务:http://localhost:8026/api-view/bugs
这样就可以访问 数据微服务 和 视微服务 集群 了,并且无需去记住那么多 ip地址 和 端口号 啦。