灰度一般指的就是灰度發(fā)布,在微服務(wù)中,當(dāng)有新的服務(wù)上線,一般都會選擇將少量請求分配給新服務(wù)上,其他的請求分配給就得服務(wù)。等到新服務(wù)的測試通過,再將服務(wù)進(jìn)行統(tǒng)一分配。下面,我給大家講講在 SpringCloud 中是如何使用 Ribbon 工具和 Zuul 來完成網(wǎng)關(guān)灰度的實(shí)現(xiàn)。
Eureka默認(rèn)集成了Ribbon,所以Ribbon的灰度實(shí)現(xiàn)原理就是借助服務(wù)注冊到Eureka中的eureka.instance.metadata-map的內(nèi)容來進(jìn)行匹配的。
Zuul網(wǎng)關(guān)的灰度實(shí)現(xiàn)也是借助了一個(gè)Ribbon的插件來實(shí)現(xiàn),相對比較簡單。
項(xiàng)目環(huán)境說明:有兩個(gè)eureka的服務(wù)端(eureka-server),有兩個(gè)相同的后端服務(wù)(service-sms),有一個(gè)網(wǎng)關(guān)服務(wù)(cloud-zuul)。
1、網(wǎng)關(guān)的依賴:
<?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.3.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.kevin</groupId>
<artifactId>cloud-zuul</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>cloud-zuul</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR10</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>
<version>2.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>io.jmnarloch</groupId>
<artifactId>ribbon-discovery-filter-spring-cloud-starter</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.48</version>
</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>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
這里我們用到的就是io.jmnarloch.ribbon-discovery-filter-spring-cloud-starter 這個(gè)插件包。需要注意的是,這個(gè)包引入之后,eureka-client自身帶的ribbon很多功能就被覆蓋了,而且不能再自定義IRule規(guī)則。因?yàn)檫@個(gè)包里面會生成一個(gè)metadataAwareRule的IRule,因?yàn)閟pring默認(rèn)是單例,所以如果再自定義IRule會出現(xiàn)沖突運(yùn)行時(shí)報(bào)錯(cuò)。
2、配置application.yaml
server: port: 9100 spring: application: name: cloud-zuul datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai username: root password: root dbcp2: initial-size: 5 min-idle: 5 max-total: 10 max-wait-millis: 1000 validation-query: select 1 test-while-idle: true test-on-borrow: false test-on-return: false eureka: client: service-url: defaultZone: http://eureka-7900:7900/eureka/ mybatis: mapper-locations: classpath:mapper/*.xml
說明: 我的eureka服務(wù)端是部署在域名eureka-7900端口7900。
3、springboot啟動類中添加@EnableZuulProxy
@SpringBootApplication
@EnableZuulProxy
public class CloudZuulApplication {
public static void main(String[] args) {
SpringApplication.run(CloudZuulApplication.class, args);
}
}
4、添加灰度過濾器
import com.kevin.dao.CommonGrayRuleDao;
import com.kevin.entity.CommonGrayRule;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import io.jmnarloch.spring.cloud.ribbon.support.RibbonFilterContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
//加入spring的管理
@Component
public class GrayFilter extends ZuulFilter {
@Override
public String filterType() {
return FilterConstants.ROUTE_TYPE;
}
@Override
public int filterOrder() {
return 0;
}
@Override
public boolean shouldFilter() {
//必須返回true,否則過濾器不起作用
return true;
}
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@Autowired
private CommonGrayRuleDao commonGrayRuleDao;
@Override
public Object run() throws ZuulException {
//模擬從數(shù)據(jù)庫中獲取灰度規(guī)則配置,此處根據(jù)id獲取的,實(shí)際項(xiàng)目中可以從數(shù)據(jù)庫查處所有放到緩存
CommonGrayRule commonGrayRule = commonGrayRuleDao.selectByPrimaryKey(1);
RequestContext currentContext = RequestContext.getCurrentContext();
HttpServletRequest request = currentContext.getRequest();
int userId = Integer.parseInt(request.getHeader("userId"));
//從head中拿出userid,根據(jù)數(shù)據(jù)庫中規(guī)則進(jìn)行匹配,如果灰度規(guī)則中有這個(gè)用戶就走灰度v1版本,此處模擬的是根據(jù)
//某一個(gè)具體的id獲取的值,實(shí)際根據(jù)情況來定,實(shí)際情況中灰度規(guī)則也不一定是根據(jù)用戶來
if(commonGrayRule.getId() == userId){
//核心代碼就這么一行,實(shí)現(xiàn)了灰度,這里的version與要訪問的服務(wù)的metadata-map中的key和value進(jìn)行對應(yīng)
RibbonFilterContextHolder.getCurrentContext().add("version","v1");
}
return null;
}
}
至此通過網(wǎng)關(guān)訪問service-sms服務(wù)就可以了。如果head中添加version就可以指定到具體服務(wù)上。
事前說明:兩個(gè)service-sms服務(wù)的application.yaml文件如下:
spring: profiles: sms1 application: name: service-sms server: port: 8001 eureka: client: service-url: defaultZone: http://eureka-7900:7900/eureka/ instance: metadata-map: version: v1 --- spring: profiles: sms2 application: name: service-sms server: port: 8002 eureka: client: service-url: defaultZone: http://eureka-7900:7900/eureka/ instance: metadata-map: version: v2
因?yàn)楸緳C(jī)測試,所以使用的是profiles進(jìn)行區(qū)分。
可以看到一個(gè)metadata-map設(shè)置的是version: v1 ,另一個(gè)是version: v2。
當(dāng)我們數(shù)據(jù)庫添加一條用戶id是1的數(shù)據(jù)時(shí),我們通過網(wǎng)關(guān)訪問服務(wù)的時(shí)候,head里面添加userId為1的內(nèi)容,那么這個(gè)用戶為1的請求就會一直走sms1這個(gè)服務(wù),否則會在sms1和sms2中輪詢切換。
以上就是 Spring Cloud 使用 Ribbon 和 Zuul 完成網(wǎng)關(guān)灰度實(shí)現(xiàn)的詳細(xì)內(nèi)容,想要了解更多關(guān)于 SpringCloud 網(wǎng)關(guān)灰度的其他資料請關(guān)注W3Cschool其它相關(guān)文章!