自学内容网 自学内容网

定制ShardingSphere-Proxy镜像满足业务需求

Sharding官方提供的proxy镜像是基础版的,如果我们使用Sharding有以下任意需求,就需要添加额外的依赖到容器{path}/ext-lib目录下。

向Docker容器中添加jar包的方式多种多样,推荐采取使用Dockerfile的方式添加依赖。将原有的镜像作为基础镜像,然后在构建过程进行依赖的下载,并添加到指定的目录下。 

添加MySQL驱动

比如:我想要使用Proxy连接MySQL,那么就需要在指定目录下放置mysql-connector.jar。

# 使用官方的apache/shardingsphere-proxy镜像作为基础镜像
FROM apache/shardingsphere-proxy:5.5.1

# 创建/opt/shardingsphere/ex-lib目录,如果它不存在的话
RUN mkdir -p /opt/shardingsphere-proxy/ext-lib/

# 使用wget下载JAR包
RUN wget -P /opt/shardingsphere-proxy/ext-lib/ https://repo1.maven.org/maven2/mysql/mysql-connector-java/8.0.11/mysql-connector-java-8.0.11.jar

Docker运行命令,其中JAVA_MEM_COMMON_OPTS是设置JVM的运行内存的,防止出现OOM的错误。

docker run -d \
  --name sharding1 \
  -p 3309:3307 \
  -e JAVA_MEM_COMMON_OPTS='-Xms256m -Xmx400m -Xss1m' \
  -v /root/sharding/conf/:/opt/shardingsphere-proxy/conf \
  sharding:1.0

这种方式的好处就是,方便镜像资源的复制、传播。不需要镜像的时候再去挂载目录。

添加Etcd的依赖

如果我们想要使用Etcd作为Proxy的集群模式持久化的存储仓库,需要将 Etcd 依赖的 vertx-grpc 4.5.1 和 vertx-core 4.5.1放入到/ext-lib目录下,不然会报:java.lang.NoClassDefFoundError: io/vertx/grpc/VertxChannelBuilder

we find java version: java21, full_version=21.0.4, full_path=/opt/java/openjdk/bin/java
unadapted java version, please notice...
The classpath is /opt/shardingsphere-proxy/conf:/opt/shardingsphere-proxy/conf:.:/opt/shardingsphere-proxy/lib/*:/opt/shardingsphere-proxy/ext-lib/*
main class org.apache.shardingsphere.proxy.Bootstrap -1 /opt/shardingsphere-proxy/conf 0.0.0.0 false
Exception in thread "main" java.lang.NoClassDefFoundError: io/vertx/grpc/VertxChannelBuilder
        at io.etcd.jetcd.impl.ClientImpl.<init>(ClientImpl.java:47)
        at io.etcd.jetcd.ClientBuilder.build(ClientBuilder.java:719)
        at org.apache.shardingsphere.mode.repository.cluster.etcd.EtcdRepository.init(EtcdRepository.java:77)
        at org.apache.shardingsphere.mode.manager.cluster.ClusterContextManagerBuilder.build(ClusterContextManagerBuilder.java:59)
        at org.apache.shardingsphere.proxy.initializer.BootstrapInitializer.createContextManager(BootstrapInitializer.java:62)
        at org.apache.shardingsphere.proxy.initializer.BootstrapInitializer.init(BootstrapInitializer.java:53)
        at org.apache.shardingsphere.proxy.Bootstrap.main(Bootstrap.java:64)
Caused by: java.lang.ClassNotFoundException: io.vertx.grpc.VertxChannelBuilder
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526)
        ... 7 more

我们的dockerfile文件就需要更改为:

# 使用官方的apache/shardingsphere-proxy镜像作为基础镜像
FROM apache/shardingsphere-proxy:5.5.1

# 创建/opt/shardingsphere/ex-lib目录,如果它不存在的话
RUN mkdir -p /opt/shardingsphere-proxy/ext-lib/

# 使用wget下载JAR包
RUN wget -P /opt/shardingsphere-proxy/ext-lib/ https://repo1.maven.org/maven2/mysql/mysql-connector-java/8.0.11/mysql-connector-java-8.0.11.jar
RUN wget -P /opt/shardingsphere-proxy/ext-lib/ https://repo1.maven.org/maven2/io/vertx/vertx-grpc/4.5.1/vertx-grpc-4.5.1.jar
RUN wget -P /opt/shardingsphere-proxy/ext-lib/ https://repo1.maven.org/maven2/io/vertx/vertx-core/4.5.1/vertx-core-4.5.1.jar

 配置文件global.yaml

mode:
  type: Cluster
  repository:  
    type: Etcd
    props:
      namespace: governance
      server-lists: http://8.141.88.61:2379
      timeToLiveSeconds: 30 
      connectionTimeout: 30 

authority:
  users:
    - user: root@%
      password: root
  privilege:
    type: ALL_PERMITTED

props:
  sql-show: true

接下来运行看到

ShardingSphereProxy - ShardingSphere-Proxy Cluster mode started successfully就算运行成功了。

值得注意的是,目前Proxy运行集群模式,并不是特别支持Etcd作为持久化仓库,所以会出现下方Error,具体可以看这个issue。Etcd在运行的过程汇会报错,但是不影响正常的使用,是否使用,个人衡量吧!

扩展阅读

定制镜像构建


原文地址:https://blog.csdn.net/wangye135/article/details/143658047

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!