ShardingSphere应用专题--4.1.1版本--服务治理(十四)

前言

1.基础的环境介绍请移步ShardingSphere应用专题–4.1.1版本–sharding jdbc环境搭建(四)
你可以同时打开两个页面,避免因查找原始配置上下翻动。

2.ShardingSphere官方文档更新不及时,很容易踩坑。贴出的4.x版本文档实际是4.0.1版本的,如果你准备使用该版本可以参考官方文档。本文使用的是此时最新的正式版本4.1.1版本,配置与官方文档配置不同。事实上,源码中有非常详细的版本配置文档,本文也是参考4.1.1 tag的源码配置。

3.ShardingSphere各版本差异很大,甚至核心依赖的包名都不一样,使用时,一定要确认使用哪个版本,目前调研的结果:3.x、4.0.1、4.1.1、5.0.0-alpha都存在很大的配置差异

服务治理介绍

1.配置中心

(1)实现动机
  • 配置集中化:越来越多的运行时实例,使得散落的配置难于管理,配置不同步导致的问题十分严重。将配置集中于配置中心,可以更加有效进行管理。

  • 配置动态化:配置修改后的分发,是配置中心可以提供的另一个重要能力。它可支持数据源、表与分片及读写分离策略的动态切换。

(2)配置中心数据结构

配置中心在定义的命名空间的config下,以YAML格式存储,包括数据源,数据分片,读写分离、Properties配置,可通过修改节点来实现对于配置的动态管理

config
    ├──authentication                            # Sharding-Proxy权限配置
    ├──props                                     # 属性配置
    ├──schema                                    # Schema配置
    ├      ├──sharding_db                        # SchemaName配置
    ├      ├      ├──datasource                  # 数据源配置
    ├      ├      ├──rule                        # 数据分片规则配置
    ├      ├──masterslave_db                     # SchemaName配置
    ├      ├      ├──datasource                  # 数据源配置
    ├      ├      ├──rule                        # 读写分离规则
(3)动态生效

在注册中心上修改、删除、新增相关配置,会动态推送到生产环境并立即生效

2.配置中心

(1)实现动机
  • 相对于配置中心管理配置数据,注册中心存放运行时的动态/临时状态数据,比如可用的proxy的实例,需要禁用或熔断的datasource实例。

  • 通过注册中心,可以提供熔断数据库访问程序对数据库的访问和禁用从库的访问的编排治理能力。治理仍然有大量未完成的功能(比如流控等)

(2)注册中心数据结构

注册中心在定义的命名空间的state下,创建数据库访问对象运行节点,用于区分不同数据库访问实例。包括instances和datasources节点。

instances
    ├──your_instance_ip_a@-@your_instance_pid_x
    ├──your_instance_ip_b@-@your_instance_pid_y
    ├──....
datasources
    ├──ds0
    ├──ds1
    ├──....

Sharding-Proxy支持多逻辑数据源,因此datasources子节点的名称采用schema_name.data_source_name的形式。

instances
    ├──your_instance_ip_a@-@your_instance_pid_x
    ├──your_instance_ip_b@-@your_instance_pid_y
    ├──....
datasources
    ├──sharding_db.ds0
    ├──sharding_db.ds1
    ├──....

3.支持的开源产品

(1)Zookeeper

ShardingSphere官方使用Apache Curator作为Zookeeper的实现方案(支持配置中心和注册中心)。
请使用Zookeeper 3.4.6及其以上版本,详情请参见官方网站

(2)Etcd

ShardingSphere官方使用io.etcd/jetcd作为Etcd的实现方案(支持配置中心和注册中心)。
请使用Etcd v3以上版本,详情请参见官方网站

(3)Apollo

ShardingSphere官方使用Apollo Client作为Apollo的实现方案(支持配置中心)。
请使用Apollo Client 1.5.0及其以上版本,详情请参见官方网站

(4)Nacos

ShardingSphere官方使用Nacos Client作为Nacos的实现方案(支持配置中心)。
请使用Nacos Client 1.0.0及其以上版本,详情请参见官方网站

(5)基于SPI自定义扩展

Service Provider Interface (SPI)是一种为了被第三方实现或扩展的API。它可以用于实现框架扩展或组件替换。

ShardingSphere在数据库治理模块使用SPI方式载入数据到配置中心/注册中心,进行实例熔断和数据库禁用。
目前,ShardingSphere内部支持Zookeeper和etcd这种常用的配置中心/注册中心。
此外,您可以使用其他第三方配置中心/注册中心,并通过SPI的方式注入到ShardingSphere,从而使用该配置中心/注册中心,实现数据库治理功能。
关于基于SPI如何扩展可以参考加密扩展及分布式主键的SPI扩展:

使用Zookeeper实现服务治理

