欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java通過證書訪問etcd的實現(xiàn)步驟

 更新時間:2024年05月08日 09:52:10   作者:amadeus_liu2  
Etcd提供了多種語言的客戶端庫,本文主要介紹了java通過證書訪問etcd的實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

一、首先,要使用cfssl生成etcd證書相關(guān)的文件(ca.pem server.pem server-key.pem ),然后把server-key.pem進行轉(zhuǎn)換:

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項目中添加相關(guān)依賴,完整依賴類似如下:

<?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>

四、創(chuàng)建客戶端,訪問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-value
        client.getKVClient().put(key,value).get();
        System.out.println("ok");

    }
}

到此這篇關(guān)于java通過證書訪問etcd的實現(xiàn)步驟的文章就介紹到這了,更多相關(guān)java 證書訪問etcd內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

  • 如何使用Spring AOP的通知類型及創(chuàng)建通知

    如何使用Spring AOP的通知類型及創(chuàng)建通知

    這篇文章主要給大家介紹了關(guān)于如何使用Spring AOP的通知類型及創(chuàng)建通知的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用Spring AOP具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • Spring的FactoryBean<Object>接口示例代碼

    Spring的FactoryBean<Object>接口示例代碼

    FactoryBean是Spring框架中的一個接口,用于創(chuàng)建和管理Bean對象,它的作用是將Bean的創(chuàng)建過程交給FactoryBean實現(xiàn)類來完成,而不是直接由Spring容器來創(chuàng)建,本文給大家介紹Spring的FactoryBean<Object>接口,感興趣的朋友一起看看吧
    2023-11-11
  • 排序算法圖解之Java選擇排序

    排序算法圖解之Java選擇排序

    選擇排序的工作原理是:第一次從待排序的數(shù)據(jù)元素中選出最?。ɑ蜃畲螅┑囊粋€元素,存放在序列的起始位置,然后再從剩余的未排序元素中尋找到最?。ù螅┰兀缓蠓诺揭雅判虻男蛄械哪┪?。本文通過圖片和示例介紹了選擇排序,需要的可以參考一下
    2022-11-11
  • spring-boot @Component和@Bean的區(qū)別詳解

    spring-boot @Component和@Bean的區(qū)別詳解

    這篇文章主要介紹了spring-boot @Component和@Bean的區(qū)別詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-07-07
  • Java 集合中關(guān)于Iterator和ListIterator的用法說明

    Java 集合中關(guān)于Iterator和ListIterator的用法說明

    這篇文章主要介紹了Java 集合中關(guān)于Iterator和ListIterator的用法說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • MybatisPlus實現(xiàn)真正批量插入的詳細步驟

    MybatisPlus實現(xiàn)真正批量插入的詳細步驟

    在數(shù)據(jù)庫操作中,批量插入是提升效率的重要手段,MyBatis-Plus提供了多種批量插入方法,但默認的saveBatch方法效率并不高,文章介紹了通過手動拼接SQL、使用IService接口以及自定義insertBatchSomeColumn方法進行優(yōu)化,以實現(xiàn)更高效的批量插入,并給出了性能優(yōu)化建議
    2024-10-10
  • Spring Boot集成 Spring Boot Admin 監(jiān)控

    Spring Boot集成 Spring Boot Admin 監(jiān)控

    這篇文章主要介紹了Spring Boot集成 Spring Boot Admin 監(jiān)控,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • java中GZIP壓縮解壓類使用實例

    java中GZIP壓縮解壓類使用實例

    這篇文章主要介紹了java中GZIP壓縮解壓類使用實例的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • 詳解SpringBoot容器的生命周期

    詳解SpringBoot容器的生命周期

    在使用SpringBoot進行開發(fā)時,我們經(jīng)常需要對Spring容器的生命周期進行了解和掌握,本文將介紹SpringBoot容器的生命周期,包括容器的創(chuàng)建、初始化、銷毀等過程,并提供相應(yīng)的代碼示例
    2023-06-06
  • java實現(xiàn)基于Tcp的socket聊天程序

    java實現(xiàn)基于Tcp的socket聊天程序

    這篇文章主要為大家詳細介紹了java實現(xiàn)基于Tcp的socket聊天程序,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-07-07

最新評論