java: 通过证书访问etcd

2024-05-07 14:36
文章标签 java 访问 证书 etcd

本文主要是介绍java: 通过证书访问etcd,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、首先,要使用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");}
}

这篇关于java: 通过证书访问etcd的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/967627

相关文章

redis在spring boot中异常退出的问题解决方案

《redis在springboot中异常退出的问题解决方案》:本文主要介绍redis在springboot中异常退出的问题解决方案,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴... 目录问题:解决 问题根源️ 解决方案1. 异步处理 + 提前ACK(关键步骤)2. 调整Redis消费者组

一文教你Java如何快速构建项目骨架

《一文教你Java如何快速构建项目骨架》在Java项目开发过程中,构建项目骨架是一项繁琐但又基础重要的工作,Java领域有许多代码生成工具可以帮助我们快速完成这一任务,下面就跟随小编一起来了解下... 目录一、代码生成工具概述常用 Java 代码生成工具简介代码生成工具的优势二、使用 MyBATis Gen

springboot项目redis缓存异常实战案例详解(提供解决方案)

《springboot项目redis缓存异常实战案例详解(提供解决方案)》redis基本上是高并发场景上会用到的一个高性能的key-value数据库,属于nosql类型,一般用作于缓存,一般是结合数据... 目录缓存异常实践案例缓存穿透问题缓存击穿问题(其中也解决了穿透问题)完整代码缓存异常实践案例Red

SpringCloud整合MQ实现消息总线服务方式

《SpringCloud整合MQ实现消息总线服务方式》:本文主要介绍SpringCloud整合MQ实现消息总线服务方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、背景介绍二、方案实践三、升级版总结一、背景介绍每当修改配置文件内容,如果需要客户端也同步更新,

java中XML的使用全过程

《java中XML的使用全过程》:本文主要介绍java中XML的使用全过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录什么是XML特点XML作用XML的编写语法基本语法特殊字符编写约束XML的书写格式DTD文档schema文档解析XML的方法​​DOM解析XM

Java 的 Condition 接口与等待通知机制详解

《Java的Condition接口与等待通知机制详解》在Java并发编程里,实现线程间的协作与同步是极为关键的任务,本文将深入探究Condition接口及其背后的等待通知机制,感兴趣的朋友一起看... 目录一、引言二、Condition 接口概述2.1 基本概念2.2 与 Object 类等待通知方法的区别

SpringBoot项目中Redis存储Session对象序列化处理

《SpringBoot项目中Redis存储Session对象序列化处理》在SpringBoot项目中使用Redis存储Session时,对象的序列化和反序列化是关键步骤,下面我们就来讲讲如何在Spri... 目录一、为什么需要序列化处理二、Spring Boot 集成 Redis 存储 Session2.1

使用Java实现Navicat密码的加密与解密的代码解析

《使用Java实现Navicat密码的加密与解密的代码解析》:本文主要介绍使用Java实现Navicat密码的加密与解密,通过本文,我们了解了如何利用Java语言实现对Navicat保存的数据库密... 目录一、背景介绍二、环境准备三、代码解析四、核心代码展示五、总结在日常开发过程中,我们有时需要处理各种软

Java List排序实例代码详解

《JavaList排序实例代码详解》:本文主要介绍JavaList排序的相关资料,Java排序方法包括自然排序、自定义排序、Lambda简化及多条件排序,实现灵活且代码简洁,文中通过代码介绍的... 目录一、自然排序二、自定义排序规则三、使用 Lambda 表达式简化 Comparator四、多条件排序五、

Java实例化对象的​7种方式详解

《Java实例化对象的​7种方式详解》在Java中,实例化对象的方式有多种,具体取决于场景需求和设计模式,本文整理了7种常用的方法,文中的示例代码讲解详细,有需要的可以了解下... 目录1. ​new 关键字(直接构造)​2. ​反射(Reflection)​​3. ​克隆(Clone)​​4. ​反序列化