example_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2016 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package clientv3_test
  15. import (
  16. "context"
  17. "log"
  18. "os"
  19. "time"
  20. "go.etcd.io/etcd/clientv3"
  21. "go.etcd.io/etcd/pkg/transport"
  22. "google.golang.org/grpc/grpclog"
  23. )
  24. var (
  25. dialTimeout = 5 * time.Second
  26. requestTimeout = 10 * time.Second
  27. endpoints = []string{"localhost:2379", "localhost:22379", "localhost:32379"}
  28. )
  29. func Example() {
  30. clientv3.SetLogger(grpclog.NewLoggerV2(os.Stderr, os.Stderr, os.Stderr))
  31. cli, err := clientv3.New(clientv3.Config{
  32. Endpoints: endpoints,
  33. DialTimeout: dialTimeout,
  34. })
  35. if err != nil {
  36. log.Fatal(err)
  37. }
  38. defer cli.Close() // make sure to close the client
  39. _, err = cli.Put(context.TODO(), "foo", "bar")
  40. if err != nil {
  41. log.Fatal(err)
  42. }
  43. }
  44. func ExampleConfig_withTLS() {
  45. tlsInfo := transport.TLSInfo{
  46. CertFile: "/tmp/test-certs/test-name-1.pem",
  47. KeyFile: "/tmp/test-certs/test-name-1-key.pem",
  48. TrustedCAFile: "/tmp/test-certs/trusted-ca.pem",
  49. }
  50. tlsConfig, err := tlsInfo.ClientConfig()
  51. if err != nil {
  52. log.Fatal(err)
  53. }
  54. cli, err := clientv3.New(clientv3.Config{
  55. Endpoints: endpoints,
  56. DialTimeout: dialTimeout,
  57. TLS: tlsConfig,
  58. })
  59. if err != nil {
  60. log.Fatal(err)
  61. }
  62. defer cli.Close() // make sure to close the client
  63. _, err = cli.Put(context.TODO(), "foo", "bar")
  64. if err != nil {
  65. log.Fatal(err)
  66. }
  67. }