一、前言
本文,引入Discovery【探索】微服務架構搭建一個簡單的灰階釋出demo,Discovery微服務架構源碼即在源碼,裡面根據不同的使用者,有很多demo。如下:
1.1 Discovery【探索】微服務架構指南示例說明
- 對于入門級玩家,參考指南示例極簡版,分支為simple。涉及到指南篇裡的灰階路由和釋出的基本功能, 參考新手快速入門
- 對于熟練級玩家,參考指南示例精進版,分支為master。除上述《極簡版》功能外,涉及到指南篇裡的絕大多數進階功能
- 對于骨灰級玩家,參考指南示例進階版,分支為premium。除上述《精進版》功能外,涉及到指南篇裡的ActiveMQ、MongoDB、RabbitMQ、Redis、RocketMQ、MySQL等進階調用鍊和灰階調用鍊的整合
1.2 Discovery【探索】微服務企業級解決方案
① Discovery【探索】微服務企業級解決方案文檔
- Discovery【探索】微服務企業級解決方案(PPT版)
- Discovery【探索】微服務企業級解決方案(PDF版)
- Discovery【探索】微服務企業級解決方案(HTML版)
② Discovery【探索】微服務企業級解決方案源碼。請通路Gitee鏡像獲得最佳體驗
- 源碼Gitee同步鏡像
- 源碼Github原鏡像
③ Discovery【探索】微服務企業級解決方案指南示例源碼。請通路Gitee鏡像獲得最佳體驗
- 指南Gitee同步鏡像
- 指南Github原鏡像
裡面結合各種架構,有很多例子,供使用者參考,這裡,僅針對,我們的架構,使用gateway作為網關,nacos作為注冊中心,去除裡面的配置中心,來實作根據版本來進行全鍊路灰階釋出,demo如下文所述。
二、搭建環境
2.1 建構父工程GrayPublishDemo
2.1.1 架構整體架構圖
2.1.2 建立pom
引入discovery架構版本為6.3.2,此版本相容SpringCloud F版及以上版本
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>GrayPublishDemo</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>consumer</module>
<module>product</module>
<module>gateway</module>
</modules>
<properties>
<discovery.version>6.3.2</discovery.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.1.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.4.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
2.2 建構消費者consumer
2.2.1 consumer結構
2.2.2 pom
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>GrayPublishDemo</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>consumer</artifactId>
<dependencies>
<!-- 1.注冊中心插件 -->
<dependency>
<groupId>com.nepxion</groupId>
<artifactId>discovery-plugin-register-center-starter-nacos</artifactId>
<version>${discovery.version}</version>
</dependency>
<!-- 2.服務的政策編排插件 -->
<dependency>
<groupId>com.nepxion</groupId>
<artifactId>discovery-plugin-strategy-starter-service</artifactId>
<version>${discovery.version}</version>
</dependency>
</dependencies>
</project>
2.2.3 yaml
server:
port: 4001
spring:
application:
name: consumer
cloud:
nacos:
discovery:
metadata:
version: 1
server-addr: localhost:8848
2.2.4 主啟動
package com.best;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args);}
}
2.2.5 業務類
package com.best.controller;
import com.best.rpc.GatewayRpc;
import com.best.rpc.ProductRpc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/consumer")
public class ConsumerController {
@Value("${spring.application.name}")
private String serverName;
@Value("${server.port}")
private String serverPort;
@Value("${spring.cloud.nacos.discovery.metadata.version}")
private String version;
@Autowired
private GatewayRpc gatewayRpc;
@Autowired
private ProductRpc productRpc;
@GetMapping("/product")
public String getProduct() {
String consumer = "application name is: 【" + serverName + "】, server port is: 【" + serverPort + "】" + ", version is: 【" + version + "】";
return consumer + " || " + productRpc.getApplicationInfo();
}
@GetMapping("/gateway")
public String getGateway() {
String consumer = "application name is: 【" + serverName + "】, server port is: 【" + serverPort + "】" + ", version is: 【" + version + "】";
return consumer + " || " + gatewayRpc.getApplicationInfo();
}
}
package com.best.rpc;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
/**
* @author Create by Brian on 2019/11/22 12:24
*/
@Component
@FeignClient(name = "gateway")
public interface GatewayRpc {
@GetMapping("/gateway/service")
String getApplicationInfo();
}
package com.best.rpc;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
/**
* @author Create by Brian on 2019/11/25 14:28
*/
@Component
@FeignClient(name = "product")
public interface ProductRpc {
@GetMapping("/product/service")
String getApplicationInfo();
}
2.3 建構生産者product
2.3.1 生産者架構圖
2.3.2 pom
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>GrayPublishDemo</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>product</artifactId>
<dependencies>
<!-- 1.注冊中心插件 -->
<dependency>
<groupId>com.nepxion</groupId>
<artifactId>discovery-plugin-register-center-starter-nacos</artifactId>
<version>${discovery.version}</version>
</dependency>
<!-- 2.服務的政策編排插件 -->
<dependency>
<groupId>com.nepxion</groupId>
<artifactId>discovery-plugin-strategy-starter-service</artifactId>
<version>${discovery.version}</version>
</dependency>
</dependencies>
</project>
2.3.3 yaml
server:
port: 3001
spring:
application:
name: product
cloud:
nacos:
discovery:
metadata:
version: 2
server-addr: localhost:8848
2.3.4 主啟動
package com.best;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class ProductApplication {
public static void main(String[] args) {
SpringApplication.run(ProductApplication.class, args);
}
}
2.3.5 業務類
package com.best.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/product")
public class ProductController {
@Value("${spring.application.name}")
private String serverName;
@Value("${server.port}")
private String serverPort;
@Value("${spring.cloud.nacos.discovery.metadata.version}")
private String version;
@GetMapping("/service")
public String getApplicationInfo() {
return "application name is: 【" + serverName + "】, server port is: 【" + serverPort + "】" + ", version is: 【" + version + "】";
}
}
2.4 建構網關gateway
2.4.1 gateway結構圖
2.4.2 pom
注意這裡引入的是,網關的政策編排插件
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>GrayPublishDemo</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>gateway</artifactId>
<dependencies>
<!-- 1.注冊中心插件 -->
<dependency>
<groupId>com.nepxion</groupId>
<artifactId>discovery-plugin-register-center-starter-nacos</artifactId>
<version>${discovery.version}</version>
</dependency>
<!-- 2.網關的政策編排插件 -->
<dependency>
<groupId>com.nepxion</groupId>
<artifactId>discovery-plugin-strategy-starter-gateway</artifactId>
<version>${discovery.version}</version>
</dependency>
</dependencies>
</project>
2.4.3 yaml
server:
port: 5001
spring:
application:
name: gateway
strategy:
gateway:
header:
priority: false
cloud:
gateway:
routes:
- id: consumer
predicates:
- Path=/consumer/**
uri: lb://consumer
- id: product
predicates:
- Path=/product/**
uri: lb://product
nacos:
discovery:
metadata:
version: 1
server-addr: localhost:8848
2.4.4 主啟動
package com.best;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
2.4.5 業務類
package com.best.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/gateway")
public class GatewayController {
@Value("${spring.application.name}")
private String serverName;
@Value("${server.port}")
private String serverPort;
@Value("${spring.cloud.nacos.discovery.metadata.version}")
private String version;
@GetMapping("/service")
public String getApplicationInfo() {
return "application name is: 【" + serverName + "】, server port is: 【" + serverPort + "】" + ", version is: 【" + version + "】";
}
}
三、利用postman根據header測試灰階路由
3.1 配置啟動
啟動6個服務
3.2 未加入版本測試
不同send,版本1版本2,輪詢通路
3.3 不同服務版本測試
3.3.1 版本路由
header中加入n-d-version:{"consumer":"1","product":"2"}
consumer通路版本1,product通路版本2
3.3.2 版本+權重
header中加入n-d-version-weight: {"consumer":"1=90;2=10", "product":"1=20;2=80"}
實作consumer90%的流量進入版本1,10%流量進入版本2;product 20%的流量進入版本1,80%的流量進入版本2
3.4 整體服務版本測試
3.4.1 根據版本
如果所有服務,統一路由到哪個版本,可以簡化為
n-d-version: 2
四、根據nacos配置測試灰階路由
4.1 加入配置
gitee源碼