Influx集群解决方案(Influx Proxy篇)

InFluxDB 集群搭建

本次搭建使用influx proxy

介绍

github地址:https://github.com/chengshiwen/influx-proxy/

Influx Proxy 是一个基于高可用、一致性哈希的 InfluxDB 集群代理服务,实现了 InfluxDB 高可用集群的部署方案,
具有动态扩/缩容、故障恢复、数据同步等能力。连接到 Influx Proxy 和连接原生的 InfluxDB Server 没有显著区别
(支持的查询语句列表),对上层客户端是透明的,上层应用可以像使用单机的 InfluxDB 一样使用,Influx Proxy
会处理请求的转发,并对各个 InfluxDB 集群节点进行管理。Influx Proxy 基于饿了么开源的 Influx-Proxy,
并进一步开发和优化,支持了更多的特性,移除了 Python、Redis 依赖,解决了受限于一个数据库、需要额外配置
KEYMAPS 、数据负载不均衡的问题。

架构说明

  • 在改造我们的系统中我们相当于要实现以下步骤

    在这里插入图片描述

实现步骤

Influx1.8环境

Influx1.8+Influx Proxy +SpringBoot +Ngnix

SpringBoot搭建
  • 引入依赖

    <dependency>
        <groupId>org.influxdb</groupId>
        <artifactId>influxdb-java</artifactId>
        <version>2.6</version>
    </dependency>
    
  • java代码(参考https://github.com/influxdata/influxdb-java)

    // Create an object to handle the communication with InfluxDB.
    // (best practice tip: reuse the 'influxDB' instance when possible)
    final String serverURL = "http://127.0.0.1:8086", username = "root", password = "root";
    final InfluxDB influxDB = InfluxDBFactory.connect(serverURL, username, password);
    
    // Create a database...
    // https://docs.influxdata.com/influxdb/v1.7/query_language/database_management/
    String databaseName = "NOAA_water_database";
    influxDB.query(new Query("CREATE DATABASE " + databaseName));
    influxDB.setDatabase(databaseName);
    
    // ... and a retention policy, if necessary.
    // https://docs.influxdata.com/influxdb/v1.7/query_language/database_management/
    String retentionPolicyName = "one_day_only";
    influxDB.query(new Query("CREATE RETENTION POLICY " + retentionPolicyName
            + " ON " + databaseName + " DURATION 1d REPLICATION 1 DEFAULT"));
    influxDB.setRetentionPolicy(retentionPolicyName);
    
    // Enable batch writes to get better performance.
    influxDB.enableBatch(
        BatchOptions.DEFAULTS
          .threadFactory(runnable -> {
            Thread thread = new Thread(runnable);
            thread.setDaemon(true);
            return thread;
          })
    );
    
    // Close it if your application is terminating or you are not using it anymore.
    Runtime.getRuntime().addShutdownHook(new Thread(influxDB::close));
    
    // Write points to InfluxDB.
    influxDB.write(Point.measurement("h2o_feet")
        .time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
        .tag("location", "santa_monica")
        .addField("level description", "below 3 feet")
        .addField("water_level", 2.064d)
        .build());
    
    influxDB.write(Point.measurement("h2o_feet")
        .time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
        .tag("location", "coyote_creek")
        .addField("level description", "between 6 and 9 feet")
        .addField("water_level", 8.12d)
        .build());
    
    // Wait a few seconds in order to let the InfluxDB client
    // write your points asynchronously (note: you can adjust the
    // internal time interval if you need via 'enableBatch' call).
    Thread.sleep(5_000L);
    
    // Query your data using InfluxQL.
    // https://docs.influxdata.com/influxdb/v1.7/query_language/data_exploration/#the-basic-select-statement
    QueryResult queryResult = influxDB.query(new Query("SELECT * FROM h2o_feet"));
    
    System.out.println(queryResult);
    // It will print something like:
    // QueryResult [results=[Result [series=[Series [name=h2o_feet, tags=null,
    //      columns=[time, level description, location, water_level],
    //      values=[
    //         [2020-03-22T20:50:12.929Z, below 3 feet, santa_monica, 2.064],
    //         [2020-03-22T20:50:12.929Z, between 6 and 9 feet, coyote_creek, 8.12]
    //      ]]], error=null]], error=null]
    
Ngnix搭建(搭建中)

请参考 custom.conf

服务器搭建与数据库部署

使用docker来搭建对应的influxdb信息

本地存在两台服务器(请确保influx1可以访问到influx2 同一局域网)

influx1: 192.168.137.130

influx2: 192.168.137.131

  1. docker-compose代码

    version: "3.5"
    
    services:
    
      influx-proxy:
        image: chengshiwen/influx-proxy:latest
        container_name: influx-proxy
        ports:
          - 7076:7076
        environment:
          - TZ=Asia/Shanghai
        volumes:
          - ./proxy.json:/etc/influx-proxy/proxy.json
        restart: unless-stopped
        networks:
          - influx_net
    
      influxdb-1:
        image: influxdb:1.8
        container_name: influxdb-1
        restart: unless-stopped
        networks:
          - influx_net
        volumes:
          - ./influxdb1/influxdb.conf:/etc/influxdb/influxdb.conf
          - ./influxdb1/meta:/var/lib/influxdb/meta
          - ./influxdb1/data:/var/lib/influxdb/data
          - ./influxdb1/wal:/var/lib/influxdb/wal
        ports:
          - "8086:8086"
        command: ["influxd", "--config", "/etc/influxdb/influxdb.conf"]
    
      influxdb-2:
        image: influxdb:1.8
        container_name: influxdb-2
        restart: unless-stopped
        networks:
          - influx_net
        volumes:
          - ./influxdb2/influxdb.conf:/etc/influxdb/influxdb.conf
          - ./influxdb2/meta:/var/lib/influxdb/meta
          - ./influxdb2/data:/var/lib/influxdb/data
          - ./influxdb2/wal:/var/lib/influxdb/wal
        ports:
          - "8087:8086"
        command: ["influxd", "--config", "/etc/influxdb/influxdb.conf"]
    
    networks:
      influx_net:
    
  2. proxy.json代码

    {
        "circles": [
            {
                "name": "circle-1",
                "backends": [
                    {
                        "name": "influxdb-1-1",
                        "url": "http://192.168.137.130:8086",
                        "username": "",
                        "password": ""
                    },
                    {
                        "name": "influxdb-1-2",
                        "url": "http://192.168.137.130:8087",
                        "username": "",
                        "password": ""
                    }
                ]
            },
            {
                "name": "circle-2",
                "backends": [
                    {
                        "name": "influxdb-2-1",
                        "url": "http://192.168.137.131:8086",
                        "username": "",
                        "password": ""
                    },
                    {
                        "name": "influxdb-2-2",
                        "url": "http://192.168.137.131:8087",
                        "username": "",
                        "password": ""
                    }
                ]
            }
        ],
        "listen_addr": ":7076",
        "db_list": [],
        "data_dir": "data",
        "tlog_dir": "log",
        "hash_key": "idx",
        "flush_size": 10000,
        "flush_time": 1,
        "check_interval": 1,
        "rewrite_interval": 10,
        "conn_pool_size": 20,
        "write_timeout": 10,
        "idle_timeout": 10,
        "username": "",
        "password": "",
        "write_tracing": false,
        "query_tracing": false,
        "pprof_enabled": false,
        "https_enabled": false,
        "https_cert": "",
        "https_key": ""
    }
    
  3. influx.conf配置

    #这里只放出几处需要修改的 其他按照默认即可 如果生产环境可以考虑把internal禁掉
      
      # Determines whether the Flux query endpoint is enabled.
      flux-enabled = true  (如果需要支持flux语句 请设置为true)
    
  4. 测试

    curl -XPOST 'http://127.0.0.1:7076/query' --data-urlencode 'q=CREATE DATABASE "testdb"'
    sudo curl -XPOST 'http://127.0.0.1:7076/api/v2/write?bucket=testdb&precision=s' --data-binary 'mem,host=host1 used_percent=25 1700469476'
    sudo curl -XPOST 'http://127.0.0.1:7076/api/v2/write?bucket=testdb&precision=s' --data-binary 'mem2,host=host2 used_percent=23 1700469476'
    sudo curl -XPOST 'http://127.0.0.1:7076/api/v2/write?bucket=testdb&precision=s' --data-binary 'mem3,host=host3 used_percent=24 1700531670'
    
    sudo curl -XPOST 'http://127.0.0.1:7076/api/v2/query' \
      -H 'Accept:application/csv' \
      -H 'Content-type:application/vnd.flux' \
      -d 'from(bucket:"testdb")
            |> range(start:-5m)
            |> filter(fn:(r) => r._measurement == "mem")'
    

Influx2.5环境

SpringBoot搭建
  • 引入依赖

    <dependency>
        <groupId>com.influxdb</groupId>
        <artifactId>influxdb-client-java</artifactId>
        <version>6.3.0</version>
    </dependency>
    
  • Java代码

    public class influxProxyTest {
        public static void main(String[] args) {
    
            String url = "http://192.168.137.130:7076";
            String token = "testinfo";
            String org = "admin";
            String bucket = "analyse";
    
            InfluxDBClient client = InfluxDBClientFactory.create(url, token.toCharArray(), org, bucket);
            QueryApi queryApi = client.getQueryApi();
            String flux = "from(bucket:\"analyse\")\n" +
                    "|> range(start:-5d)\n" +
                    "|> filter(fn:(r) => r._measurement == \"mem\")";
            List<FluxTable> list = queryApi.query(flux, org);
            for (FluxTable fluxTable:list){
                List<FluxRecord> records = fluxTable.getRecords();
                for (FluxRecord fluxRecord:records){
                    System.out.println(fluxRecord.getValue());
                }
            }
        }
    
Ngnix搭建(实现中)

请参考 custom.conf

服务器搭建与数据库部署

使用docker来搭建对应的influxdb信息

本地存在两台服务器(请确保influx1可以访问到influx2 同一局域网)

influx1: 192.168.137.130

influx2: 192.168.137.131

  1. docker-compose代码

    version: "3.5"
    
    services:
    
      influx-proxy:
        image: chengshiwen/influx-proxy:3.0.0-preview
        container_name: influx-proxy
        ports:
          - 7076:7076
        environment:
          - TZ=Asia/Shanghai
        volumes:
          - ./proxy.json:/etc/influx-proxy/proxy.json
        restart: unless-stopped
        networks:
          - influx_net
    
      influxdb-1:
        image: influxdb:2.5.1
        container_name: influxdb-1
        restart: unless-stopped
        ports:
          - "8086:8086"
        networks:
          - influx_net
        volumes:
          - ./influxdb1:/var/lib/influxdb2
    
      influxdb-2:
        image: influxdb:2.5.1
        container_name: influxdb-2
        restart: unless-stopped
        ports:
          - "8087:8086"
        networks:
          - influx_net
        volumes:
          - ./influxdb2:/var/lib/influxdb2
    
    networks:
      influx_net:
    
  2. proxy.json代码

    {
        "circles": [
            {
                "name": "circle-1",
                "backends": [
                    {
                        "name": "influxdb-1-1",
                        "url": "http://192.168.137.130:8086",
                        "token": ""
                    },
                    {
                        "name": "influxdb-1-2",
                        "url": "http://192.168.137.130:8087",
                        "token": ""
                    }
                ]
            },
            {
                "name": "circle-2",
                "backends": [
                    {
                        "name": "influxdb-2-1",
                        "url": "http://192.168.137.131:8086",
                        "token": ""
                    },
                    {
                        "name": "influxdb-2-2",
                        "url": "http://192.168.137.131:8087",
                        "token": ""
                    }
                ]
            }
        ],
        "dbrp": {
            "separator": "/",
            "mapping": {"mydb": "admin/analyse", "mydb/myrp": "admin/analyse"}
        },
        "listen_addr": ":7076",
        "data_dir": "data",
        "flush_size": 10000,
        "flush_time": 1,
        "check_interval": 1,
        "rewrite_interval": 10,
        "conn_pool_size": 20,
        "write_timeout": 10,
        "write_tracing": false,
        "query_tracing": false,
        "token": "",
        "pprof_enabled": false,
        "https_enabled": false,
        "https_cert": "",
        "https_key": ""
    }
    
  3. 测试

    数据写入
    sudo curl -XPOST 'http://192.168.137.130:7076/api/v2/write?org=admin&bucket=analyse&precision=s' --data-binary 'mem,host=host3 used_percent=241 1700531671'
    
出现这个错误的原因是在导入seaborn包时,无法从typing模块中导入名为'Protocol'的对象。 解决这个问题的方法有以下几种: 1. 检查你的Python版本是否符合seaborn包的要求,如果不符合,尝试更新Python版本。 2. 检查你的环境中是否安装了typing_extensions包,如果没有安装,可以使用以下命令安装:pip install typing_extensions。 3. 如果你使用的是Python 3.8版本以下的版本,你可以尝试使用typing_extensions包来代替typing模块来解决该问题。 4. 检查你的代码是否正确导入了seaborn包,并且没有其他导入错误。 5. 如果以上方法都无法解决问题,可以尝试在你的代码中使用其他的可替代包或者更新seaborn包的版本来解决该问题。 总结: 出现ImportError: cannot import name 'Protocol' from 'typing'错误的原因可能是由于Python版本不兼容、缺少typing_extensions包或者导入错误等原因造成的。可以根据具体情况尝试上述方法来解决该问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [ImportError: cannot import name ‘Literal‘ from ‘typing‘ (D:\Anaconda\envs\tensorflow\lib\typing....](https://blog.csdn.net/yuhaix/article/details/124528628)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吃货智

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值