|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * <p> |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * <p> |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.jooby.consul; |
| 20 | + |
| 21 | +import com.google.inject.Binder; |
| 22 | +import com.orbitz.consul.AgentClient; |
| 23 | +import com.orbitz.consul.Consul; |
| 24 | +import com.orbitz.consul.model.agent.ImmutableRegistration; |
| 25 | +import com.orbitz.consul.model.agent.Registration; |
| 26 | +import com.typesafe.config.Config; |
| 27 | +import com.typesafe.config.ConfigFactory; |
| 28 | +import org.jooby.Env; |
| 29 | +import org.jooby.Jooby; |
| 30 | + |
| 31 | +import java.text.MessageFormat; |
| 32 | +import java.util.Objects; |
| 33 | +import java.util.UUID; |
| 34 | +import java.util.concurrent.TimeUnit; |
| 35 | +import java.util.function.Consumer; |
| 36 | + |
| 37 | +/** |
| 38 | + * <p>Consul client module.</p> |
| 39 | + * |
| 40 | + * <p>Exports the {@link Consul} client.</p> |
| 41 | + * |
| 42 | + * <p>Also register the application as a service and setup a health check.</p> |
| 43 | + * |
| 44 | + * <h1>usage</h1> |
| 45 | + * |
| 46 | + * <pre> |
| 47 | + * { |
| 48 | + * use(new Consulby()); |
| 49 | + * |
| 50 | + * get("/myservice/health", req {@literal ->} { |
| 51 | + * Consul consul = require(Consul.class); |
| 52 | + * List<ServiceHealth> serviceHealths = consul.healthClient() |
| 53 | + * .getHealthyServiceInstances("myservice") |
| 54 | + * .getResponse(); |
| 55 | + * return serviceHealths; |
| 56 | + * }); |
| 57 | + * } |
| 58 | + * </pre> |
| 59 | + * |
| 60 | + * <h1>configuration</h1> |
| 61 | + * |
| 62 | + * <p>Configuration is done via <code>.conf</code>.</p> |
| 63 | + * |
| 64 | + * <p>For example, one can change the consul endpoint url, |
| 65 | + * change the advertised service host, and disable registration health check:</p> |
| 66 | + * |
| 67 | + * <pre> |
| 68 | + * consul.default.url = "http://consul.internal.domain.com:8500" |
| 69 | + * consul.default.register.host = 10.0.0.2 |
| 70 | + * consul.default.register.check = null |
| 71 | + * </pre> |
| 72 | + * |
| 73 | + * <p>or, disable the automatic registration feature completely:</p> |
| 74 | + * |
| 75 | + * <pre> |
| 76 | + * consul.default.register = null |
| 77 | + * </pre> |
| 78 | + * |
| 79 | + * @since 1.1.1 |
| 80 | + */ |
| 81 | +public class Consulby implements Jooby.Module { |
| 82 | + |
| 83 | + private final String name; |
| 84 | + |
| 85 | + private Consumer<Consul.Builder> consulBuilderConsumer; |
| 86 | + private Consumer<ImmutableRegistration.Builder> registrationBuilderConsumer; |
| 87 | + |
| 88 | + /** |
| 89 | + * A new {@link Consulby} instance, with the default config name: <code>default</code>. |
| 90 | + */ |
| 91 | + public Consulby() { |
| 92 | + this("default"); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * <p>A new {@link Consulby} instance, with a provided config name.</p> |
| 97 | + * |
| 98 | + * <p>The module can be instantiated more than one time to allow connecting to many Consul installations:</p> |
| 99 | + * |
| 100 | + * <pre> |
| 101 | + * { |
| 102 | + * use(new Consulby("consul1")); |
| 103 | + * use(new Consulby("consul2")); |
| 104 | + * } |
| 105 | + * </pre> |
| 106 | + * |
| 107 | + * <p>Since the module will fallback on the <code>consul.default</code> config prefix, |
| 108 | + * it is possible to only override the desired properties in the <code>.conf</code>, |
| 109 | + * for example, here, disabling health check only for `consul2`:</p> |
| 110 | + * |
| 111 | + * <pre> |
| 112 | + * consul.consul1.url = "http://consul1.internal.domain.com:8500" |
| 113 | + * |
| 114 | + * consul.consul2.url = "http://consul2.internal.domain.com:8500" |
| 115 | + * consul.consul2.register.check = null |
| 116 | + * </pre> |
| 117 | + * |
| 118 | + * @param name A config name |
| 119 | + */ |
| 120 | + public Consulby(String name) { |
| 121 | + this.name = Objects.requireNonNull(name, "A consul config name is required."); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * <p>{@link Consul} object can be configured programmatically:</p> |
| 126 | + * |
| 127 | + * <pre> |
| 128 | + * { |
| 129 | + * use(new Consulby() |
| 130 | + * .withConsulBuilder(consulBuilder -> { |
| 131 | + * consulBuilder.withPing(false); |
| 132 | + * consulBuilder.withBasicAuth("admin", "changeme"); |
| 133 | + * })); |
| 134 | + * } |
| 135 | + * </pre> |
| 136 | + * |
| 137 | + * @param consulBuilderConsumer A {@link Consumer} that accepts {@link Consul.Builder} |
| 138 | + * @return This {@link Consulby} to allow chaining |
| 139 | + */ |
| 140 | + public Consulby withConsulBuilder(Consumer<Consul.Builder> consulBuilderConsumer) { |
| 141 | + this.consulBuilderConsumer = consulBuilderConsumer; |
| 142 | + return this; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * <p>{@link Registration} object can be configured programmatically:</p> |
| 147 | + * |
| 148 | + * <pre> |
| 149 | + * { |
| 150 | + * use(new Consulby() |
| 151 | + * .withRegistrationBuilder(registrationBuilder -> { |
| 152 | + * registrationBuilder.enableTagOverride(true); |
| 153 | + * registrationBuilder.id("custom-service-id"); |
| 154 | + * })); |
| 155 | + * } |
| 156 | + * </pre> |
| 157 | + * |
| 158 | + * @param registrationBuilderConsumer A {@link Consumer} that accepts {@link ImmutableRegistration.Builder} |
| 159 | + * @return This {@link Consulby} to allow chaining |
| 160 | + */ |
| 161 | + public Consulby withRegistrationBuilder(Consumer<ImmutableRegistration.Builder> registrationBuilderConsumer) { |
| 162 | + this.registrationBuilderConsumer = registrationBuilderConsumer; |
| 163 | + return this; |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public void configure(Env env, Config config, Binder binder) throws Throwable { |
| 168 | + |
| 169 | + Config consulConfig = config.getConfig("consul.default"); |
| 170 | + if (!name.equals("default") && config.hasPath("consul." + name)) { |
| 171 | + consulConfig = config.getConfig("consul." + name).withFallback(consulConfig); |
| 172 | + } |
| 173 | + |
| 174 | + Consul.Builder consulBuilder = Consul.builder() |
| 175 | + .withUrl(consulConfig.getString("url")); |
| 176 | + |
| 177 | + if (consulBuilderConsumer != null) { |
| 178 | + consulBuilderConsumer.accept(consulBuilder); |
| 179 | + } |
| 180 | + |
| 181 | + Consul consul = consulBuilder.build(); |
| 182 | + |
| 183 | + env.onStop(consul::destroy); |
| 184 | + |
| 185 | + env.serviceKey().generate(Consul.class, name, k -> binder.bind(k).toInstance(consul)); |
| 186 | + |
| 187 | + if (consulConfig.hasPath("register")) { |
| 188 | + |
| 189 | + Config registerConfig = consulConfig.getConfig("register"); |
| 190 | + |
| 191 | + ImmutableRegistration.Builder registrationBuilder = ImmutableRegistration.builder() |
| 192 | + .name(registerConfig.getString("name")) |
| 193 | + .address(registerConfig.getString("host")) |
| 194 | + .port(registerConfig.getInt("port")) |
| 195 | + .tags(registerConfig.getStringList("tags")) |
| 196 | + .id(UUID.randomUUID().toString()); |
| 197 | + |
| 198 | + if (registerConfig.hasPath("check")) { |
| 199 | + |
| 200 | + Config checkConfig = registerConfig.getConfig("check"); |
| 201 | + |
| 202 | + String http = MessageFormat.format("http://{0}:{1,number,####}{2}", |
| 203 | + registerConfig.getString("host"), |
| 204 | + registerConfig.getInt("port"), |
| 205 | + checkConfig.getString("path")); |
| 206 | + |
| 207 | + Registration.RegCheck check = Registration.RegCheck.http(http, |
| 208 | + checkConfig.getDuration("interval", TimeUnit.SECONDS), |
| 209 | + checkConfig.getDuration("timeout", TimeUnit.SECONDS)); |
| 210 | + |
| 211 | + registrationBuilder.check(check); |
| 212 | + |
| 213 | + String response = checkConfig.getString("response"); |
| 214 | + env.router().get(checkConfig.getString("path"), () -> response); |
| 215 | + } |
| 216 | + |
| 217 | + if (registrationBuilderConsumer != null) { |
| 218 | + registrationBuilderConsumer.accept(registrationBuilder); |
| 219 | + } |
| 220 | + |
| 221 | + Registration registration = registrationBuilder.build(); |
| 222 | + |
| 223 | + AgentClient agentClient = consul.agentClient(); |
| 224 | + env.onStarted(() -> agentClient.register(registration)); |
| 225 | + env.onStop(() -> agentClient.deregister(registration.getId())); |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + @Override |
| 230 | + public Config config() { |
| 231 | + return ConfigFactory.parseResources(getClass(), "consul.conf"); |
| 232 | + } |
| 233 | +} |
0 commit comments