Skip to content

Commit

Permalink
java: modify Java example to move healthService to the admin server (#45
Browse files Browse the repository at this point in the history
)
  • Loading branch information
sanjaypujare committed Jun 16, 2021
1 parent 6c9c2d8 commit 6a204dc
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 66 deletions.
37 changes: 17 additions & 20 deletions java/src/main/java/io/grpc/examples/wallet/AccountServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import io.grpc.examples.wallet.account.MembershipType;
import io.grpc.health.v1.HealthCheckResponse.ServingStatus;
import io.grpc.protobuf.services.ProtoReflectionService;
import io.grpc.services.AdminInterface;
import io.grpc.services.HealthStatusManager;
import io.grpc.stub.StreamObserver;
import io.grpc.xds.XdsServerBuilder;
Expand All @@ -46,8 +47,9 @@ private enum CredentialsType {
XDS
}
private Server server;
private Server healthServer;
private Server adminServer;
private int port = 18883;
private int adminPort = 28883;
private String hostnameSuffix = "";
private String gcpClientProject = "";
private CredentialsType credentialsType = CredentialsType.INSECURE;
Expand All @@ -74,6 +76,8 @@ void parseArgs(String[] args) {
String value = parts[1];
if ("port".equals(key)) {
port = Integer.parseInt(value);
} else if ("admin_port".equals(key)) {
adminPort = Integer.parseInt(value);
} else if ("hostname_suffix".equals(key)) {
hostnameSuffix = value;
} else if ("gcp_client_project".equals(key)) {
Expand All @@ -93,6 +97,8 @@ void parseArgs(String[] args) {
+ "\n"
+ "\n --port=PORT The port to listen on. Default "
+ s.port
+ "\n --admin_port=PORT The admin port to listen on. Default "
+ s.adminPort
+ "\n --hostname_suffix=STR Suffix to append to hostname in response header. "
+ "Default \""
+ s.hostnameSuffix
Expand All @@ -111,13 +117,18 @@ private void start() throws IOException {
Observability.registerExporters(gcpClientProject);
}
HealthStatusManager health = new HealthStatusManager();
// start an admin+health server in plaintext mode
adminServer =
ServerBuilder.forPort(adminPort)
.addServices(AdminInterface.getStandardServices())
.addService(health.getHealthService())
.build()
.start();
logger.info("Admin & health server started, listening on " + adminPort);
ServerCredentials serverCredentials =
credentialsType == CredentialsType.XDS
? XdsServerCredentials.create(InsecureServerCredentials.create())
: InsecureServerCredentials.create();
// Since the main server may be using TLS, we start a second server just for plaintext health
// checks
int healthPort = port + 1;
if (credentialsType == CredentialsType.XDS) {
server =
XdsServerBuilder.forPort(port, serverCredentials)
Expand All @@ -128,11 +139,6 @@ private void start() throws IOException {
.addService(health.getHealthService())
.build()
.start();
healthServer =
XdsServerBuilder.forPort(healthPort, InsecureServerCredentials.create())
.addService(health.getHealthService()) // allow management servers to monitor health
.build()
.start();
} else {
server =
ServerBuilder.forPort(port)
Expand All @@ -143,15 +149,9 @@ private void start() throws IOException {
.addService(health.getHealthService())
.build()
.start();
healthServer =
ServerBuilder.forPort(healthPort)
.addService(health.getHealthService()) // allow management servers to monitor health
.build()
.start();
}
health.setStatus("", ServingStatus.SERVING);
logger.info("Server started, listening on " + port);
logger.info("Plaintext health server started, listening on " + healthPort);
Runtime.getRuntime()
.addShutdownHook(
new Thread() {
Expand All @@ -172,18 +172,15 @@ private void stop() throws InterruptedException {
if (server != null) {
server.shutdown().awaitTermination(30, SECONDS);
}
if (healthServer != null) {
healthServer.shutdown().awaitTermination(30, SECONDS);
if (adminServer != null) {
adminServer.shutdown().awaitTermination(30, SECONDS);
}
}

private void blockUntilShutdown() throws InterruptedException {
if (server != null) {
server.awaitTermination();
}
if (healthServer != null) {
healthServer.awaitTermination();
}
}

public static void main(String[] args) throws IOException, InterruptedException {
Expand Down
27 changes: 4 additions & 23 deletions java/src/main/java/io/grpc/examples/wallet/StatsServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ private enum CredentialsType {
XDS
}
private Server server;
private Server healthServer;
private Server adminServer;

private int port = 18882;
Expand Down Expand Up @@ -148,25 +147,24 @@ private void start() throws IOException {
if (!gcpClientProject.isEmpty()) {
Observability.registerExporters(gcpClientProject);
}
HealthStatusManager health = new HealthStatusManager();
// start an admin+health server in plaintext mode
adminServer = ServerBuilder.forPort(adminPort)
.addServices(AdminInterface.getStandardServices())
.addService(health.getHealthService())
.build()
.start();
logger.info("Admin server started, listening on " + adminPort);
logger.info("Admin & health server started, listening on " + adminPort);
ChannelCredentials channelCredentials =
credentialsType == CredentialsType.XDS
? XdsChannelCredentials.create(InsecureChannelCredentials.create())
: InsecureChannelCredentials.create();
accountChannel = Grpc.newChannelBuilder(accountServer, channelCredentials).build();
exec = MoreExecutors.listeningDecorator(Executors.newSingleThreadScheduledExecutor());
HealthStatusManager health = new HealthStatusManager();
ServerCredentials serverCredentials =
credentialsType == CredentialsType.XDS
? XdsServerCredentials.create(InsecureServerCredentials.create())
: InsecureServerCredentials.create();
// Since the main server may be using TLS, we start a second server just for plaintext health
// checks
int healthPort = port + 1;
if (credentialsType == CredentialsType.XDS) {
server =
XdsServerBuilder.forPort(port, serverCredentials)
Expand All @@ -179,11 +177,6 @@ private void start() throws IOException {
.addService(health.getHealthService())
.build()
.start();
healthServer =
XdsServerBuilder.forPort(healthPort, InsecureServerCredentials.create())
.addService(health.getHealthService()) // allow management servers to monitor health
.build()
.start();
} else {
server =
ServerBuilder.forPort(port)
Expand All @@ -196,15 +189,9 @@ private void start() throws IOException {
.addService(health.getHealthService())
.build()
.start();
healthServer =
ServerBuilder.forPort(healthPort)
.addService(health.getHealthService()) // allow management servers to monitor health
.build()
.start();
}
health.setStatus("", ServingStatus.SERVING);
logger.info("Server started, listening on " + port);
logger.info("Plaintext health server started, listening on " + healthPort);
Runtime.getRuntime()
.addShutdownHook(
new Thread() {
Expand All @@ -225,9 +212,6 @@ private void stop() throws InterruptedException {
if (server != null) {
server.shutdown().awaitTermination(30, SECONDS);
}
if (healthServer != null) {
healthServer.shutdown().awaitTermination(30, SECONDS);
}
if (adminServer != null) {
adminServer.shutdown().awaitTermination(30, SECONDS);
}
Expand All @@ -243,9 +227,6 @@ private void blockUntilShutdown() throws InterruptedException {
if (server != null) {
server.awaitTermination();
}
if (healthServer != null) {
healthServer.awaitTermination();
}
}

public static void main(String[] args) throws IOException, InterruptedException {
Expand Down
27 changes: 4 additions & 23 deletions java/src/main/java/io/grpc/examples/wallet/WalletServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ private enum CredentialsType {
XDS
}
private Server server;
private Server healthServer;
private Server adminServer;
private int port = 18881;
private int adminPort = 28881;
Expand Down Expand Up @@ -153,25 +152,24 @@ private void start() throws IOException {
if (!gcpClientProject.isEmpty()) {
Observability.registerExporters(gcpClientProject);
}
HealthStatusManager health = new HealthStatusManager();
// start an admin+health server in plaintext mode
adminServer = ServerBuilder.forPort(adminPort)
.addServices(AdminInterface.getStandardServices())
.addService(health.getHealthService())
.build()
.start();
logger.info("Admin server started, listening on " + adminPort);
logger.info("Admin & health server started, listening on " + adminPort);
ChannelCredentials channelCredentials =
credentialsType == CredentialsType.XDS
? XdsChannelCredentials.create(InsecureChannelCredentials.create())
: InsecureChannelCredentials.create();
accountChannel = Grpc.newChannelBuilder(accountServer, channelCredentials).build();
statsChannel = Grpc.newChannelBuilder(statsServer, channelCredentials).build();
HealthStatusManager health = new HealthStatusManager();
ServerCredentials serverCredentials =
credentialsType == CredentialsType.XDS
? XdsServerCredentials.create(InsecureServerCredentials.create())
: InsecureServerCredentials.create();
// Since the main server may be using TLS, we start a second server just for plaintext health
// checks
int healthPort = port + 1;
if (credentialsType == CredentialsType.XDS) {
server =
XdsServerBuilder.forPort(port, serverCredentials)
Expand All @@ -184,11 +182,6 @@ private void start() throws IOException {
.addService(health.getHealthService())
.build()
.start();
healthServer =
XdsServerBuilder.forPort(healthPort, InsecureServerCredentials.create())
.addService(health.getHealthService()) // allow management servers to monitor health
.build()
.start();
} else {
server =
ServerBuilder.forPort(port)
Expand All @@ -201,15 +194,9 @@ private void start() throws IOException {
.addService(health.getHealthService())
.build()
.start();
healthServer =
ServerBuilder.forPort(healthPort)
.addService(health.getHealthService()) // allow management servers to monitor health
.build()
.start();
}
health.setStatus("", ServingStatus.SERVING);
logger.info("Server started, listening on " + port);
logger.info("Plaintext health server started, listening on " + healthPort);
Runtime.getRuntime()
.addShutdownHook(
new Thread() {
Expand All @@ -230,9 +217,6 @@ private void stop() throws InterruptedException {
if (server != null) {
server.shutdown().awaitTermination(30, SECONDS);
}
if (healthServer != null) {
healthServer.shutdown().awaitTermination(30, SECONDS);
}
if (adminServer != null) {
adminServer.shutdown().awaitTermination(30, SECONDS);
}
Expand All @@ -248,9 +232,6 @@ private void blockUntilShutdown() throws InterruptedException {
if (server != null) {
server.awaitTermination();
}
if (healthServer != null) {
healthServer.awaitTermination();
}
}

public static void main(String[] args) throws IOException, InterruptedException {
Expand Down

0 comments on commit 6a204dc

Please sign in to comment.