原因

  • 本身官方支持的既支持注册中心又支持配置中心的组件目前就Zookeeper和Etcd,且后面用到的Sharding-UI(提供可视化操作界面的平台,具体参阅:ShardingSphere应用专题–4.1.1版本–Sharding-UI的使用(十五)仅支持zookeeper。为了整个链路方便测试,选择使用zookeeper作为测试demo,其他的实现组件可以参考本文自行探索

1.zookeeper

(1)zookeeper下载

去官网的下载页面(需要翻墙),或者自行找国内资源下载
在这里插入图片描述
本次使用的时3.6.2,官方推荐使用3.6.x

(2)启动zookeeper

cd到zookeeper的bin目录下,执行

zkServer start

在这里插入图片描述
关于zookeeper的安装及使用,如果使用的是windows,或者不熟悉zookeeper的使用,自行百度解决

2.pom依赖

 <!-- https://mvnrepository.com/artifact/org.apache.shardingsphere/sharding-jdbc -->
        <!--        <dependency>-->
        <!--            <groupId>org.apache.shardingsphere</groupId>-->
        <!--            <artifactId>sharding-jdbc-spring-boot-starter</artifactId>-->
        <!--            <version>${sharding-version}</version>-->
        <!--        </dependency>-->
        <!-- 如果使用sp-distributed 服务治理环境,需引入该依赖,并关闭上面sharding-jdbc-spring-boot-starter的依赖-->
        <dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>sharding-jdbc-orchestration-spring-boot-starter</artifactId>
            <version>${sharding-version}</version>
        </dependency>

        <!-- 如果使用sp-distributed 服务治理环境,且使用zookeeper作为配置或注册中心,需引入该依赖,并关闭上面sharding-jdbc-spring-boot-starter的依赖-->
        <dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>sharding-orchestration-center-zookeeper-curator</artifactId>
            <version>${sharding-version}</version>
        </dependency>

        <!-- 如果使用nacos作为配置中心,打开该依赖 注意,nacos官方仅支持配置中心,不支持注册中心-->
        <!--        <dependency>-->
        <!--            <groupId>org.apache.shardingsphere</groupId>-->
        <!--            <artifactId>sharding-orchestration-center-nacos</artifactId>-->
        <!--            <version>${sharding-version}</version>
                </dependency>-->

注意:分布式治理的依赖starter为:sharding-jdbc-orchestration-spring-boot-starter
和主从,分库分表等使用的sharding-jdbc-spring-boot-starter不一样

3.新增application-sp-distributed.properties配置

#打印sql
spring.shardingsphere.props.sql.show=true
#设置单次请求可适用的最大线程数,以决定是线程限制还是内存限制。增大该参数可提高数据库元数据加载速度(默认为1)
spring.shardingsphere.props.max.connections.size.per.query=3

spring.shardingsphere.datasource.names=master0,slave0,master1,slave1

spring.shardingsphere.datasource.master0.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.master0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.master0.jdbc-url=jdbc:mysql://localhost:4406/mydb0?characterEncoding=utf-8
spring.shardingsphere.datasource.master0.username=root
spring.shardingsphere.datasource.master0.password=111

spring.shardingsphere.datasource.slave0.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.slave0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.slave0.jdbc-url=jdbc:mysql://localhost:5506/mydb0?characterEncoding=utf-8
spring.shardingsphere.datasource.slave0.username=root
spring.shardingsphere.datasource.slave0.password=111

spring.shardingsphere.datasource.master1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.master1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.master1.jdbc-url=jdbc:mysql://localhost:4406/mydb1?characterEncoding=utf-8
spring.shardingsphere.datasource.master1.username=root
spring.shardingsphere.datasource.master1.password=111

spring.shardingsphere.datasource.slave1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.slave1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.slave1.jdbc-url=jdbc:mysql://localhost:5506/mydb1?characterEncoding=utf-8
spring.shardingsphere.datasource.slave1.username=root
spring.shardingsphere.datasource.slave1.password=111


spring.shardingsphere.sharding.default-data-source-name=master0
#根据id分表
spring.shardingsphere.sharding.tables.bill.actual-data-nodes=master$->{0..1}.bill_$->{0..1}
spring.shardingsphere.sharding.tables.bill.table-strategy.inline.sharding-column=id
spring.shardingsphere.sharding.tables.bill.table-strategy.inline.algorithm-expression=bill_$->{id % 2}

#根据amount分库
spring.shardingsphere.sharding.tables.bill.database-strategy.inline.sharding-column=bill_amount
spring.shardingsphere.sharding.tables.bill.database-strategy.inline.algorithm-expression=master$->{bill_amount % 2}

spring.shardingsphere.sharding.tables.bill.key-generator.column=id
spring.shardingsphere.sharding.tables.bill.key-generator.type=SNOWFLAKE
spring.shardingsphere.sharding.tables.bill.key-generator.props.worker.id=123

#指定master0为主库,slave0为它的从库
spring.shardingsphere.sharding.master-slave-rules.master0.master-data-source-name=master0
spring.shardingsphere.sharding.master-slave-rules.master0.slave-data-source-names=slave0
#指定master1为主库,slave1为它的从库
spring.shardingsphere.sharding.master-slave-rules.master1.master-data-source-name=master1
spring.shardingsphere.sharding.master-slave-rules.master1.slave-data-source-names=slave1

#配置
spring.shardingsphere.orchestration.registry.orchestration-type=config_center,registry_center
spring.shardingsphere.orchestration.registry.instance-type=zookeeper
spring.shardingsphere.orchestration.registry.server-lists=localhost:2181
spring.shardingsphere.orchestration.registry.namespace=sharding-namespace
spring.shardingsphere.orchestration.registry.props.overwrite=true

注意:

4.配置说明

在这里插入图片描述

  1. 数据治理实例的名字,自定义的
  2. 治理的配置类型,这里即是注册中心,又是配置中心
  3. 治理的实例类型,其他可选见:支持的开源产品
  4. 服务链接list,多个使用逗号隔开
  5. 命名空间(就是用来区分的),自定义
  6. 是否开启本地启动覆盖远程配置

这里的【命名空间】以及【数据治理实例名称】,在zookeeper中的存储结构如下:

在这里插入图片描述
这个state和config就熟悉了,state下面就是上面提到的的注册中心数据结构,config下就是上面提到的配置中心数据结构

5.总结与预告

基于zookeeper的配置中心及注册中心配置完了,对应的配置参数及注册参数也已经进入zookeeper,至于动态刷新以及配置存储的详情,就不在这里说了,下面基于ShardingSphere提供的Sharding-UI项目,直接在页面上聊聊这方面的问题。具体前往:
ShardingSphere应用专题–4.1.1版本–Sharding-UI的使用(十五)

评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值