本文,引入Discovery【探索】微服务框架搭建一个简单的灰度发布demo,Discovery微服务框架源码即在源码,里面根据不同的使用者,有很多demo。如下:
① Discovery【探索】微服务企业级解决方案文档
② Discovery【探索】微服务企业级解决方案源码。请访问Gitee镜像获得最佳体验
③ Discovery【探索】微服务企业级解决方案指南示例源码。请访问Gitee镜像获得最佳体验
里面结合各种框架,有很多例子,供使用者参考,这里,仅针对,我们的架构,使用gateway作为网关,nacos作为注册中心,去除里面的配置中心,来实现根据版本来进行全链路灰度发布,demo如下文所述。
引入discovery框架版本为6.3.2,此版本兼容SpringCloud F版及以上版本
4.0.0
org.example
GrayPublishDemo
pom
1.0-SNAPSHOT
consumer
product
gateway
6.3.2
org.springframework.cloud
spring-cloud-dependencies
Finchley.SR2
pom
import
com.alibaba.cloud
spring-cloud-alibaba-dependencies
2.1.0.RELEASE
pom
import
org.springframework.boot
spring-boot-dependencies
2.0.4.RELEASE
pom
import
GrayPublishDemo
org.example
1.0-SNAPSHOT
4.0.0
consumer
com.nepxion
discovery-plugin-register-center-starter-nacos
${discovery.version}
com.nepxion
discovery-plugin-strategy-starter-service
${discovery.version}
server:
port: 4001
spring:
application:
name: consumer
cloud:
nacos:
discovery:
metadata:
version: 1
server-addr: localhost:8848
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);}
}
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();
}
GrayPublishDemo
org.example
1.0-SNAPSHOT
4.0.0
product
com.nepxion
discovery-plugin-register-center-starter-nacos
${discovery.version}
com.nepxion
discovery-plugin-strategy-starter-service
${discovery.version}
server:
port: 3001
spring:
application:
name: product
cloud:
nacos:
discovery:
metadata:
version: 2
server-addr: localhost:8848
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);
}
}
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 + "】";
}
}
注意这里引入的是,网关的策略编排插件
GrayPublishDemo
org.example
1.0-SNAPSHOT
4.0.0
gateway
com.nepxion
discovery-plugin-register-center-starter-nacos
${discovery.version}
com.nepxion
discovery-plugin-strategy-starter-gateway
${discovery.version}
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
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);
}
}
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 + "】";
}
}
启动6个服务
不同send,版本1版本2,轮询访问
header中加入n-d-version:{"consumer":"1","product":"2"}
consumer访问版本1,product访问版本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
如果所有服务,统一路由到哪个版本,可以简化为
n-d-version: 2
四、根据nacos配置测试灰度路由
4.1 加入配置
gitee源码