java: 通过证书访问etcd

ops/2024/9/23 7:18:22/

一、首先,要使用cfssl生成etcd证书相关的文件(ca.pem server.pem server-key.pem ),然后把server-key.pem进行转换:

openssl pkcs8 -topk8 -nocrypt -in server-key.pem -out server.key

二、带证书启动etcd

./etcd --name infra0   --cert-file=/root/server.pem --key-file=/root/server-key.pem   --advertise-client-urls=https://0.0.0.0:2379 --listen-client-urls=https://0.0.0.0:2379

可通过etcdctl 进行连接验证

./etcdctl --cacert=/root/ca.pem --cert=/root/server.pem --key=/root/server-key.pem --endpoints="https://10.180.23.10:2379" get Elon

三、在java项目中添加相关依赖,完整依赖类似如下:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>springbootetcd3</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.6.6</version><relativePath/> <!-- lookup parent from repository --></parent><dependencies><dependency><groupId>io.etcd</groupId><artifactId>jetcd-core</artifactId><version>0.7.7</version></dependency><!-- https://mvnrepository.com/artifact/com.coreos/jetcd-core -->
<!--        <dependency><groupId>com.coreos</groupId><artifactId>jetcd-core</artifactId><version>0.0.2</version></dependency>--><!--                <dependency><groupId>io.etcd</groupId><artifactId>jetcd-core</artifactId><version>0.5.0</version></dependency>--><!--        <dependency><groupId>io.grpc</groupId><artifactId>grpc-netty-shaded</artifactId><version>1.50.0</version></dependency>--><!-- https://mvnrepository.com/artifact/io.netty/netty-all --><dependency><groupId>io.netty</groupId><artifactId>netty-all</artifactId><version>4.1.90.Final</version></dependency><!-- https://mvnrepository.com/artifact/io.netty/netty-tcnative --><dependency><groupId>io.netty</groupId><artifactId>netty-tcnative</artifactId><version>2.0.65.Final</version></dependency><!-- https://mvnrepository.com/artifact/io.netty/netty-tcnative-boringssl-static --><dependency><groupId>io.netty</groupId><artifactId>netty-tcnative-boringssl-static</artifactId><version>2.0.65.Final</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies></project>

四、创建客户端,访问etcd

package cn.edu.tju;import io.etcd.jetcd.ByteSequence;
import io.etcd.jetcd.Client;
import io.etcd.jetcd.KV;
import io.etcd.jetcd.api.PutResponse;
import io.grpc.netty.GrpcSslContexts;
import io.netty.handler.ssl.SslContext;import java.io.File;
import java.io.IOException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;public class EtcdExample {public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {File cert = new File("d:\\ca.pem");File keyCertChainFile = new File("d:\\server.pem");File keyFile = new File("d:\\server.key");SslContext context = GrpcSslContexts.forClient().trustManager(cert).keyManager(keyCertChainFile, keyFile).build();Client client = Client.builder().endpoints("https://xx.xx.xx.xx:2379").sslContext(context).build();ByteSequence key = ByteSequence.from("Elon".getBytes());ByteSequence value = ByteSequence.from("Musk".getBytes());// put the key-valueclient.getKVClient().put(key,value).get();System.out.println("ok");}
}

http://www.ppmy.cn/ops/35911.html

相关文章

西奥机电CHT-01软胶囊弹性硬度测试仪:守护药品运输安全,检测弹性确保不易变形

西奥机电CHT-01软胶囊弹性硬度测试仪&#xff1a;守护药品运输安全&#xff0c;检测弹性确保不易变形 在当今药品行业&#xff0c;药品运输的安全性成为了消费者和生产企业共同关注的焦点。特别是对于软胶囊这类易变形的药品&#xff0c;如何在运输过程中保持其形态完整、药效…

物料厘不清?企业如何做好“物料管理”

物料包括原材料、半成品、成品、辅助用品以及生产过程中必然产生的边角余料、废料等。在制造企业中&#xff0c;各个部门的业务流程几乎都要用到物料&#xff1a; 销售和订单录入部门要通过物料确定客户定制产品的构形&#xff1b; 计划部门要根据物料来计划物料和能力的需求…

紧固件失效的原因及预防方法,收藏——SunTorque智能扭矩系统

智能扭矩系统-智能拧紧系统-扭矩自动控制系统-SunTorque 紧固件的失效&#xff0c;指的是紧固件在使用过程中&#xff0c;由于各种原因导致其性能下降或完全丧失&#xff0c;从而无法继续完成预定的功能。紧固件失效不仅会影响设备的正常运行&#xff0c;还可能引发安全事故&a…

Linux-03

cat 由第一行开始显示文件内容 tac 从最后一行开始显示&#xff0c;可以看出 tac 是 cat 的倒着写 nl 显示的时候&#xff0c;顺道输出行号 more 一页一页的显示文件内容 less 与 more 类似&#xff0c;但是比 more 更好的是&#xff0c;他可以往前翻页 (空格翻页 退出q命令) h…

企业微信hook接口协议,ipad协议http,客户群发送任务,获取要发送的客户群列表

客户群发送任务&#xff0c;获取要发送的客户群列表 参数名必选类型说明uuid是String每个实例的唯一标识&#xff0c;根据uuid操作具体企业微信 请求示例 {"uuid": "1688853790533324","id":1101292747044333637, //群发任务id"keyword…

头歌-Java编程基础进阶

第1关 两整数的四则运算 package step1;import java.util.Scanner;public class Arithmetic2Num {public static void main(String[] args) { // ---------------------Begin------------------------Scanner scnew Scanner(System.in);int asc.nextInt();int bsc.nextInt();…

stackqueue类——适配器模式 双端队列deque(C++)

接下来我们将实现 stack、queue 类的常用函数&#xff0c;其实对于 stack 和 queue 的常用函数实现可以说得上是非常简单&#xff0c;若想详细了解可以看这篇&#xff1a;栈和队列&循环队列&#xff08;C/C&#xff09;_栈和循环队列-CSDN博客&#xff1b;在本篇中我们将使…

自定义指令基本用法

自定义指令基本用法 1.自定义指令案例一&#xff1a;输入框自动聚焦2.自定义指令生命周期和参数-修饰符3.封装全局自定义指令3.1 创建directive目录&#xff0c;创建index.js&#xff0c;创建format-xx文件3.1 main.js文件引入并传入全局对象3.3 组件中引用 自定义指令 Vue可以…