Spring Cloud分区发布实践(1) 环境准备
最近研究了一下Spring Cloud里面的灰度发布, 看到各种各样的使用方式, 真是纷繁复杂, 眼花缭乱, 不同的场景需要不同的解决思路.
那我们也来实践一下最简单的场景:
区域划分:
服务分为beta(线上预发布环境)和main主生产环境
区域隔离情况
试情况可能有三种选择:
- A. main和beta互不相通, 绝对隔离 (资源相对充裕)
- B. main和beta正常情况下不通, 缺少实例时互通 (比较简单, 但可能无法区分异常服务, 不知道访问的是那个区域)
- C. beta绝对隔离, main在发布过程中可以切换到beta (main区域只有一台机器, 资源比较紧张)
- D. 发布前标注一套区域为beta的服务, 测试通过后修改beta服务的区域为main, 多个实例都可上线服务 (资源非常紧张, 操作相对复杂)
以上4种情况适合不同的公司, 但也各有利弊.
实现分析
我们先从Spring Cloud框架的技术方面来考虑
- B 最简单, 简单配置即可实现
- A, C 自己定制一下ServiceInstanceListSupplier 可以实现
- D 和A一样, 只不过需要额外修改服务的区域设置
项目环境
- Java 8 (1.8.0_291, build低版本有个bug)
- Spring Cloud 2020.0.3
- Spring Boot 2.5.0
- Intellij Idea 2021
- Maven 项目
项目目标
- 使用Spring Cloud开发微服务
- 支持2个区域: beta, main
- 服务链保持相同区域: gateway -> Service A -> Service B
- 使用Feign调用
- 使用Spring Cloud Gateway, LoadBalancer
创建根项目
首先我们在IDE里创建一个空的Maven项目, 把项目所有模块的公共依赖放在pom.xml
<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>
<groupId>com.cnscud.betazone</groupId>
<artifactId>betazone-root</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<description>hello</description>
<url>https://www.cnscud.com</url>
<modules>
</modules>
<properties>
<spring-boot.version>2.5.0</spring-boot.version>
<spring-cloud.version>2020.0.3</spring-cloud.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!--监控 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<!--测试依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<!-- spring boot 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- spring cloud 依赖 -->
<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>
</project>
准备注册中心
然后我们来启动一个Eureka作为我们的服务注册中心.
新建一个Module, 名字为 eureka-server, 使用IDE的Spring Initializr, 也可以使用 https://start.spring.io/{:target="_blank"} 创建.

