├── .editorconfig ├── .gitattributes ├── .github ├── FUNDING.yml ├── dependabot.yml ├── settings.xml └── workflows │ ├── close_stale.yml │ ├── codeql-analysis.yml │ └── maven.yml ├── .gitignore ├── .mvn └── wrapper │ ├── MavenWrapperDownloader.java │ └── maven-wrapper.properties ├── .travis.yml ├── .travis_after_success.sh ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── SECURITY.md ├── consul-core ├── LICENSE ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── smoketurner │ │ └── dropwizard │ │ └── consul │ │ ├── ConsulBundle.java │ │ ├── ConsulConfiguration.java │ │ ├── ConsulFactory.java │ │ ├── config │ │ ├── ConsulLookup.java │ │ └── ConsulSubstitutor.java │ │ ├── core │ │ ├── ConsulAdvertiser.java │ │ └── ConsulServiceListener.java │ │ ├── health │ │ └── ConsulHealthCheck.java │ │ ├── managed │ │ └── ConsulAdvertiserManager.java │ │ └── task │ │ └── MaintenanceTask.java │ └── test │ └── java │ └── com │ └── smoketurner │ └── dropwizard │ └── consul │ ├── ConsulBundleTest.java │ ├── ConsulFactoryTest.java │ ├── core │ ├── ConsulAdvertiserTest.java │ └── ConsulServiceListenerTest.java │ ├── health │ └── ConsulHealthCheckTest.java │ └── managed │ └── ConsulAdvertiserManagerTest.java ├── consul-example ├── LICENSE ├── example.keystore ├── hello-world.yml ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── example │ └── helloworld │ ├── HelloWorldApplication.java │ ├── HelloWorldConfiguration.java │ ├── api │ └── Saying.java │ └── resources │ └── HelloWorldResource.java ├── consul-ribbon ├── LICENSE ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── smoketurner │ └── dropwizard │ └── consul │ └── ribbon │ ├── ConsulServerList.java │ ├── ConsulServiceDiscoverer.java │ ├── HealthyConsulServiceDiscoverer.java │ ├── RibbonJerseyClient.java │ ├── RibbonJerseyClientBuilder.java │ └── RibbonJerseyClientConfiguration.java ├── maven_deploy_settings.xml ├── mvnw ├── mvnw.cmd ├── pom.xml └── spotbugs.xml /.editorconfig: -------------------------------------------------------------------------------- 1 | # Configuration file for EditorConfig: http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | charset = utf-8 6 | end_of_line = lf 7 | indent_style = space 8 | indent_size = 4 9 | insert_final_newline = true 10 | trim_trailing_whitespace = true 11 | 12 | [*.properties] 13 | charset = latin1 14 | 15 | [travis.yml] 16 | indent_size = 2 17 | indent_style = space 18 | 19 | [*.md] 20 | trim_trailing_whitespace = false 21 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text auto 2 | 3 | *.java text eol=lf 4 | *.yml text eol=lf 5 | *.xml text eol=lf 6 | 7 | *.png binary 8 | *.jpg binary 9 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: jplock # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: jplock # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | custom: # Replace with a single custom sponsorship URL 9 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: "maven" 4 | directory: "/" 5 | schedule: 6 | interval: "weekly" 7 | - package-ecosystem: "github-actions" 8 | directory: "/" 9 | schedule: 10 | interval: "weekly" 11 | -------------------------------------------------------------------------------- /.github/settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 20 | 21 | 22 | jcenter 23 | JCenter 24 | https://jcenter.bintray.com 25 | central 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /.github/workflows/close_stale.yml: -------------------------------------------------------------------------------- 1 | name: "Close stale issues" 2 | on: 3 | schedule: 4 | - cron: "0 0 * * *" 5 | jobs: 6 | stale: 7 | runs-on: ubuntu-latest 8 | steps: 9 | - uses: actions/stale@v8 10 | with: 11 | repo-token: ${{ secrets.GITHUB_TOKEN }} 12 | stale-issue-message: 'This issue is stale because it has been open 90 days with no activity. Remove the "stale" label or comment or this will be closed in 14 days.' 13 | stale-pr-message: 'This pull request is stale because it has been open 90 days with no activity. Remove the "stale" label or comment or this will be closed in 14 days.' 14 | days-before-stale: 90 15 | days-before-close: 14 16 | -------------------------------------------------------------------------------- /.github/workflows/codeql-analysis.yml: -------------------------------------------------------------------------------- 1 | name: "CodeQL" 2 | 3 | on: 4 | push: 5 | branches: [master, ] 6 | pull_request: 7 | # The branches below must be a subset of the branches above 8 | branches: [master] 9 | schedule: 10 | - cron: '0 13 * * 2' 11 | 12 | jobs: 13 | analyze: 14 | name: Analyze 15 | runs-on: ubuntu-latest 16 | 17 | steps: 18 | - name: Checkout repository 19 | uses: actions/checkout@v3 20 | with: 21 | # We must fetch at least the immediate parents so that if this is 22 | # a pull request then we can checkout the head. 23 | fetch-depth: 2 24 | 25 | # If this run was triggered by a pull request event, then checkout 26 | # the head of the pull request instead of the merge commit. 27 | - run: git checkout HEAD^2 28 | if: ${{ github.event_name == 'pull_request' }} 29 | 30 | # Initializes the CodeQL tools for scanning. 31 | - name: Initialize CodeQL 32 | uses: github/codeql-action/init@v2 33 | # Override language selection by uncommenting this and choosing your languages 34 | with: 35 | languages: java 36 | 37 | # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). 38 | # If this step fails, then you should remove it and run the build manually (see below) 39 | - name: Autobuild 40 | uses: github/codeql-action/autobuild@v2 41 | 42 | # ℹ️ Command-line programs to run using the OS shell. 43 | # 📚 https://git.io/JvXDl 44 | 45 | # ✏️ If the Autobuild fails above, remove it and uncomment the following three lines 46 | # and modify them (or add more) to build your code if your project 47 | # uses a compiled language 48 | 49 | #- run: | 50 | # make bootstrap 51 | # make release 52 | 53 | - name: Perform CodeQL Analysis 54 | uses: github/codeql-action/analyze@v2 55 | -------------------------------------------------------------------------------- /.github/workflows/maven.yml: -------------------------------------------------------------------------------- 1 | name: Java CI 2 | on: 3 | push: 4 | branches: 5 | - master 6 | - release/* 7 | jobs: 8 | build: 9 | runs-on: ${{ matrix.os }} 10 | strategy: 11 | matrix: 12 | java_version: [11, 14] 13 | os: [ubuntu-latest, macOS-latest] 14 | steps: 15 | - uses: actions/checkout@v3 16 | - name: Set up JDK 17 | uses: actions/setup-java@v3 18 | with: 19 | java-version: ${{ matrix.java_version }} 20 | - name: Print Java and Maven versions 21 | run: ./mvnw -B -s .github/settings.xml -v 22 | - name: Build with Maven 23 | run: ./mvnw -B -s .github/settings.xml install -Dmaven.test.skip=true -Dmaven.javadoc.skip=true 24 | - name: Run tests 25 | run: ./mvnw -B -s .github/settings.xml verify 26 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .classpath 2 | .project 3 | .settings 4 | .idea 5 | *.iml 6 | .DS_Store 7 | *.class 8 | *.jar 9 | hs_err_pid* 10 | target 11 | dependency-reduced-pom.xml 12 | -------------------------------------------------------------------------------- /.mvn/wrapper/MavenWrapperDownloader.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2007-present the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | import java.net.*; 17 | import java.io.*; 18 | import java.nio.channels.*; 19 | import java.util.Properties; 20 | 21 | public class MavenWrapperDownloader { 22 | 23 | private static final String WRAPPER_VERSION = "0.5.5"; 24 | /** 25 | * Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided. 26 | */ 27 | private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/" 28 | + WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar"; 29 | 30 | /** 31 | * Path to the maven-wrapper.properties file, which might contain a downloadUrl property to 32 | * use instead of the default one. 33 | */ 34 | private static final String MAVEN_WRAPPER_PROPERTIES_PATH = 35 | ".mvn/wrapper/maven-wrapper.properties"; 36 | 37 | /** 38 | * Path where the maven-wrapper.jar will be saved to. 39 | */ 40 | private static final String MAVEN_WRAPPER_JAR_PATH = 41 | ".mvn/wrapper/maven-wrapper.jar"; 42 | 43 | /** 44 | * Name of the property which should be used to override the default download url for the wrapper. 45 | */ 46 | private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl"; 47 | 48 | public static void main(String args[]) { 49 | System.out.println("- Downloader started"); 50 | File baseDirectory = new File(args[0]); 51 | System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath()); 52 | 53 | // If the maven-wrapper.properties exists, read it and check if it contains a custom 54 | // wrapperUrl parameter. 55 | File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH); 56 | String url = DEFAULT_DOWNLOAD_URL; 57 | if(mavenWrapperPropertyFile.exists()) { 58 | FileInputStream mavenWrapperPropertyFileInputStream = null; 59 | try { 60 | mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile); 61 | Properties mavenWrapperProperties = new Properties(); 62 | mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream); 63 | url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url); 64 | } catch (IOException e) { 65 | System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'"); 66 | } finally { 67 | try { 68 | if(mavenWrapperPropertyFileInputStream != null) { 69 | mavenWrapperPropertyFileInputStream.close(); 70 | } 71 | } catch (IOException e) { 72 | // Ignore ... 73 | } 74 | } 75 | } 76 | System.out.println("- Downloading from: " + url); 77 | 78 | File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH); 79 | if(!outputFile.getParentFile().exists()) { 80 | if(!outputFile.getParentFile().mkdirs()) { 81 | System.out.println( 82 | "- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'"); 83 | } 84 | } 85 | System.out.println("- Downloading to: " + outputFile.getAbsolutePath()); 86 | try { 87 | downloadFileFromURL(url, outputFile); 88 | System.out.println("Done"); 89 | System.exit(0); 90 | } catch (Throwable e) { 91 | System.out.println("- Error downloading"); 92 | e.printStackTrace(); 93 | System.exit(1); 94 | } 95 | } 96 | 97 | private static void downloadFileFromURL(String urlString, File destination) throws Exception { 98 | if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) { 99 | String username = System.getenv("MVNW_USERNAME"); 100 | char[] password = System.getenv("MVNW_PASSWORD").toCharArray(); 101 | Authenticator.setDefault(new Authenticator() { 102 | @Override 103 | protected PasswordAuthentication getPasswordAuthentication() { 104 | return new PasswordAuthentication(username, password); 105 | } 106 | }); 107 | } 108 | URL website = new URL(urlString); 109 | ReadableByteChannel rbc; 110 | rbc = Channels.newChannel(website.openStream()); 111 | FileOutputStream fos = new FileOutputStream(destination); 112 | fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); 113 | fos.close(); 114 | rbc.close(); 115 | } 116 | 117 | } 118 | -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.1/apache-maven-3.6.1-bin.zip 2 | wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.5/maven-wrapper-0.5.5.jar 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | os: 3 | - linux 4 | jdk: 5 | - openjdk11 6 | services: docker 7 | before_install: 8 | - rm ~/.m2/settings.xml || true 9 | - ulimit -c unlimited -S 10 | - mvn -N io.takari:maven:wrapper 11 | - docker run -d -p 127.0.0.1:8500:8500 consul:latest 12 | after_success: 13 | - bash .travis_after_success.sh 14 | cache: 15 | directories: 16 | - $HOME/.m2 17 | 18 | notifications: 19 | email: false 20 | 21 | env: 22 | global: 23 | # CI_DEPLOY_USERNAME 24 | - secure: "Si7FrUAxJ1ZZlYWR9gLQxR3PJ1uCgi5fsw7aPZwA+XVsIkSZ1tGU0AHU3xpfOaXbx01A3EsSOXdDM6Y3MT7NaucL9vo8/cOAoJTAL71SXhUH73FYeHfzlvG7Ok2wf/kqqL9pWIKn2ucpYZiZw8QXYh+xGddbicJmTwH5NA1pZJ4sWUnhpQ6RFTGgJX1xIGWJ+A5Y1u6SHGXLiplPq1uwBpOZOoho8tO7AtGYtCh64Ns6/+wSyx/BFzKkb8jT6IfKZC3ZVLtlb9Y14iwi8L0+oeVIwPtNTymbVP0TH/iwAk9+7mJ3q/sl6Hx5672n2CjyWJVPzyKODs+O0DmVjEclAeiFLr+JJZkldzv9FXmZrt8WnKhHvwtpN5YzMFiD6qExl2cyd6NHSb0qeLzhWvNhSS0kPzalfr2xF7+tiGuDofUu9dOup5mcZYIk2IZtucSwHfRaphZfxvLWeAD5AdI2uSazTOdS/A/kDbwBjYVUxudElIYdZmZFxuxncdizN59ezAFqvj5MJ3EnW2Cv8HtIapnubVqH2fDrvLRYjQCxtnIKih6jMRKACsgIuewZjKcGvhQ7A5fkW9XD0vYYlMuE3b2m7x1MKrjewpeBJNDb10o9/bdytlsjDiJF7wq+en9B5yZJ+V2MxZ0y7/6/JCzb2WMM+KmGiUUrv0ThptE+KGI=" 25 | # CI_DEPLOY_PASSWORD 26 | - secure: "Oh4nvjDBbZB4pTQ7eymrCt3BFXCcRt2EZkhq+ornXPwBXG6slALaaXekf2+CItv6U9223zBGFyfX3L7HVCzFO9ViHyUSnMjetcgru+/gQXzX6JBHBIxt/AdO4umoMcbwVzPTAXmZuuAvLyxKUFZdD5WnaJqTTKAkuTx8bsgeO6i3dnGNHg/PPJ+HQO0OU+MlVkIZB//jbV+RRmC8OcSJFNn88lwVdlocsS2i0i9piZ6CHknDYCzFSWoSGzY9U12HUaQeYkQSh8ipndtP9dRjr8fec5kOoyzS1Y7NzhkZ86/DKrm3Upvc16mRl8UWaUX1z9RRLn9WnBhNRezvJxFnKVX9AcufgcancENlrLpJ9ljZxW9O1DJKZtTNblIgWH7HSEzZeh1EE+2icgZQ9+N7Ei7REXSC1es+3ATADtUuol6Jd2VyuEr1d1pFtVKuuFTpMVCNtJxkb8Z0oSyLS0MUopd0JtNVl5pQn0FxwSDnwho6RzIsSLiDZWk1mtNGBA8lXv6RQamBgMBO9m9+dT7PHx7mO3YOc52dukrA0uhsJt7uvQlx/qR8ekHjy8Xq3Ni5lGqhw/rEjZvjZZKZRTvypC9CJi0zDZt8aR1qFlRL8Nb1v09e/qGBT7P6zqw1v8v2Jh+rEZnzcn1fHCmuIt14c3Pr4p3qiP9t0NkmAjcQUug=" 27 | -------------------------------------------------------------------------------- /.travis_after_success.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [[ "${TRAVIS_JDK_VERSION}" != "openjdk11" ]]; then 4 | echo "Skipping after_success actions for JDK version \"${TRAVIS_JDK_VERSION}\"" 5 | exit 6 | fi 7 | 8 | if [[ -n ${TRAVIS_TAG} ]]; then 9 | echo "Skipping deployment for tag \"${TRAVIS_TAG}\"" 10 | exit 11 | fi 12 | 13 | if [[ ${TRAVIS_BRANCH} != 'master' ]]; then 14 | echo "Skipping deployment for branch \"${TRAVIS_BRANCH}\"" 15 | exit 16 | fi 17 | 18 | if [[ "$TRAVIS_PULL_REQUEST" = "true" ]]; then 19 | echo "Skipping deployment for pull request" 20 | exit 21 | fi 22 | 23 | ./mvnw -B deploy --settings maven_deploy_settings.xml -Dmaven.test.skip=true 24 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation. 6 | 7 | ## Our Standards 8 | 9 | Examples of behavior that contributes to creating a positive environment include: 10 | 11 | * Using welcoming and inclusive language 12 | * Being respectful of differing viewpoints and experiences 13 | * Gracefully accepting constructive criticism 14 | * Focusing on what is best for the community 15 | * Showing empathy towards other community members 16 | 17 | Examples of unacceptable behavior by participants include: 18 | 19 | * The use of sexualized language or imagery and unwelcome sexual attention or advances 20 | * Trolling, insulting/derogatory comments, and personal or political attacks 21 | * Public or private harassment 22 | * Publishing others' private information, such as a physical or electronic address, without explicit permission 23 | * Other conduct which could reasonably be considered inappropriate in a professional setting 24 | 25 | ## Our Responsibilities 26 | 27 | Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. 28 | 29 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. 30 | 31 | ## Scope 32 | 33 | This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. 34 | 35 | ## Enforcement 36 | 37 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at github@smoketurner.com. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. 38 | 39 | Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. 40 | 41 | ## Attribution 42 | 43 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version] 44 | 45 | [homepage]: http://contributor-covenant.org 46 | [version]: http://contributor-covenant.org/version/1/4/ 47 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Guidelines for contributing to dropwizard-consul. 2 | 3 | Please create a pull request against `master`. 4 | 5 | Try to make the code in the pull request as focused and clean as possible. See 6 | our [style guide](http://google.github.io/styleguide/javaguide.html). If the pull 7 | request is too large, we may ask you to split it into smaller ones. 8 | 9 | # License 10 | By contributing your code, you agree to license your contribution under the 11 | terms of the Apache Public License v2: 12 | https://github.com/smoketurner/dropwizard-consul/blob/master/LICENSE 13 | 14 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Dropwizard Consul Bundle 2 | ======================== 3 | [![Build Status](https://travis-ci.org/smoketurner/dropwizard-consul.svg?branch=master)](https://travis-ci.org/smoketurner/dropwizard-consul) 4 | [![Maven Central](https://img.shields.io/maven-central/v/com.smoketurner.dropwizard/dropwizard-consul.svg?style=flat-square)](https://maven-badges.herokuapp.com/maven-central/com.smoketurner.dropwizard/dropwizard-consul/) 5 | [![GitHub license](https://img.shields.io/github/license/smoketurner/dropwizard-consul.svg?style=flat-square)](https://github.com/smoketurner/dropwizard-consul/tree/master) 6 | [![Become a Patron](https://img.shields.io/badge/Patron-Patreon-red.svg)](https://www.patreon.com/bePatron?u=9567343) 7 | 8 | A bundle for using [Consul](https://consul.io) in Dropwizard applications. Features: 9 | 10 | * Integrated client-side load balancer based on [Ribbon](https://github.com/netflix/ribbon) 11 | * Dropwizard health check that monitors reachablility of Consul 12 | * The Dropwizard service is registered as a Consul service with a Consul-side health check querying the Dropwizard [health check](https://www.dropwizard.io/en/latest/manual/core.html#health-checks) 13 | * Ability to resolve [configuration](https://www.dropwizard.io/en/latest/manual/core.html#configuration) properties from Consul's KV store 14 | * Admin task to toggle Consul's [maintenance](https://www.consul.io/api/agent.html#enable-maintenance-mode) mode 15 | 16 | Dependency Info 17 | --------------- 18 | ```xml 19 | 20 | com.smoketurner.dropwizard 21 | consul-core 22 | 2.0.7-1 23 | 24 | 25 | com.smoketurner.dropwizard 26 | consul-ribbon 27 | 2.0.7-1 28 | 29 | ``` 30 | 31 | Usage 32 | ----- 33 | Add a `ConsulBundle` to your [Application](https://javadoc.io/doc/io.dropwizard/dropwizard-project/latest/io/dropwizard/Application.html) class. 34 | 35 | ```java 36 | @Override 37 | public void initialize(Bootstrap bootstrap) { 38 | // ... 39 | bootstrap.addBundle(new ConsulBundle(getName()) { 40 | @Override 41 | public ConsulFactory getConsulFactory(MyConfiguration configuration) { 42 | return configuration.getConsulFactory(); 43 | } 44 | }); 45 | } 46 | ``` 47 | 48 | The bundle also includes a `ConsulSubsitutor` to retrieve configuration values from the Consul KV store. You can define settings in your YAML configuration file: 49 | 50 | ``` 51 | template: ${helloworld/template:-Hello, %s!} 52 | defaultName: ${helloworld/defaultName:-Stranger} 53 | ``` 54 | 55 | The setting with the path `helloworld/template` will be looked up in the KV store and will be replaced in the configuration file when the application is started. You can specify a default value after the `:-`. This currently does not support dynamically updating values in a running Dropwizard application. 56 | 57 | Configuration 58 | ------------- 59 | For configuring the Consul connection, there is a `ConsulFactory`: 60 | 61 | ```yaml 62 | consul: 63 | # Optional properties 64 | # endpoint for consul (defaults to localhost:8500) 65 | endpoint: localhost:8500 66 | # service port 67 | servicePort: 8080 68 | # check interval frequency 69 | checkInterval: 1 second 70 | ``` 71 | 72 | Example Application 73 | ------------------- 74 | This bundle includes a modified version of the `HelloWorldApplication` from Dropwizard's [Getting Started](https://www.dropwizard.io/1.3.12/docs/getting-started.html) documentation. 75 | 76 | You can execute this application by first starting Consul on your local machine then running: 77 | 78 | ``` 79 | mvn clean package 80 | java -jar consul-example/target/consul-example-2.0.7-4-SNAPSHOT.jar server consul-example/hello-world.yml 81 | ``` 82 | 83 | This will start the application on port `8080` (admin port `8180`). This application demonstrations the following Consul integration points: 84 | 85 | - The application is registered as a service with Consul (with the [service port](https://www.consul.io/docs/agent/services.html) set to the applicationConnectors port in the configuration file. 86 | - The application will lookup any variables in the configuration file from Consul upon startup (it defaults to connecting to a Consul agent running on `localhost:8500` for this functionality) 87 | - The application exposes an additional HTTP endpoint for querying Consul for available healthy services: 88 | ``` 89 | curl -X GET localhost:8080/consul/hello-world -i 90 | HTTP/1.1 200 OK 91 | Date: Mon, 25 Jan 2016 03:42:10 GMT 92 | Content-Type: application/json 93 | Vary: Accept-Encoding 94 | Content-Length: 870 95 | 96 | [ 97 | { 98 | "Node": { 99 | "Node": "mac", 100 | "Address": "192.168.1.100", 101 | "Datacenter": "dc1", 102 | "TaggedAddresses": { 103 | "wan": "192.168.1.100", 104 | "lan": "192.168.1.100" 105 | }, 106 | "Meta": { 107 | "consul-network-segment": "" 108 | } 109 | }, 110 | "Service": { 111 | "ID": "test123", 112 | "Service": "hello-world", 113 | "EnableTagOverride": false, 114 | "Tags": [], 115 | "Address": "", 116 | "Meta": { 117 | "scheme": "http" 118 | }, 119 | "Port": 8080, 120 | "Weights": { 121 | "Passing": 1, 122 | "Warning": 1 123 | } 124 | }, 125 | "Checks": [ 126 | { 127 | "Node": "mac", 128 | "CheckID": "serfHealth", 129 | "Name": "Serf Health Status", 130 | "Status": "passing", 131 | "Notes": "", 132 | "Output": "Agent alive and reachable", 133 | "ServiceID": "", 134 | "ServiceName": "", 135 | "ServiceTags": [] 136 | }, 137 | { 138 | "Node": "mac", 139 | "CheckID": "service:test123", 140 | "Name": "Service 'hello-world' check", 141 | "Status": "passing", 142 | "Notes": "", 143 | "Output": "HTTP GET http:\/\/127.0.0.1:8180\/healthcheck: 200 OK Output: {\"consul\":{\"healthy\":true},\"deadlocks\":{\"healthy\":true}}", 144 | "ServiceID": "test123", 145 | "ServiceName": "hello-world", 146 | "ServiceTags": [] 147 | } 148 | ] 149 | } 150 | ] 151 | ``` 152 | - The application will periodically checkin with Consul every second to notify the service check that it is still alive 153 | - Upon shutdown, the application will deregister itself from Consul 154 | 155 | Credits 156 | ------- 157 | This bundle was inspired by an older bundle (Dropwizard 0.6.2) that [Chris Gray](https://github.com/chrisgray) created at https://github.com/chrisgray/dropwizard-consul. I also incorporated the configuration provider changes from https://github.com/remmelt/dropwizard-consul-config-provider 158 | 159 | Support 160 | ------- 161 | Please file bug reports and feature requests in [GitHub issues](https://github.com/smoketurner/dropwizard-consul/issues). 162 | 163 | License 164 | ------- 165 | Copyright (c) 2020 Smoke Turner, LLC 166 | 167 | This library is licensed under the Apache License, Version 2.0. 168 | 169 | See http://www.apache.org/licenses/LICENSE-2.0.html or the [LICENSE](LICENSE) file in this repository for the full license text. 170 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | ## Supported Versions 4 | 5 | Use this section to tell people about which versions of your project are 6 | currently being supported with security updates. 7 | 8 | | Version | Supported | 9 | | ------- | ------------------ | 10 | | < 1.3.0 | :x: | 11 | | 1.3.x | :white_check_mark: | 12 | | 2.0.x | :white_check_mark: | 13 | 14 | ## Reporting a Vulnerability 15 | 16 | Please email any security-related issues or concerns to security@smoketurner.com. 17 | You should expect an email reply within 24-48 hours. If the vulnerability is accepeted, 18 | a new pull request will be created to address the issue and a new patch release 19 | created. 20 | -------------------------------------------------------------------------------- /consul-core/LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /consul-core/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 20 | 4.0.0 21 | 22 | 23 | com.smoketurner.dropwizard 24 | dropwizard-consul 25 | 2.0.12-2-SNAPSHOT 26 | 27 | 28 | consul-core 29 | Dropwizard Consul Bundle 30 | 31 | 32 | 33 | io.dropwizard 34 | dropwizard-core 35 | 36 | 37 | com.orbitz.consul 38 | consul-client 39 | 1.5.3 40 | 41 | 42 | commons-net 43 | commons-net 44 | 3.9.0 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /consul-core/src/main/java/com/smoketurner/dropwizard/consul/ConsulBundle.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2019 Smoke Turner, LLC (github@smoketurner.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.smoketurner.dropwizard.consul; 17 | 18 | import com.google.common.annotations.VisibleForTesting; 19 | import com.google.common.base.Strings; 20 | import com.google.common.collect.ImmutableMap; 21 | import com.google.common.net.HostAndPort; 22 | import com.orbitz.consul.Consul; 23 | import com.orbitz.consul.ConsulException; 24 | import com.smoketurner.dropwizard.consul.config.ConsulSubstitutor; 25 | import com.smoketurner.dropwizard.consul.core.ConsulAdvertiser; 26 | import com.smoketurner.dropwizard.consul.core.ConsulServiceListener; 27 | import com.smoketurner.dropwizard.consul.health.ConsulHealthCheck; 28 | import com.smoketurner.dropwizard.consul.managed.ConsulAdvertiserManager; 29 | import com.smoketurner.dropwizard.consul.task.MaintenanceTask; 30 | import io.dropwizard.Configuration; 31 | import io.dropwizard.ConfiguredBundle; 32 | import io.dropwizard.configuration.SubstitutingSourceProvider; 33 | import io.dropwizard.setup.Bootstrap; 34 | import io.dropwizard.setup.Environment; 35 | import io.dropwizard.util.Duration; 36 | import java.util.Objects; 37 | import java.util.Optional; 38 | import java.util.UUID; 39 | import java.util.concurrent.Executors; 40 | import java.util.concurrent.ScheduledExecutorService; 41 | import org.slf4j.Logger; 42 | import org.slf4j.LoggerFactory; 43 | 44 | /** 45 | * Replace variables with values from Consul KV. By default, this only works with a Consul agent 46 | * running on localhost:8500 (the default) as there's no way to configure Consul in the initialize 47 | * methods. You may override {@link #getConsulAgentHost()} and {@link #getConsulAgentPort()} to 48 | * provide other defaults. 49 | * 50 | * @param The configuration class for your Dropwizard Application. 51 | */ 52 | public abstract class ConsulBundle 53 | implements ConfiguredBundle, ConsulConfiguration { 54 | 55 | private static final Logger LOGGER = LoggerFactory.getLogger(ConsulBundle.class); 56 | private static final String CONSUL_AUTH_HEADER_KEY = "X-Consul-Token"; 57 | 58 | private final String defaultServiceName; 59 | private final boolean strict; 60 | private final boolean substitutionInVariables; 61 | 62 | /** 63 | * Constructor 64 | * 65 | * @param name Service Name 66 | */ 67 | public ConsulBundle(final String name) { 68 | this(name, false); 69 | } 70 | 71 | /** 72 | * @param name Service Name 73 | * @param strict If true, the application fails fast if a key cannot be found in Consul KV 74 | */ 75 | public ConsulBundle(final String name, final boolean strict) { 76 | this(name, strict, false); 77 | } 78 | 79 | /** 80 | * @param name Service Name 81 | * @param strict If true, the application fails fast if a key cannot be found in Consul KV 82 | * @param substitutionInVariables If true, substitution will be done within variable names. 83 | */ 84 | public ConsulBundle( 85 | final String name, final boolean strict, final boolean substitutionInVariables) { 86 | this.defaultServiceName = Objects.requireNonNull(name); 87 | this.strict = strict; 88 | this.substitutionInVariables = substitutionInVariables; 89 | } 90 | 91 | @Override 92 | public void initialize(Bootstrap bootstrap) { 93 | // Replace variables with values from Consul KV. Please override 94 | // getConsulAgentHost() and getConsulAgentPort() if Consul is not 95 | // listening on the default localhost:8500. 96 | try { 97 | LOGGER.debug("Connecting to Consul at {}:{}", getConsulAgentHost(), getConsulAgentPort()); 98 | 99 | final Consul.Builder builder = 100 | Consul.builder() 101 | .withHostAndPort(HostAndPort.fromParts(getConsulAgentHost(), getConsulAgentPort())); 102 | 103 | getConsulAclToken() 104 | .ifPresent( 105 | token -> { 106 | // setting both ACL token here and with header, supplying an 107 | // auth header. This should cover both use cases: endpoint 108 | // supports legacy ?token query param and other case 109 | // in which endpoint requires an X-Consul-Token header. 110 | // @see https://www.consul.io/api/index.html#acls 111 | 112 | LOGGER.debug("Using Consul ACL token: {}", token); 113 | 114 | builder 115 | .withAclToken(token) 116 | .withHeaders(ImmutableMap.of(CONSUL_AUTH_HEADER_KEY, token)); 117 | }); 118 | 119 | // using Consul as a configuration substitution provider 120 | bootstrap.setConfigurationSourceProvider( 121 | new SubstitutingSourceProvider( 122 | bootstrap.getConfigurationSourceProvider(), 123 | new ConsulSubstitutor(builder.build(), strict, substitutionInVariables))); 124 | 125 | } catch (ConsulException e) { 126 | LOGGER.warn( 127 | "Unable to query Consul running on {}:{}," + " disabling configuration substitution", 128 | getConsulAgentHost(), 129 | getConsulAgentPort(), 130 | e); 131 | } 132 | } 133 | 134 | @Override 135 | public void run(C configuration, Environment environment) throws Exception { 136 | final ConsulFactory consulConfig = getConsulFactory(configuration); 137 | if (!consulConfig.isEnabled()) { 138 | LOGGER.warn("Consul bundle disabled."); 139 | } else { 140 | runEnabled(consulConfig, environment); 141 | } 142 | } 143 | 144 | protected void runEnabled(ConsulFactory consulConfig, Environment environment) { 145 | if (Strings.isNullOrEmpty(consulConfig.getServiceName())) { 146 | consulConfig.setSeviceName(defaultServiceName); 147 | } 148 | setupEnvironment(consulConfig, environment); 149 | } 150 | 151 | protected void setupEnvironment(ConsulFactory consulConfig, Environment environment) { 152 | 153 | final Consul consul = consulConfig.build(); 154 | final String serviceId = consulConfig.getServiceId().orElse(UUID.randomUUID().toString()); 155 | final ConsulAdvertiser advertiser = 156 | new ConsulAdvertiser(environment, consulConfig, consul, serviceId); 157 | 158 | final Optional retryInterval = consulConfig.getRetryInterval(); 159 | final Optional scheduler = 160 | retryInterval.map(i -> Executors.newScheduledThreadPool(1)); 161 | 162 | // Register a Jetty listener to get the listening host and port 163 | environment 164 | .lifecycle() 165 | .addServerLifecycleListener( 166 | new ConsulServiceListener(advertiser, retryInterval, scheduler)); 167 | 168 | // Register a ping healthcheck to the Consul agent 169 | environment.healthChecks().register("consul", new ConsulHealthCheck(consul)); 170 | 171 | // Register a shutdown manager to deregister the service 172 | environment.lifecycle().manage(new ConsulAdvertiserManager(advertiser, scheduler)); 173 | 174 | // Add an administrative task to toggle maintenance mode 175 | environment.admin().addTask(new MaintenanceTask(consul, serviceId)); 176 | } 177 | 178 | /** 179 | * Override as necessary to provide an alternative Consul Agent Host. This is only required if 180 | * using Consul KV for configuration variable substitution. 181 | * 182 | * @return By default, "localhost" 183 | */ 184 | @VisibleForTesting 185 | public String getConsulAgentHost() { 186 | return Consul.DEFAULT_HTTP_HOST; 187 | } 188 | 189 | /** 190 | * Override as necessary to provide an alternative Consul Agent Port. This is only required if 191 | * using Consul KV for configuration variable substitution. 192 | * 193 | * @return By default, 8500 194 | */ 195 | @VisibleForTesting 196 | public int getConsulAgentPort() { 197 | return Consul.DEFAULT_HTTP_PORT; 198 | } 199 | 200 | /** 201 | * Override as necessary to provide an alternative ACL Token. This is only required if using 202 | * Consul KV for configuration variable substitution. 203 | * 204 | * @return By default, empty string (no ACL support) 205 | */ 206 | @VisibleForTesting 207 | public Optional getConsulAclToken() { 208 | return Optional.empty(); 209 | } 210 | } 211 | -------------------------------------------------------------------------------- /consul-core/src/main/java/com/smoketurner/dropwizard/consul/ConsulConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2019 Smoke Turner, LLC (github@smoketurner.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.smoketurner.dropwizard.consul; 17 | 18 | import io.dropwizard.Configuration; 19 | 20 | @FunctionalInterface 21 | public interface ConsulConfiguration { 22 | ConsulFactory getConsulFactory(C configuration); 23 | } 24 | -------------------------------------------------------------------------------- /consul-core/src/main/java/com/smoketurner/dropwizard/consul/ConsulFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2019 Smoke Turner, LLC (github@smoketurner.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.smoketurner.dropwizard.consul; 17 | 18 | import com.fasterxml.jackson.annotation.JsonIgnore; 19 | import com.fasterxml.jackson.annotation.JsonProperty; 20 | import com.google.common.base.Preconditions; 21 | import com.google.common.collect.ImmutableMap; 22 | import com.google.common.net.HostAndPort; 23 | import com.orbitz.consul.Consul; 24 | import io.dropwizard.util.Duration; 25 | import io.dropwizard.validation.MinDuration; 26 | import java.util.Map; 27 | import java.util.Objects; 28 | import java.util.Optional; 29 | import java.util.concurrent.TimeUnit; 30 | import java.util.function.Supplier; 31 | import javax.annotation.Nullable; 32 | import javax.validation.constraints.NotNull; 33 | import org.apache.commons.net.util.SubnetUtils; 34 | 35 | public class ConsulFactory { 36 | private static final String CONSUL_AUTH_HEADER_KEY = "X-Consul-Token"; 37 | 38 | @NotNull 39 | private HostAndPort endpoint = 40 | HostAndPort.fromParts(Consul.DEFAULT_HTTP_HOST, Consul.DEFAULT_HTTP_PORT); 41 | 42 | @Nullable private String serviceName; 43 | 44 | private boolean enabled = true; 45 | private Optional serviceId = Optional.empty(); 46 | private Optional servicePort = Optional.empty(); 47 | private Optional adminPort = Optional.empty(); 48 | private Optional serviceAddress = Optional.empty(); 49 | private Optional serviceSubnet = Optional.empty(); 50 | private Optional> serviceAddressSupplier = Optional.empty(); 51 | private Optional> tags = Optional.empty(); 52 | private Optional aclToken = Optional.empty(); 53 | private Optional> serviceMeta = Optional.empty(); 54 | private boolean servicePing = true; 55 | 56 | @Nullable 57 | @MinDuration(value = 1, unit = TimeUnit.SECONDS) 58 | private Duration retryInterval; 59 | 60 | @NotNull 61 | @MinDuration(value = 1, unit = TimeUnit.SECONDS) 62 | private Duration checkInterval = Duration.seconds(1); 63 | 64 | @NotNull 65 | @MinDuration(value = 1, unit = TimeUnit.MINUTES) 66 | private Duration deregisterInterval = Duration.minutes(1); 67 | 68 | private Optional healthCheckPath = Optional.empty(); 69 | 70 | @JsonProperty 71 | public boolean isEnabled() { 72 | return enabled; 73 | } 74 | 75 | @JsonProperty 76 | public void setEnabled(boolean enabled) { 77 | this.enabled = enabled; 78 | } 79 | 80 | @JsonProperty 81 | public HostAndPort getEndpoint() { 82 | return endpoint; 83 | } 84 | 85 | @JsonProperty 86 | public void setEndpoint(HostAndPort endpoint) { 87 | this.endpoint = endpoint; 88 | } 89 | 90 | @JsonProperty 91 | public Optional getServiceId() { 92 | return serviceId; 93 | } 94 | 95 | @JsonProperty 96 | public void setServiceId(@Nullable String serviceId) { 97 | this.serviceId = Optional.ofNullable(serviceId); 98 | } 99 | 100 | @Nullable 101 | @JsonProperty 102 | public String getServiceName() { 103 | return serviceName; 104 | } 105 | 106 | @JsonProperty 107 | public void setServiceName(@Nullable String serviceName) { 108 | this.serviceName = serviceName; 109 | } 110 | 111 | @Deprecated 112 | @JsonProperty 113 | public void setSeviceName(@Nullable String serviceName) { 114 | this.serviceName = serviceName; 115 | } 116 | 117 | @JsonProperty 118 | public Optional> getTags() { 119 | return tags; 120 | } 121 | 122 | @JsonProperty 123 | public void setTags(Iterable tags) { 124 | this.tags = Optional.ofNullable(tags); 125 | } 126 | 127 | @JsonProperty 128 | public Optional getServicePort() { 129 | return servicePort; 130 | } 131 | 132 | @JsonProperty 133 | public void setServicePort(Integer servicePort) { 134 | this.servicePort = Optional.ofNullable(servicePort); 135 | } 136 | 137 | @JsonProperty 138 | public Optional getAdminPort() { 139 | return adminPort; 140 | } 141 | 142 | @JsonProperty 143 | public void setAdminPort(Integer adminPort) { 144 | this.adminPort = Optional.ofNullable(adminPort); 145 | } 146 | 147 | @JsonProperty 148 | public Optional getServiceAddress() { 149 | return serviceAddress; 150 | } 151 | 152 | @JsonProperty 153 | public void setServiceAddress(String serviceAddress) { 154 | this.serviceAddress = Optional.ofNullable(serviceAddress); 155 | } 156 | 157 | @JsonProperty 158 | public Optional getRetryInterval() { 159 | return Optional.ofNullable(retryInterval); 160 | } 161 | 162 | @JsonProperty 163 | public void setRetryInterval(@Nullable Duration interval) { 164 | this.retryInterval = interval; 165 | } 166 | 167 | @JsonProperty 168 | public Duration getCheckInterval() { 169 | return checkInterval; 170 | } 171 | 172 | @JsonProperty 173 | public void setCheckInterval(Duration interval) { 174 | this.checkInterval = interval; 175 | } 176 | 177 | @JsonProperty 178 | public Duration getDeregisterInterval() { 179 | return deregisterInterval; 180 | } 181 | 182 | @JsonProperty 183 | public void setDeregisterInterval(Duration interval) { 184 | this.deregisterInterval = interval; 185 | } 186 | 187 | @JsonProperty 188 | public Optional getAclToken() { 189 | return aclToken; 190 | } 191 | 192 | @JsonProperty 193 | public void setAclToken(@Nullable String aclToken) { 194 | this.aclToken = Optional.ofNullable(aclToken); 195 | } 196 | 197 | @JsonProperty 198 | public Optional> getServiceMeta() { 199 | return serviceMeta; 200 | } 201 | 202 | @JsonProperty 203 | public void setServiceMeta(Map serviceMeta) { 204 | this.serviceMeta = Optional.ofNullable(serviceMeta); 205 | } 206 | 207 | @JsonProperty 208 | public boolean isServicePing() { 209 | return servicePing; 210 | } 211 | 212 | @JsonProperty 213 | public void setServicePing(boolean servicePing) { 214 | this.servicePing = servicePing; 215 | } 216 | 217 | public Optional getServiceSubnet() { 218 | return serviceSubnet; 219 | } 220 | 221 | public void setServiceSubnet(String serviceSubnet) { 222 | Preconditions.checkArgument( 223 | isValidCidrIp(serviceSubnet), "%s is not a valid Subnet in CIDR notation", serviceSubnet); 224 | this.serviceSubnet = Optional.ofNullable(serviceSubnet); 225 | } 226 | 227 | public void setServiceAddressSupplier(Supplier serviceAddressSupplier) { 228 | this.serviceAddressSupplier = Optional.ofNullable(serviceAddressSupplier); 229 | } 230 | 231 | public Optional> getServiceAddressSupplier() { 232 | return serviceAddressSupplier; 233 | } 234 | 235 | public Optional getHealthCheckPath() { 236 | return healthCheckPath; 237 | } 238 | 239 | public void setHealthCheckPath(String healthCheckPath) { 240 | this.healthCheckPath = Optional.ofNullable(healthCheckPath); 241 | } 242 | 243 | @JsonIgnore 244 | public Consul build() { 245 | 246 | final Consul.Builder builder = Consul.builder().withHostAndPort(endpoint).withPing(servicePing); 247 | 248 | aclToken.ifPresent( 249 | token -> { 250 | // setting both acl token here and with header, supplying an auth 251 | // header. This should cover both use cases: endpoint supports 252 | // legacy ?token query param and other case in which endpoint 253 | // requires an X-Consul-Token header. 254 | // @see https://www.consul.io/api/index.html#acls 255 | builder.withAclToken(token).withHeaders(ImmutableMap.of(CONSUL_AUTH_HEADER_KEY, token)); 256 | }); 257 | 258 | return builder.build(); 259 | } 260 | 261 | @Override 262 | public int hashCode() { 263 | return Objects.hash( 264 | endpoint, 265 | serviceName, 266 | enabled, 267 | servicePort, 268 | adminPort, 269 | serviceAddress, 270 | tags, 271 | retryInterval, 272 | checkInterval, 273 | deregisterInterval, 274 | aclToken, 275 | serviceMeta, 276 | servicePing); 277 | } 278 | 279 | @Override 280 | public boolean equals(Object obj) { 281 | if (this == obj) { 282 | return true; 283 | } 284 | if (obj == null || getClass() != obj.getClass()) { 285 | return false; 286 | } 287 | final ConsulFactory other = (ConsulFactory) obj; 288 | return Objects.equals(this.endpoint, other.endpoint) 289 | && Objects.equals(this.serviceName, other.serviceName) 290 | && Objects.equals(this.enabled, other.enabled) 291 | && Objects.equals(this.servicePort, other.servicePort) 292 | && Objects.equals(this.adminPort, other.adminPort) 293 | && Objects.equals(this.serviceAddress, other.serviceAddress) 294 | && Objects.equals(this.tags, other.tags) 295 | && Objects.equals(this.retryInterval, other.retryInterval) 296 | && Objects.equals(this.checkInterval, other.checkInterval) 297 | && Objects.equals(this.deregisterInterval, other.deregisterInterval) 298 | && Objects.equals(this.aclToken, other.aclToken) 299 | && Objects.equals(this.serviceMeta, other.serviceMeta) 300 | && Objects.equals(this.servicePing, other.servicePing); 301 | } 302 | 303 | private static boolean isValidCidrIp(String cidrIp) { 304 | boolean isValid = true; 305 | try { 306 | new SubnetUtils(cidrIp); 307 | } catch (IllegalArgumentException e) { 308 | isValid = false; 309 | } 310 | return isValid; 311 | } 312 | } 313 | -------------------------------------------------------------------------------- /consul-core/src/main/java/com/smoketurner/dropwizard/consul/config/ConsulLookup.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2019 Smoke Turner, LLC (github@smoketurner.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.smoketurner.dropwizard.consul.config; 17 | 18 | import com.orbitz.consul.Consul; 19 | import io.dropwizard.configuration.UndefinedEnvironmentVariableException; 20 | import java.util.Objects; 21 | import java.util.Optional; 22 | import javax.annotation.Nullable; 23 | import org.apache.commons.text.lookup.StringLookup; 24 | import org.slf4j.Logger; 25 | import org.slf4j.LoggerFactory; 26 | 27 | /** 28 | * A custom {@link org.apache.commons.text.lookup.StringLookup} implementation using Consul KV as 29 | * lookup source. 30 | */ 31 | public class ConsulLookup implements StringLookup { 32 | 33 | private static final Logger LOGGER = LoggerFactory.getLogger(ConsulLookup.class); 34 | private final boolean strict; 35 | private final Consul consul; 36 | 37 | /** 38 | * Create a new instance with strict behavior. 39 | * 40 | * @param consul Consul client 41 | */ 42 | public ConsulLookup(final Consul consul) { 43 | this(consul, true); 44 | } 45 | 46 | /** 47 | * Constructor 48 | * 49 | * @param consul Consul client 50 | * @param strict {@code true} if looking up undefined environment variables should throw a {@link 51 | * UndefinedEnvironmentVariableException}, {@code false} otherwise. 52 | * @throws UndefinedEnvironmentVariableException if the environment variable doesn't exist and 53 | * strict behavior is enabled. 54 | */ 55 | public ConsulLookup(final Consul consul, final boolean strict) { 56 | this.consul = Objects.requireNonNull(consul); 57 | this.strict = strict; 58 | } 59 | 60 | /** 61 | * {@inheritDoc} 62 | * 63 | * @throws UndefinedEnvironmentVariableException if the environment variable doesn't exist and 64 | * strict behavior is enabled. 65 | */ 66 | @Nullable 67 | @Override 68 | public String lookup(String key) { 69 | try { 70 | final Optional value = consul.keyValueClient().getValueAsString(key); 71 | if (value.isPresent()) { 72 | return value.get(); 73 | } 74 | } catch (Exception e) { 75 | LOGGER.warn("Unable to lookup key in consul", e); 76 | } 77 | 78 | if (strict) { 79 | throw new UndefinedEnvironmentVariableException( 80 | String.format( 81 | "The variable with key '%s' is not found in the Consul KV store;" 82 | + " could not substitute the expression '${%s}'.", 83 | key, key)); 84 | } 85 | return null; 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /consul-core/src/main/java/com/smoketurner/dropwizard/consul/config/ConsulSubstitutor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2019 Smoke Turner, LLC (github@smoketurner.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.smoketurner.dropwizard.consul.config; 17 | 18 | import com.orbitz.consul.Consul; 19 | import io.dropwizard.configuration.EnvironmentVariableSubstitutor; 20 | import io.dropwizard.configuration.UndefinedEnvironmentVariableException; 21 | 22 | /** A custom {@link EnvironmentVariableSubstitutor} using Consul KV as lookup source. */ 23 | public class ConsulSubstitutor extends EnvironmentVariableSubstitutor { 24 | 25 | public ConsulSubstitutor(final Consul consul) { 26 | this(consul, true, false); 27 | } 28 | 29 | public ConsulSubstitutor(final Consul consul, boolean strict) { 30 | this(consul, strict, false); 31 | } 32 | 33 | /** 34 | * Constructor 35 | * 36 | * @param consul Consul client 37 | * @param strict {@code true} if looking up undefined environment variables should throw a {@link 38 | * UndefinedEnvironmentVariableException}, {@code false} otherwise. 39 | * @param substitutionInVariables a flag whether substitution is done in variable names. 40 | * @see org.apache.commons.text.StringSubstitutor#setEnableSubstitutionInVariables(boolean) 41 | */ 42 | public ConsulSubstitutor(final Consul consul, boolean strict, boolean substitutionInVariables) { 43 | super(strict); 44 | this.setVariableResolver(new ConsulLookup(consul, strict)); 45 | this.setEnableSubstitutionInVariables(substitutionInVariables); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /consul-core/src/main/java/com/smoketurner/dropwizard/consul/core/ConsulAdvertiser.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2019 Smoke Turner, LLC (github@smoketurner.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.smoketurner.dropwizard.consul.core; 17 | 18 | import static java.util.Objects.nonNull; 19 | 20 | import com.orbitz.consul.AgentClient; 21 | import com.orbitz.consul.Consul; 22 | import com.orbitz.consul.ConsulException; 23 | import com.orbitz.consul.model.agent.ImmutableRegCheck; 24 | import com.orbitz.consul.model.agent.ImmutableRegistration; 25 | import com.orbitz.consul.model.agent.Registration; 26 | import com.smoketurner.dropwizard.consul.ConsulFactory; 27 | import io.dropwizard.setup.Environment; 28 | import java.util.Collection; 29 | import java.util.Map; 30 | import java.util.Objects; 31 | import java.util.Optional; 32 | import java.util.concurrent.atomic.AtomicReference; 33 | import java.util.function.Supplier; 34 | import javax.ws.rs.core.UriBuilder; 35 | import org.apache.commons.net.util.SubnetUtils; 36 | import org.slf4j.Logger; 37 | import org.slf4j.LoggerFactory; 38 | 39 | public class ConsulAdvertiser { 40 | 41 | private static final Logger LOGGER = LoggerFactory.getLogger(ConsulAdvertiser.class); 42 | private static final String LOCALHOST = "127.0.0.1"; 43 | private static final String DEFAULT_HEALTH_CHECK_PATH = "healthcheck"; 44 | 45 | private final AtomicReference servicePort = new AtomicReference<>(); 46 | private final AtomicReference serviceAdminPort = new AtomicReference<>(); 47 | private final AtomicReference serviceAddress = new AtomicReference<>(); 48 | private final AtomicReference serviceSubnet = new AtomicReference<>(); 49 | private final AtomicReference> serviceAddressSupplier = new AtomicReference<>(); 50 | private final AtomicReference aclToken = new AtomicReference<>(); 51 | private final AtomicReference> tags = new AtomicReference<>(); 52 | private final AtomicReference> serviceMeta = new AtomicReference<>(); 53 | private final Environment environment; 54 | private final ConsulFactory configuration; 55 | private final Consul consul; 56 | private final String serviceId; 57 | private final AtomicReference healthCheckPath = new AtomicReference<>(); 58 | 59 | /** 60 | * Constructor 61 | * 62 | * @param environment Dropwizard environment 63 | * @param configuration Consul configuration 64 | * @param consul Consul client 65 | * @param serviceId Consul service ID 66 | */ 67 | public ConsulAdvertiser( 68 | final Environment environment, 69 | final ConsulFactory configuration, 70 | final Consul consul, 71 | final String serviceId) { 72 | 73 | this.environment = Objects.requireNonNull(environment, "environment == null"); 74 | this.configuration = Objects.requireNonNull(configuration, "configuration == null"); 75 | this.consul = Objects.requireNonNull(consul, "consul == null"); 76 | this.serviceId = Objects.requireNonNull(serviceId, "serviceId == null"); 77 | 78 | configuration 79 | .getServicePort() 80 | .ifPresent( 81 | port -> { 82 | LOGGER.info("Using \"{}\" as servicePort from configuration file", port); 83 | servicePort.set(port); 84 | }); 85 | 86 | configuration 87 | .getAdminPort() 88 | .ifPresent( 89 | port -> { 90 | LOGGER.info("Using \"{}\" as adminPort from configuration file", port); 91 | serviceAdminPort.set(port); 92 | }); 93 | 94 | configuration 95 | .getServiceAddress() 96 | .ifPresent( 97 | address -> { 98 | LOGGER.info("Using \"{}\" as serviceAddress from configuration file", address); 99 | serviceAddress.set(address); 100 | }); 101 | 102 | configuration 103 | .getServiceSubnet() 104 | .ifPresent( 105 | subnet -> { 106 | LOGGER.info("Using \"{}\" as serviceSubnet from configuration file", subnet); 107 | serviceSubnet.set(subnet); 108 | }); 109 | 110 | configuration 111 | .getServiceAddressSupplier() 112 | .ifPresent( 113 | supplier -> { 114 | LOGGER.info("Using \"{}\" as serviceSupplier from configuration file", supplier); 115 | serviceAddressSupplier.set(supplier); 116 | }); 117 | 118 | configuration 119 | .getTags() 120 | .ifPresent( 121 | newTags -> { 122 | LOGGER.info("Using \"{}\" as tags from the configuration file", newTags); 123 | tags.set(newTags); 124 | }); 125 | 126 | configuration 127 | .getAclToken() 128 | .ifPresent( 129 | token -> { 130 | LOGGER.info("Using \"{}\" as ACL token from the configuration file.", token); 131 | aclToken.set(token); 132 | }); 133 | 134 | configuration 135 | .getServiceMeta() 136 | .ifPresent( 137 | newServiceMeta -> { 138 | LOGGER.info( 139 | "Using \"{}\" as serviceMeta from the configuration file", newServiceMeta); 140 | serviceMeta.set(newServiceMeta); 141 | }); 142 | 143 | configuration 144 | .getHealthCheckPath() 145 | .ifPresent( 146 | newHealthCheckPath -> { 147 | LOGGER.info( 148 | "Using \"{}\" as health check path from the configuration file", newHealthCheckPath); 149 | healthCheckPath.set(newHealthCheckPath); 150 | }); 151 | } 152 | 153 | /** 154 | * Return the Service ID 155 | * 156 | * @return service ID 157 | */ 158 | public String getServiceId() { 159 | return serviceId; 160 | } 161 | 162 | public boolean register( 163 | final String applicationScheme, final int applicationPort, final int adminPort) { 164 | return register(applicationScheme, applicationPort, adminPort, null); 165 | } 166 | 167 | /** 168 | * Register the service with Consul 169 | * 170 | * @param applicationScheme Scheme the server is listening on 171 | * @param applicationPort Port the service is listening on 172 | * @param adminPort Port the admin server is listening on 173 | * @param ipAddresses IP addresses of the available that the application is listening on 174 | * @return true if successfully registered, otherwise false 175 | * @throws ConsulException When registration fails 176 | */ 177 | public boolean register( 178 | final String applicationScheme, 179 | final int applicationPort, 180 | final int adminPort, 181 | Collection ipAddresses) { 182 | final AgentClient agent = consul.agentClient(); 183 | if (agent.isRegistered(serviceId)) { 184 | LOGGER.info( 185 | "Service ({}) [{}] already registered", configuration.getServiceName(), serviceId); 186 | return false; 187 | } 188 | 189 | // If we haven't set the servicePort via the configuration file already, 190 | // set it from the listening applicationPort. 191 | servicePort.compareAndSet(null, applicationPort); 192 | serviceAdminPort.compareAndSet(null, adminPort); 193 | healthCheckPath.compareAndSet(null, DEFAULT_HEALTH_CHECK_PATH); 194 | 195 | LOGGER.info( 196 | "Registering service ({}) [{}] on port {} (admin port {}) with a health check at {} with interval of {}s", 197 | configuration.getServiceName(), 198 | serviceId, 199 | servicePort.get(), 200 | serviceAdminPort.get(), 201 | healthCheckPath.get(), 202 | configuration.getCheckInterval().toSeconds()); 203 | 204 | final Registration.RegCheck check = 205 | ImmutableRegCheck.builder() 206 | .http(getHealthCheckUrl(applicationScheme, ipAddresses)) 207 | .interval(String.format("%ds", configuration.getCheckInterval().toSeconds())) 208 | .deregisterCriticalServiceAfter( 209 | String.format("%dm", configuration.getDeregisterInterval().toMinutes())) 210 | .build(); 211 | 212 | final ImmutableRegistration.Builder builder = 213 | ImmutableRegistration.builder().port(servicePort.get()).check(check).id(serviceId); 214 | 215 | final String serviceName = configuration.getServiceName(); 216 | if (serviceName != null) { 217 | builder.name(serviceName); 218 | } 219 | 220 | // If we have set the serviceAddress, add it to the registration. 221 | getServiceAddress(ipAddresses).ifPresent(builder::address); 222 | 223 | // If we have tags, add them to the registration. 224 | if (tags.get() != null) { 225 | builder.tags(tags.get()); 226 | } 227 | 228 | // If we have service meta, add them to the registration. 229 | if (serviceMeta.get() != null) { 230 | builder.meta(serviceMeta.get()); 231 | } 232 | 233 | builder.putMeta("scheme", applicationScheme); 234 | 235 | consul.agentClient().register(builder.build()); 236 | return true; 237 | } 238 | 239 | /** 240 | * Returns the service address from best provided options. The order of precedence is as follows: 241 | * serviceAddress, if provided, then the subnet resolution, lastly the supplier. If none of the 242 | * above is provided or matched, Optional.empty() is returned. 243 | * 244 | * @param ipAddresses List of ipAddresses the application is listening on. 245 | * @return Optional of the host to register as the service address or empty otherwise 246 | */ 247 | private Optional getServiceAddress(Collection ipAddresses) { 248 | if (nonNull(serviceAddress.get())) { 249 | return Optional.of(serviceAddress.get()); 250 | } 251 | 252 | if (nonNull(ipAddresses) && !ipAddresses.isEmpty() && nonNull(serviceSubnet.get())) { 253 | Optional ip = findFirstEligibleIpBySubnet(ipAddresses); 254 | if (ip.isPresent()) { 255 | return ip; 256 | } 257 | } 258 | 259 | if (nonNull(serviceAddressSupplier.get())) { 260 | try { 261 | return Optional.ofNullable(serviceAddressSupplier.get().get()); 262 | } catch (Exception ex) { 263 | LOGGER.debug("Service address supplier threw an exception.", ex); 264 | } 265 | } 266 | return Optional.empty(); 267 | } 268 | 269 | /** 270 | * Returns the service address from the list of hosts. It iterates through the list and finds the 271 | * first host tht matched the subnet. If none is found, an empty Optional is returned. 272 | * 273 | * @param ipAddresses List of ipAddresses the application is listening on. 274 | * @return Optional of the host to register as the service address or empty otherwise 275 | */ 276 | private Optional findFirstEligibleIpBySubnet(Collection ipAddresses) { 277 | SubnetUtils subnetUtils = new SubnetUtils(serviceSubnet.get()); 278 | SubnetUtils.SubnetInfo subNetInfo = subnetUtils.getInfo(); 279 | return ipAddresses.stream().filter(subNetInfo::isInRange).findFirst(); 280 | } 281 | 282 | /** Deregister a service from Consul */ 283 | public void deregister() { 284 | final AgentClient agent = consul.agentClient(); 285 | try { 286 | if (!agent.isRegistered(serviceId)) { 287 | LOGGER.info("No service registered with ID \"{}\"", serviceId); 288 | return; 289 | } 290 | } catch (ConsulException e) { 291 | LOGGER.error("Failed to determine if service ID \"{}\" is registered", serviceId, e); 292 | return; 293 | } 294 | 295 | LOGGER.info("Deregistering service ID \"{}\"", serviceId); 296 | 297 | try { 298 | consul.agentClient().deregister(serviceId); 299 | } catch (ConsulException e) { 300 | LOGGER.error("Failed to deregister service from Consul", e); 301 | } 302 | } 303 | 304 | /** 305 | * Return the health check URL for the service 306 | * 307 | * @param applicationScheme Scheme the server is listening on 308 | * @return health check URL 309 | */ 310 | protected String getHealthCheckUrl(String applicationScheme, Collection hosts) { 311 | final UriBuilder builder = UriBuilder.fromPath(environment.getAdminContext().getContextPath()); 312 | builder 313 | .path(healthCheckPath.get()) 314 | .scheme(applicationScheme) 315 | .host(getServiceAddress(hosts).orElse(LOCALHOST)) 316 | .port(serviceAdminPort.get()); 317 | return builder.build().toString(); 318 | } 319 | } 320 | -------------------------------------------------------------------------------- /consul-core/src/main/java/com/smoketurner/dropwizard/consul/core/ConsulServiceListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2019 Smoke Turner, LLC (github@smoketurner.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.smoketurner.dropwizard.consul.core; 17 | 18 | import com.orbitz.consul.ConsulException; 19 | import io.dropwizard.lifecycle.ServerLifecycleListener; 20 | import io.dropwizard.util.Duration; 21 | import java.util.Collection; 22 | import java.util.HashSet; 23 | import java.util.List; 24 | import java.util.Objects; 25 | import java.util.Optional; 26 | import java.util.Set; 27 | import java.util.concurrent.ScheduledExecutorService; 28 | import java.util.concurrent.TimeUnit; 29 | import org.eclipse.jetty.server.Connector; 30 | import org.eclipse.jetty.server.Server; 31 | import org.eclipse.jetty.server.ServerConnector; 32 | import org.slf4j.Logger; 33 | import org.slf4j.LoggerFactory; 34 | 35 | public class ConsulServiceListener implements ServerLifecycleListener { 36 | private static final Logger LOGGER = LoggerFactory.getLogger(ConsulServiceListener.class); 37 | 38 | private static final String APPLICATION_NAME = "application"; 39 | private static final String ADMIN_NAME = "admin"; 40 | 41 | private final ConsulAdvertiser advertiser; 42 | private final Optional retryInterval; 43 | private final Optional scheduler; 44 | 45 | /** 46 | * Constructor 47 | * 48 | * @param advertiser Consul advertiser 49 | * @param retryInterval When specified, will retry if service registration fails 50 | * @param scheduler When specified, will retry if service registration fails 51 | */ 52 | public ConsulServiceListener( 53 | final ConsulAdvertiser advertiser, 54 | final Optional retryInterval, 55 | final Optional scheduler) { 56 | 57 | this.advertiser = Objects.requireNonNull(advertiser, "advertiser == null"); 58 | this.retryInterval = Objects.requireNonNull(retryInterval, "retryInterval == null"); 59 | this.scheduler = Objects.requireNonNull(scheduler, "scheduler == null"); 60 | } 61 | 62 | @Override 63 | public void serverStarted(final Server server) { 64 | 65 | String applicationScheme = null; 66 | int applicationPort = -1; 67 | int adminPort = -1; 68 | Set hosts = new HashSet<>(); 69 | 70 | for (Connector connector : server.getConnectors()) { 71 | @SuppressWarnings("resource") 72 | final ServerConnector serverConnector = (ServerConnector) connector; 73 | hosts.add(serverConnector.getHost()); 74 | if (APPLICATION_NAME.equals(connector.getName())) { 75 | applicationPort = serverConnector.getLocalPort(); 76 | applicationScheme = getScheme(connector.getProtocols()); 77 | } else if (ADMIN_NAME.equals(connector.getName())) { 78 | adminPort = serverConnector.getLocalPort(); 79 | } else { 80 | applicationPort = serverConnector.getLocalPort(); 81 | applicationScheme = getScheme(connector.getProtocols()); 82 | adminPort = applicationPort; 83 | } 84 | } 85 | 86 | LOGGER.debug( 87 | "applicationScheme: {}, applicationPort: {}, adminPort: {}", 88 | applicationScheme, 89 | applicationPort, 90 | adminPort); 91 | 92 | register(applicationScheme, applicationPort, adminPort, hosts); 93 | } 94 | 95 | /** 96 | * Return the protocol scheme from a list of protocols. 97 | * 98 | * @param protocols Configured protocols 99 | * @return protocol scheme 100 | */ 101 | private static String getScheme(List protocols) { 102 | if (protocols.contains("ssl")) { 103 | return "https"; 104 | } 105 | return "http"; 106 | } 107 | 108 | /** 109 | * Register ports with Consul and retry if unavailable 110 | * 111 | * @param applicationScheme Application protocol scheme 112 | * @param applicationPort Application listening port 113 | * @param adminPort Administration listening port 114 | * @param hosts List of addresses the service is bound to. 115 | */ 116 | void register( 117 | String applicationScheme, int applicationPort, int adminPort, Collection hosts) { 118 | try { 119 | advertiser.register(applicationScheme, applicationPort, adminPort, hosts); 120 | scheduler.ifPresent(ScheduledExecutorService::shutdownNow); 121 | } catch (ConsulException e) { 122 | LOGGER.error("Failed to register service in Consul", e); 123 | 124 | retryInterval.ifPresent( 125 | (interval) -> { 126 | scheduler.ifPresent( 127 | (service) -> { 128 | LOGGER.info( 129 | "Will try to register service again in {} seconds", interval.toSeconds()); 130 | service.schedule( 131 | () -> register(applicationScheme, applicationPort, adminPort, hosts), 132 | interval.toSeconds(), 133 | TimeUnit.SECONDS); 134 | }); 135 | }); 136 | } 137 | } 138 | } 139 | -------------------------------------------------------------------------------- /consul-core/src/main/java/com/smoketurner/dropwizard/consul/health/ConsulHealthCheck.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2019 Smoke Turner, LLC (github@smoketurner.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.smoketurner.dropwizard.consul.health; 17 | 18 | import com.codahale.metrics.health.HealthCheck; 19 | import com.orbitz.consul.Consul; 20 | import com.orbitz.consul.ConsulException; 21 | import java.util.Objects; 22 | import org.slf4j.Logger; 23 | import org.slf4j.LoggerFactory; 24 | 25 | public class ConsulHealthCheck extends HealthCheck { 26 | 27 | private static final Logger LOGGER = LoggerFactory.getLogger(ConsulHealthCheck.class); 28 | private final Consul consul; 29 | 30 | /** 31 | * Constructor 32 | * 33 | * @param consul Consul client 34 | */ 35 | public ConsulHealthCheck(final Consul consul) { 36 | this.consul = Objects.requireNonNull(consul); 37 | } 38 | 39 | @Override 40 | protected Result check() throws Exception { 41 | try { 42 | consul.agentClient().ping(); 43 | return Result.healthy(); 44 | } catch (ConsulException e) { 45 | LOGGER.warn("Unable to ping consul", e); 46 | } 47 | return Result.unhealthy("Could not ping consul"); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /consul-core/src/main/java/com/smoketurner/dropwizard/consul/managed/ConsulAdvertiserManager.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2019 Smoke Turner, LLC (github@smoketurner.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.smoketurner.dropwizard.consul.managed; 17 | 18 | import com.smoketurner.dropwizard.consul.core.ConsulAdvertiser; 19 | import io.dropwizard.lifecycle.Managed; 20 | import java.util.Objects; 21 | import java.util.Optional; 22 | import java.util.concurrent.ScheduledExecutorService; 23 | 24 | public class ConsulAdvertiserManager implements Managed { 25 | 26 | private final ConsulAdvertiser advertiser; 27 | private final Optional scheduler; 28 | 29 | /** 30 | * Constructor 31 | * 32 | * @param advertiser Consul advertiser 33 | * @param scheduler Optional retry scheduler 34 | */ 35 | public ConsulAdvertiserManager( 36 | final ConsulAdvertiser advertiser, final Optional scheduler) { 37 | this.advertiser = Objects.requireNonNull(advertiser, "advertiser == null"); 38 | this.scheduler = Objects.requireNonNull(scheduler, "scheduler == null"); 39 | } 40 | 41 | @Override 42 | public void start() throws Exception { 43 | // the advertiser is register as a Jetty startup listener 44 | } 45 | 46 | @Override 47 | public void stop() throws Exception { 48 | advertiser.deregister(); 49 | scheduler.ifPresent(ScheduledExecutorService::shutdownNow); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /consul-core/src/main/java/com/smoketurner/dropwizard/consul/task/MaintenanceTask.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2019 Smoke Turner, LLC (github@smoketurner.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.smoketurner.dropwizard.consul.task; 17 | 18 | import com.google.common.base.Strings; 19 | import com.orbitz.consul.Consul; 20 | import io.dropwizard.servlets.tasks.Task; 21 | import java.io.PrintWriter; 22 | import java.util.List; 23 | import java.util.Map; 24 | import java.util.Objects; 25 | import org.slf4j.Logger; 26 | import org.slf4j.LoggerFactory; 27 | 28 | public class MaintenanceTask extends Task { 29 | 30 | private static final Logger LOGGER = LoggerFactory.getLogger(MaintenanceTask.class); 31 | private final Consul consul; 32 | private final String serviceId; 33 | 34 | /** 35 | * Constructor 36 | * 37 | * @param consul Consul client 38 | * @param serviceId Service ID to toggle maintenance mode 39 | */ 40 | public MaintenanceTask(final Consul consul, final String serviceId) { 41 | super("maintenance"); 42 | this.consul = Objects.requireNonNull(consul); 43 | this.serviceId = Objects.requireNonNull(serviceId); 44 | } 45 | 46 | @Override 47 | public void execute(Map> parameters, PrintWriter output) throws Exception { 48 | 49 | if (!parameters.containsKey("enable")) { 50 | throw new IllegalArgumentException("Parameter \"enable\" not found"); 51 | } 52 | 53 | final boolean enable = Boolean.parseBoolean(parameters.get("enable").get(0)); 54 | final String reason; 55 | if (parameters.containsKey("reason")) { 56 | reason = Strings.nullToEmpty(parameters.get("reason").get(0)); 57 | } else { 58 | reason = ""; 59 | } 60 | 61 | if (enable) { 62 | if (!Strings.isNullOrEmpty(reason)) { 63 | LOGGER.warn("Enabling maintenance mode for service {} (reason: {})", serviceId, reason); 64 | } else { 65 | LOGGER.warn("Enabling maintenance mode for service {} (no reason given)", serviceId); 66 | } 67 | } else { 68 | LOGGER.warn("Disabling maintenance mode for service {}", serviceId); 69 | } 70 | 71 | consul.agentClient().toggleMaintenanceMode(serviceId, enable, reason); 72 | 73 | output.println("OK"); 74 | output.flush(); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /consul-core/src/test/java/com/smoketurner/dropwizard/consul/ConsulBundleTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2019 Smoke Turner, LLC (github@smoketurner.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.smoketurner.dropwizard.consul; 17 | 18 | import static org.assertj.core.api.Assertions.assertThat; 19 | import static org.mockito.Mockito.doNothing; 20 | import static org.mockito.Mockito.doReturn; 21 | import static org.mockito.Mockito.mock; 22 | import static org.mockito.Mockito.spy; 23 | import static org.mockito.Mockito.times; 24 | import static org.mockito.Mockito.verify; 25 | 26 | import io.dropwizard.Configuration; 27 | import io.dropwizard.setup.Environment; 28 | import org.junit.Before; 29 | import org.junit.Test; 30 | 31 | public class ConsulBundleTest { 32 | 33 | private final ConsulFactory factory = spy(new ConsulFactory()); 34 | private final Environment environment = mock(Environment.class); 35 | private final TestConfiguration config = new TestConfiguration(); 36 | private ConsulBundle bundle; 37 | 38 | class TestConfiguration extends Configuration implements ConsulConfiguration { 39 | @Override 40 | public ConsulFactory getConsulFactory(TestConfiguration configuration) { 41 | return factory; 42 | } 43 | } 44 | 45 | @Before 46 | public void setUp() throws Exception { 47 | bundle = 48 | spy( 49 | new ConsulBundle("test") { 50 | @Override 51 | public ConsulFactory getConsulFactory(TestConfiguration c) { 52 | return c.getConsulFactory(c); 53 | } 54 | }); 55 | 56 | doNothing().when(bundle).setupEnvironment(factory, environment); 57 | } 58 | 59 | @Test 60 | public void testDefaultsToEnabled() throws Exception { 61 | assertThat(factory.isEnabled()).isTrue(); 62 | } 63 | 64 | @Test 65 | public void testEnabled() throws Exception { 66 | doReturn(true).when(factory).isEnabled(); 67 | bundle.run(config, environment); 68 | verify(bundle, times(1)).setupEnvironment(factory, environment); 69 | } 70 | 71 | @Test 72 | public void testNotEnabled() throws Exception { 73 | doReturn(false).when(factory).isEnabled(); 74 | bundle.run(config, environment); 75 | verify(bundle, times(0)).setupEnvironment(factory, environment); 76 | } 77 | 78 | @Test 79 | public void testMissingServiceName() throws Exception { 80 | factory.setSeviceName(null); 81 | bundle.run(config, environment); 82 | assertThat(factory.getServiceName()).isEqualTo("test"); 83 | } 84 | 85 | @Test 86 | public void testPopulatedServiceName() throws Exception { 87 | factory.setSeviceName("test-service-name"); 88 | bundle.run(config, environment); 89 | assertThat(factory.getServiceName()).isEqualTo("test-service-name"); 90 | } 91 | 92 | @Test 93 | public void testAclToken() throws Exception { 94 | String token = "acl-token"; 95 | factory.setAclToken(token); 96 | bundle.run(config, environment); 97 | assertThat(factory.getAclToken().get()).isEqualTo(token); 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /consul-core/src/test/java/com/smoketurner/dropwizard/consul/ConsulFactoryTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2019 Smoke Turner, LLC (github@smoketurner.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.smoketurner.dropwizard.consul; 17 | 18 | import static org.assertj.core.api.Assertions.assertThat; 19 | import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; 20 | 21 | import com.google.common.collect.ImmutableList; 22 | import io.dropwizard.util.Duration; 23 | import org.junit.Test; 24 | 25 | public class ConsulFactoryTest { 26 | 27 | @Test 28 | public void testEquality() { 29 | final ConsulFactory actual = createFullyPopulatedConsulFactory(); 30 | final ConsulFactory expected = createFullyPopulatedConsulFactory(); 31 | assertThat(actual).isEqualTo(expected); 32 | } 33 | 34 | @Test 35 | public void testCorrectlyFormattedSubnet() { 36 | final ConsulFactory factory = createFullyPopulatedConsulFactory(); 37 | factory.setServiceSubnet("192.168.3.0/24"); 38 | assertThat(factory.getServiceSubnet()).isPresent().contains("192.168.3.0/24"); 39 | } 40 | 41 | @Test 42 | public void testIncorrectlyFormattedSubnet() { 43 | final ConsulFactory factory = createFullyPopulatedConsulFactory(); 44 | assertThatIllegalArgumentException().isThrownBy(() -> factory.setServiceSubnet("192.168.3.0/")); 45 | } 46 | 47 | @Test 48 | public void testNotEqual() { 49 | final ConsulFactory actual = createFullyPopulatedConsulFactory(); 50 | final ConsulFactory expected = createFullyPopulatedConsulFactory(); 51 | expected.setAdminPort(200); 52 | assertThat(actual).isNotEqualTo((expected)); 53 | } 54 | 55 | @Test 56 | public void testHashCode() { 57 | final ConsulFactory actual = createFullyPopulatedConsulFactory(); 58 | final ConsulFactory expected = createFullyPopulatedConsulFactory(); 59 | assertThat(actual.hashCode()).isEqualTo(expected.hashCode()); 60 | } 61 | 62 | @Test 63 | public void testMutatedHashCode() { 64 | final ConsulFactory actual = createFullyPopulatedConsulFactory(); 65 | final ConsulFactory expected = createFullyPopulatedConsulFactory(); 66 | expected.setAdminPort(200); 67 | assertThat(actual.hashCode()).isNotEqualTo(expected.hashCode()); 68 | } 69 | 70 | @Test 71 | public void testSetServiceName() { 72 | ConsulFactory consulFactory = new ConsulFactory(); 73 | String serviceName = "test-service"; 74 | consulFactory.setServiceName(serviceName); 75 | assertThat(consulFactory.getServiceName()).isEqualTo(serviceName); 76 | } 77 | 78 | private ConsulFactory createFullyPopulatedConsulFactory() { 79 | final ConsulFactory consulFactory = new ConsulFactory(); 80 | consulFactory.setSeviceName("serviceName"); 81 | consulFactory.setEnabled(true); 82 | consulFactory.setServicePort(1000); 83 | consulFactory.setAdminPort(2000); 84 | consulFactory.setServiceSubnet("192.168.1.0/24"); 85 | consulFactory.setServiceAddress("localhost"); 86 | consulFactory.setTags(ImmutableList.of("tag1", "tag2")); 87 | consulFactory.setRetryInterval(Duration.seconds(5)); 88 | consulFactory.setCheckInterval(Duration.seconds(1)); 89 | consulFactory.setAclToken("acl-token"); 90 | consulFactory.setServicePing(false); 91 | return consulFactory; 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /consul-core/src/test/java/com/smoketurner/dropwizard/consul/core/ConsulAdvertiserTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2019 Smoke Turner, LLC (github@smoketurner.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.smoketurner.dropwizard.consul.core; 17 | 18 | import static org.assertj.core.api.Assertions.assertThat; 19 | import static org.mockito.ArgumentMatchers.anyInt; 20 | import static org.mockito.ArgumentMatchers.anyList; 21 | import static org.mockito.ArgumentMatchers.anyLong; 22 | import static org.mockito.ArgumentMatchers.anyMap; 23 | import static org.mockito.ArgumentMatchers.anyString; 24 | import static org.mockito.Mockito.doThrow; 25 | import static org.mockito.Mockito.mock; 26 | import static org.mockito.Mockito.never; 27 | import static org.mockito.Mockito.verify; 28 | import static org.mockito.Mockito.when; 29 | 30 | import com.google.common.collect.ImmutableMap; 31 | import com.orbitz.consul.AgentClient; 32 | import com.orbitz.consul.Consul; 33 | import com.orbitz.consul.ConsulException; 34 | import com.orbitz.consul.model.agent.ImmutableRegCheck; 35 | import com.orbitz.consul.model.agent.ImmutableRegistration; 36 | import com.smoketurner.dropwizard.consul.ConsulFactory; 37 | import io.dropwizard.jetty.MutableServletContextHandler; 38 | import io.dropwizard.setup.Environment; 39 | import java.util.Arrays; 40 | import java.util.HashMap; 41 | import java.util.List; 42 | import java.util.Map; 43 | import java.util.function.Supplier; 44 | import org.junit.Before; 45 | import org.junit.Test; 46 | 47 | public class ConsulAdvertiserTest { 48 | 49 | public static final String SECOND_SUBNET_IP = "192.168.2.99"; 50 | public static final String FIRST_SUBNET_IP = "192.168.1.53"; 51 | public static final String THIRD_SUBNET_IP = "192.168.3.32"; 52 | private static final String DEFAULT_HEALTH_CHECK_PATH = "healthcheck"; 53 | private final Consul consul = mock(Consul.class); 54 | private final AgentClient agent = mock(AgentClient.class); 55 | private final Environment environment = mock(Environment.class); 56 | private final MutableServletContextHandler handler = mock(MutableServletContextHandler.class); 57 | private final Supplier supplierMock = mock(Supplier.class); 58 | private final String serviceId = "test"; 59 | private ConsulAdvertiser advertiser; 60 | private ConsulFactory factory; 61 | private String healthCheckUrl; 62 | 63 | @Before 64 | public void setUp() { 65 | when(consul.agentClient()).thenReturn(agent); 66 | when(environment.getAdminContext()).thenReturn(handler); 67 | when(handler.getContextPath()).thenReturn("admin"); 68 | when(supplierMock.get()).thenReturn(null); 69 | factory = new ConsulFactory(); 70 | factory.setSeviceName("test"); 71 | factory.setServiceSubnet("192.168.2.0/24"); 72 | factory.setServiceAddressSupplier(supplierMock); 73 | factory.setHealthCheckPath(DEFAULT_HEALTH_CHECK_PATH); 74 | advertiser = new ConsulAdvertiser(environment, factory, consul, serviceId); 75 | healthCheckUrl = "http://127.0.0.1:8081/admin/" + DEFAULT_HEALTH_CHECK_PATH; 76 | } 77 | 78 | @Test 79 | public void testGetServiceId() { 80 | assertThat(advertiser.getServiceId()).isEqualTo(serviceId); 81 | } 82 | 83 | @Test 84 | public void testRegister() { 85 | when(agent.isRegistered(serviceId)).thenReturn(false); 86 | advertiser.register("http", 8080, 8081); 87 | 88 | final ImmutableRegistration registration = 89 | ImmutableRegistration.builder() 90 | .port(8080) 91 | .check( 92 | ImmutableRegCheck.builder() 93 | .http(healthCheckUrl) 94 | .interval("1s") 95 | .deregisterCriticalServiceAfter("1m") 96 | .build()) 97 | .name("test") 98 | .meta(ImmutableMap.of("scheme", "http")) 99 | .id(serviceId) 100 | .build(); 101 | 102 | verify(agent).register(registration); 103 | } 104 | 105 | /** 106 | * Added to verify that NullPointerException is not thrown when a healthCheckPath 107 | * is not specified on ConsulFactory. 108 | */ 109 | @Test 110 | public void testRegisterWhenHealthCheckPathNotSpecifiedOnFactory() { 111 | factory = new ConsulFactory(); 112 | factory.setServiceName("test"); 113 | factory.setServiceSubnet("192.168.2.0/24"); 114 | factory.setServiceAddressSupplier(supplierMock); 115 | advertiser = new ConsulAdvertiser(environment, factory, consul, serviceId); 116 | 117 | when(agent.isRegistered(serviceId)).thenReturn(false); 118 | advertiser.register("http", 8080, 8081); 119 | 120 | final ImmutableRegistration registration = 121 | ImmutableRegistration.builder() 122 | .port(8080) 123 | .check( 124 | ImmutableRegCheck.builder() 125 | .http(healthCheckUrl) 126 | .interval("1s") 127 | .deregisterCriticalServiceAfter("1m") 128 | .build()) 129 | .name("test") 130 | .meta(ImmutableMap.of("scheme", "http")) 131 | .id(serviceId) 132 | .build(); 133 | 134 | verify(agent).register(registration); 135 | } 136 | 137 | @Test 138 | public void testRegisterWithSubnet() { 139 | when(agent.isRegistered(serviceId)).thenReturn(false); 140 | advertiser.register( 141 | "http", 8080, 8081, Arrays.asList(FIRST_SUBNET_IP, SECOND_SUBNET_IP, THIRD_SUBNET_IP)); 142 | 143 | String healthCheckUrlWithCorrectSubnet = "http://192.168.2.99:8081/admin/" + DEFAULT_HEALTH_CHECK_PATH; 144 | final ImmutableRegistration registration = 145 | ImmutableRegistration.builder() 146 | .port(8080) 147 | .check( 148 | ImmutableRegCheck.builder() 149 | .http(healthCheckUrlWithCorrectSubnet) 150 | .interval("1s") 151 | .deregisterCriticalServiceAfter("1m") 152 | .build()) 153 | .name("test") 154 | .address(SECOND_SUBNET_IP) 155 | .meta(ImmutableMap.of("scheme", "http")) 156 | .id(serviceId) 157 | .build(); 158 | 159 | verify(agent).register(registration); 160 | } 161 | 162 | @Test 163 | public void testRegisterWithSubnetNoEligibleIps() { 164 | when(agent.isRegistered(serviceId)).thenReturn(false); 165 | advertiser.register( 166 | "http", 8080, 8081, Arrays.asList(FIRST_SUBNET_IP, "192.168.7.23", THIRD_SUBNET_IP)); 167 | 168 | final ImmutableRegistration registration = 169 | ImmutableRegistration.builder() 170 | .port(8080) 171 | .check( 172 | ImmutableRegCheck.builder() 173 | .http(healthCheckUrl) 174 | .interval("1s") 175 | .deregisterCriticalServiceAfter("1m") 176 | .build()) 177 | .name("test") 178 | .meta(ImmutableMap.of("scheme", "http")) 179 | .id(serviceId) 180 | .build(); 181 | 182 | verify(agent).register(registration); 183 | } 184 | 185 | @Test 186 | public void testRegisterWithSupplier() { 187 | when(agent.isRegistered(serviceId)).thenReturn(false); 188 | when(supplierMock.get()).thenReturn("192.168.8.99"); 189 | advertiser.register( 190 | "http", 8080, 8081, Arrays.asList(FIRST_SUBNET_IP, "192.168.7.23", THIRD_SUBNET_IP)); 191 | 192 | String healthCheckUrlWithCorrectSubnet = "http://192.168.8.99:8081/admin/healthcheck"; 193 | final ImmutableRegistration registration = 194 | ImmutableRegistration.builder() 195 | .port(8080) 196 | .check( 197 | ImmutableRegCheck.builder() 198 | .http(healthCheckUrlWithCorrectSubnet) 199 | .interval("1s") 200 | .deregisterCriticalServiceAfter("1m") 201 | .build()) 202 | .name("test") 203 | .meta(ImmutableMap.of("scheme", "http")) 204 | .address("192.168.8.99") 205 | .id(serviceId) 206 | .build(); 207 | 208 | verify(agent).register(registration); 209 | } 210 | 211 | @Test 212 | public void testRegisterWithSupplierException() { 213 | when(agent.isRegistered(serviceId)).thenReturn(false); 214 | when(supplierMock.get()).thenThrow(new IllegalArgumentException()); 215 | advertiser.register( 216 | "http", 8080, 8081, Arrays.asList(FIRST_SUBNET_IP, "192.168.7.23", THIRD_SUBNET_IP)); 217 | 218 | final ImmutableRegistration registration = 219 | ImmutableRegistration.builder() 220 | .port(8080) 221 | .check( 222 | ImmutableRegCheck.builder() 223 | .http(healthCheckUrl) 224 | .interval("1s") 225 | .deregisterCriticalServiceAfter("1m") 226 | .build()) 227 | .name("test") 228 | .meta(ImmutableMap.of("scheme", "http")) 229 | .id(serviceId) 230 | .build(); 231 | 232 | verify(agent).register(registration); 233 | } 234 | 235 | @Test 236 | public void testRegisterWithHttps() { 237 | when(agent.isRegistered(serviceId)).thenReturn(false); 238 | advertiser.register("https", 8080, 8081); 239 | 240 | String httpsHealthCheckUrl = "https://127.0.0.1:8081/admin/healthcheck"; 241 | final ImmutableRegistration registration = 242 | ImmutableRegistration.builder() 243 | .port(8080) 244 | .check( 245 | ImmutableRegCheck.builder() 246 | .http(httpsHealthCheckUrl) 247 | .interval("1s") 248 | .deregisterCriticalServiceAfter("1m") 249 | .build()) 250 | .name("test") 251 | .meta(ImmutableMap.of("scheme", "https")) 252 | .id(serviceId) 253 | .build(); 254 | 255 | verify(agent).register(registration); 256 | } 257 | 258 | @Test 259 | public void testRegisterAlreadyRegistered() { 260 | when(agent.isRegistered(anyString())).thenReturn(true); 261 | advertiser.register("http", 8080, 8081); 262 | verify(agent, never()) 263 | .register(anyInt(), anyString(), anyLong(), anyString(), anyString(), anyList(), anyMap()); 264 | } 265 | 266 | @Test 267 | public void testHostFromConfig() { 268 | factory.setServicePort(8888); 269 | factory.setServiceAddress("127.0.0.1"); 270 | 271 | when(agent.isRegistered(anyString())).thenReturn(false); 272 | final ConsulAdvertiser advertiser = 273 | new ConsulAdvertiser(environment, factory, consul, serviceId); 274 | advertiser.register("http", 8080, 8081); 275 | 276 | final ImmutableRegistration registration = 277 | ImmutableRegistration.builder() 278 | .id(serviceId) 279 | .port(8888) 280 | .address("127.0.0.1") 281 | .check( 282 | ImmutableRegCheck.builder() 283 | .http(healthCheckUrl) 284 | .interval("1s") 285 | .deregisterCriticalServiceAfter("1m") 286 | .build()) 287 | .name("test") 288 | .meta(ImmutableMap.of("scheme", "http")) 289 | .build(); 290 | 291 | verify(agent).register(registration); 292 | } 293 | 294 | @Test 295 | public void testTagsFromConfig() { 296 | final List tags = Arrays.asList("test", "second-test"); 297 | factory.setTags(tags); 298 | 299 | when(agent.isRegistered(serviceId)).thenReturn(false); 300 | final ConsulAdvertiser advertiser = 301 | new ConsulAdvertiser(environment, factory, consul, serviceId); 302 | advertiser.register("http", 8080, 8081); 303 | 304 | final ImmutableRegistration registration = 305 | ImmutableRegistration.builder() 306 | .tags(tags) 307 | .check( 308 | ImmutableRegCheck.builder() 309 | .http(healthCheckUrl) 310 | .interval("1s") 311 | .deregisterCriticalServiceAfter("1m") 312 | .build()) 313 | .name("test") 314 | .meta(ImmutableMap.of("scheme", "http")) 315 | .port(8080) 316 | .id(serviceId) 317 | .build(); 318 | 319 | verify(agent).register(registration); 320 | } 321 | 322 | @Test 323 | public void testAclTokenFromConfig() { 324 | String aclToken = "acl-token"; 325 | factory.setAclToken(aclToken); 326 | 327 | when(agent.isRegistered(serviceId)).thenReturn(false); 328 | final ConsulAdvertiser advertiser = 329 | new ConsulAdvertiser(environment, factory, consul, serviceId); 330 | advertiser.register("http", 8080, 8081); 331 | 332 | final ImmutableRegistration registration = 333 | ImmutableRegistration.builder() 334 | .id(serviceId) 335 | .check( 336 | ImmutableRegCheck.builder() 337 | .http(healthCheckUrl) 338 | .interval("1s") 339 | .deregisterCriticalServiceAfter("1m") 340 | .build()) 341 | .name("test") 342 | .port(8080) 343 | .meta(ImmutableMap.of("scheme", "http")) 344 | .id(serviceId) 345 | .build(); 346 | 347 | verify(agent).register(registration); 348 | } 349 | 350 | @Test 351 | public void testServiceMetaFromConfig() { 352 | final Map serviceMeta = new HashMap<>(); 353 | serviceMeta.put("meta1-key", "meta1-value"); 354 | serviceMeta.put("meta2-key", "meta2-value"); 355 | factory.setServiceMeta(serviceMeta); 356 | 357 | when(agent.isRegistered(serviceId)).thenReturn(false); 358 | final ConsulAdvertiser advertiser = 359 | new ConsulAdvertiser(environment, factory, consul, serviceId); 360 | advertiser.register("http", 8080, 8081); 361 | 362 | final ImmutableRegistration registration = 363 | ImmutableRegistration.builder() 364 | .meta(serviceMeta) 365 | .putMeta("scheme", "http") 366 | .check( 367 | ImmutableRegCheck.builder() 368 | .http(healthCheckUrl) 369 | .interval("1s") 370 | .deregisterCriticalServiceAfter("1m") 371 | .build()) 372 | .name("test") 373 | .port(8080) 374 | .id(serviceId) 375 | .build(); 376 | 377 | verify(agent).register(registration); 378 | } 379 | 380 | @Test 381 | public void testHealthCheckUrlFromConfig() { 382 | factory.setServicePort(8888); 383 | factory.setServiceAddress("127.0.0.1"); 384 | factory.setHealthCheckPath("ping"); 385 | String configuredHealthCheckUrl = "http://127.0.0.1:8081/admin/ping"; 386 | 387 | when(agent.isRegistered(anyString())).thenReturn(false); 388 | final ConsulAdvertiser advertiser = 389 | new ConsulAdvertiser(environment, factory, consul, serviceId); 390 | advertiser.register("http", 8080, 8081); 391 | 392 | final ImmutableRegistration registration = 393 | ImmutableRegistration.builder() 394 | .id(serviceId) 395 | .port(8888) 396 | .address("127.0.0.1") 397 | .check( 398 | ImmutableRegCheck.builder() 399 | .http(configuredHealthCheckUrl) 400 | .interval("1s") 401 | .deregisterCriticalServiceAfter("1m") 402 | .build()) 403 | .name("test") 404 | .meta(ImmutableMap.of("scheme", "http")) 405 | .build(); 406 | 407 | verify(agent).register(registration); 408 | } 409 | 410 | @Test 411 | public void testDeregister() { 412 | final String serviceId = advertiser.getServiceId(); 413 | when(agent.isRegistered(serviceId)).thenReturn(true); 414 | advertiser.deregister(); 415 | verify(agent).deregister(serviceId); 416 | } 417 | 418 | @Test 419 | public void testDeregisterNotRegistered() { 420 | final String serviceId = advertiser.getServiceId(); 421 | when(agent.isRegistered(serviceId)).thenReturn(false); 422 | advertiser.deregister(); 423 | verify(agent, never()).deregister(serviceId); 424 | } 425 | 426 | @Test 427 | public void testDeregisterException() { 428 | when(agent.isRegistered(anyString())).thenReturn(true); 429 | doThrow(new ConsulException("error")).when(agent).deregister(anyString()); 430 | advertiser.deregister(); 431 | verify(agent).deregister(anyString()); 432 | } 433 | } 434 | -------------------------------------------------------------------------------- /consul-core/src/test/java/com/smoketurner/dropwizard/consul/core/ConsulServiceListenerTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2019 Smoke Turner, LLC (github@smoketurner.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.smoketurner.dropwizard.consul.core; 17 | 18 | import static org.mockito.ArgumentMatchers.any; 19 | import static org.mockito.ArgumentMatchers.anyCollection; 20 | import static org.mockito.ArgumentMatchers.anyInt; 21 | import static org.mockito.Mockito.mock; 22 | import static org.mockito.Mockito.timeout; 23 | import static org.mockito.Mockito.verify; 24 | import static org.mockito.Mockito.when; 25 | 26 | import com.orbitz.consul.ConsulException; 27 | import io.dropwizard.util.Duration; 28 | import io.dropwizard.util.Sets; 29 | import java.util.Collection; 30 | import java.util.Optional; 31 | import java.util.concurrent.Executors; 32 | import java.util.concurrent.ScheduledExecutorService; 33 | import org.junit.After; 34 | import org.junit.Before; 35 | import org.junit.Test; 36 | 37 | public class ConsulServiceListenerTest { 38 | 39 | private final ConsulAdvertiser advertiser = mock(ConsulAdvertiser.class); 40 | private ScheduledExecutorService scheduler; 41 | 42 | @Before 43 | public void setUp() { 44 | scheduler = Executors.newScheduledThreadPool(1); 45 | } 46 | 47 | @After 48 | public void tearDown() { 49 | if (scheduler != null) { 50 | scheduler.shutdownNow(); 51 | } 52 | } 53 | 54 | @Test 55 | public void testRegister() { 56 | final ConsulServiceListener listener = 57 | new ConsulServiceListener( 58 | advertiser, Optional.of(Duration.milliseconds(1)), Optional.of(scheduler)); 59 | 60 | when(advertiser.register(any(), anyInt(), anyInt(), anyCollection())) 61 | .thenThrow(new ConsulException("Cannot connect to Consul")) 62 | .thenReturn(true); 63 | 64 | Collection hosts = Sets.of("192.168.1.22"); 65 | listener.register("http", 0, 0, hosts); 66 | 67 | verify(advertiser, timeout(100).atLeast(1)).register("http", 0, 0, hosts); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /consul-core/src/test/java/com/smoketurner/dropwizard/consul/health/ConsulHealthCheckTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2019 Smoke Turner, LLC (github@smoketurner.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.smoketurner.dropwizard.consul.health; 17 | 18 | import static org.assertj.core.api.Assertions.assertThat; 19 | import static org.mockito.Mockito.doThrow; 20 | import static org.mockito.Mockito.mock; 21 | import static org.mockito.Mockito.verify; 22 | import static org.mockito.Mockito.when; 23 | 24 | import com.codahale.metrics.health.HealthCheck.Result; 25 | import com.orbitz.consul.AgentClient; 26 | import com.orbitz.consul.Consul; 27 | import com.orbitz.consul.ConsulException; 28 | import org.junit.Before; 29 | import org.junit.Test; 30 | 31 | public class ConsulHealthCheckTest { 32 | 33 | private final Consul consul = mock(Consul.class); 34 | private final AgentClient agent = mock(AgentClient.class); 35 | private final ConsulHealthCheck healthCheck = new ConsulHealthCheck(consul); 36 | 37 | @Before 38 | public void setUp() { 39 | when(consul.agentClient()).thenReturn(agent); 40 | } 41 | 42 | @Test 43 | public void testCheckHealthy() throws Exception { 44 | final Result actual = healthCheck.check(); 45 | verify(agent).ping(); 46 | assertThat(actual.isHealthy()).isTrue(); 47 | } 48 | 49 | @Test 50 | public void testCheckUnhealthy() throws Exception { 51 | doThrow(new ConsulException("error")).when(agent).ping(); 52 | final Result actual = healthCheck.check(); 53 | verify(agent).ping(); 54 | assertThat(actual.isHealthy()).isFalse(); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /consul-core/src/test/java/com/smoketurner/dropwizard/consul/managed/ConsulAdvertiserManagerTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2019 Smoke Turner, LLC (github@smoketurner.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.smoketurner.dropwizard.consul.managed; 17 | 18 | import static org.mockito.Mockito.mock; 19 | import static org.mockito.Mockito.verify; 20 | 21 | import com.smoketurner.dropwizard.consul.core.ConsulAdvertiser; 22 | import java.util.Optional; 23 | import java.util.concurrent.ScheduledExecutorService; 24 | import org.junit.Test; 25 | 26 | public class ConsulAdvertiserManagerTest { 27 | 28 | private final ConsulAdvertiser advertiser = mock(ConsulAdvertiser.class); 29 | private final ScheduledExecutorService scheduler = mock(ScheduledExecutorService.class); 30 | private final ConsulAdvertiserManager manager = 31 | new ConsulAdvertiserManager(advertiser, Optional.of(scheduler)); 32 | 33 | @Test 34 | public void testStop() throws Exception { 35 | manager.stop(); 36 | verify(advertiser).deregister(); 37 | verify(scheduler).shutdownNow(); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /consul-example/LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /consul-example/example.keystore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smoketurner/dropwizard-consul/ba734b37d23231da96d7e90f4cad158ce078bc36/consul-example/example.keystore -------------------------------------------------------------------------------- /consul-example/hello-world.yml: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright © 2019 Smoke Turner, LLC (github@smoketurner.com) 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | template: ${helloworld/template:-Hello, %s!} 18 | defaultName: ${helloworld/defaultName:-Stranger} 19 | 20 | consul: 21 | endpoint: ${CONSUL-URL:-localhost:8500} 22 | serviceId: test123 23 | retryInterval: 5 seconds 24 | 25 | client: 26 | refreshInterval: 10 seconds 27 | 28 | # HTTP-specific options. 29 | server: 30 | 31 | applicationConnectors: 32 | - type: http 33 | port: 8080 34 | 35 | adminConnectors: 36 | - type: http 37 | port: 8180 38 | 39 | requestLog: 40 | appenders: 41 | - type: console 42 | timeZone: UTC 43 | target: stdout 44 | 45 | logging: 46 | level: INFO 47 | loggers: 48 | com.smoketurner.dropwizard.consul: DEBUG 49 | com.example.helloworld: DEBUG 50 | com.netflix.loadbalancer: DEBUG 51 | appenders: 52 | - type: console 53 | timeZone: UTC 54 | target: stdout 55 | -------------------------------------------------------------------------------- /consul-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 20 | 4.0.0 21 | 22 | 23 | com.smoketurner.dropwizard 24 | dropwizard-consul 25 | 2.0.12-2-SNAPSHOT 26 | 27 | 28 | consul-example 29 | Dropwizard Consul Example 30 | 31 | 32 | true 33 | true 34 | true 35 | true 36 | 37 | 38 | 39 | 40 | io.dropwizard 41 | dropwizard-core 42 | 43 | 44 | ${project.groupId} 45 | consul-core 46 | ${project.version} 47 | 48 | 49 | ${project.groupId} 50 | consul-ribbon 51 | ${project.version} 52 | 53 | 54 | javax.xml.bind 55 | jaxb-api 56 | 2.3.1 57 | runtime 58 | 59 | 60 | javax.activation 61 | javax.activation-api 62 | 1.2.0 63 | runtime 64 | 65 | 66 | 67 | 68 | 69 | 70 | org.apache.maven.plugins 71 | maven-shade-plugin 72 | 3.4.1 73 | 74 | true 75 | 76 | 77 | *:* 78 | 79 | META-INF/*.SF 80 | META-INF/*.DSA 81 | META-INF/*.RSA 82 | 83 | 84 | 85 | 86 | 87 | 88 | package 89 | 90 | shade 91 | 92 | 93 | 94 | 95 | 96 | com.example.helloworld.HelloWorldApplication 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | -------------------------------------------------------------------------------- /consul-example/src/main/java/com/example/helloworld/HelloWorldApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2019 Smoke Turner, LLC (github@smoketurner.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.example.helloworld; 17 | 18 | import com.example.helloworld.resources.HelloWorldResource; 19 | import com.orbitz.consul.Consul; 20 | import com.smoketurner.dropwizard.consul.ConsulBundle; 21 | import com.smoketurner.dropwizard.consul.ConsulFactory; 22 | import com.smoketurner.dropwizard.consul.ribbon.RibbonJerseyClient; 23 | import com.smoketurner.dropwizard.consul.ribbon.RibbonJerseyClientBuilder; 24 | import io.dropwizard.Application; 25 | import io.dropwizard.configuration.EnvironmentVariableSubstitutor; 26 | import io.dropwizard.configuration.SubstitutingSourceProvider; 27 | import io.dropwizard.setup.Bootstrap; 28 | import io.dropwizard.setup.Environment; 29 | 30 | public class HelloWorldApplication extends Application { 31 | 32 | public static void main(String[] args) throws Exception { 33 | new HelloWorldApplication().run(args); 34 | } 35 | 36 | @Override 37 | public String getName() { 38 | return "hello-world"; 39 | } 40 | 41 | @Override 42 | public void initialize(Bootstrap bootstrap) { 43 | 44 | bootstrap.setConfigurationSourceProvider( 45 | new SubstitutingSourceProvider( 46 | bootstrap.getConfigurationSourceProvider(), new EnvironmentVariableSubstitutor(false))); 47 | 48 | bootstrap.addBundle( 49 | new ConsulBundle(getName(), false, true) { 50 | @Override 51 | public ConsulFactory getConsulFactory(HelloWorldConfiguration configuration) { 52 | return configuration.getConsulFactory(); 53 | } 54 | }); 55 | } 56 | 57 | @Override 58 | public void run(HelloWorldConfiguration configuration, Environment environment) throws Exception { 59 | final Consul consul = configuration.getConsulFactory().build(); 60 | final RibbonJerseyClient loadBalancingClient = 61 | new RibbonJerseyClientBuilder(environment, consul, configuration.getClient()) 62 | .build("hello-world"); 63 | 64 | final HelloWorldResource resource = 65 | new HelloWorldResource( 66 | consul, 67 | loadBalancingClient, 68 | configuration.getTemplate(), 69 | configuration.getDefaultName()); 70 | environment.jersey().register(resource); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /consul-example/src/main/java/com/example/helloworld/HelloWorldConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2019 Smoke Turner, LLC (github@smoketurner.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.example.helloworld; 17 | 18 | import com.fasterxml.jackson.annotation.JsonProperty; 19 | import com.smoketurner.dropwizard.consul.ConsulFactory; 20 | import com.smoketurner.dropwizard.consul.ribbon.RibbonJerseyClientConfiguration; 21 | import io.dropwizard.Configuration; 22 | import javax.validation.Valid; 23 | import javax.validation.constraints.NotEmpty; 24 | import javax.validation.constraints.NotNull; 25 | 26 | public class HelloWorldConfiguration extends Configuration { 27 | 28 | @NotEmpty private String template = "Hello, %s!"; 29 | 30 | @NotEmpty private String defaultName = "Stranger"; 31 | 32 | @NotNull @Valid public final ConsulFactory consul = new ConsulFactory(); 33 | 34 | @NotNull @Valid 35 | public final RibbonJerseyClientConfiguration client = new RibbonJerseyClientConfiguration(); 36 | 37 | @JsonProperty 38 | public String getTemplate() { 39 | return template; 40 | } 41 | 42 | @JsonProperty 43 | public void setTemplate(String template) { 44 | this.template = template; 45 | } 46 | 47 | @JsonProperty 48 | public String getDefaultName() { 49 | return defaultName; 50 | } 51 | 52 | @JsonProperty 53 | public void setDefaultName(String name) { 54 | this.defaultName = name; 55 | } 56 | 57 | @JsonProperty 58 | public ConsulFactory getConsulFactory() { 59 | return consul; 60 | } 61 | 62 | @JsonProperty 63 | public RibbonJerseyClientConfiguration getClient() { 64 | return client; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /consul-example/src/main/java/com/example/helloworld/api/Saying.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2019 Smoke Turner, LLC (github@smoketurner.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.example.helloworld.api; 17 | 18 | import com.fasterxml.jackson.annotation.JsonCreator; 19 | import com.fasterxml.jackson.annotation.JsonProperty; 20 | import org.hibernate.validator.constraints.Length; 21 | 22 | public final class Saying { 23 | private final long id; 24 | 25 | @Length(max = 3) 26 | private final String content; 27 | 28 | @JsonCreator 29 | public Saying(@JsonProperty("id") long id, @JsonProperty("content") String content) { 30 | this.id = id; 31 | this.content = content; 32 | } 33 | 34 | @JsonProperty 35 | public long getId() { 36 | return id; 37 | } 38 | 39 | @JsonProperty 40 | public String getContent() { 41 | return content; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /consul-example/src/main/java/com/example/helloworld/resources/HelloWorldResource.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2019 Smoke Turner, LLC (github@smoketurner.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.example.helloworld.resources; 17 | 18 | import com.codahale.metrics.annotation.Timed; 19 | import com.example.helloworld.api.Saying; 20 | import com.google.common.base.Optional; 21 | import com.netflix.loadbalancer.Server; 22 | import com.orbitz.consul.Consul; 23 | import com.orbitz.consul.model.health.ServiceHealth; 24 | import com.smoketurner.dropwizard.consul.ribbon.RibbonJerseyClient; 25 | import java.util.List; 26 | import java.util.concurrent.atomic.AtomicLong; 27 | import javax.ws.rs.GET; 28 | import javax.ws.rs.Path; 29 | import javax.ws.rs.PathParam; 30 | import javax.ws.rs.Produces; 31 | import javax.ws.rs.QueryParam; 32 | import javax.ws.rs.core.MediaType; 33 | 34 | @Path("/") 35 | @Produces(MediaType.APPLICATION_JSON) 36 | public class HelloWorldResource { 37 | private final Consul consul; 38 | private final RibbonJerseyClient client; 39 | private final String template; 40 | private final String defaultName; 41 | private final AtomicLong counter; 42 | 43 | public HelloWorldResource( 44 | Consul consul, RibbonJerseyClient client, String template, String defaultName) { 45 | this.consul = consul; 46 | this.client = client; 47 | this.template = template; 48 | this.defaultName = defaultName; 49 | this.counter = new AtomicLong(); 50 | } 51 | 52 | @GET 53 | @Timed 54 | @Path("/hello-world") 55 | public Saying sayHello(@QueryParam("name") Optional name) { 56 | final String value = String.format(template, name.or(defaultName)); 57 | return new Saying(counter.incrementAndGet(), value); 58 | } 59 | 60 | @GET 61 | @Timed 62 | @Path("/consul/{service}") 63 | public List getHealthyServiceInstances(@PathParam("service") String service) { 64 | return consul.healthClient().getHealthyServiceInstances(service).getResponse(); 65 | } 66 | 67 | @GET 68 | @Timed 69 | @Path("/available") 70 | public List getAvailableServers() { 71 | return client.getAvailableServers(); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /consul-ribbon/LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /consul-ribbon/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 20 | 4.0.0 21 | 22 | 23 | com.smoketurner.dropwizard 24 | dropwizard-consul 25 | 2.0.12-2-SNAPSHOT 26 | 27 | 28 | consul-ribbon 29 | Dropwizard Consul Load Balancer 30 | 31 | 32 | 2.7.18 33 | 34 | 35 | 36 | 37 | ${project.groupId} 38 | consul-core 39 | ${project.version} 40 | 41 | 42 | io.dropwizard 43 | dropwizard-client 44 | 45 | 46 | 47 | com.netflix.archaius 48 | archaius-core 49 | 0.7.6 50 | 51 | 52 | com.netflix.servo 53 | servo-core 54 | 0.10.1 55 | 56 | 57 | io.reactivex 58 | rxjava 59 | 1.0.9 60 | 61 | 62 | com.netflix.ribbon 63 | ribbon-core 64 | ${ribbon.version} 65 | 66 | 67 | com.netflix.ribbon 68 | ribbon-loadbalancer 69 | ${ribbon.version} 70 | 71 | 72 | com.netflix.ribbon 73 | ribbon-archaius 74 | ${ribbon.version} 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /consul-ribbon/src/main/java/com/smoketurner/dropwizard/consul/ribbon/ConsulServerList.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2019 Smoke Turner, LLC (github@smoketurner.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.smoketurner.dropwizard.consul.ribbon; 17 | 18 | import com.google.common.base.Strings; 19 | import com.netflix.loadbalancer.Server; 20 | import com.netflix.loadbalancer.ServerList; 21 | import com.orbitz.consul.Consul; 22 | import com.orbitz.consul.model.health.ServiceHealth; 23 | import java.util.Collection; 24 | import java.util.List; 25 | import java.util.Objects; 26 | import java.util.stream.Collectors; 27 | import javax.annotation.Nullable; 28 | 29 | public class ConsulServerList implements ServerList { 30 | 31 | private final Consul consul; 32 | private final ConsulServiceDiscoverer serviceDiscoverer; 33 | 34 | /** 35 | * Constructor 36 | * 37 | * @param consul Consul client 38 | * @param serviceDiscoverer Discoverer 39 | */ 40 | public ConsulServerList(final Consul consul, final ConsulServiceDiscoverer serviceDiscoverer) { 41 | this.consul = Objects.requireNonNull(consul); 42 | this.serviceDiscoverer = Objects.requireNonNull(serviceDiscoverer); 43 | } 44 | 45 | @Override 46 | public List getInitialListOfServers() { 47 | return buildServerList(serviceDiscoverer.discover(consul)); 48 | } 49 | 50 | @Override 51 | public List getUpdatedListOfServers() { 52 | return buildServerList(serviceDiscoverer.discover(consul)); 53 | } 54 | 55 | /** 56 | * Converts a list of {@link ServiceHealth} objects into {@link Server} objects 57 | * 58 | * @param services list of healthy service instances 59 | * @return list of server instances 60 | */ 61 | private List buildServerList(final Collection services) { 62 | return services.stream().map(this::buildServer).collect(Collectors.toList()); 63 | } 64 | 65 | /** 66 | * Build a {@link Server} instance from a Consul {@link ServiceHealth} instance. If the service 67 | * has an address defined, use that as the server host, otherwise default to using the node 68 | * address. 69 | * 70 | * @param service Consul service health record 71 | * @return Ribbon Server instance 72 | */ 73 | private Server buildServer(final ServiceHealth service) { 74 | @Nullable final String scheme = service.getService().getMeta().get("scheme"); 75 | final int port = service.getService().getPort(); 76 | 77 | final String address; 78 | if (!Strings.isNullOrEmpty(service.getService().getAddress())) { 79 | address = service.getService().getAddress(); 80 | } else { 81 | address = service.getNode().getAddress(); 82 | } 83 | 84 | final Server server = new Server(scheme, address, port); 85 | server.setZone(service.getNode().getDatacenter().orElse(Server.UNKNOWN_ZONE)); 86 | server.setReadyToServe(true); 87 | 88 | return server; 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /consul-ribbon/src/main/java/com/smoketurner/dropwizard/consul/ribbon/ConsulServiceDiscoverer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2019 Smoke Turner, LLC (github@smoketurner.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.smoketurner.dropwizard.consul.ribbon; 17 | 18 | import com.orbitz.consul.Consul; 19 | import com.orbitz.consul.model.health.ServiceHealth; 20 | import java.util.Collection; 21 | 22 | @FunctionalInterface 23 | public interface ConsulServiceDiscoverer { 24 | Collection discover(Consul consul); 25 | } 26 | -------------------------------------------------------------------------------- /consul-ribbon/src/main/java/com/smoketurner/dropwizard/consul/ribbon/HealthyConsulServiceDiscoverer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2019 Smoke Turner, LLC (github@smoketurner.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.smoketurner.dropwizard.consul.ribbon; 17 | 18 | import com.orbitz.consul.Consul; 19 | import com.orbitz.consul.model.health.ServiceHealth; 20 | import java.util.Collection; 21 | import java.util.Objects; 22 | 23 | public class HealthyConsulServiceDiscoverer implements ConsulServiceDiscoverer { 24 | 25 | private final String service; 26 | 27 | /** 28 | * Constructor 29 | * 30 | * @param service Service name 31 | */ 32 | public HealthyConsulServiceDiscoverer(final String service) { 33 | this.service = Objects.requireNonNull(service); 34 | } 35 | 36 | @Override 37 | public Collection discover(final Consul consul) { 38 | return consul.healthClient().getHealthyServiceInstances(service).getResponse(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /consul-ribbon/src/main/java/com/smoketurner/dropwizard/consul/ribbon/RibbonJerseyClient.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2019 Smoke Turner, LLC (github@smoketurner.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.smoketurner.dropwizard.consul.ribbon; 17 | 18 | import com.netflix.loadbalancer.Server; 19 | import com.netflix.loadbalancer.ZoneAwareLoadBalancer; 20 | import java.io.Closeable; 21 | import java.net.URI; 22 | import java.util.List; 23 | import java.util.Map; 24 | import java.util.Objects; 25 | import javax.net.ssl.HostnameVerifier; 26 | import javax.net.ssl.SSLContext; 27 | import javax.ws.rs.client.Client; 28 | import javax.ws.rs.client.Invocation.Builder; 29 | import javax.ws.rs.client.WebTarget; 30 | import javax.ws.rs.core.Configuration; 31 | import javax.ws.rs.core.Link; 32 | import javax.ws.rs.core.UriBuilder; 33 | 34 | public class RibbonJerseyClient implements Client, Closeable { 35 | private final ZoneAwareLoadBalancer loadBalancer; 36 | private final Client delegate; 37 | 38 | /** 39 | * Constructor 40 | * 41 | * @param loadBalancer Load Balancer 42 | * @param delegate Jersey Client delegate 43 | */ 44 | public RibbonJerseyClient( 45 | final ZoneAwareLoadBalancer loadBalancer, final Client delegate) { 46 | this.loadBalancer = Objects.requireNonNull(loadBalancer); 47 | this.delegate = Objects.requireNonNull(delegate); 48 | } 49 | 50 | /** 51 | * Constructor 52 | * 53 | * @param scheme Communication scheme (usually http or https) 54 | * @param loadBalancer Load Balancer 55 | * @param delegate Jersey Client delegate 56 | * @deprecated Use non-scheme constructor instead 57 | */ 58 | @Deprecated 59 | public RibbonJerseyClient( 60 | final String scheme, 61 | final ZoneAwareLoadBalancer loadBalancer, 62 | final Client delegate) { 63 | this.loadBalancer = Objects.requireNonNull(loadBalancer); 64 | this.delegate = Objects.requireNonNull(delegate); 65 | } 66 | 67 | /** 68 | * Return a list of available servers from this load balancing client 69 | * 70 | * @return a list of available servers 71 | */ 72 | public List getAvailableServers() { 73 | return loadBalancer.getServerList(true); 74 | } 75 | 76 | /** 77 | * Fetch a server from the load balancer or throw an exception if none are available. 78 | * 79 | * @return a server 80 | * @throws IllegalStateException if no servers are available 81 | */ 82 | private Server fetchServerOrThrow() { 83 | final Server server = loadBalancer.chooseServer(); 84 | if (server == null) { 85 | throw new IllegalStateException("No available servers for " + loadBalancer.getName()); 86 | } 87 | return server; 88 | } 89 | 90 | @Override 91 | public void close() { 92 | delegate.close(); 93 | loadBalancer.shutdown(); 94 | } 95 | 96 | @Override 97 | public Configuration getConfiguration() { 98 | return delegate.getConfiguration(); 99 | } 100 | 101 | @Override 102 | public Client property(String name, Object value) { 103 | delegate.property(name, value); 104 | return this; 105 | } 106 | 107 | @Override 108 | public Client register(Class componentClass) { 109 | delegate.register(componentClass); 110 | return this; 111 | } 112 | 113 | @Override 114 | public Client register(Class componentClass, int priority) { 115 | delegate.register(componentClass, priority); 116 | return this; 117 | } 118 | 119 | @Override 120 | public Client register(Class componentClass, Class... contracts) { 121 | delegate.register(componentClass, contracts); 122 | return this; 123 | } 124 | 125 | @Override 126 | public Client register(Class componentClass, Map, Integer> contracts) { 127 | delegate.register(componentClass, contracts); 128 | return this; 129 | } 130 | 131 | @Override 132 | public Client register(Object component) { 133 | delegate.register(component); 134 | return this; 135 | } 136 | 137 | @Override 138 | public Client register(Object component, int priority) { 139 | delegate.register(component, priority); 140 | return this; 141 | } 142 | 143 | @Override 144 | public Client register(Object component, Class... contracts) { 145 | delegate.register(component, contracts); 146 | return this; 147 | } 148 | 149 | @Override 150 | public Client register(Object component, Map, Integer> contracts) { 151 | delegate.register(component, contracts); 152 | return this; 153 | } 154 | 155 | /** 156 | * {@inheritDoc} 157 | * 158 | * @throws IllegalStateException if there are no available servers 159 | */ 160 | @Override 161 | public WebTarget target(String uri) { 162 | final Server server = fetchServerOrThrow(); 163 | final UriBuilder builder = UriBuilder.fromUri(uri); 164 | builder.scheme(server.getScheme()); 165 | builder.host(server.getHost()); 166 | builder.port(server.getPort()); 167 | return delegate.target(builder); 168 | } 169 | 170 | /** 171 | * {@inheritDoc} 172 | * 173 | * @throws IllegalStateException if there are no available servers 174 | */ 175 | @Override 176 | public WebTarget target(URI uri) { 177 | final Server server = fetchServerOrThrow(); 178 | final UriBuilder builder = UriBuilder.fromUri(uri); 179 | builder.scheme(server.getScheme()); 180 | builder.host(server.getHost()); 181 | builder.port(server.getPort()); 182 | return delegate.target(builder); 183 | } 184 | 185 | /** 186 | * {@inheritDoc} 187 | * 188 | * @throws IllegalStateException if there are no available servers 189 | */ 190 | @Override 191 | public WebTarget target(UriBuilder uriBuilder) { 192 | final Server server = fetchServerOrThrow(); 193 | uriBuilder.scheme(server.getScheme()); 194 | uriBuilder.host(server.getHost()); 195 | uriBuilder.port(server.getPort()); 196 | return delegate.target(uriBuilder); 197 | } 198 | 199 | /** 200 | * {@inheritDoc} 201 | * 202 | * @throws IllegalStateException if there are no available servers 203 | */ 204 | @Override 205 | public WebTarget target(Link link) { 206 | final Server server = fetchServerOrThrow(); 207 | final UriBuilder builder = UriBuilder.fromLink(link); 208 | builder.scheme(server.getScheme()); 209 | builder.host(server.getHost()); 210 | builder.port(server.getPort()); 211 | return delegate.target(builder); 212 | } 213 | 214 | @Override 215 | public Builder invocation(Link link) { 216 | return delegate.invocation(link); 217 | } 218 | 219 | @Override 220 | public SSLContext getSslContext() { 221 | return delegate.getSslContext(); 222 | } 223 | 224 | @Override 225 | public HostnameVerifier getHostnameVerifier() { 226 | return delegate.getHostnameVerifier(); 227 | } 228 | } 229 | -------------------------------------------------------------------------------- /consul-ribbon/src/main/java/com/smoketurner/dropwizard/consul/ribbon/RibbonJerseyClientBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2019 Smoke Turner, LLC (github@smoketurner.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.smoketurner.dropwizard.consul.ribbon; 17 | 18 | import com.google.common.primitives.Ints; 19 | import com.netflix.client.config.CommonClientConfigKey; 20 | import com.netflix.client.config.DefaultClientConfigImpl; 21 | import com.netflix.loadbalancer.LoadBalancerBuilder; 22 | import com.netflix.loadbalancer.Server; 23 | import com.netflix.loadbalancer.WeightedResponseTimeRule; 24 | import com.netflix.loadbalancer.ZoneAwareLoadBalancer; 25 | import com.orbitz.consul.Consul; 26 | import io.dropwizard.client.JerseyClientBuilder; 27 | import io.dropwizard.lifecycle.Managed; 28 | import io.dropwizard.setup.Environment; 29 | import java.util.Objects; 30 | import javax.ws.rs.client.Client; 31 | 32 | public class RibbonJerseyClientBuilder { 33 | 34 | private final Environment environment; 35 | private final Consul consul; 36 | private final RibbonJerseyClientConfiguration configuration; 37 | 38 | /** 39 | * Constructor 40 | * 41 | * @param environment Dropwizard environment 42 | * @param consul Consul client 43 | * @param configuration Load balancer Configuration 44 | */ 45 | public RibbonJerseyClientBuilder( 46 | final Environment environment, 47 | final Consul consul, 48 | final RibbonJerseyClientConfiguration configuration) { 49 | this.environment = Objects.requireNonNull(environment); 50 | this.consul = Objects.requireNonNull(consul); 51 | this.configuration = Objects.requireNonNull(configuration); 52 | } 53 | 54 | /** 55 | * Builds a new {@link RibbonJerseyClient} using service discovery by health 56 | * 57 | * @param name Service name 58 | * @return new RibbonJerseyClient 59 | */ 60 | public RibbonJerseyClient build(final String name) { 61 | return build(name, new HealthyConsulServiceDiscoverer(name)); 62 | } 63 | 64 | /** 65 | * Builds a new {@link RibbonJerseyClient} using the provided service discoverer 66 | * 67 | * @param name Jersey client name 68 | * @param serviceDiscoverer Service discoverer 69 | * @return new RibbonJerseyClient 70 | */ 71 | public RibbonJerseyClient build( 72 | final String name, final ConsulServiceDiscoverer serviceDiscoverer) { 73 | 74 | // create a new Jersey client 75 | final Client jerseyClient = 76 | new JerseyClientBuilder(environment).using(configuration).build(name); 77 | 78 | return build(name, jerseyClient, serviceDiscoverer); 79 | } 80 | 81 | /** 82 | * Builds a new {@link RibbonJerseyClient} using service discovery by health 83 | * 84 | * @param name Service name 85 | * @param jerseyClient Jersey Client 86 | * @return new {@link RibbonJerseyClient} 87 | */ 88 | public RibbonJerseyClient build(final String name, final Client jerseyClient) { 89 | return build(name, jerseyClient, new HealthyConsulServiceDiscoverer(name)); 90 | } 91 | 92 | /** 93 | * Builds a new {@link RibbonJerseyClient} with an existing Jersey Client and service discoverer 94 | * 95 | * @param name Client name 96 | * @param jerseyClient Jersey Client 97 | * @param serviceDiscoverer Service discoverer 98 | * @return new RibbonJerseyClient 99 | */ 100 | public RibbonJerseyClient build( 101 | final String name, 102 | final Client jerseyClient, 103 | final ConsulServiceDiscoverer serviceDiscoverer) { 104 | 105 | // dynamic server list that is refreshed from Consul 106 | final ConsulServerList serverList = new ConsulServerList(consul, serviceDiscoverer); 107 | 108 | // build a new load balancer based on the configuration 109 | final DefaultClientConfigImpl clientConfig = new DefaultClientConfigImpl(); 110 | clientConfig.set(CommonClientConfigKey.AppName, name); 111 | clientConfig.set( 112 | CommonClientConfigKey.ServerListRefreshInterval, 113 | Ints.checkedCast(configuration.getRefreshInterval().toMilliseconds())); 114 | 115 | final ZoneAwareLoadBalancer loadBalancer = 116 | LoadBalancerBuilder.newBuilder() 117 | .withClientConfig(clientConfig) 118 | .withRule(new WeightedResponseTimeRule()) 119 | .withDynamicServerList(serverList) 120 | .buildDynamicServerListLoadBalancer(); 121 | 122 | final RibbonJerseyClient client = new RibbonJerseyClient(loadBalancer, jerseyClient); 123 | 124 | environment 125 | .lifecycle() 126 | .manage( 127 | new Managed() { 128 | @Override 129 | public void start() throws Exception { 130 | // nothing to start 131 | } 132 | 133 | @Override 134 | public void stop() throws Exception { 135 | client.close(); 136 | } 137 | }); 138 | return client; 139 | } 140 | } 141 | -------------------------------------------------------------------------------- /consul-ribbon/src/main/java/com/smoketurner/dropwizard/consul/ribbon/RibbonJerseyClientConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2019 Smoke Turner, LLC (github@smoketurner.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.smoketurner.dropwizard.consul.ribbon; 17 | 18 | import com.fasterxml.jackson.annotation.JsonProperty; 19 | import io.dropwizard.client.JerseyClientConfiguration; 20 | import io.dropwizard.util.Duration; 21 | import io.dropwizard.validation.MinDuration; 22 | import java.util.concurrent.TimeUnit; 23 | import javax.validation.constraints.NotNull; 24 | 25 | public class RibbonJerseyClientConfiguration extends JerseyClientConfiguration { 26 | 27 | @NotNull 28 | @MinDuration(value = 1, unit = TimeUnit.SECONDS) 29 | private Duration refreshInterval = Duration.seconds(10); 30 | 31 | @JsonProperty 32 | public Duration getRefreshInterval() { 33 | return refreshInterval; 34 | } 35 | 36 | @JsonProperty 37 | public void setRefreshInterval(Duration interval) { 38 | refreshInterval = interval; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /maven_deploy_settings.xml: -------------------------------------------------------------------------------- 1 | 18 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | sonatype-nexus-snapshots 30 | ${env.CI_DEPLOY_USERNAME} 31 | ${env.CI_DEPLOY_PASSWORD} 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /mvnw: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # ---------------------------------------------------------------------------- 3 | # Licensed to the Apache Software Foundation (ASF) under one 4 | # or more contributor license agreements. See the NOTICE file 5 | # distributed with this work for additional information 6 | # regarding copyright ownership. The ASF licenses this file 7 | # to you under the Apache License, Version 2.0 (the 8 | # "License"); you may not use this file except in compliance 9 | # with the License. You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, 14 | # software distributed under the License is distributed on an 15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | # KIND, either express or implied. See the License for the 17 | # specific language governing permissions and limitations 18 | # under the License. 19 | # ---------------------------------------------------------------------------- 20 | 21 | # ---------------------------------------------------------------------------- 22 | # Maven2 Start Up Batch script 23 | # 24 | # Required ENV vars: 25 | # ------------------ 26 | # JAVA_HOME - location of a JDK home dir 27 | # 28 | # Optional ENV vars 29 | # ----------------- 30 | # M2_HOME - location of maven2's installed home dir 31 | # MAVEN_OPTS - parameters passed to the Java VM when running Maven 32 | # e.g. to debug Maven itself, use 33 | # set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 34 | # MAVEN_SKIP_RC - flag to disable loading of mavenrc files 35 | # ---------------------------------------------------------------------------- 36 | 37 | if [ -z "$MAVEN_SKIP_RC" ] ; then 38 | 39 | if [ -f /etc/mavenrc ] ; then 40 | . /etc/mavenrc 41 | fi 42 | 43 | if [ -f "$HOME/.mavenrc" ] ; then 44 | . "$HOME/.mavenrc" 45 | fi 46 | 47 | fi 48 | 49 | # OS specific support. $var _must_ be set to either true or false. 50 | cygwin=false; 51 | darwin=false; 52 | mingw=false 53 | case "`uname`" in 54 | CYGWIN*) cygwin=true ;; 55 | MINGW*) mingw=true;; 56 | Darwin*) darwin=true 57 | # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home 58 | # See https://developer.apple.com/library/mac/qa/qa1170/_index.html 59 | if [ -z "$JAVA_HOME" ]; then 60 | if [ -x "/usr/libexec/java_home" ]; then 61 | export JAVA_HOME="`/usr/libexec/java_home`" 62 | else 63 | export JAVA_HOME="/Library/Java/Home" 64 | fi 65 | fi 66 | ;; 67 | esac 68 | 69 | if [ -z "$JAVA_HOME" ] ; then 70 | if [ -r /etc/gentoo-release ] ; then 71 | JAVA_HOME=`java-config --jre-home` 72 | fi 73 | fi 74 | 75 | if [ -z "$M2_HOME" ] ; then 76 | ## resolve links - $0 may be a link to maven's home 77 | PRG="$0" 78 | 79 | # need this for relative symlinks 80 | while [ -h "$PRG" ] ; do 81 | ls=`ls -ld "$PRG"` 82 | link=`expr "$ls" : '.*-> \(.*\)$'` 83 | if expr "$link" : '/.*' > /dev/null; then 84 | PRG="$link" 85 | else 86 | PRG="`dirname "$PRG"`/$link" 87 | fi 88 | done 89 | 90 | saveddir=`pwd` 91 | 92 | M2_HOME=`dirname "$PRG"`/.. 93 | 94 | # make it fully qualified 95 | M2_HOME=`cd "$M2_HOME" && pwd` 96 | 97 | cd "$saveddir" 98 | # echo Using m2 at $M2_HOME 99 | fi 100 | 101 | # For Cygwin, ensure paths are in UNIX format before anything is touched 102 | if $cygwin ; then 103 | [ -n "$M2_HOME" ] && 104 | M2_HOME=`cygpath --unix "$M2_HOME"` 105 | [ -n "$JAVA_HOME" ] && 106 | JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 107 | [ -n "$CLASSPATH" ] && 108 | CLASSPATH=`cygpath --path --unix "$CLASSPATH"` 109 | fi 110 | 111 | # For Mingw, ensure paths are in UNIX format before anything is touched 112 | if $mingw ; then 113 | [ -n "$M2_HOME" ] && 114 | M2_HOME="`(cd "$M2_HOME"; pwd)`" 115 | [ -n "$JAVA_HOME" ] && 116 | JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" 117 | fi 118 | 119 | if [ -z "$JAVA_HOME" ]; then 120 | javaExecutable="`which javac`" 121 | if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then 122 | # readlink(1) is not available as standard on Solaris 10. 123 | readLink=`which readlink` 124 | if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then 125 | if $darwin ; then 126 | javaHome="`dirname \"$javaExecutable\"`" 127 | javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" 128 | else 129 | javaExecutable="`readlink -f \"$javaExecutable\"`" 130 | fi 131 | javaHome="`dirname \"$javaExecutable\"`" 132 | javaHome=`expr "$javaHome" : '\(.*\)/bin'` 133 | JAVA_HOME="$javaHome" 134 | export JAVA_HOME 135 | fi 136 | fi 137 | fi 138 | 139 | if [ -z "$JAVACMD" ] ; then 140 | if [ -n "$JAVA_HOME" ] ; then 141 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 142 | # IBM's JDK on AIX uses strange locations for the executables 143 | JAVACMD="$JAVA_HOME/jre/sh/java" 144 | else 145 | JAVACMD="$JAVA_HOME/bin/java" 146 | fi 147 | else 148 | JAVACMD="`which java`" 149 | fi 150 | fi 151 | 152 | if [ ! -x "$JAVACMD" ] ; then 153 | echo "Error: JAVA_HOME is not defined correctly." >&2 154 | echo " We cannot execute $JAVACMD" >&2 155 | exit 1 156 | fi 157 | 158 | if [ -z "$JAVA_HOME" ] ; then 159 | echo "Warning: JAVA_HOME environment variable is not set." 160 | fi 161 | 162 | CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher 163 | 164 | # traverses directory structure from process work directory to filesystem root 165 | # first directory with .mvn subdirectory is considered project base directory 166 | find_maven_basedir() { 167 | 168 | if [ -z "$1" ] 169 | then 170 | echo "Path not specified to find_maven_basedir" 171 | return 1 172 | fi 173 | 174 | basedir="$1" 175 | wdir="$1" 176 | while [ "$wdir" != '/' ] ; do 177 | if [ -d "$wdir"/.mvn ] ; then 178 | basedir=$wdir 179 | break 180 | fi 181 | # workaround for JBEAP-8937 (on Solaris 10/Sparc) 182 | if [ -d "${wdir}" ]; then 183 | wdir=`cd "$wdir/.."; pwd` 184 | fi 185 | # end of workaround 186 | done 187 | echo "${basedir}" 188 | } 189 | 190 | # concatenates all lines of a file 191 | concat_lines() { 192 | if [ -f "$1" ]; then 193 | echo "$(tr -s '\n' ' ' < "$1")" 194 | fi 195 | } 196 | 197 | BASE_DIR=`find_maven_basedir "$(pwd)"` 198 | if [ -z "$BASE_DIR" ]; then 199 | exit 1; 200 | fi 201 | 202 | ########################################################################################## 203 | # Extension to allow automatically downloading the maven-wrapper.jar from Maven-central 204 | # This allows using the maven wrapper in projects that prohibit checking in binary data. 205 | ########################################################################################## 206 | if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then 207 | if [ "$MVNW_VERBOSE" = true ]; then 208 | echo "Found .mvn/wrapper/maven-wrapper.jar" 209 | fi 210 | else 211 | if [ "$MVNW_VERBOSE" = true ]; then 212 | echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." 213 | fi 214 | if [ -n "$MVNW_REPOURL" ]; then 215 | jarUrl="$MVNW_REPOURL/io/takari/maven-wrapper/0.5.5/maven-wrapper-0.5.5.jar" 216 | else 217 | jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.5/maven-wrapper-0.5.5.jar" 218 | fi 219 | while IFS="=" read key value; do 220 | case "$key" in (wrapperUrl) jarUrl="$value"; break ;; 221 | esac 222 | done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" 223 | if [ "$MVNW_VERBOSE" = true ]; then 224 | echo "Downloading from: $jarUrl" 225 | fi 226 | wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" 227 | if $cygwin; then 228 | wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"` 229 | fi 230 | 231 | if command -v wget > /dev/null; then 232 | if [ "$MVNW_VERBOSE" = true ]; then 233 | echo "Found wget ... using wget" 234 | fi 235 | if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then 236 | wget "$jarUrl" -O "$wrapperJarPath" 237 | else 238 | wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath" 239 | fi 240 | elif command -v curl > /dev/null; then 241 | if [ "$MVNW_VERBOSE" = true ]; then 242 | echo "Found curl ... using curl" 243 | fi 244 | if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then 245 | curl -o "$wrapperJarPath" "$jarUrl" -f 246 | else 247 | curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f 248 | fi 249 | 250 | else 251 | if [ "$MVNW_VERBOSE" = true ]; then 252 | echo "Falling back to using Java to download" 253 | fi 254 | javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" 255 | # For Cygwin, switch paths to Windows format before running javac 256 | if $cygwin; then 257 | javaClass=`cygpath --path --windows "$javaClass"` 258 | fi 259 | if [ -e "$javaClass" ]; then 260 | if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then 261 | if [ "$MVNW_VERBOSE" = true ]; then 262 | echo " - Compiling MavenWrapperDownloader.java ..." 263 | fi 264 | # Compiling the Java class 265 | ("$JAVA_HOME/bin/javac" "$javaClass") 266 | fi 267 | if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then 268 | # Running the downloader 269 | if [ "$MVNW_VERBOSE" = true ]; then 270 | echo " - Running MavenWrapperDownloader.java ..." 271 | fi 272 | ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") 273 | fi 274 | fi 275 | fi 276 | fi 277 | ########################################################################################## 278 | # End of extension 279 | ########################################################################################## 280 | 281 | export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} 282 | if [ "$MVNW_VERBOSE" = true ]; then 283 | echo $MAVEN_PROJECTBASEDIR 284 | fi 285 | MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" 286 | 287 | # For Cygwin, switch paths to Windows format before running java 288 | if $cygwin; then 289 | [ -n "$M2_HOME" ] && 290 | M2_HOME=`cygpath --path --windows "$M2_HOME"` 291 | [ -n "$JAVA_HOME" ] && 292 | JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` 293 | [ -n "$CLASSPATH" ] && 294 | CLASSPATH=`cygpath --path --windows "$CLASSPATH"` 295 | [ -n "$MAVEN_PROJECTBASEDIR" ] && 296 | MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` 297 | fi 298 | 299 | # Provide a "standardized" way to retrieve the CLI args that will 300 | # work with both Windows and non-Windows executions. 301 | MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" 302 | export MAVEN_CMD_LINE_ARGS 303 | 304 | WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 305 | 306 | exec "$JAVACMD" \ 307 | $MAVEN_OPTS \ 308 | -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ 309 | "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ 310 | ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" 311 | -------------------------------------------------------------------------------- /mvnw.cmd: -------------------------------------------------------------------------------- 1 | @REM ---------------------------------------------------------------------------- 2 | @REM Licensed to the Apache Software Foundation (ASF) under one 3 | @REM or more contributor license agreements. See the NOTICE file 4 | @REM distributed with this work for additional information 5 | @REM regarding copyright ownership. The ASF licenses this file 6 | @REM to you under the Apache License, Version 2.0 (the 7 | @REM "License"); you may not use this file except in compliance 8 | @REM with the License. You may obtain a copy of the License at 9 | @REM 10 | @REM http://www.apache.org/licenses/LICENSE-2.0 11 | @REM 12 | @REM Unless required by applicable law or agreed to in writing, 13 | @REM software distributed under the License is distributed on an 14 | @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | @REM KIND, either express or implied. See the License for the 16 | @REM specific language governing permissions and limitations 17 | @REM under the License. 18 | @REM ---------------------------------------------------------------------------- 19 | 20 | @REM ---------------------------------------------------------------------------- 21 | @REM Maven2 Start Up Batch script 22 | @REM 23 | @REM Required ENV vars: 24 | @REM JAVA_HOME - location of a JDK home dir 25 | @REM 26 | @REM Optional ENV vars 27 | @REM M2_HOME - location of maven2's installed home dir 28 | @REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands 29 | @REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a key stroke before ending 30 | @REM MAVEN_OPTS - parameters passed to the Java VM when running Maven 31 | @REM e.g. to debug Maven itself, use 32 | @REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 33 | @REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files 34 | @REM ---------------------------------------------------------------------------- 35 | 36 | @REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' 37 | @echo off 38 | @REM set title of command window 39 | title %0 40 | @REM enable echoing by setting MAVEN_BATCH_ECHO to 'on' 41 | @if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% 42 | 43 | @REM set %HOME% to equivalent of $HOME 44 | if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") 45 | 46 | @REM Execute a user defined script before this one 47 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre 48 | @REM check for pre script, once with legacy .bat ending and once with .cmd ending 49 | if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" 50 | if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" 51 | :skipRcPre 52 | 53 | @setlocal 54 | 55 | set ERROR_CODE=0 56 | 57 | @REM To isolate internal variables from possible post scripts, we use another setlocal 58 | @setlocal 59 | 60 | @REM ==== START VALIDATION ==== 61 | if not "%JAVA_HOME%" == "" goto OkJHome 62 | 63 | echo. 64 | echo Error: JAVA_HOME not found in your environment. >&2 65 | echo Please set the JAVA_HOME variable in your environment to match the >&2 66 | echo location of your Java installation. >&2 67 | echo. 68 | goto error 69 | 70 | :OkJHome 71 | if exist "%JAVA_HOME%\bin\java.exe" goto init 72 | 73 | echo. 74 | echo Error: JAVA_HOME is set to an invalid directory. >&2 75 | echo JAVA_HOME = "%JAVA_HOME%" >&2 76 | echo Please set the JAVA_HOME variable in your environment to match the >&2 77 | echo location of your Java installation. >&2 78 | echo. 79 | goto error 80 | 81 | @REM ==== END VALIDATION ==== 82 | 83 | :init 84 | 85 | @REM Find the project base dir, i.e. the directory that contains the folder ".mvn". 86 | @REM Fallback to current working directory if not found. 87 | 88 | set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% 89 | IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir 90 | 91 | set EXEC_DIR=%CD% 92 | set WDIR=%EXEC_DIR% 93 | :findBaseDir 94 | IF EXIST "%WDIR%"\.mvn goto baseDirFound 95 | cd .. 96 | IF "%WDIR%"=="%CD%" goto baseDirNotFound 97 | set WDIR=%CD% 98 | goto findBaseDir 99 | 100 | :baseDirFound 101 | set MAVEN_PROJECTBASEDIR=%WDIR% 102 | cd "%EXEC_DIR%" 103 | goto endDetectBaseDir 104 | 105 | :baseDirNotFound 106 | set MAVEN_PROJECTBASEDIR=%EXEC_DIR% 107 | cd "%EXEC_DIR%" 108 | 109 | :endDetectBaseDir 110 | 111 | IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig 112 | 113 | @setlocal EnableExtensions EnableDelayedExpansion 114 | for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a 115 | @endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% 116 | 117 | :endReadAdditionalConfig 118 | 119 | SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" 120 | set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" 121 | set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 122 | 123 | set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.5/maven-wrapper-0.5.5.jar" 124 | 125 | FOR /F "tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( 126 | IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B 127 | ) 128 | 129 | @REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central 130 | @REM This allows using the maven wrapper in projects that prohibit checking in binary data. 131 | if exist %WRAPPER_JAR% ( 132 | if "%MVNW_VERBOSE%" == "true" ( 133 | echo Found %WRAPPER_JAR% 134 | ) 135 | ) else ( 136 | if not "%MVNW_REPOURL%" == "" ( 137 | SET DOWNLOAD_URL="%MVNW_REPOURL%/io/takari/maven-wrapper/0.5.5/maven-wrapper-0.5.5.jar" 138 | ) 139 | if "%MVNW_VERBOSE%" == "true" ( 140 | echo Couldn't find %WRAPPER_JAR%, downloading it ... 141 | echo Downloading from: %DOWNLOAD_URL% 142 | ) 143 | 144 | powershell -Command "&{"^ 145 | "$webclient = new-object System.Net.WebClient;"^ 146 | "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^ 147 | "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^ 148 | "}"^ 149 | "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^ 150 | "}" 151 | if "%MVNW_VERBOSE%" == "true" ( 152 | echo Finished downloading %WRAPPER_JAR% 153 | ) 154 | ) 155 | @REM End of extension 156 | 157 | @REM Provide a "standardized" way to retrieve the CLI args that will 158 | @REM work with both Windows and non-Windows executions. 159 | set MAVEN_CMD_LINE_ARGS=%* 160 | 161 | %MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* 162 | if ERRORLEVEL 1 goto error 163 | goto end 164 | 165 | :error 166 | set ERROR_CODE=1 167 | 168 | :end 169 | @endlocal & set ERROR_CODE=%ERROR_CODE% 170 | 171 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost 172 | @REM check for post script, once with legacy .bat ending and once with .cmd ending 173 | if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" 174 | if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" 175 | :skipRcPost 176 | 177 | @REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' 178 | if "%MAVEN_BATCH_PAUSE%" == "on" pause 179 | 180 | if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% 181 | 182 | exit /B %ERROR_CODE% 183 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 20 | 4.0.0 21 | 22 | com.smoketurner.dropwizard 23 | dropwizard-pom 24 | 2.1.4-3 25 | 26 | 27 | dropwizard-consul 28 | 2.0.12-2-SNAPSHOT 29 | pom 30 | 31 | dropwizard-consul 32 | Dropwizard Consul Bundle 33 | https://github.com/smoketurner/dropwizard-consul 34 | 35 | 36 | consul-core 37 | consul-ribbon 38 | consul-example 39 | 40 | 41 | 42 | scm:git:git://github.com/smoketurner/dropwizard-consul.git 43 | scm:git:git@github.com:smoketurner/dropwizard-consul.git 44 | https://github.com/smoketurner/dropwizard-consul 45 | HEAD 46 | 47 | 48 | 49 | 50 | 51 | com.squareup.okhttp3 52 | okhttp 53 | 3.12.2 54 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /spotbugs.xml: -------------------------------------------------------------------------------- 1 | 18 | 19 | --------------------------------------------------------------------------------