1. First, use cfssl to generate etcd certificate-related files ( ), and then convert them:
openssl pkcs8 -topk8 -nocrypt -in -out
2. Start etcd with certificate
./etcd --name infra0 --cert-file=/root/ --key-file=/root/ --advertise-client-urls=https://0.0.0.0:2379 --listen-client-urls=https://0.0.0.0:2379
Connection verification can be performed through etcdctl
./etcdctl --cacert=/root/ --cert=/root/ --key=/root/ --endpoints="https://10.180.23.10:2379" get Elon
3. Add relevant dependencies to the java project, and the complete dependencies are similar to the following:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="/POM/4.0.0" xmlns:xsi="http:///2001/XMLSchema-instance" xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0."> <modelVersion>4.0.0</modelVersion> <groupId></groupId> <artifactId>springbootetcd3</artifactId> <version>1.0-SNAPSHOT</version> <properties> <>8</> <>8</> </properties> <parent> <groupId></groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.6</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId></groupId> <artifactId>jetcd-core</artifactId> <version>0.7.7</version> </dependency> <!-- /artifact//jetcd-core --> <!-- <dependency> <groupId></groupId> <artifactId>jetcd-core</artifactId> <version>0.0.2</version> </dependency>--> <!-- <dependency> <groupId></groupId> <artifactId>jetcd-core</artifactId> <version>0.5.0</version> </dependency>--> <!-- <dependency> <groupId></groupId> <artifactId>grpc-netty-shaded</artifactId> <version>1.50.0</version> </dependency>--> <!-- /artifact//netty-all --> <dependency> <groupId></groupId> <artifactId>netty-all</artifactId> <version>4.1.</version> </dependency> <!-- /artifact//netty-tcnative --> <dependency> <groupId></groupId> <artifactId>netty-tcnative</artifactId> <version>2.0.</version> </dependency> <!-- /artifact//netty-tcnative-boringssl-static --> <dependency> <groupId></groupId> <artifactId>netty-tcnative-boringssl-static</artifactId> <version>2.0.</version> </dependency> <dependency> <groupId></groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
4. Create a client and access etcd
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; public class EtcdExample { public static void main(String[] args) throws IOException, ExecutionException, InterruptedException { File cert = new File("d:\\"); File keyCertChainFile = new File("d:\\"); File keyFile = new File("d:\\"); SslContext context = () .trustManager(cert) .keyManager(keyCertChainFile, keyFile) .build(); Client client = () .endpoints(":2379") .sslContext(context) .build(); ByteSequence key = ("Elon".getBytes()); ByteSequence value = ("Musk".getBytes()); // put the key-value ().put(key,value).get(); ("ok"); } }
This is the article about the implementation steps of Java accessing etcd through certificates. For more related Java certificate accessing etcd content, please search for my previous articles or continue browsing the following related articles. I hope everyone will support me in the future!