在微服务架构中,需要几个基础的服务治理组件,包括服务注册与发现、服务消费、负载均衡、熔断器、智能路由、配置管理等等。由这几个基础组件相互协作,共同组建了一个简单的微服务系统。
一个简单的微服务系统如下:
在Spring Cloud微服务系统中,一种常见的负载均衡方式是,客户端的请求首先经过负载均衡(Zuul、Nginx),在到达服务网关(Zuul集群),然后再到具体的服务, 服务统一注册到高可用的服务注册中心集群,服务的所有配置文件由配置服务管理,配置服务的配置文件放到Git仓库,方便开发人员随时更改配置。
Zuul简介
Zuul的主要功能是路由转发和过滤器,路由功能是微服务的一部分,比如/api/user
转发到User服务,/api/shop
转发到Shop服务。Zull默认和Ribbon结合实现了负载均衡功能。
创建路由网关
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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springcloud.bh</groupId>
<artifactId>zuul</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>zuul</name>
<description>统一路由网关Zuul</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
</properties>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
主要是增加了Zuul的依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
Application
增加@EnableZuulProxy
注解,开启Zuul功能
package com.springcloud.bh;
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);
}
}
application.yml
- 设置端口号为:
8766
- 增加Zuul配置 ```yaml server: port: 8766
spring: application: name: zuul
eureka: client: service-url: defaultZone: http://localhost:8761/eureka/
zuul: routes: api-a: path: /api/a/** serviceId: eureka-consumer-ribbon api-b: path: /api/b/** serviceId: eureka-consumer-feign
路由说明:
* 以`/api/a`开头的请求都转发给`eureka-consumer-ribbon`服务
* 以`/api/b`开头的请求都转发给`eureka-consumer-feign`服务
## 测试访问
打开浏览器访问:http://localhost:8766:/api/a/hi?message=hi,Ribbon 浏览器显示
> 你的消息是:hi,Ribbon 端口号:8763
打开浏览器访问:http://localhost:8766:/api/b/hi?message=hi,Feign 浏览器显示
> 你的消息是:hi,Feign 端口号:8763
至此说明Zuul的路由功能配置成功。
## 配置网关路由失败时的回调
```java
package com.springcloud.bh.provider;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
@Component
public class AdminFeignFallbackProvider implements FallbackProvider {
@Override
public String getRoute() {
return "eureka-consumer-feign";
}
/**
* 如果请求服务失败,则返回指定信息给调用者
* @param route
* @param cause
* @return
*/
@Override
public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
return new ClientHttpResponse() {
/**
* 网关向Api服务请求失败了,但是消费者客户端向网关发起的请求是成功的,
* 不应该把Api的404,500等问题抛给客户端
* 网关和Api服务集群对于客户端来说是黑盒
* @return
* @throws IOException
*/
@Override
public HttpStatus getStatusCode() throws IOException {
return HttpStatus.OK;
}
@Override
public int getRawStatusCode() throws IOException {
return HttpStatus.OK.value();
}
@Override
public String getStatusText() throws IOException {
return HttpStatus.OK.getReasonPhrase();
}
@Override
public void close() {
}
@Override
public InputStream getBody() throws IOException {
ObjectMapper objectMapper=new ObjectMapper();
Map<String,Object> map=new HashMap<>();
map.put("status",200);
map.put("message","无法连接,请检查您的网络");
return new ByteArrayInputStream(objectMapper.writeValueAsString(map).getBytes("UTF-8"));
}
@Override
public HttpHeaders getHeaders() {
HttpHeaders httpHeaders=new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
return null;
}
};
}
}
当网关路由失败时,则自动执行此回调。
源码地址
Github: https://github.com/yeshang5/springcloud-example