|
| 1 | +# grpc-java-tools |
| 2 | + |
| 3 | +[](https://github.com/evolution-gaming/grpc-java-tools/actions/workflows/ci.yml?query=branch%3Amain) |
| 4 | +[](https://central.sonatype.com/artifact/com.evolution.jgrpc.tools/k8s-dns-name-resolver) |
| 5 | + |
| 6 | +A collection of Java libraries that supplement [grpc-java](https://github.com/grpc/grpc-java). |
| 7 | + |
| 8 | +The minimum supported JDK version is 17. |
| 9 | + |
| 10 | +## Table of Contents |
| 11 | + |
| 12 | +- [Modules](#modules) |
| 13 | + - [k8s-dns-name-resolver](#k8s-dns-name-resolver) |
| 14 | +- [For Contributors](#for-contributors) |
| 15 | + |
| 16 | +## Modules |
| 17 | + |
| 18 | +### k8s-dns-name-resolver |
| 19 | + |
| 20 | +Provides a custom name resolver for working with headless Kubernetes services using DNS. |
| 21 | + |
| 22 | +Compared to the grpc-java built-in DNS name resolver, this one properly supports on-the-fly updates |
| 23 | +of the service pod list: |
| 24 | + |
| 25 | +- The built-in DNS name resolver relies on the JVM's DNS query logic and its cache. |
| 26 | +- If a new pod is added, it is not visible to the clients until a server closes one of the |
| 27 | + connections, or the JVM DNS cache entry expires. |
| 28 | +- The JVM DNS cache (in JDK 17 and earlier, at least) does not honor DNS record TTL values and |
| 29 | + instead uses a globally configured TTL for expiration. |
| 30 | +- So with the vanilla DNS name resolver, to discover a new pod quickly, you either need to set a |
| 31 | + very low global JVM DNS TTL, or use a very low connection TTL on the servers. |
| 32 | +- Tampering with global JVM DNS settings might be undesirable because it could affect other |
| 33 | + networking code, such as code that calls external HTTP services. |
| 34 | +- A low server connection TTL negates the benefits of long-lived HTTP/2 connections. |
| 35 | + |
| 36 | +`K8sDnsNameResolver` avoids these issues by using [dnsjava](https://github.com/dnsjava/dnsjava) |
| 37 | +to query DNS servers periodically (every 10 seconds by default) without using a DNS cache. |
| 38 | + |
| 39 | +It is useful if you cannot use a Kubernetes API-based or an xDS API-based resolver for your |
| 40 | +Kubernetes-deployed gRPC services. |
| 41 | + |
| 42 | +#### Getting Started |
| 43 | + |
| 44 | +Declare dependency on the library: |
| 45 | +[Maven Central](https://central.sonatype.com/artifact/com.evolution.jgrpc.tools/k8s-dns-name-resolver) |
| 46 | + |
| 47 | +`K8sDnsNameResolverProvider` is automatically discovered by the grpc-java runtime. |
| 48 | + |
| 49 | +You can then use the `k8s-dns` scheme in your gRPC client channel builder: |
| 50 | + |
| 51 | +```java |
| 52 | +ManagedChannel channel = NettyChannelBuilder |
| 53 | + .forTarget("k8s-dns://my-svc.my-namespace.svc.my-cluster.local:8080") |
| 54 | + .build(); |
| 55 | +``` |
| 56 | + |
| 57 | +`K8sDnsNameResolver` supports almost the same target URI format as the standard `DnsNameResolver`: |
| 58 | + |
| 59 | +- `k8s-dns://my-svc.my-namespace.svc.my-cluster.local` (default port) |
| 60 | +- `k8s-dns:///my-svc.my-namespace.svc.my-cluster.local` (default port) |
| 61 | +- `k8s-dns://my-svc.my-namespace.svc.my-cluster.local:8080` |
| 62 | +- `k8s-dns:///my-svc.my-namespace.svc.my-cluster.local:8080` |
| 63 | + |
| 64 | +## For Contributors |
| 65 | + |
| 66 | +### Build Requirements |
| 67 | + |
| 68 | +You will need: |
| 69 | + |
| 70 | +- JDK 17 |
| 71 | +- [sbt](https://www.scala-sbt.org) |
| 72 | + |
| 73 | +The published library modules are pure Java, but some of the test code is written in Scala. |
| 74 | + |
| 75 | +### Useful SBT commands |
| 76 | + |
| 77 | +To reformat Java and Scala code run: |
| 78 | + |
| 79 | +```shell |
| 80 | +sbt fmt |
| 81 | +``` |
| 82 | + |
| 83 | +Code formatting is verified by the build commands, and pull requests with incorrectly formatted code |
| 84 | +will not be accepted. |
| 85 | + |
| 86 | +A full build can be run with: |
| 87 | + |
| 88 | +```shell |
| 89 | +sbt build |
| 90 | +``` |
0 commit comments