其中pom我们做一下修改, 继承父项目的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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.cnscud.betazone</groupId>
<artifactId>betazone-root</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<artifactId>eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-server</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
里面spring-cloud-starter-netflix-eureka-server 只是为了启动Eureka Server, 其他都不需要.
为了启动, 我们还需要配置一个application.yml:
# 指定运行端口
server:
port: 8001
# 指定服务名称
spring:
application:
name: eureka-server
# 指定主机地址
eureka:
instance:
hostname: localhost
client:
# 指定是否从注册中心获取服务(注册中心不需要开启)
fetch-registry: false
# 指定是否将服务注册到注册中心(注册中心不需要开启)
register-with-eureka: false
然后就可以启动了, 访问 http://localhost:8001/{:target="_blank"} 就可以看到WEB界面, http://localhost:8001/eureka/apps{:target="_blank"} 可以看到详细的xml内容, 可以用来验证metadata是否设置正确.
然后接下来我们准备用于测试的微服务...
Spring Cloud分区发布实践(1) 环境准备的更多相关文章
- Spring Cloud分区发布实践(6)--灰度服务-根据Header选择实例区域
此文是一个完整的例子, 包含可运行起来的源码. 此例子包含以下部分: 网关层实现自定义LoadBalancer, 根据Header选取实例 服务中的Feign使用拦截器, 读取Header Feign ...
- Spring Cloud分区发布实践(3) 网关和负载均衡
注意: 因为涉及到配置测试切换, 中间环节需按此文章操作体验, 代码仓库里面的只有最后一步的代码 准备好了微服务, 那我们就来看看网关+负载均衡如何一起工作 新建一个模块hello-gateway, ...
- Spring Cloud分区发布实践(5)--定制ServiceInstanceListSupplier
现在我们简单地来定制二个 ServiceInstanceListSupplier, 都是zone-preference的变种. 为了方便, 我重新调整了一下项目的结构, 把一些公用的类移动到hello ...
- Spring Cloud分区发布实践(4) FeignClient
上面看到直接通过网关访问微服务是可以实现按区域调用的, 那么微服务之间调用是否也能按区域划分哪? 下面我们使用FeignClient来调用微服务, 就可以配合LoadBalancer实现按区域调用. ...
- Spring Cloud分区发布实践(2) 微服务
我们准备一下用于查询姓名的微服务. 首先定义一下服务的接口, 新建一个空的Maven模块hello-remotename-core, 里面新建一个类: public interface RemoteN ...
- spring cloud微服务实践二
在上一篇,我们已经搭建了spring cloud微服务中的注册中心.但只有一个注册中心还远远不够. 接下来我们就来尝试提供服务. 注:这一个系列的开发环境版本为 java1.8, spring boo ...
- 厉害了,Spring Cloud Alibaba 发布 GA 版本!
? 小马哥 & Josh Long ? 喜欢写一首诗一般的代码,更喜欢和你共同 code review,英雄的相惜,犹如时间沉淀下来的对话,历久方弥新. 相见如故,@杭州. 4 月 18 日, ...
- Spring Cloud Alibaba发布第二个版本,Spring 发来贺电
还是熟悉的面孔,还是熟悉的味道,不同的是,这次的配方升级了. 今年10月底,Spring Cloud联合创始人Spencer Gibb在Spring官网的博客页面宣布:阿里巴巴开源 Spring Cl ...
- spring cloud微服务实践一
最近在学习spring框架.其中spring cloud在微服务方面很火,所以在学习过程中,也做一些记录. 注:这一个系列的开发环境版本为 java1.8, spring boot2.x, sprin ...
随机推荐
- 一次 RocketMQ 顺序消费延迟的问题定位
一次 RocketMQ 顺序消费延迟的问题定位 问题背景与现象 昨晚收到了应用报警,发现线上某个业务消费消息延迟了 54s 多(从消息发送到MQ 到被消费的间隔): 2021-06-30T23:12: ...
- nginx服务跳转
1.什么是页面跳转 将URL信息做改变 将URI信息做改变 完成伪静态配置 2.实现页面跳转的方法 http://nginx.org/en/docs/http/ngx_http_rewrite_mod ...
- MyBatis温故而知新-底层运行原理
准备工作 public class MainClass { public static void main(String[] args) throws Exception { String resou ...
- centos7安装chrome+chromeDriver+Xvfb
安装chrome 创建yum源 # cd /etc/yum.repos.d/ # vim google-chrome.repo 创建yum源信息 [google-chrome] name=google ...
- Kafka:Springboot整合Kafka消息队列
本文主要分享下Spring Boot和Spring Kafka如何配置整合,实现发送和接收来自Spring Kafka的消息. 项目结构 pom依赖包 <?xml version="1 ...
- inux下查看最消耗CPU、内存的进程
1.CPU占用最多的前10个进程: ps auxw|head -1;ps auxw|sort -rn -k3|head -10 2.内存消耗最多的前10个进程 ps auxw|head -1;ps a ...
- Game游戏分析
1.鲁棒图分析 2.系统上下文及交互方式 3.用例 4.逻辑拓扑图 5.物理拓扑图 6.时序图 7.状态图 8.物理数据模型 9.类图 10.技术选型 11.框架搭建 12.工具及通用服务 13.架构 ...
- base64文件解码
$str = str_replace(' ', '+', $str); //替换空字符串为+$str = str_replace('\n', '',$str); //置空换行符$str = str_r ...
- js定时器中引用的外部函数如何传递参数
问题:比如在一个点击事件中我需要将点击事件参数event传入到定时器中,如果只是单纯的在setTimeout(timer(e),1000)中写上括弧e,则该定时器不会等到1s才执行,而是会立即执行.那 ...
- C语言:九宫格
#include <stdio.h> /* 如下排列表示 A00 A01 A02 A10 A11 A12 A20 A21 A22 */ int main() { unsigned char ...