├── .github ├── CONTRIBUTING.md ├── ISSUE_TEMPLATE.md ├── ISSUE_TEMPLATE │ ├── bug_report.md │ └── feature_request.md ├── dco.yml ├── dependabot.yml └── workflows │ ├── deploy-docs.yml │ └── maven.yml ├── .gitignore ├── .java-version ├── .mvn ├── jvm.config ├── maven.config └── wrapper │ ├── maven-wrapper.jar │ └── maven-wrapper.properties ├── .sdkmanrc ├── .settings.xml ├── LICENSE.txt ├── README.adoc ├── SECURITY.md ├── docker-compose.yml ├── docs ├── antora-playbook.yml ├── antora.yml ├── modules │ └── ROOT │ │ ├── assets │ │ └── images │ │ │ └── .gitkeep │ │ ├── nav.adoc │ │ ├── pages │ │ ├── _attributes.adoc │ │ ├── appendix.adoc │ │ ├── config.adoc │ │ ├── configprops.adoc │ │ ├── dependencies.adoc │ │ ├── dependency-watcher.adoc │ │ ├── discovery.adoc │ │ ├── index.adoc │ │ ├── install.adoc │ │ ├── intro.adoc │ │ ├── other-componentes.adoc │ │ ├── quickstart.adoc │ │ └── service-registry.adoc │ │ └── partials │ │ └── _configprops.adoc ├── package.json ├── pom.xml └── src │ └── main │ ├── antora │ └── resources │ │ └── antora-resources │ │ └── antora.yml │ └── asciidoc │ ├── README.adoc │ ├── ghpages.sh │ └── images │ └── .gitkeep ├── mvnw ├── mvnw.cmd ├── pom.xml ├── scripts └── runAcceptanceTests.sh ├── spring-cloud-starter-zookeeper-all └── pom.xml ├── spring-cloud-starter-zookeeper-config └── pom.xml ├── spring-cloud-starter-zookeeper-discovery └── pom.xml ├── spring-cloud-starter-zookeeper └── pom.xml ├── spring-cloud-zookeeper-config ├── pom.xml └── src │ ├── main │ ├── java │ │ └── org │ │ │ └── springframework │ │ │ └── cloud │ │ │ └── zookeeper │ │ │ └── config │ │ │ ├── AbstractZookeeperPropertySource.java │ │ │ ├── ConfigWatcher.java │ │ │ ├── ZookeeperBootstrapper.java │ │ │ ├── ZookeeperConfigAutoConfiguration.java │ │ │ ├── ZookeeperConfigBootstrapConfiguration.java │ │ │ ├── ZookeeperConfigDataLoader.java │ │ │ ├── ZookeeperConfigDataLocationResolver.java │ │ │ ├── ZookeeperConfigDataMissingEnvironmentPostProcessor.java │ │ │ ├── ZookeeperConfigDataResource.java │ │ │ ├── ZookeeperConfigProperties.java │ │ │ ├── ZookeeperPropertySource.java │ │ │ ├── ZookeeperPropertySourceLocator.java │ │ │ └── ZookeeperPropertySources.java │ └── resources │ │ └── META-INF │ │ ├── spring.factories │ │ └── spring │ │ └── org.springframework.boot.autoconfigure.AutoConfiguration.imports │ └── test │ ├── java │ └── org │ │ └── springframework │ │ └── cloud │ │ └── zookeeper │ │ └── config │ │ ├── ZookeeperConfigAutoConfigurationTests.java │ │ ├── ZookeeperConfigDataCustomizationIntegrationTests.java │ │ ├── ZookeeperConfigDataIntegrationTests.java │ │ ├── ZookeeperConfigDataLocationResolverTests.java │ │ ├── ZookeeperConfigDataMissingEnvironmentPostProcessorTests.java │ │ ├── ZookeeperConfigDataNoImportIntegrationTests.java │ │ ├── ZookeeperConfigDataNotOptionalIntegrationTests.java │ │ ├── ZookeeperPropertySourceLocatorFailFastTests.java │ │ ├── ZookeeperPropertySourceLocatorNoApplicationNameTests.java │ │ └── ZookeeperPropertySourceLocatorTests.java │ └── resources │ └── application.yml ├── spring-cloud-zookeeper-core ├── pom.xml └── src │ ├── main │ ├── java │ │ └── org │ │ │ └── springframework │ │ │ └── cloud │ │ │ └── zookeeper │ │ │ ├── ConditionalOnZookeeperEnabled.java │ │ │ ├── CuratorFactory.java │ │ │ ├── CuratorFrameworkCustomizer.java │ │ │ ├── ZookeeperAutoConfiguration.java │ │ │ ├── ZookeeperHealthAutoConfiguration.java │ │ │ ├── ZookeeperHealthIndicator.java │ │ │ └── ZookeeperProperties.java │ └── resources │ │ └── META-INF │ │ ├── additional-spring-configuration-metadata.json │ │ └── spring │ │ ├── aot.factories │ │ └── org.springframework.boot.autoconfigure.AutoConfiguration.imports │ └── test │ └── java │ └── org │ └── springframework │ └── cloud │ └── zookeeper │ ├── CuratorFactoryTests.java │ ├── ZookeeperAutoConfigurationTests.java │ ├── ZookeeperHealthAutoConfigurationTests.java │ └── test │ └── ZookeeperTestingServer.java ├── spring-cloud-zookeeper-dependencies └── pom.xml ├── spring-cloud-zookeeper-discovery ├── pom.xml └── src │ ├── main │ ├── java │ │ └── org │ │ │ └── springframework │ │ │ └── cloud │ │ │ └── zookeeper │ │ │ ├── discovery │ │ │ ├── ConditionalOnLoadBalancerForZookeeperEnabled.java │ │ │ ├── ConditionalOnZookeeperDiscoveryEnabled.java │ │ │ ├── DependencyPathUtils.java │ │ │ ├── LoadBalancerZookeeperAutoConfiguration.java │ │ │ ├── ZookeeperDiscoveryAutoConfiguration.java │ │ │ ├── ZookeeperDiscoveryClient.java │ │ │ ├── ZookeeperDiscoveryClientConfiguration.java │ │ │ ├── ZookeeperDiscoveryHealthIndicator.java │ │ │ ├── ZookeeperDiscoveryProperties.java │ │ │ ├── ZookeeperInstance.java │ │ │ ├── ZookeeperLoadBalancerConfiguration.java │ │ │ ├── ZookeeperServiceInstance.java │ │ │ ├── ZookeeperServiceInstanceListSupplier.java │ │ │ ├── ZookeeperServiceInstances.java │ │ │ ├── ZookeeperServiceWatch.java │ │ │ ├── configclient │ │ │ │ ├── ZookeeperConfigServerAutoConfiguration.java │ │ │ │ ├── ZookeeperConfigServerBootstrapper.java │ │ │ │ └── ZookeeperDiscoveryClientConfigServiceBootstrapConfiguration.java │ │ │ ├── dependency │ │ │ │ ├── ConditionalOnDependenciesNotPassed.java │ │ │ │ ├── ConditionalOnDependenciesPassed.java │ │ │ │ ├── DependenciesNotPassedCondition.java │ │ │ │ ├── DependenciesPassedCondition.java │ │ │ │ ├── DependencyEnvironmentPostProcessor.java │ │ │ │ ├── DependencyFeignClientAutoConfiguration.java │ │ │ │ ├── DependencyRestTemplateAutoConfiguration.java │ │ │ │ ├── StubsConfiguration.java │ │ │ │ ├── ZookeeperDependencies.java │ │ │ │ ├── ZookeeperDependenciesAutoConfiguration.java │ │ │ │ └── ZookeeperDependency.java │ │ │ ├── reactive │ │ │ │ ├── ZookeeperReactiveDiscoveryClient.java │ │ │ │ └── ZookeeperReactiveDiscoveryClientConfiguration.java │ │ │ └── watcher │ │ │ │ ├── DefaultDependencyWatcher.java │ │ │ │ ├── DependencyRegistrationHookProvider.java │ │ │ │ ├── DependencyState.java │ │ │ │ ├── DependencyStateChangeListenerRegistry.java │ │ │ │ ├── DependencyWatcherAutoConfiguration.java │ │ │ │ ├── DependencyWatcherListener.java │ │ │ │ └── presence │ │ │ │ ├── DefaultDependencyPresenceOnStartupVerifier.java │ │ │ │ ├── DependencyPresenceOnStartupVerifier.java │ │ │ │ ├── FailOnMissingDependencyChecker.java │ │ │ │ ├── LogMissingDependencyChecker.java │ │ │ │ ├── NoInstancesRunningException.java │ │ │ │ └── PresenceChecker.java │ │ │ ├── serviceregistry │ │ │ ├── ServiceInstanceRegistration.java │ │ │ ├── ZookeeperAutoServiceRegistration.java │ │ │ ├── ZookeeperAutoServiceRegistrationAutoConfiguration.java │ │ │ ├── ZookeeperRegistration.java │ │ │ ├── ZookeeperServiceRegistry.java │ │ │ └── ZookeeperServiceRegistryAutoConfiguration.java │ │ │ └── support │ │ │ ├── CuratorServiceDiscoveryAutoConfiguration.java │ │ │ ├── DefaultServiceDiscoveryCustomizer.java │ │ │ ├── ServiceDiscoveryCustomizer.java │ │ │ └── StatusConstants.java │ └── resources │ │ └── META-INF │ │ ├── spring.factories │ │ └── spring │ │ ├── aot.factories │ │ └── org.springframework.boot.autoconfigure.AutoConfiguration.imports │ └── test │ ├── java │ └── org │ │ └── springframework │ │ └── cloud │ │ └── zookeeper │ │ ├── discovery │ │ ├── TestServiceRegistrar.java │ │ ├── ZookeeperDiscoveryAutoRegistrationFalseTests.java │ │ ├── ZookeeperDiscoveryClientTests.java │ │ ├── ZookeeperDiscoveryDisabledTests.java │ │ ├── ZookeeperDiscoveryHealthIndicatorDisabledTests.java │ │ ├── ZookeeperDiscoveryHealthIndicatorWithNestedStructureTests.java │ │ ├── ZookeeperDiscoveryPropertiesIntegrationTests.java │ │ ├── ZookeeperDiscoveryPropertiesTests.java │ │ ├── ZookeeperDiscoverySecurePortTests.java │ │ ├── ZookeeperDiscoveryTests.java │ │ ├── ZookeeperLifecycleRegistrationDisabledTests.java │ │ ├── ZookeeprDiscoveryNonWebAppTests.java │ │ ├── configclient │ │ │ ├── DiscoveryClientConfigServiceAutoConfigurationTests.java │ │ │ ├── ZookeeperConfigServerAutoConfigurationTests.java │ │ │ ├── ZookeeperConfigServerBootstrapperCustomizerTests.java │ │ │ ├── ZookeeperConfigServerBootstrapperNoConfigClientTests.java │ │ │ ├── ZookeeperConfigServerBootstrapperTests.java │ │ │ └── ZookeeperConfigServerBootstrapperTestsIT.java │ │ ├── dependency │ │ │ ├── DependencyConfig.java │ │ │ ├── StubsConfigurationTests.java │ │ │ ├── ZookeeperDependenciesTest.java │ │ │ ├── ZookeeperDependenciesTests.java │ │ │ ├── ZookeeperDiscoveryWithDependenciesIntegrationTests.java │ │ │ └── ZookeeperDiscoveryWithDyingDependenciesTests.java │ │ ├── reactive │ │ │ ├── ZookeeperReactiveDiscoveryClientConfigurationTests.java │ │ │ └── ZookeeperReactiveDiscoveryClientTests.java │ │ ├── test │ │ │ ├── CommonTestConfig.java │ │ │ ├── TestLoadBalancedClient.java │ │ │ └── TestServiceRestClient.java │ │ └── watcher │ │ │ ├── DefaultDependencyWatcherSpringTests.java │ │ │ └── presence │ │ │ ├── DefaultDependencyPresenceOnStartupVerifierTests.java │ │ │ └── DependencyPresenceOnStartupVerifierTests.java │ │ └── serviceregistry │ │ └── ZookeeperAutoServiceRegistrationTests.java │ └── resources │ ├── application-client.yml │ ├── application-dependencies.yml │ ├── application-loadbalancer.yml │ ├── application-loadbalancerclient.yml │ ├── application-nestedstructure.yml │ ├── application-server.yml │ ├── application-watcher.yml │ ├── application.yml │ ├── bootstrapper.yaml │ └── logback-test.xml ├── spring-cloud-zookeeper-sample ├── pom.xml └── src │ ├── main │ ├── java │ │ └── org │ │ │ └── springframework │ │ │ └── cloud │ │ │ └── zookeeper │ │ │ └── sample │ │ │ └── SampleZookeeperApplication.java │ └── resources │ │ ├── application.yml │ │ └── bootstrap.yml │ └── test │ ├── java │ └── org │ │ └── springframework │ │ └── cloud │ │ └── zookeeper │ │ └── sample │ │ ├── SampleApplicationTests.java │ │ ├── ZookeeperConfigDataOrderingIntegrationTests.java │ │ └── ZookeeperDisabledTests.java │ └── resources │ ├── orderingtest-dev.properties │ └── orderingtest.properties └── src └── checkstyle └── checkstyle-suppressions.xml /.github/CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | 2 | # Contributing 3 | 4 | Spring Cloud is released under the non-restrictive Apache 2.0 license, 5 | and follows a very standard Github development process, using Github 6 | tracker for issues and merging pull requests into master. If you want 7 | to contribute even something trivial please do not hesitate, but 8 | follow the guidelines below. 9 | 10 | ## Sign the Contributor License Agreement 11 | Before we accept a non-trivial patch or pull request we will need you to sign the 12 | [Contributor License Agreement](https://cla.pivotal.io/sign/spring). 13 | Signing the contributor's agreement does not grant anyone commit rights to the main 14 | repository, but it does mean that we can accept your contributions, and you will get an 15 | author credit if we do. Active contributors might be asked to join the core team, and 16 | given the ability to merge pull requests. 17 | 18 | ## Code of Conduct 19 | This project adheres to the Contributor Covenant [code of 20 | conduct](https://github.com/spring-cloud/spring-cloud-build/blob/main/docs/modules/ROOT/partials/code-of-conduct.adoc). By participating, you are expected to uphold this code. Please report 21 | unacceptable behavior to spring-code-of-conduct@pivotal.io. 22 | 23 | ## Code Conventions and Housekeeping 24 | None of these is essential for a pull request, but they will all help. They can also be 25 | added after the original pull request but before a merge. 26 | 27 | * Use the Spring Framework code format conventions. If you use Eclipse 28 | you can import formatter settings using the 29 | `eclipse-code-formatter.xml` file from the 30 | [Spring Cloud Build](https://raw.githubusercontent.com/spring-cloud/spring-cloud-build/master/spring-cloud-dependencies-parent/eclipse-code-formatter.xml) project. If using IntelliJ, you can use the 31 | [Eclipse Code Formatter Plugin](https://plugins.jetbrains.com/plugin/6546) to import the same file. 32 | * Make sure all new `.java` files to have a simple Javadoc class comment with at least an 33 | `@author` tag identifying you, and preferably at least a paragraph on what the class is 34 | for. 35 | * Add the ASF license header comment to all new `.java` files (copy from existing files 36 | in the project) 37 | * Add yourself as an `@author` to the .java files that you modify substantially (more 38 | than cosmetic changes). 39 | * Add some Javadocs and, if you change the namespace, some XSD doc elements. 40 | * A few unit tests would help a lot as well -- someone has to do it. 41 | * If no-one else is using your branch, please rebase it against the current master (or 42 | other target branch in the main project). 43 | * When writing a commit message please follow [these conventions](https://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html), 44 | if you are fixing an existing issue please add `Fixes gh-XXXX` at the end of the commit 45 | message (where XXXX is the issue number). 46 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 21 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | Please provide details of the problem, including the version of Spring Cloud that you 12 | are using. 13 | 14 | **Sample** 15 | If possible, please provide a test case or sample application that reproduces 16 | the problem. This makes it much easier for us to diagnose the problem and to verify that 17 | we have fixed it. 18 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Is your feature request related to a problem? Please describe.** 11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 12 | 13 | **Describe the solution you'd like** 14 | A clear and concise description of what you want to happen. 15 | 16 | **Describe alternatives you've considered** 17 | A clear and concise description of any alternative solutions or features you've considered. 18 | 19 | **Additional context** 20 | Add any other context or screenshots about the feature request here. 21 | -------------------------------------------------------------------------------- /.github/dco.yml: -------------------------------------------------------------------------------- 1 | require: 2 | members: false 3 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: "github-actions" 4 | directory: "/" 5 | target-branch: "4.1.x" 6 | schedule: 7 | interval: "weekly" 8 | - package-ecosystem: "github-actions" 9 | directory: "/" 10 | target-branch: "4.2.x" 11 | schedule: 12 | interval: "weekly" 13 | - package-ecosystem: "github-actions" 14 | directory: "/" 15 | target-branch: "main" 16 | schedule: 17 | interval: "weekly" 18 | - package-ecosystem: maven 19 | directory: / 20 | schedule: 21 | interval: daily 22 | target-branch: 4.1.x 23 | ignore: 24 | # only upgrade patch versions for maintenance branch 25 | - dependency-name: "*" 26 | update-types: 27 | - version-update:semver-major 28 | - version-update:semver-minor 29 | - package-ecosystem: maven 30 | directory: / 31 | schedule: 32 | interval: daily 33 | target-branch: 4.2.x 34 | ignore: 35 | # only upgrade patch versions for maintenance branch 36 | - dependency-name: "*" 37 | update-types: 38 | - version-update:semver-major 39 | - version-update:semver-minor 40 | - package-ecosystem: maven 41 | directory: / 42 | schedule: 43 | interval: daily 44 | target-branch: main 45 | ignore: 46 | # only upgrade by minor or patch 47 | - dependency-name: "*" 48 | update-types: 49 | - version-update:semver-major 50 | - package-ecosystem: npm 51 | target-branch: docs-build 52 | directory: / 53 | schedule: 54 | interval: weekly 55 | - package-ecosystem: npm 56 | target-branch: main 57 | directory: /docs 58 | schedule: 59 | interval: weekly 60 | - package-ecosystem: npm 61 | target-branch: 4.1.x 62 | directory: /docs 63 | schedule: 64 | interval: weekly 65 | - package-ecosystem: npm 66 | target-branch: 4.2.x 67 | directory: /docs 68 | schedule: 69 | interval: weekly 70 | -------------------------------------------------------------------------------- /.github/workflows/deploy-docs.yml: -------------------------------------------------------------------------------- 1 | name: Deploy Docs 2 | on: 3 | push: 4 | branches-ignore: [ gh-pages ] 5 | tags: '**' 6 | repository_dispatch: 7 | types: request-build-reference # legacy 8 | #schedule: 9 | #- cron: '0 10 * * *' # Once per day at 10am UTC 10 | workflow_dispatch: 11 | permissions: 12 | actions: write 13 | jobs: 14 | build: 15 | runs-on: ubuntu-latest 16 | # if: github.repository_owner == 'spring-cloud' 17 | steps: 18 | - name: Checkout 19 | uses: actions/checkout@v4 20 | with: 21 | ref: docs-build 22 | fetch-depth: 1 23 | - name: Dispatch (partial build) 24 | if: github.ref_type == 'branch' 25 | env: 26 | GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} 27 | run: gh workflow run deploy-docs.yml -r $(git rev-parse --abbrev-ref HEAD) -f build-refname=${{ github.ref_name }} 28 | - name: Dispatch (full build) 29 | if: github.ref_type == 'tag' 30 | env: 31 | GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} 32 | run: gh workflow run deploy-docs.yml -r $(git rev-parse --abbrev-ref HEAD) 33 | -------------------------------------------------------------------------------- /.github/workflows/maven.yml: -------------------------------------------------------------------------------- 1 | # This workflow will build a Java project with Maven 2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven 3 | 4 | name: Build 5 | 6 | on: 7 | push: 8 | branches: [ main, 4.2.x, 4.1.x, 4.0.x, 3.1.x ] 9 | pull_request: 10 | branches: [ main, 4.2.x, 4.1.x, 4.0.x, 3.1.x ] 11 | 12 | jobs: 13 | build: 14 | 15 | runs-on: ubuntu-latest 16 | 17 | steps: 18 | - uses: actions/checkout@v4 19 | - name: Set up JDK 20 | uses: actions/setup-java@v4 21 | with: 22 | distribution: 'temurin' 23 | java-version: '17' 24 | - name: Cache local Maven repository 25 | uses: actions/cache@v4 26 | with: 27 | path: ~/.m2/repository 28 | key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }} 29 | restore-keys: | 30 | ${{ runner.os }}-maven- 31 | - name: Build with Maven 32 | run: ./mvnw clean install -B -U -Pspring -Dmaven.test.redirectTestOutputToFile=true 33 | - name: Publish Test Report 34 | uses: mikepenz/action-junit-report@v5 35 | if: always() # always run even if the previous step fails 36 | with: 37 | report_paths: '**/surefire-reports/TEST-*.xml' 38 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | #* 3 | *# 4 | .#* 5 | .classpath 6 | .project 7 | .settings/ 8 | .factorypath 9 | .springBeans 10 | target/ 11 | _site/ 12 | .idea 13 | *.iml 14 | *.swp 15 | *.log 16 | .checkstyle 17 | .DS_Store 18 | .vscode/ 19 | .flattened-pom.xml 20 | 21 | node 22 | node_modules 23 | build 24 | /package.json 25 | package-lock.json 26 | -------------------------------------------------------------------------------- /.java-version: -------------------------------------------------------------------------------- 1 | 17 2 | -------------------------------------------------------------------------------- /.mvn/jvm.config: -------------------------------------------------------------------------------- 1 | -Xmx1024m -XX:CICompilerCount=1 -XX:TieredStopAtLevel=1 -Djava.security.egd=file:/dev/./urandom -------------------------------------------------------------------------------- /.mvn/maven.config: -------------------------------------------------------------------------------- 1 | -P spring 2 | -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/spring-cloud/spring-cloud-zookeeper/34597b99c84b5ed8c6a9212b8e6a2d37073bfe5d/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | # Licensed to the Apache Software Foundation (ASF) under one 2 | # or more contributor license agreements. See the NOTICE file 3 | # distributed with this work for additional information 4 | # regarding copyright ownership. The ASF licenses this file 5 | # to you under the Apache License, Version 2.0 (the 6 | # "License"); you may not use this file except in compliance 7 | # with the License. You may obtain a copy of the License at 8 | # 9 | # https://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.0/apache-maven-3.9.0-bin.zip 18 | wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.1/maven-wrapper-3.1.1.jar 19 | -------------------------------------------------------------------------------- /.sdkmanrc: -------------------------------------------------------------------------------- 1 | # Enable auto-env through the sdkman_auto_env config 2 | # Add key=value pairs of SDKs to use below 3 | java=17.0.1-tem 4 | -------------------------------------------------------------------------------- /.settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | repo.spring.io 6 | ${env.CI_DEPLOY_USERNAME} 7 | ${env.CI_DEPLOY_PASSWORD} 8 | 9 | 10 | 11 | 12 | 18 | spring 19 | true 20 | 21 | 22 | spring-snapshots 23 | Spring Snapshots 24 | https://repo.spring.io/snapshot 25 | 26 | true 27 | 28 | 29 | 30 | spring-milestones 31 | Spring Milestones 32 | https://repo.spring.io/libs-milestone-local 33 | 34 | false 35 | 36 | 37 | 38 | spring-releases 39 | Spring Releases 40 | https://repo.spring.io/release 41 | 42 | false 43 | 44 | 45 | 46 | 47 | 48 | spring-snapshots 49 | Spring Snapshots 50 | https://repo.spring.io/snapshot 51 | 52 | true 53 | 54 | 55 | 56 | spring-milestones 57 | Spring Milestones 58 | https://repo.spring.io/libs-milestone-local 59 | 60 | false 61 | 62 | 63 | 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | ## Reporting a Vulnerability 4 | 5 | To report security vulnerabilities, please go to https://pivotal.io/security. 6 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | zookeeper: 2 | image: zookeeper 3 | ports: 4 | - "2181:2181" 5 | -------------------------------------------------------------------------------- /docs/antora-playbook.yml: -------------------------------------------------------------------------------- 1 | antora: 2 | extensions: 3 | - require: '@springio/antora-extensions' 4 | root_component_name: 'cloud-zookeeper' 5 | site: 6 | title: Spring Cloud Zookeeper 7 | url: https://docs.spring.io/spring-cloud-zookeeper/reference/ 8 | content: 9 | sources: 10 | - url: ./.. 11 | branches: HEAD 12 | start_path: docs 13 | worktrees: true 14 | asciidoc: 15 | attributes: 16 | page-stackoverflow-url: https://stackoverflow.com/tags/spring-cloud 17 | page-pagination: '' 18 | hide-uri-scheme: '@' 19 | tabs-sync-option: '@' 20 | chomp: 'all' 21 | extensions: 22 | - '@asciidoctor/tabs' 23 | - '@springio/asciidoctor-extensions' 24 | sourcemap: true 25 | urls: 26 | latest_version_segment: '' 27 | runtime: 28 | log: 29 | failure_level: warn 30 | format: pretty 31 | ui: 32 | bundle: 33 | url: https://github.com/spring-io/antora-ui-spring/releases/download/v0.4.15/ui-bundle.zip 34 | -------------------------------------------------------------------------------- /docs/antora.yml: -------------------------------------------------------------------------------- 1 | name: cloud-zookeeper 2 | version: true 3 | title: Spring Cloud Zookeeper 4 | nav: 5 | - modules/ROOT/nav.adoc 6 | ext: 7 | collector: 8 | run: 9 | command: ./mvnw --no-transfer-progress -B process-resources -Pdocs -pl docs -Dantora-maven-plugin.phase=none -Dgenerate-docs.phase=none -Dgenerate-readme.phase=none -Dgenerate-cloud-resources.phase=none -Dmaven-dependency-plugin-for-docs.phase=none -Dmaven-dependency-plugin-for-docs-classes.phase=none -DskipTests -DdisableConfigurationProperties 10 | local: true 11 | scan: 12 | dir: ./target/classes/antora-resources/ 13 | -------------------------------------------------------------------------------- /docs/modules/ROOT/assets/images/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/spring-cloud/spring-cloud-zookeeper/34597b99c84b5ed8c6a9212b8e6a2d37073bfe5d/docs/modules/ROOT/assets/images/.gitkeep -------------------------------------------------------------------------------- /docs/modules/ROOT/nav.adoc: -------------------------------------------------------------------------------- 1 | * xref:index.adoc[Introduction] 2 | * xref:quickstart.adoc[] 3 | * xref:install.adoc[] 4 | * xref:discovery.adoc[] 5 | * xref:other-componentes.adoc[] 6 | * xref:service-registry.adoc[] 7 | * xref:dependencies.adoc[] 8 | * xref:dependency-watcher.adoc[] 9 | * xref:config.adoc[] 10 | * xref:appendix.adoc[] 11 | -------------------------------------------------------------------------------- /docs/modules/ROOT/pages/_attributes.adoc: -------------------------------------------------------------------------------- 1 | 2 | :doctype: book 3 | :idprefix: 4 | :idseparator: - 5 | :tabsize: 4 6 | :numbered: 7 | :sectanchors: 8 | :sectnums: 9 | :icons: font 10 | :hide-uri-scheme: 11 | :docinfo: shared,private 12 | 13 | :sc-ext: java 14 | :project-full-name: Spring Cloud Zookeeper 15 | -------------------------------------------------------------------------------- /docs/modules/ROOT/pages/appendix.adoc: -------------------------------------------------------------------------------- 1 | :numbered!: 2 | [appendix] 3 | [[common-application-properties]] 4 | = Common application properties 5 | :page-section-summary-toc: 1 6 | 7 | 8 | Various properties can be specified inside your `application.properties` file, inside your `application.yml` file, or as command line switches. 9 | This appendix provides a list of common Spring Cloud Zookeeper properties and references to the underlying classes that consume them. 10 | 11 | NOTE: Property contributions can come from additional jar files on your classpath, so you should not consider this an exhaustive list. 12 | Also, you can define your own properties. 13 | 14 | include::partial$_configprops.adoc[] 15 | -------------------------------------------------------------------------------- /docs/modules/ROOT/pages/configprops.adoc: -------------------------------------------------------------------------------- 1 | [[configuration-properties]] 2 | = Configuration Properties 3 | 4 | Below you can find a list of configuration properties. 5 | 6 | include::partial$_configprops.adoc[] 7 | -------------------------------------------------------------------------------- /docs/modules/ROOT/pages/dependency-watcher.adoc: -------------------------------------------------------------------------------- 1 | [[spring-cloud-zookeeper-dependency-watcher]] 2 | = Spring Cloud Zookeeper Dependency Watcher 3 | 4 | The Dependency Watcher mechanism lets you register listeners to your dependencies. The 5 | functionality is, in fact, an implementation of the `Observer` pattern. When a 6 | dependency changes, its state (to either UP or DOWN), some custom logic can be applied. 7 | 8 | [[activating]] 9 | == Activating 10 | 11 | Spring Cloud Zookeeper Dependencies functionality needs to be enabled for you to use the 12 | Dependency Watcher mechanism. 13 | // TODO: How can the reader do that? 14 | 15 | [[registering-a-listener]] 16 | == Registering a Listener 17 | 18 | To register a listener, you must implement an interface called 19 | `org.springframework.cloud.zookeeper.discovery.watcher.DependencyWatcherListener` and 20 | register it as a bean. The interface gives you one method: 21 | 22 | [source,java,indent=0] 23 | ---- 24 | void stateChanged(String dependencyName, DependencyState newState); 25 | ---- 26 | 27 | If you want to register a listener for a particular dependency, the `dependencyName` would 28 | be the discriminator for your concrete implementation. `newState` provides you with 29 | information about whether your dependency has changed to `CONNECTED` or `DISCONNECTED`. 30 | 31 | [[spring-cloud-zookeeper-dependency-watcher-presence-checker]] 32 | == Using the Presence Checker 33 | 34 | Bound with the Dependency Watcher is the functionality called Presence Checker. It lets 35 | you provide custom behavior when your application boots, to react according to the state 36 | of your dependencies. 37 | 38 | The default implementation of the abstract 39 | `org.springframework.cloud.zookeeper.discovery.watcher.presence.DependencyPresenceOnStartupVerifier` 40 | class is the 41 | `org.springframework.cloud.zookeeper.discovery.watcher.presence.DefaultDependencyPresenceOnStartupVerifier`, 42 | which works in the following way. 43 | 44 | . If the dependency is marked us `required` and is not in Zookeeper, when your application 45 | boots, it throws an exception and shuts down. 46 | . If the dependency is not `required`, the 47 | `org.springframework.cloud.zookeeper.discovery.watcher.presence.LogMissingDependencyChecker` 48 | logs that the dependency is missing at the `WARN` level. 49 | 50 | Because the `DefaultDependencyPresenceOnStartupVerifier` is registered only when there is 51 | no bean of type `DependencyPresenceOnStartupVerifier`, this functionality can be 52 | overridden. 53 | 54 | -------------------------------------------------------------------------------- /docs/modules/ROOT/pages/index.adoc: -------------------------------------------------------------------------------- 1 | include::intro.adoc[] -------------------------------------------------------------------------------- /docs/modules/ROOT/pages/install.adoc: -------------------------------------------------------------------------------- 1 | [[spring-cloud-zookeeper-install]] 2 | = Working with Zookeeper 3 | 4 | See the https://zookeeper.apache.org/doc/current/zookeeperStarted.html[installation 5 | documentation] for instructions on how to install Zookeeper. 6 | 7 | // TODO: describe Testcontainers and zk 8 | 9 | -------------------------------------------------------------------------------- /docs/modules/ROOT/pages/intro.adoc: -------------------------------------------------------------------------------- 1 | [[spring-cloud-gateway-intro]] 2 | = Spring Cloud Zookeeper 3 | 4 | This project provides Zookeeper integrations for Spring Boot applications through 5 | autoconfiguration and binding to the Spring Environment and other Spring programming model 6 | idioms. With a few annotations, you can quickly enable and configure the common patterns 7 | inside your application and build large distributed systems with Zookeeper based 8 | components. The provided patterns include Service Discovery and Configuration. The project 9 | also provides client-side load-balancing via integration with Spring Cloud LoadBalancer. 10 | -------------------------------------------------------------------------------- /docs/modules/ROOT/pages/other-componentes.adoc: -------------------------------------------------------------------------------- 1 | [[spring-cloud-zookeeper-other-componentes]] 2 | = Using Spring Cloud Zookeeper with Spring Cloud Components 3 | :page-section-summary-toc: 1 4 | 5 | Feign, Spring Cloud Gateway and Spring Cloud LoadBalancer all work with Spring Cloud Zookeeper. 6 | 7 | [[spring-cloud-loadbalancer-with-zookeeper]] 8 | == Spring Cloud LoadBalancer with Zookeeper 9 | 10 | Spring Cloud Zookeeper provides an implementation of Spring Cloud LoadBalancer `ServiceInstanceListSupplier`. 11 | When you use the `spring-cloud-starter-zookeeper-discovery`, Spring Cloud LoadBalancer is autoconfigured to use the 12 | `ZookeeperServiceInstanceListSupplier` by default. 13 | 14 | TIP: If you were previously using the StickyRule in Zookeeper, its replacement in the current stack 15 | is the `SameInstancePreferenceServiceInstanceListSupplier` in SC LoadBalancer. You can read on how to set it up in the https://docs.spring.io/spring-cloud-commons/docs/current/reference/html/#spring-cloud-loadbalancer[Spring Cloud Commons documentation]. 16 | 17 | -------------------------------------------------------------------------------- /docs/modules/ROOT/pages/service-registry.adoc: -------------------------------------------------------------------------------- 1 | [[spring-cloud-zookeeper-service-registry]] 2 | = Spring Cloud Zookeeper and Service Registry 3 | 4 | Spring Cloud Zookeeper implements the `ServiceRegistry` interface, letting developers 5 | register arbitrary services in a programmatic way. 6 | 7 | The `ServiceInstanceRegistration` class offers a `builder()` method to create a 8 | `Registration` object that can be used by the `ServiceRegistry`, as shown in the following 9 | example: 10 | 11 | [source,java,indent=0] 12 | ---- 13 | @Autowired 14 | private ZookeeperServiceRegistry serviceRegistry; 15 | 16 | public void registerThings() { 17 | ZookeeperRegistration registration = ServiceInstanceRegistration.builder() 18 | .defaultUriSpec() 19 | .address("anyUrl") 20 | .port(10) 21 | .name("/a/b/c/d/anotherservice") 22 | .build(); 23 | this.serviceRegistry.register(registration); 24 | } 25 | ---- 26 | 27 | [[instance-status]] 28 | == Instance Status 29 | 30 | Netflix Eureka supports having instances that are `OUT_OF_SERVICE` registered with the server. 31 | These instances are not returned as active service instances. 32 | This is useful for behaviors such as blue/green deployments. 33 | (Note that the Curator Service Discovery recipe does not support this behavior.) Taking advantage of the flexible payload has let Spring Cloud Zookeeper implement `OUT_OF_SERVICE` by updating some specific metadata and then filtering on that metadata in the Spring Cloud LoadBalancer `ZookeeperServiceInstanceListSupplier`. 34 | The `ZookeeperServiceInstanceListSupplier` filters out all non-null instance statuses that do not equal `UP`. 35 | If the instance status field is empty, it is considered to be `UP` for backwards compatibility. 36 | To change the status of an instance, make a `POST` with `OUT_OF_SERVICE` to the `ServiceRegistry` 37 | instance status actuator endpoint, as shown in the following example: 38 | 39 | [source,sh,indent=0] 40 | ---- 41 | $ http POST http://localhost:8081/serviceregistry status=OUT_OF_SERVICE 42 | ---- 43 | 44 | NOTE: The preceding example uses the `http` command from https://httpie.org. 45 | 46 | -------------------------------------------------------------------------------- /docs/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "antora": "3.2.0-alpha.8", 4 | "@antora/atlas-extension": "1.0.0-alpha.2", 5 | "@antora/collector-extension": "1.0.1", 6 | "@asciidoctor/tabs": "1.0.0-beta.6", 7 | "@springio/antora-extensions": "1.14.4", 8 | "@springio/asciidoctor-extensions": "1.0.0-alpha.17" 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /docs/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.springframework.cloud 8 | spring-cloud-zookeeper 9 | 4.3.1-SNAPSHOT 10 | .. 11 | 12 | spring-cloud-zookeeper-docs 13 | jar 14 | Spring Cloud Zookeeper Docs 15 | Spring Cloud Zookeeper Docs 16 | 17 | spring-cloud-zookeeper 18 | ${basedir}/.. 19 | spring.cloud.zookeeper.* 20 | 21 | none 22 | 23 | 24 | 1.0.2 25 | ${maven.multiModuleProjectDirectory}/ 26 | .* 27 | ${maven.multiModuleProjectDirectory}/docs/modules/ROOT/partials/ 28 | 29 | 30 | src/main/asciidoc 31 | 32 | 33 | 34 | enable-configuration-properties 35 | 36 | 37 | !disableConfigurationProperties 38 | 39 | 40 | 41 | 42 | ${project.groupId} 43 | spring-cloud-starter-zookeeper-all 44 | 45 | 46 | 47 | 48 | docs 49 | 50 | 51 | 52 | src/main/antora/resources/antora-resources 53 | true 54 | 55 | 56 | 57 | 58 | pl.project13.maven 59 | git-commit-id-plugin 60 | 61 | 62 | org.apache.maven.plugins 63 | maven-dependency-plugin 64 | 65 | 66 | org.codehaus.mojo 67 | exec-maven-plugin 68 | 69 | 70 | io.spring.maven.antora 71 | antora-component-version-maven-plugin 72 | 73 | 74 | org.antora 75 | antora-maven-plugin 76 | 77 | 78 | org.apache.maven.plugins 79 | maven-antrun-plugin 80 | 81 | 82 | maven-deploy-plugin 83 | 84 | 85 | 86 | 87 | 88 | 89 | -------------------------------------------------------------------------------- /docs/src/main/antora/resources/antora-resources/antora.yml: -------------------------------------------------------------------------------- 1 | version: @antora-component.version@ 2 | prerelease: @antora-component.prerelease@ 3 | 4 | asciidoc: 5 | attributes: 6 | attribute-missing: 'warn' 7 | chomp: 'all' 8 | project-root: @maven.multiModuleProjectDirectory@ 9 | github-repo: @docs.main@ 10 | github-raw: https://raw.githubusercontent.com/spring-cloud/@docs.main@/@github-tag@ 11 | github-code: https://github.com/spring-cloud/@docs.main@/tree/@github-tag@ 12 | github-issues: https://github.com/spring-cloud/@docs.main@/issues/ 13 | github-wiki: https://github.com/spring-cloud/@docs.main@/wiki 14 | spring-cloud-version: @project.version@ 15 | github-tag: @github-tag@ 16 | version-type: @version-type@ 17 | docs-url: https://docs.spring.io/@docs.main@/docs/@project.version@ 18 | raw-docs-url: https://raw.githubusercontent.com/spring-cloud/@docs.main@/@github-tag@ 19 | project-version: @project.version@ 20 | project-name: @docs.main@ 21 | -------------------------------------------------------------------------------- /docs/src/main/asciidoc/README.adoc: -------------------------------------------------------------------------------- 1 | image::https://github.com/spring-cloud/spring-cloud-zookeeper/workflows/Build/badge.svg?style=svg["Actions Status", link="https://github.com/spring-cloud/spring-cloud-zookeeper/actions"] 2 | 3 | 4 | 5 | [[quick-start]] 6 | = Quick Start 7 | 8 | 9 | [[zookeeper-overview]] 10 | = Zookeeper overview 11 | 12 | ZooKeeper is a centralized service for maintaining configuration information, naming, providing distributed synchronization, and providing group services. 13 | See the 14 | https://zookeeper.apache.org[Zookeeper site] for more information. 15 | Spring Cloud Zookeeper also builds on the https://curator.apache.org[Apache Curator] project, which started life at Netflix. 16 | 17 | [[spring-cloud-zookeeper-features]] 18 | = Spring Cloud Zookeeper Features 19 | 20 | Spring Cloud Zookeeper includes the following features: 21 | 22 | * Spring Cloud `DiscoveryClient` implementation (supports Spring Cloud LoadBalancer) 23 | * Zookeeper-based `PropertySource` loaded during the 'bootstrap' phase 24 | 25 | [[running-the-sample]] 26 | = Running the Sample 27 | 28 | . https://zookeeper.apache.org/doc/current/zookeeperStarted.html#sc_Download[Install 29 | zookeeper] (On a mac with homebrew, use `brew install zookeeper`). 30 | . https://zookeeper.apache.org/doc/current/zookeeperStarted.html#sc_InstallingSingleMode[Start 31 | zookeeper]. 32 | . https://zookeeper.apache.org/doc/current/zookeeperStarted.html#sc_ConnectingToZooKeeper[Verify 33 | zookeeper is running]. 34 | . Run `mvn --settings .settings.xml package`. 35 | Doing so brings in the required Spring Cloud Maven repositories and Build. 36 | . Run `java -jar spring-cloud-zookeeper-sample/target/spring-cloud-zookeeper-sample-1.2.0.BUILD-SNAPSHOT.jar`. 37 | . Visit http://localhost:8080 to verify that `{"serviceId":"testZookeeperApp","host":"","port":8080}` works. 38 | . Run `java -jar spring-cloud-zookeeper-sample/target/spring-cloud-zookeeper-sample-1.2.0.BUILD-SNAPSHOT.jar --server.port=8081` 39 | . Visit http://localhost:8080 again to verify that `{"serviceId":"testZookeeperApp","host":"","port":8081}` eventually shows up in the results in a round-robin fashion. (It may take a minute or so.) 40 | 41 | [[building]] 42 | = Building 43 | 44 | include::https://raw.githubusercontent.com/spring-cloud/spring-cloud-build/main/docs/modules/ROOT/partials/building.adoc[] 45 | 46 | [[contributing]] 47 | = Contributing 48 | 49 | include::https://raw.githubusercontent.com/spring-cloud/spring-cloud-build/main/docs/modules/ROOT/partials/contributing.adoc[] 50 | -------------------------------------------------------------------------------- /docs/src/main/asciidoc/images/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/spring-cloud/spring-cloud-zookeeper/34597b99c84b5ed8c6a9212b8e6a2d37073bfe5d/docs/src/main/asciidoc/images/.gitkeep -------------------------------------------------------------------------------- /scripts/runAcceptanceTests.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -o errexit 4 | 5 | SCRIPT_URL="https://raw.githubusercontent.com/spring-cloud-samples/brewery/2021.0.x/runAcceptanceTests.sh" 6 | AT_WHAT_TO_TEST="ZOOKEEPER" 7 | 8 | curl "${SCRIPT_URL}" --output runAcceptanceTests.sh 9 | 10 | chmod +x runAcceptanceTests.sh 11 | 12 | ./runAcceptanceTests.sh --whattotest "${AT_WHAT_TO_TEST}" --killattheend 13 | -------------------------------------------------------------------------------- /spring-cloud-starter-zookeeper-all/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.cloud 7 | spring-cloud-zookeeper 8 | 4.3.1-SNAPSHOT 9 | .. 10 | 11 | spring-cloud-starter-zookeeper-all 12 | Spring Cloud Starter Zookeeper All 13 | Spring Cloud Starter Zookeeper All 14 | https://projects.spring.io/spring-cloud 15 | 16 | Pivotal Software, Inc. 17 | https://www.spring.io 18 | 19 | 20 | ${basedir}/../.. 21 | 22 | 23 | 24 | org.springframework.cloud 25 | spring-cloud-starter-zookeeper-config 26 | 27 | 28 | org.springframework.cloud 29 | spring-cloud-starter-zookeeper-discovery 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /spring-cloud-starter-zookeeper-config/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.cloud 7 | spring-cloud-zookeeper 8 | 4.3.1-SNAPSHOT 9 | .. 10 | 11 | spring-cloud-starter-zookeeper-config 12 | Spring Cloud Starter Zookeeper Config 13 | Spring Cloud Starter Zookeeper Config 14 | https://projects.spring.io/spring-cloud 15 | 16 | Pivotal Software, Inc. 17 | https://www.spring.io 18 | 19 | 20 | ${basedir}/../.. 21 | 22 | 23 | 24 | org.springframework.cloud 25 | spring-cloud-starter-zookeeper 26 | 27 | 28 | org.springframework.cloud 29 | spring-cloud-zookeeper-config 30 | 31 | 32 | org.apache.curator 33 | curator-recipes 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /spring-cloud-starter-zookeeper-discovery/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.springframework.cloud 8 | spring-cloud-zookeeper 9 | 4.3.1-SNAPSHOT 10 | .. 11 | 12 | spring-cloud-starter-zookeeper-discovery 13 | Spring Cloud Starter Zookeeper Discovery 14 | Spring Cloud Starter Zookeeper Discovery 15 | https://projects.spring.io/spring-cloud 16 | 17 | Pivotal Software, Inc. 18 | https://www.spring.io 19 | 20 | 21 | ${basedir}/../.. 22 | 23 | 24 | 25 | org.springframework.cloud 26 | spring-cloud-starter-zookeeper 27 | 28 | 29 | org.springframework.cloud 30 | spring-cloud-zookeeper-discovery 31 | 32 | 33 | org.apache.curator 34 | curator-x-discovery 35 | 36 | 37 | org.springframework.cloud 38 | spring-cloud-starter-loadbalancer 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /spring-cloud-starter-zookeeper/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.cloud 7 | spring-cloud-zookeeper 8 | 4.3.1-SNAPSHOT 9 | .. 10 | 11 | spring-cloud-starter-zookeeper 12 | Spring Cloud Starter Zookeeper 13 | Spring Cloud Starter Zookeeper 14 | https://projects.spring.io/spring-cloud 15 | 16 | Pivotal Software, Inc. 17 | https://www.spring.io 18 | 19 | 20 | ${basedir}/../.. 21 | 22 | 23 | 24 | org.springframework.cloud 25 | spring-cloud-starter 26 | 27 | 28 | org.springframework.cloud 29 | spring-cloud-zookeeper-core 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-config/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | spring-cloud-zookeeper-config 8 | jar 9 | Spring Cloud Zookeeper Config 10 | Spring Cloud Zookeeper Config 11 | 12 | 13 | org.springframework.cloud 14 | spring-cloud-zookeeper 15 | 4.3.1-SNAPSHOT 16 | .. 17 | 18 | 19 | 20 | 21 | 22 | org.apache.maven.plugins 23 | maven-compiler-plugin 24 | 25 | 26 | 27 | 28 | 29 | 30 | org.springframework.cloud 31 | spring-cloud-zookeeper-core 32 | 33 | 34 | org.apache.curator 35 | curator-x-discovery 36 | 37 | 38 | org.springframework.cloud 39 | spring-cloud-commons 40 | 41 | 42 | org.springframework.cloud 43 | spring-cloud-context 44 | true 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-starter-web 49 | true 50 | 51 | 52 | org.springframework.boot 53 | spring-boot-starter-actuator 54 | true 55 | 56 | 57 | org.apache.curator 58 | curator-recipes 59 | true 60 | 61 | 62 | org.springframework.boot 63 | spring-boot-autoconfigure-processor 64 | true 65 | 66 | 67 | org.springframework.boot 68 | spring-boot-configuration-processor 69 | true 70 | 71 | 72 | org.springframework.boot 73 | spring-boot-starter-test 74 | test 75 | 76 | 77 | org.junit.vintage 78 | junit-vintage-engine 79 | test 80 | 81 | 82 | org.springframework.cloud 83 | spring-cloud-zookeeper-core 84 | ${project.version} 85 | test-jar 86 | test 87 | 88 | 89 | org.apache.curator 90 | curator-test 91 | test 92 | 93 | 94 | 95 | 96 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/AbstractZookeeperPropertySource.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.config; 18 | 19 | import org.apache.curator.framework.CuratorFramework; 20 | 21 | import org.springframework.core.env.EnumerablePropertySource; 22 | 23 | /** 24 | * A {@link EnumerablePropertySource} that has a notion of a context which is the root 25 | * folder in Zookeeper. 26 | * 27 | * @author Spencer Gibb 28 | * @since 1.0.0 29 | */ 30 | public abstract class AbstractZookeeperPropertySource 31 | extends EnumerablePropertySource { 32 | 33 | private String context; 34 | 35 | public AbstractZookeeperPropertySource(String context, CuratorFramework source) { 36 | super(context, source); 37 | this.context = context; 38 | if (!this.context.startsWith("/")) { 39 | this.context = "/" + this.context; 40 | } 41 | } 42 | 43 | protected String sanitizeKey(String path) { 44 | return path.replace(this.context + "/", "").replace('/', '.'); 45 | } 46 | 47 | public String getContext() { 48 | return this.context; 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ZookeeperBootstrapper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2020 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 | * https://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 | package org.springframework.cloud.zookeeper.config; 18 | 19 | import java.util.function.Function; 20 | 21 | import org.apache.curator.RetryPolicy; 22 | import org.apache.curator.drivers.TracerDriver; 23 | import org.apache.curator.ensemble.EnsembleProvider; 24 | import org.apache.curator.framework.CuratorFramework; 25 | 26 | import org.springframework.boot.BootstrapContext; 27 | import org.springframework.boot.BootstrapRegistry; 28 | import org.springframework.boot.BootstrapRegistryInitializer; 29 | import org.springframework.cloud.zookeeper.CuratorFrameworkCustomizer; 30 | 31 | public class ZookeeperBootstrapper implements BootstrapRegistryInitializer { 32 | 33 | private Function retryPolicy; 34 | 35 | private Function ensembleProvider; 36 | 37 | private Function tracerDriver; 38 | 39 | private Function curatorFrameworkCustomizer; 40 | 41 | static BootstrapRegistryInitializer fromBootstrapContext(Function factory) { 42 | return registry -> registry.register(CuratorFramework.class, factory::apply); 43 | } 44 | 45 | static ZookeeperBootstrapper create() { 46 | return new ZookeeperBootstrapper(); 47 | } 48 | 49 | public ZookeeperBootstrapper retryPolicy(Function retryPolicy) { 50 | this.retryPolicy = retryPolicy; 51 | return this; 52 | } 53 | 54 | public ZookeeperBootstrapper ensembleProvider(Function ensembleProvider) { 55 | this.ensembleProvider = ensembleProvider; 56 | return this; 57 | } 58 | 59 | public ZookeeperBootstrapper tracerDriver(Function tracerDriver) { 60 | this.tracerDriver = tracerDriver; 61 | return this; 62 | } 63 | 64 | public ZookeeperBootstrapper curatorFrameworkCustomizer(Function curatorFrameworkCustomizer) { 65 | this.curatorFrameworkCustomizer = curatorFrameworkCustomizer; 66 | return this; 67 | } 68 | 69 | @Override 70 | public void initialize(BootstrapRegistry registry) { 71 | register(registry, RetryPolicy.class, retryPolicy); 72 | register(registry, EnsembleProvider.class, ensembleProvider); 73 | register(registry, TracerDriver.class, tracerDriver); 74 | register(registry, CuratorFrameworkCustomizer.class, curatorFrameworkCustomizer); 75 | } 76 | 77 | private void register(BootstrapRegistry registry, Class type, Function factory) { 78 | if (this.retryPolicy != null) { 79 | registry.register(type, factory::apply); 80 | } 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ZookeeperConfigAutoConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.config; 18 | 19 | import java.util.Collections; 20 | import java.util.List; 21 | 22 | import org.apache.curator.framework.CuratorFramework; 23 | 24 | import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; 25 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 26 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 27 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 28 | import org.springframework.cloud.endpoint.RefreshEndpoint; 29 | import org.springframework.cloud.zookeeper.ConditionalOnZookeeperEnabled; 30 | import org.springframework.context.annotation.Bean; 31 | import org.springframework.context.annotation.Configuration; 32 | import org.springframework.core.env.Environment; 33 | 34 | /** 35 | * {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration 36 | * Auto-configuration} that registers a Zookeeper configuration watcher. 37 | * 38 | * @author Spencer Gibb 39 | * @since 1.0.0 40 | */ 41 | @Configuration(proxyBeanMethods = false) 42 | @ConditionalOnZookeeperEnabled 43 | @ConditionalOnProperty(value = "spring.cloud.zookeeper.config.enabled", matchIfMissing = true) 44 | public class ZookeeperConfigAutoConfiguration { 45 | 46 | @Configuration(proxyBeanMethods = false) 47 | @ConditionalOnClass(RefreshEndpoint.class) 48 | @ConditionalOnProperty(name = "spring.cloud.zookeeper.config.watcher.enabled", matchIfMissing = true) 49 | protected static class ZkRefreshConfiguration { 50 | 51 | @Bean 52 | @ConditionalOnBean(ZookeeperPropertySourceLocator.class) 53 | public ConfigWatcher propertySourceLocatorConfigWatcher(ZookeeperPropertySourceLocator locator, 54 | CuratorFramework curator) { 55 | return new ConfigWatcher(locator.getContexts(), curator); 56 | } 57 | 58 | @Bean 59 | @ConditionalOnMissingBean(ZookeeperPropertySourceLocator.class) 60 | public ConfigWatcher configDataConfigWatcher(CuratorFramework curator, Environment env) { 61 | List contexts = env.getProperty("spring.cloud.zookeeper.config.property-source-contexts", 62 | List.class, Collections.emptyList()); 63 | return new ConfigWatcher(contexts, curator); 64 | } 65 | 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ZookeeperConfigBootstrapConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.config; 18 | 19 | import org.apache.curator.framework.CuratorFramework; 20 | 21 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 22 | import org.springframework.cloud.zookeeper.ConditionalOnZookeeperEnabled; 23 | import org.springframework.cloud.zookeeper.ZookeeperAutoConfiguration; 24 | import org.springframework.context.annotation.Bean; 25 | import org.springframework.context.annotation.Configuration; 26 | import org.springframework.context.annotation.Import; 27 | import org.springframework.core.env.Environment; 28 | import org.springframework.util.StringUtils; 29 | 30 | /** 31 | * Bootstrap Configuration for Zookeeper Configuration. 32 | * 33 | * @author Spencer Gibb 34 | * @since 1.0.0 35 | */ 36 | @Configuration(proxyBeanMethods = false) 37 | @ConditionalOnZookeeperEnabled 38 | @Import(ZookeeperAutoConfiguration.class) 39 | public class ZookeeperConfigBootstrapConfiguration { 40 | 41 | @Bean 42 | @ConditionalOnMissingBean 43 | public ZookeeperPropertySourceLocator zookeeperPropertySourceLocator( 44 | CuratorFramework curator, ZookeeperConfigProperties properties) { 45 | return new ZookeeperPropertySourceLocator(curator, properties); 46 | } 47 | 48 | @Bean 49 | @ConditionalOnMissingBean 50 | public ZookeeperConfigProperties zookeeperConfigProperties(Environment env) { 51 | ZookeeperConfigProperties properties = new ZookeeperConfigProperties(); 52 | if (!StringUtils.hasLength(properties.getName())) { 53 | properties.setName(env.getProperty("spring.application.name", "application")); 54 | } 55 | return properties; 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ZookeeperConfigDataLoader.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2020 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 | * https://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 | package org.springframework.cloud.zookeeper.config; 18 | 19 | import java.util.ArrayList; 20 | import java.util.Collections; 21 | import java.util.List; 22 | 23 | import org.apache.commons.logging.Log; 24 | import org.apache.curator.framework.CuratorFramework; 25 | 26 | import org.springframework.boot.context.config.ConfigData; 27 | import org.springframework.boot.context.config.ConfigDataLoader; 28 | import org.springframework.boot.context.config.ConfigDataLoaderContext; 29 | import org.springframework.boot.context.config.ConfigDataResourceNotFoundException; 30 | import org.springframework.boot.logging.DeferredLogFactory; 31 | import org.springframework.util.StringUtils; 32 | 33 | public class ZookeeperConfigDataLoader implements ConfigDataLoader { 34 | 35 | private final Log log; 36 | 37 | public ZookeeperConfigDataLoader(DeferredLogFactory logFactory) { 38 | this.log = logFactory.getLog(ZookeeperConfigDataLoader.class); 39 | } 40 | 41 | @Override 42 | public ConfigData load(ConfigDataLoaderContext context, ZookeeperConfigDataResource resource) { 43 | try { 44 | CuratorFramework curator = context.getBootstrapContext().get(CuratorFramework.class); 45 | if (curator == null) { 46 | // this can happen if certain conditions are met 47 | return null; 48 | } 49 | ZookeeperPropertySource propertySource = new ZookeeperPropertySource(resource.getContext(), 50 | curator); 51 | List propertySources = Collections.singletonList(propertySource); 52 | 53 | return new ConfigData(propertySources, source -> { 54 | List options = new ArrayList<>(); 55 | options.add(ConfigData.Option.IGNORE_IMPORTS); 56 | options.add(ConfigData.Option.IGNORE_PROFILES); 57 | if (StringUtils.hasText(resource.getProfile())) { 58 | options.add(ConfigData.Option.PROFILE_SPECIFIC); 59 | } 60 | return ConfigData.Options.of(options.toArray(new ConfigData.Option[0])); 61 | }); 62 | 63 | } 64 | catch (Exception e) { 65 | if (log.isDebugEnabled()) { 66 | log.debug("Error getting properties from zookeeper: " + resource, e); 67 | } 68 | throw new ConfigDataResourceNotFoundException(resource, e); 69 | } 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ZookeeperConfigDataResource.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2020 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 | * https://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 | package org.springframework.cloud.zookeeper.config; 18 | 19 | import java.util.Objects; 20 | 21 | import org.springframework.boot.context.config.ConfigDataResource; 22 | import org.springframework.core.style.ToStringCreator; 23 | 24 | public class ZookeeperConfigDataResource extends ConfigDataResource { 25 | 26 | private final String context; 27 | private final boolean optional; 28 | private final String profile; 29 | 30 | public ZookeeperConfigDataResource(String context, boolean optional, String profile) { 31 | this.context = context; 32 | this.optional = optional; 33 | this.profile = profile; 34 | } 35 | 36 | @Deprecated 37 | public ZookeeperConfigDataResource(String context, boolean optional) { 38 | this(context, optional, null); 39 | } 40 | 41 | public String getContext() { 42 | return this.context; 43 | } 44 | 45 | public boolean isOptional() { 46 | return this.optional; 47 | } 48 | 49 | public String getProfile() { 50 | return this.profile; 51 | } 52 | 53 | @Override 54 | public boolean equals(Object o) { 55 | if (this == o) { 56 | return true; 57 | } 58 | if (o == null || getClass() != o.getClass()) { 59 | return false; 60 | } 61 | ZookeeperConfigDataResource that = (ZookeeperConfigDataResource) o; 62 | return this.optional == that.optional && this.context.equals(that.context) && Objects.equals(this.profile, that.profile); 63 | } 64 | 65 | @Override 66 | public int hashCode() { 67 | return Objects.hash(this.optional, this.context, this.profile); 68 | } 69 | 70 | @Override 71 | public String toString() { 72 | return new ToStringCreator(this) 73 | .append("context", context) 74 | .append("optional", optional) 75 | .append("profile", profile) 76 | .toString(); 77 | 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-config/src/main/resources/META-INF/spring.factories: -------------------------------------------------------------------------------- 1 | # Bootstrap Configuration 2 | org.springframework.cloud.bootstrap.BootstrapConfiguration=\ 3 | org.springframework.cloud.zookeeper.config.ZookeeperConfigBootstrapConfiguration 4 | 5 | # Environment PostProcessor 6 | org.springframework.boot.env.EnvironmentPostProcessor=\ 7 | org.springframework.cloud.zookeeper.config.ZookeeperConfigDataMissingEnvironmentPostProcessor 8 | 9 | org.springframework.boot.diagnostics.FailureAnalyzer=\ 10 | org.springframework.cloud.zookeeper.config.ZookeeperConfigDataMissingEnvironmentPostProcessor.ImportExceptionFailureAnalyzer 11 | 12 | # ConfigData Location Resolvers 13 | org.springframework.boot.context.config.ConfigDataLocationResolver=\ 14 | org.springframework.cloud.zookeeper.config.ZookeeperConfigDataLocationResolver 15 | 16 | # ConfigData Loaders 17 | org.springframework.boot.context.config.ConfigDataLoader=\ 18 | org.springframework.cloud.zookeeper.config.ZookeeperConfigDataLoader 19 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-config/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports: -------------------------------------------------------------------------------- 1 | org.springframework.cloud.zookeeper.config.ZookeeperConfigAutoConfiguration 2 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-config/src/test/java/org/springframework/cloud/zookeeper/config/ZookeeperConfigDataNotOptionalIntegrationTests.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.config; 18 | 19 | import org.assertj.core.api.Assertions; 20 | import org.junit.jupiter.api.Test; 21 | 22 | import org.springframework.boot.WebApplicationType; 23 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 24 | import org.springframework.boot.builder.SpringApplicationBuilder; 25 | import org.springframework.boot.context.config.ConfigDataResourceNotFoundException; 26 | import org.springframework.cloud.context.refresh.ConfigDataContextRefresher; 27 | import org.springframework.cloud.context.refresh.ContextRefresher; 28 | import org.springframework.cloud.context.scope.refresh.RefreshScope; 29 | import org.springframework.context.ConfigurableApplicationContext; 30 | import org.springframework.context.annotation.Bean; 31 | import org.springframework.context.annotation.Configuration; 32 | 33 | /** 34 | * @author Spencer Gibb 35 | */ 36 | public class ZookeeperConfigDataNotOptionalIntegrationTests { 37 | 38 | @Test 39 | public void configDataNotFoundThrowsException() { 40 | Assertions.assertThatThrownBy(() -> { 41 | ConfigurableApplicationContext context = null; 42 | try { 43 | context = new SpringApplicationBuilder(Config.class).web(WebApplicationType.NONE).run( 44 | "--spring.cloud.zookeeper.connect-string=notexistantdomain:5000", "--debug=true", 45 | "--spring.cloud.zookeeper.max-retries=0", 46 | "--spring.cloud.zookeeper.blockUntilConnectedWait=1", 47 | "--spring.cloud.zookeeper.blockUntilConnectedUnit=MILLISECONDS", 48 | "--spring.cloud.zookeeper.connection-timeout=1ms", 49 | "--spring.config.import=zookeeper:", 50 | "--spring.application.name=testZkConfigDataNotOptionalIntegration", 51 | "--logging.level.org.springframework.cloud.zookeeper=DEBUG", 52 | "--spring.cloud.zookeeper.config.root=/shouldfail"); 53 | 54 | } 55 | finally { 56 | if (context != null) { 57 | context.close(); 58 | } 59 | } 60 | }).isInstanceOf(ConfigDataResourceNotFoundException.class); 61 | } 62 | 63 | @Configuration 64 | @EnableAutoConfiguration 65 | static class Config { 66 | 67 | @Bean 68 | public ContextRefresher contextRefresher(ConfigurableApplicationContext context, RefreshScope scope) { 69 | return new ConfigDataContextRefresher(context, scope); 70 | } 71 | 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-config/src/test/java/org/springframework/cloud/zookeeper/config/ZookeeperPropertySourceLocatorFailFastTests.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.config; 18 | 19 | import org.junit.After; 20 | import org.junit.Before; 21 | import org.junit.Test; 22 | 23 | import org.springframework.boot.SpringBootConfiguration; 24 | import org.springframework.boot.WebApplicationType; 25 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 26 | import org.springframework.boot.builder.SpringApplicationBuilder; 27 | 28 | import static org.assertj.core.api.Assertions.assertThatCode; 29 | import static org.assertj.core.api.Assertions.assertThatThrownBy; 30 | 31 | /** 32 | * @author Enrique Recarte Llorens 33 | * @author Olga Maciaszek-Sharma 34 | */ 35 | public class ZookeeperPropertySourceLocatorFailFastTests { 36 | 37 | @Before 38 | public void setUp() { 39 | // This system property makes Curator fail faster, otherwise it takes 15 seconds 40 | // to trigger a retry 41 | System.setProperty("curator-default-connection-timeout", "0"); 42 | } 43 | 44 | @After 45 | public void tearDown() { 46 | System.clearProperty("curator-default-connection-timeout"); 47 | } 48 | 49 | @Test 50 | public void testFailFastFalseLoadsTheApplicationContext() { 51 | assertThatCode(() -> { 52 | new SpringApplicationBuilder().sources(Config.class) 53 | .web(WebApplicationType.NONE) 54 | .run("--spring.application.name=testZookeeperPropertySourceLocatorFailFast", 55 | "--spring.config.use-legacy-processing=true", 56 | "--spring.cloud.zookeeper.config.connectString=localhost:2188", 57 | "--spring.cloud.zookeeper.baseSleepTimeMs=0", 58 | "--spring.cloud.zookeeper.maxRetries=0", 59 | "--spring.cloud.zookeeper.maxSleepMs=0", 60 | "--spring.cloud.zookeeper.blockUntilConnectedWait=0", 61 | "--spring.cloud.zookeeper.config.failFast=false"); 62 | }).doesNotThrowAnyException(); 63 | } 64 | 65 | @Test 66 | public void testFailFastTrueDoesNotLoadTheApplicationContext() { 67 | assertThatThrownBy(() -> { 68 | new SpringApplicationBuilder().sources(Config.class) 69 | .web(WebApplicationType.NONE) 70 | .run("--spring.application.name=testZookeeperPropertySourceLocatorFailFast", 71 | "--spring.config.use-legacy-processing=true", 72 | "--spring.cloud.zookeeper.config.connectString=localhost:2188", 73 | "--spring.cloud.zookeeper.baseSleepTimeMs=0", 74 | "--spring.cloud.zookeeper.maxRetries=0", 75 | "--spring.cloud.zookeeper.maxSleepMs=0", 76 | "--spring.cloud.zookeeper.blockUntilConnectedWait=0", 77 | "--spring.cloud.zookeeper.config.failFast=true"); 78 | }) 79 | .isNotNull(); 80 | } 81 | 82 | @SpringBootConfiguration 83 | @EnableAutoConfiguration 84 | static class Config { 85 | 86 | } 87 | 88 | } 89 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-config/src/test/java/org/springframework/cloud/zookeeper/config/ZookeeperPropertySourceLocatorNoApplicationNameTests.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.config; 18 | 19 | import org.apache.curator.framework.CuratorFramework; 20 | import org.apache.curator.framework.api.GetChildrenBuilder; 21 | import org.junit.Test; 22 | 23 | import org.springframework.mock.env.MockEnvironment; 24 | 25 | import static org.mockito.Mockito.mock; 26 | import static org.mockito.Mockito.when; 27 | 28 | /** 29 | * @author Spencer Gibb 30 | */ 31 | public class ZookeeperPropertySourceLocatorNoApplicationNameTests { 32 | 33 | @Test 34 | public void defaultSpringApplicationNameWorks() { 35 | CuratorFramework curator = mock(CuratorFramework.class); 36 | when(curator.getChildren()).thenReturn(mock(GetChildrenBuilder.class)); 37 | ZookeeperConfigProperties properties = new ZookeeperConfigProperties(); 38 | properties.setName("notempty"); 39 | ZookeeperPropertySourceLocator locator = new ZookeeperPropertySourceLocator( 40 | curator, properties); 41 | locator.locate(new MockEnvironment()); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-config/src/test/resources/application.yml: -------------------------------------------------------------------------------- 1 | logging: 2 | level: 3 | org.apache.zookeeper.ClientCnxn: ERROR -------------------------------------------------------------------------------- /spring-cloud-zookeeper-core/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | spring-cloud-zookeeper-core 8 | jar 9 | Spring Cloud Zookeeper Core 10 | Spring Cloud Zookeeper Core 11 | 12 | 13 | org.springframework.cloud 14 | spring-cloud-zookeeper 15 | 4.3.1-SNAPSHOT 16 | .. 17 | 18 | 19 | 20 | 21 | 22 | org.apache.maven.plugins 23 | maven-compiler-plugin 24 | 25 | 26 | org.apache.maven.plugins 27 | maven-jar-plugin 28 | 3.4.2 29 | 30 | 31 | 32 | test-jar 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | org.springframework.boot 43 | spring-boot-configuration-processor 44 | true 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-starter-web 49 | true 50 | 51 | 52 | org.springframework.boot 53 | spring-boot-starter-actuator 54 | true 55 | 56 | 57 | org.springframework.cloud 58 | spring-cloud-commons 59 | true 60 | 61 | 62 | org.apache.curator 63 | curator-x-discovery 64 | true 65 | 66 | 67 | org.apache.curator 68 | curator-framework 69 | true 70 | 71 | 72 | org.springframework.boot 73 | spring-boot-autoconfigure-processor 74 | true 75 | 76 | 77 | org.apache.curator 78 | curator-test 79 | test 80 | 81 | 82 | org.springframework.boot 83 | spring-boot-starter-test 84 | test 85 | 86 | 87 | org.junit.vintage 88 | junit-vintage-engine 89 | test 90 | 91 | 92 | 93 | 94 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-core/src/main/java/org/springframework/cloud/zookeeper/ConditionalOnZookeeperEnabled.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper; 18 | 19 | import java.lang.annotation.ElementType; 20 | import java.lang.annotation.Retention; 21 | import java.lang.annotation.RetentionPolicy; 22 | import java.lang.annotation.Target; 23 | 24 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 25 | 26 | /** 27 | * Wrapper annotation to enable Zookeeper. 28 | * 29 | * @author Marcin Grzejszczak 30 | * @since 1.1.0 31 | */ 32 | @Retention(RetentionPolicy.RUNTIME) 33 | @Target({ ElementType.TYPE, ElementType.METHOD }) 34 | @ConditionalOnProperty(value = "spring.cloud.zookeeper.enabled", matchIfMissing = true) 35 | public @interface ConditionalOnZookeeperEnabled { 36 | 37 | } 38 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-core/src/main/java/org/springframework/cloud/zookeeper/CuratorFrameworkCustomizer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper; 18 | 19 | import org.apache.curator.RetryPolicy; 20 | import org.apache.curator.framework.CuratorFramework; 21 | import org.apache.curator.framework.CuratorFrameworkFactory; 22 | 23 | import org.springframework.beans.factory.ObjectProvider; 24 | 25 | /** 26 | * Beans that implement this interface will be used by the 27 | * {@link ZookeeperAutoConfiguration#curatorFramework(RetryPolicy, ZookeeperProperties, ObjectProvider, ObjectProvider, ObjectProvider) 28 | * ZookeeperAutoConfiguration} ZookeeperAutoConfiguration} to further customize the 29 | * {@link CuratorFramework} that it provides. 30 | * 31 | * @author Bernardo Gomez Palacio. 32 | * @see ZookeeperAutoConfiguration 33 | */ 34 | @FunctionalInterface 35 | public interface CuratorFrameworkCustomizer { 36 | 37 | /** 38 | * Customize the {@link CuratorFrameworkFactory.Builder}. 39 | * @param builder instance of the builder that you can further customize. 40 | */ 41 | void customize(CuratorFrameworkFactory.Builder builder); 42 | 43 | } 44 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-core/src/main/java/org/springframework/cloud/zookeeper/ZookeeperHealthAutoConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper; 18 | 19 | import org.apache.curator.framework.CuratorFramework; 20 | 21 | import org.springframework.boot.actuate.autoconfigure.health.ConditionalOnEnabledHealthIndicator; 22 | import org.springframework.boot.actuate.endpoint.annotation.Endpoint; 23 | import org.springframework.boot.autoconfigure.AutoConfigureAfter; 24 | import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; 25 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 26 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 27 | import org.springframework.context.annotation.Bean; 28 | import org.springframework.context.annotation.Configuration; 29 | 30 | /** 31 | * Auto {@link Configuration} for adding a Zookeeper health endpoint to actuator if 32 | * required. 33 | * 34 | * @author Tom Gianos 35 | * @since 2.0.1 36 | */ 37 | @Configuration(proxyBeanMethods = false) 38 | @ConditionalOnZookeeperEnabled 39 | @ConditionalOnClass(Endpoint.class) 40 | @AutoConfigureAfter({ ZookeeperAutoConfiguration.class }) 41 | public class ZookeeperHealthAutoConfiguration { 42 | 43 | /** 44 | * If there is an active curator, if the zookeeper health endpoint is enabled and if a 45 | * health indicator hasn't already been added by a user add one. 46 | * @param curator The curator connection to zookeeper to use 47 | * @return An instance of {@link ZookeeperHealthIndicator} to add to actuator health 48 | * report 49 | */ 50 | @Bean 51 | @ConditionalOnMissingBean(ZookeeperHealthIndicator.class) 52 | @ConditionalOnBean(CuratorFramework.class) 53 | @ConditionalOnEnabledHealthIndicator("zookeeper") 54 | public ZookeeperHealthIndicator zookeeperHealthIndicator(CuratorFramework curator) { 55 | return new ZookeeperHealthIndicator(curator); 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-core/src/main/java/org/springframework/cloud/zookeeper/ZookeeperHealthIndicator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper; 18 | 19 | import org.apache.curator.framework.CuratorFramework; 20 | import org.apache.curator.framework.imps.CuratorFrameworkState; 21 | 22 | import org.springframework.boot.actuate.health.AbstractHealthIndicator; 23 | import org.springframework.boot.actuate.health.Health; 24 | 25 | /** 26 | * A {@link org.springframework.boot.actuate.health.HealthIndicator} that checks the 27 | * status of the Zookeeper connection. 28 | * 29 | * @author Spencer Gibb 30 | * @since 1.0.0 31 | */ 32 | public class ZookeeperHealthIndicator extends AbstractHealthIndicator { 33 | 34 | private final CuratorFramework curator; 35 | 36 | public ZookeeperHealthIndicator(CuratorFramework curator) { 37 | this.curator = curator; 38 | } 39 | 40 | @Override 41 | protected void doHealthCheck(Health.Builder builder) throws Exception { 42 | try { 43 | CuratorFrameworkState state = this.curator.getState(); 44 | if (state != CuratorFrameworkState.STARTED) { 45 | builder.down().withDetail("error", "Client not started"); 46 | } 47 | else if (this.curator.checkExists().forPath("/") == null) { 48 | builder.down().withDetail("error", "Root for namespace does not exist"); 49 | } 50 | else { 51 | builder.up(); 52 | } 53 | builder.withDetail("connectionString", 54 | this.curator.getZookeeperClient().getCurrentConnectionString()) 55 | .withDetail("state", state); 56 | } 57 | catch (Exception e) { 58 | builder.down(e); 59 | } 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-core/src/main/resources/META-INF/additional-spring-configuration-metadata.json: -------------------------------------------------------------------------------- 1 | {"properties": [ 2 | { 3 | "name": "management.health.zookeeper.enabled", 4 | "type": "java.lang.Boolean", 5 | "description": "Enable the health endpoint for zookeeper.", 6 | "defaultValue": true 7 | }, 8 | { 9 | "name": "endpoints.zookeeper.enabled", 10 | "type": "java.lang.Boolean", 11 | "description": "Enable the /zookeeper endpoint to inspect the state of zookeeper.", 12 | "defaultValue": true 13 | } 14 | ]} 15 | 16 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-core/src/main/resources/META-INF/spring/aot.factories: -------------------------------------------------------------------------------- 1 | org.springframework.aot.hint.RuntimeHintsRegistrar=\ 2 | org.springframework.cloud.zookeeper.ZookeeperCoreHints -------------------------------------------------------------------------------- /spring-cloud-zookeeper-core/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports: -------------------------------------------------------------------------------- 1 | org.springframework.cloud.zookeeper.ZookeeperAutoConfiguration 2 | org.springframework.cloud.zookeeper.ZookeeperHealthAutoConfiguration 3 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-core/src/test/java/org/springframework/cloud/zookeeper/CuratorFactoryTests.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2020 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 | * https://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 | package org.springframework.cloud.zookeeper; 18 | 19 | import org.junit.jupiter.api.Test; 20 | 21 | import org.springframework.boot.context.properties.bind.Binder; 22 | import org.springframework.mock.env.MockEnvironment; 23 | import org.springframework.web.util.UriComponentsBuilder; 24 | 25 | import static org.assertj.core.api.Assertions.assertThat; 26 | 27 | public class CuratorFactoryTests { 28 | 29 | @Test 30 | public void testLoadProperties() { 31 | ZookeeperProperties properties = CuratorFactory.loadProperties( 32 | Binder.get(new MockEnvironment()), 33 | UriComponentsBuilder.fromUriString("zookeeper://myhost:8502").build()); 34 | assertThat(properties.getConnectString()).isEqualTo("myhost:8502"); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-core/src/test/java/org/springframework/cloud/zookeeper/ZookeeperHealthAutoConfigurationTests.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper; 18 | 19 | import org.apache.curator.framework.CuratorFramework; 20 | import org.assertj.core.api.Assertions; 21 | import org.junit.Test; 22 | 23 | import org.springframework.boot.autoconfigure.AutoConfigurations; 24 | import org.springframework.boot.test.context.runner.ApplicationContextRunner; 25 | import org.springframework.cloud.zookeeper.test.ZookeeperTestingServer; 26 | import org.springframework.context.annotation.Bean; 27 | 28 | /** 29 | * Tests for {@link ZookeeperHealthAutoConfiguration}. 30 | * 31 | * @author Tom Gianos 32 | * @since 2.0.1 33 | */ 34 | public class ZookeeperHealthAutoConfigurationTests { 35 | 36 | private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() 37 | .withInitializer(new ZookeeperTestingServer.Initializer()) 38 | .withConfiguration(AutoConfigurations.of(ZookeeperAutoConfiguration.class, 39 | ZookeeperHealthAutoConfiguration.class)) 40 | .withUserConfiguration(ZookeeperAutoConfigurationTests.BaseTestConfig.class); 41 | 42 | @Test 43 | public void testDefaultPropertiesCreateZookeeperHealthIndicator() { 44 | this.contextRunner.run((context) -> Assertions.assertThat(context) 45 | .hasSingleBean(ZookeeperHealthIndicator.class)); 46 | } 47 | 48 | @Test 49 | public void testZookeeperHealthIndicatorDisabled() { 50 | this.contextRunner.withPropertyValues("management.health.zookeeper.enabled=false") 51 | .run((context) -> Assertions.assertThat(context) 52 | .doesNotHaveBean(ZookeeperHealthIndicator.class)); 53 | } 54 | 55 | @Test 56 | public void testZookeeperHealthIndicatorAlreadyAdded() { 57 | this.contextRunner.withUserConfiguration(HealthIndicatorCustomConfig.class) 58 | .run((context) -> { 59 | Assertions.assertThat(context) 60 | .hasSingleBean(ZookeeperHealthIndicator.class); 61 | Assertions.assertThat(context) 62 | .doesNotHaveBean("zookeeperHealthIndicator"); 63 | Assertions.assertThat(context) 64 | .hasBean("customZookeeperHealthIndicator"); 65 | }); 66 | } 67 | 68 | static class HealthIndicatorCustomConfig { 69 | 70 | @Bean 71 | ZookeeperHealthIndicator customZookeeperHealthIndicator( 72 | CuratorFramework curatorFramework) { 73 | return new ZookeeperHealthIndicator(curatorFramework); 74 | } 75 | 76 | } 77 | 78 | } 79 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/discovery/ConditionalOnLoadBalancerForZookeeperEnabled.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.discovery; 18 | 19 | import java.lang.annotation.ElementType; 20 | import java.lang.annotation.Retention; 21 | import java.lang.annotation.RetentionPolicy; 22 | import java.lang.annotation.Target; 23 | 24 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 25 | 26 | /** 27 | * Wrapper annotation to enable Spring Cloud LoadBalancer for Zookeeper. 28 | * 29 | * @author Olga Maciaszek-Sharma 30 | * @since 3.0.0 31 | */ 32 | @Retention(RetentionPolicy.RUNTIME) 33 | @Target({ElementType.TYPE, ElementType.METHOD}) 34 | @ConditionalOnProperty(value = "spring.cloud.zookeeper.loadbalancer.enabled", matchIfMissing = true) 35 | public @interface ConditionalOnLoadBalancerForZookeeperEnabled { 36 | } 37 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/discovery/ConditionalOnZookeeperDiscoveryEnabled.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.discovery; 18 | 19 | import java.lang.annotation.ElementType; 20 | import java.lang.annotation.Retention; 21 | import java.lang.annotation.RetentionPolicy; 22 | import java.lang.annotation.Target; 23 | 24 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 25 | import org.springframework.cloud.zookeeper.ConditionalOnZookeeperEnabled; 26 | 27 | /** 28 | * Wrapper annotation to enable Zookeeper Discovery. 29 | * 30 | * @author Marcin Grzejszczak 31 | * @since 1.1.0 32 | */ 33 | @Retention(RetentionPolicy.RUNTIME) 34 | @Target({ ElementType.TYPE, ElementType.METHOD }) 35 | @ConditionalOnZookeeperEnabled 36 | @ConditionalOnProperty(value = ConditionalOnZookeeperDiscoveryEnabled.PROPERTY, matchIfMissing = true) 37 | public @interface ConditionalOnZookeeperDiscoveryEnabled { 38 | /** 39 | * Property name. 40 | */ 41 | String PROPERTY = "spring.cloud.zookeeper.discovery.enabled"; 42 | } 43 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/discovery/DependencyPathUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.discovery; 18 | 19 | /** 20 | * Utils for correct dependency path format. 21 | * 22 | * @author Denis Stepanov 23 | * @since 1.0.4 24 | */ 25 | public final class DependencyPathUtils { 26 | 27 | private DependencyPathUtils() { 28 | } 29 | 30 | /** 31 | * Sanitizes path by ensuring that path starts with a slash and doesn't have one at 32 | * the end. 33 | * @param path file path to sanitize. 34 | * @return sanitized path. 35 | */ 36 | public static String sanitize(String path) { 37 | return withLeadingSlash(withoutSlashAtEnd(path)); 38 | } 39 | 40 | private static String withLeadingSlash(String value) { 41 | return value.startsWith("/") ? value : "/" + value; 42 | } 43 | 44 | private static String withoutSlashAtEnd(String value) { 45 | return value.endsWith("/") ? value.substring(0, value.length() - 1) : value; 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/discovery/LoadBalancerZookeeperAutoConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.discovery; 18 | 19 | import org.springframework.boot.autoconfigure.AutoConfigureAfter; 20 | import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; 21 | import org.springframework.boot.context.properties.EnableConfigurationProperties; 22 | import org.springframework.cloud.client.loadbalancer.reactive.ReactiveLoadBalancer; 23 | import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClients; 24 | import org.springframework.cloud.loadbalancer.config.LoadBalancerAutoConfiguration; 25 | import org.springframework.cloud.zookeeper.ConditionalOnZookeeperEnabled; 26 | import org.springframework.cloud.zookeeper.discovery.dependency.ZookeeperDependenciesAutoConfiguration; 27 | import org.springframework.context.annotation.Configuration; 28 | 29 | /** 30 | * {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration 31 | * Auto-configuration} that sets up Spring Cloud LoadBalancer for Zookeeper. 32 | * 33 | * @author Olga Maciaszek-Sharma 34 | * @since 3.0.0 35 | */ 36 | @Configuration(proxyBeanMethods = false) 37 | @EnableConfigurationProperties 38 | @ConditionalOnZookeeperEnabled 39 | @ConditionalOnBean(ReactiveLoadBalancer.Factory.class) 40 | @ConditionalOnLoadBalancerForZookeeperEnabled 41 | @AutoConfigureAfter({LoadBalancerAutoConfiguration.class, ZookeeperDependenciesAutoConfiguration.class}) 42 | @LoadBalancerClients(defaultConfiguration = ZookeeperLoadBalancerConfiguration.class) 43 | public class LoadBalancerZookeeperAutoConfiguration { 44 | } 45 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/discovery/ZookeeperDiscoveryClientConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.discovery; 18 | 19 | import org.apache.curator.x.discovery.ServiceDiscovery; 20 | 21 | import org.springframework.beans.factory.annotation.Autowired; 22 | import org.springframework.boot.autoconfigure.AutoConfigureBefore; 23 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 24 | import org.springframework.cloud.client.ConditionalOnBlockingDiscoveryEnabled; 25 | import org.springframework.cloud.client.ConditionalOnDiscoveryEnabled; 26 | import org.springframework.cloud.zookeeper.discovery.dependency.ZookeeperDependencies; 27 | import org.springframework.context.annotation.Bean; 28 | import org.springframework.context.annotation.Configuration; 29 | 30 | /** 31 | * {@link org.springframework.cloud.client.discovery.DiscoveryClient} configuration for 32 | * Zookeeper. 33 | * 34 | * @author Spencer Gibb 35 | * @author Tim Ysewyn 36 | * @since 1.0.0 37 | */ 38 | @Configuration(proxyBeanMethods = false) 39 | @ConditionalOnDiscoveryEnabled 40 | @ConditionalOnBlockingDiscoveryEnabled 41 | @ConditionalOnZookeeperDiscoveryEnabled 42 | @AutoConfigureBefore({ ZookeeperDiscoveryAutoConfiguration.class }) 43 | public class ZookeeperDiscoveryClientConfiguration { 44 | 45 | @Autowired(required = false) 46 | private ZookeeperDependencies zookeeperDependencies; 47 | 48 | @Bean 49 | @ConditionalOnMissingBean 50 | // currently means auto-registration is false. That will change when 51 | // ZookeeperServiceDiscovery is gone 52 | public ZookeeperDiscoveryClient zookeeperDiscoveryClient( 53 | ServiceDiscovery serviceDiscovery, 54 | ZookeeperDiscoveryProperties zookeeperDiscoveryProperties) { 55 | return new ZookeeperDiscoveryClient(serviceDiscovery, zookeeperDependencies, 56 | zookeeperDiscoveryProperties); 57 | } 58 | 59 | @Bean 60 | public Marker zookeeperDiscoveryClientMarker() { 61 | return new Marker(); 62 | } 63 | 64 | class Marker { 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/discovery/ZookeeperDiscoveryHealthIndicator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.discovery; 18 | 19 | import org.apache.commons.logging.Log; 20 | import org.apache.commons.logging.LogFactory; 21 | import org.apache.curator.framework.CuratorFramework; 22 | import org.apache.curator.x.discovery.ServiceDiscovery; 23 | import org.apache.curator.x.discovery.ServiceInstance; 24 | 25 | import org.springframework.boot.actuate.health.Health; 26 | import org.springframework.cloud.client.discovery.health.DiscoveryHealthIndicator; 27 | import org.springframework.cloud.zookeeper.discovery.dependency.ZookeeperDependencies; 28 | 29 | /** 30 | * {@link org.springframework.boot.actuate.health.HealthIndicator} that presents the 31 | * status of all instances registered in Zookeeper. 32 | * 33 | * @author Spencer Gibb 34 | * @since 1.0.0 35 | */ 36 | public class ZookeeperDiscoveryHealthIndicator implements DiscoveryHealthIndicator { 37 | 38 | private static final Log log = LogFactory 39 | .getLog(ZookeeperDiscoveryHealthIndicator.class); 40 | 41 | private CuratorFramework curatorFramework; 42 | 43 | private ServiceDiscovery serviceDiscovery; 44 | 45 | private final ZookeeperDependencies zookeeperDependencies; 46 | 47 | private final ZookeeperDiscoveryProperties zookeeperDiscoveryProperties; 48 | 49 | public ZookeeperDiscoveryHealthIndicator(CuratorFramework curatorFramework, 50 | ServiceDiscovery serviceDiscovery, 51 | ZookeeperDependencies zookeeperDependencies, 52 | ZookeeperDiscoveryProperties zookeeperDiscoveryProperties) { 53 | this.curatorFramework = curatorFramework; 54 | this.serviceDiscovery = serviceDiscovery; 55 | this.zookeeperDependencies = zookeeperDependencies; 56 | this.zookeeperDiscoveryProperties = zookeeperDiscoveryProperties; 57 | } 58 | 59 | @Override 60 | public String getName() { 61 | return "zookeeper"; 62 | } 63 | 64 | @Override 65 | public Health health() { 66 | Health.Builder builder = Health.unknown(); 67 | try { 68 | Iterable> allInstances = new ZookeeperServiceInstances( 69 | this.curatorFramework, this.serviceDiscovery, 70 | this.zookeeperDependencies, this.zookeeperDiscoveryProperties); 71 | builder.up().withDetail("services", allInstances); 72 | } 73 | catch (Exception e) { 74 | log.error("Error", e); 75 | builder.down(e); 76 | } 77 | 78 | return builder.build(); 79 | } 80 | 81 | } 82 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/discovery/ZookeeperInstance.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.discovery; 18 | 19 | import java.util.HashMap; 20 | import java.util.Map; 21 | 22 | /** 23 | * Represents the default payload of a registered service in Zookeeper. 24 | * 25 | * @author Spencer Gibb 26 | * @since 1.0.0 27 | */ 28 | public class ZookeeperInstance { 29 | 30 | private String id; 31 | 32 | private String name; 33 | 34 | private Map metadata = new HashMap<>(); 35 | 36 | @SuppressWarnings("unused") 37 | private ZookeeperInstance() { 38 | } 39 | 40 | public ZookeeperInstance(String id, String name, Map metadata) { 41 | this.id = id; 42 | this.name = name; 43 | this.metadata = metadata; 44 | } 45 | 46 | public String getId() { 47 | return this.id; 48 | } 49 | 50 | public String getName() { 51 | return this.name; 52 | } 53 | 54 | public void setId(String id) { 55 | this.id = id; 56 | } 57 | 58 | public void setName(String name) { 59 | this.name = name; 60 | } 61 | 62 | public Map getMetadata() { 63 | return this.metadata; 64 | } 65 | 66 | public void setMetadata(Map metadata) { 67 | this.metadata = metadata; 68 | } 69 | 70 | @Override 71 | public String toString() { 72 | return "ZookeeperInstance{" + "id='" + this.id + '\'' + ", name='" + this.name 73 | + '\'' + ", metadata=" + this.metadata + '}'; 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/discovery/ZookeeperLoadBalancerConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.discovery; 18 | 19 | import org.springframework.beans.factory.ObjectProvider; 20 | import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; 21 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 22 | import org.springframework.cloud.client.discovery.DiscoveryClient; 23 | import org.springframework.cloud.loadbalancer.cache.LoadBalancerCacheManager; 24 | import org.springframework.cloud.loadbalancer.core.CachingServiceInstanceListSupplier; 25 | import org.springframework.cloud.loadbalancer.core.DiscoveryClientServiceInstanceListSupplier; 26 | import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier; 27 | import org.springframework.cloud.zookeeper.discovery.dependency.ZookeeperDependencies; 28 | import org.springframework.context.ApplicationContext; 29 | import org.springframework.context.annotation.Bean; 30 | import org.springframework.context.annotation.Configuration; 31 | import org.springframework.core.env.Environment; 32 | 33 | /** 34 | * Zookeeper-specific {@link ServiceInstanceListSupplier} that provides a delegate that 35 | * filters available instances based on status retrieved from Zookeeper. 36 | * 37 | * @author Olga Maciaszek-Sharma 38 | * @since 3.0.0 39 | */ 40 | @Configuration(proxyBeanMethods = false) 41 | public class ZookeeperLoadBalancerConfiguration { 42 | 43 | @Bean 44 | @ConditionalOnBean(DiscoveryClient.class) 45 | @ConditionalOnMissingBean 46 | public ServiceInstanceListSupplier zookeeperDiscoveryClientServiceInstanceListSupplier( 47 | DiscoveryClient discoveryClient, Environment env, 48 | ApplicationContext context, 49 | ZookeeperDependencies zookeeperDependencies) { 50 | DiscoveryClientServiceInstanceListSupplier firstDelegate = new DiscoveryClientServiceInstanceListSupplier( 51 | discoveryClient, env); 52 | ZookeeperServiceInstanceListSupplier secondDelegate = new ZookeeperServiceInstanceListSupplier(firstDelegate, 53 | zookeeperDependencies); 54 | ObjectProvider cacheManagerProvider = context 55 | .getBeanProvider(LoadBalancerCacheManager.class); 56 | if (cacheManagerProvider.getIfAvailable() != null) { 57 | return new CachingServiceInstanceListSupplier(secondDelegate, 58 | cacheManagerProvider.getIfAvailable()); 59 | } 60 | return secondDelegate; 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/discovery/ZookeeperServiceInstance.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.discovery; 18 | 19 | import java.net.URI; 20 | import java.util.HashMap; 21 | import java.util.Map; 22 | 23 | import org.springframework.cloud.client.ServiceInstance; 24 | 25 | /** 26 | * A specific {@link ServiceInstance} describing a zookeeper service instance. 27 | * 28 | * @author Reda Housni-Alaoui 29 | * @author Tim Ysewyn 30 | * @since 1.1.0 31 | */ 32 | public class ZookeeperServiceInstance implements ServiceInstance { 33 | 34 | private final String serviceId; 35 | 36 | private final String host; 37 | 38 | private final int port; 39 | 40 | private final boolean secure; 41 | 42 | private final URI uri; 43 | 44 | private final Map metadata; 45 | 46 | private final org.apache.curator.x.discovery.ServiceInstance serviceInstance; 47 | 48 | /** 49 | * @param serviceId The service id to be used 50 | * @param serviceInstance The zookeeper service instance described by this service 51 | * instance 52 | */ 53 | public ZookeeperServiceInstance(String serviceId, 54 | org.apache.curator.x.discovery.ServiceInstance serviceInstance) { 55 | this.serviceId = serviceId; 56 | this.serviceInstance = serviceInstance; 57 | this.host = this.serviceInstance.getAddress(); 58 | this.secure = serviceInstance.getSslPort() != null; 59 | Integer port = serviceInstance.getPort(); 60 | if (this.secure) { 61 | port = serviceInstance.getSslPort(); 62 | } 63 | this.port = port; 64 | this.uri = URI.create(serviceInstance.buildUriSpec()); 65 | if (serviceInstance.getPayload() != null) { 66 | this.metadata = serviceInstance.getPayload().getMetadata(); 67 | } 68 | else { 69 | this.metadata = new HashMap<>(); 70 | } 71 | } 72 | 73 | @Override 74 | public String getInstanceId() { 75 | return this.serviceInstance.getId(); 76 | } 77 | 78 | @Override 79 | public String getServiceId() { 80 | return this.serviceId; 81 | } 82 | 83 | @Override 84 | public String getHost() { 85 | return this.host; 86 | } 87 | 88 | @Override 89 | public int getPort() { 90 | return this.port; 91 | } 92 | 93 | @Override 94 | public boolean isSecure() { 95 | return this.secure; 96 | } 97 | 98 | @Override 99 | public URI getUri() { 100 | return this.uri; 101 | } 102 | 103 | @Override 104 | public Map getMetadata() { 105 | return this.metadata; 106 | } 107 | 108 | public org.apache.curator.x.discovery.ServiceInstance getServiceInstance() { 109 | return this.serviceInstance; 110 | } 111 | 112 | } 113 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/discovery/configclient/ZookeeperConfigServerAutoConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013-2016 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 | * https://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 | package org.springframework.cloud.zookeeper.discovery.configclient; 18 | 19 | import jakarta.annotation.PostConstruct; 20 | import org.apache.curator.framework.CuratorFramework; 21 | 22 | import org.springframework.beans.factory.annotation.Autowired; 23 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 24 | import org.springframework.boot.context.properties.EnableConfigurationProperties; 25 | import org.springframework.cloud.config.server.config.ConfigServerProperties; 26 | import org.springframework.cloud.zookeeper.discovery.ZookeeperDiscoveryProperties; 27 | import org.springframework.context.annotation.Configuration; 28 | import org.springframework.util.StringUtils; 29 | 30 | /** 31 | * Extra configuration for config server if it happens to be registered with Zookeeper. 32 | * 33 | * @author Dave Syer 34 | */ 35 | @Configuration(proxyBeanMethods = false) 36 | @EnableConfigurationProperties 37 | @ConditionalOnClass({ ZookeeperDiscoveryProperties.class, CuratorFramework.class, 38 | ConfigServerProperties.class }) 39 | public class ZookeeperConfigServerAutoConfiguration { 40 | 41 | @Autowired(required = false) 42 | private ZookeeperDiscoveryProperties properties; 43 | 44 | @Autowired(required = false) 45 | private ConfigServerProperties server; 46 | 47 | @PostConstruct 48 | public void init() { 49 | if (this.properties == null || this.server == null) { 50 | return; 51 | } 52 | String prefix = this.server.getPrefix(); 53 | if (StringUtils.hasText(prefix)) { 54 | this.properties.getMetadata().put("configPath", prefix); 55 | } 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/discovery/configclient/ZookeeperDiscoveryClientConfigServiceBootstrapConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.discovery.configclient; 18 | 19 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 20 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 21 | import org.springframework.boot.context.properties.EnableConfigurationProperties; 22 | import org.springframework.cloud.client.discovery.health.DiscoveryClientHealthIndicatorProperties; 23 | import org.springframework.cloud.commons.util.InetUtils; 24 | import org.springframework.cloud.config.client.ConfigServicePropertySourceLocator; 25 | import org.springframework.cloud.zookeeper.ZookeeperAutoConfiguration; 26 | import org.springframework.cloud.zookeeper.discovery.ZookeeperDiscoveryAutoConfiguration; 27 | import org.springframework.cloud.zookeeper.discovery.ZookeeperDiscoveryClientConfiguration; 28 | import org.springframework.cloud.zookeeper.discovery.ZookeeperDiscoveryProperties; 29 | import org.springframework.cloud.zookeeper.support.CuratorServiceDiscoveryAutoConfiguration; 30 | import org.springframework.context.annotation.Bean; 31 | import org.springframework.context.annotation.Configuration; 32 | import org.springframework.context.annotation.Import; 33 | import org.springframework.core.annotation.Order; 34 | 35 | /** 36 | * Helper for config client that wants to lookup the config server via discovery. 37 | * 38 | * @author Spencer Gibb 39 | * @author Tim Ysewyn 40 | */ 41 | @ConditionalOnClass(ConfigServicePropertySourceLocator.class) 42 | @ConditionalOnProperty(value = "spring.cloud.config.discovery.enabled", matchIfMissing = false) 43 | @Configuration(proxyBeanMethods = false) 44 | @Import({ ZookeeperAutoConfiguration.class, ZookeeperDiscoveryClientConfiguration.class, 45 | CuratorServiceDiscoveryAutoConfiguration.class, 46 | ZookeeperDiscoveryAutoConfiguration.class }) 47 | @EnableConfigurationProperties({DiscoveryClientHealthIndicatorProperties.class}) 48 | @Order(0) 49 | public class ZookeeperDiscoveryClientConfigServiceBootstrapConfiguration { 50 | 51 | @Bean 52 | public ZookeeperDiscoveryProperties zookeeperDiscoveryProperties( 53 | InetUtils inetUtils) { 54 | ZookeeperDiscoveryProperties properties = new ZookeeperDiscoveryProperties( 55 | inetUtils); 56 | // for bootstrap, registration is not needed, just discovery client 57 | properties.setRegister(false); 58 | return properties; 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/discovery/dependency/ConditionalOnDependenciesNotPassed.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.discovery.dependency; 18 | 19 | import java.lang.annotation.ElementType; 20 | import java.lang.annotation.Retention; 21 | import java.lang.annotation.RetentionPolicy; 22 | import java.lang.annotation.Target; 23 | 24 | import org.springframework.context.annotation.Conditional; 25 | 26 | /** 27 | * Annotation to turn off a feature if Zookeeper dependencies have NOT been passed. 28 | * 29 | * @author Marcin Grzejszczak 30 | * @since 1.0.0 31 | */ 32 | @Target({ ElementType.TYPE, ElementType.METHOD }) 33 | @Retention(RetentionPolicy.RUNTIME) 34 | @Conditional(DependenciesNotPassedCondition.class) 35 | public @interface ConditionalOnDependenciesNotPassed { 36 | 37 | } 38 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/discovery/dependency/ConditionalOnDependenciesPassed.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.discovery.dependency; 18 | 19 | import java.lang.annotation.ElementType; 20 | import java.lang.annotation.Retention; 21 | import java.lang.annotation.RetentionPolicy; 22 | import java.lang.annotation.Target; 23 | 24 | import org.springframework.context.annotation.Conditional; 25 | 26 | /** 27 | * Annotation to turn on a feature if Zookeeper dependencies have been passed. Also checks 28 | * if switch for zookeeper dependencies is turned on. 29 | * 30 | * @author Marcin Grzejszczak 31 | * @since 1.0.0 32 | */ 33 | @Target({ ElementType.TYPE, ElementType.METHOD }) 34 | @Retention(RetentionPolicy.RUNTIME) 35 | @Conditional(DependenciesPassedCondition.class) 36 | public @interface ConditionalOnDependenciesPassed { 37 | 38 | } 39 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/discovery/dependency/DependenciesNotPassedCondition.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.discovery.dependency; 18 | 19 | import org.springframework.boot.autoconfigure.condition.ConditionOutcome; 20 | import org.springframework.context.annotation.ConditionContext; 21 | import org.springframework.core.type.AnnotatedTypeMetadata; 22 | 23 | /** 24 | * Inverse of the {@link ConditionalOnDependenciesPassed} condition. 25 | * 26 | * @author Marcin Grzejszczak 27 | * @since 1.0.0 28 | */ 29 | public class DependenciesNotPassedCondition extends DependenciesPassedCondition { 30 | 31 | @Override 32 | public ConditionOutcome getMatchOutcome(ConditionContext context, 33 | AnnotatedTypeMetadata metadata) { 34 | ConditionOutcome propertiesSet = super.getMatchOutcome(context, metadata); 35 | return ConditionOutcome.inverse(propertiesSet); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/discovery/dependency/DependenciesPassedCondition.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.discovery.dependency; 18 | 19 | import java.util.Collections; 20 | import java.util.Map; 21 | 22 | import org.springframework.boot.autoconfigure.condition.ConditionOutcome; 23 | import org.springframework.boot.autoconfigure.condition.SpringBootCondition; 24 | import org.springframework.boot.context.properties.bind.Bindable; 25 | import org.springframework.boot.context.properties.bind.Binder; 26 | import org.springframework.context.annotation.ConditionContext; 27 | import org.springframework.core.type.AnnotatedTypeMetadata; 28 | 29 | /** 30 | * Condition that verifies if the Dependencies have been passed in an appropriate place in 31 | * the application properties. 32 | * 33 | * @author Marcin Grzejszczak 34 | * @since 1.0.0 35 | */ 36 | public class DependenciesPassedCondition extends SpringBootCondition { 37 | 38 | private static final Bindable> STRING_STRING_MAP = Bindable 39 | .mapOf(String.class, String.class); 40 | 41 | private static final String ZOOKEEPER_DEPENDENCIES_PROP = "spring.cloud.zookeeper.dependencies"; 42 | 43 | @Override 44 | public ConditionOutcome getMatchOutcome(ConditionContext context, 45 | AnnotatedTypeMetadata metadata) { 46 | Map subProperties = Binder.get(context.getEnvironment()) 47 | .bind(ZOOKEEPER_DEPENDENCIES_PROP, STRING_STRING_MAP) 48 | .orElseGet(Collections::emptyMap); 49 | if (!subProperties.isEmpty()) { 50 | return ConditionOutcome.match("Dependencies are defined in configuration"); 51 | } 52 | Boolean dependenciesEnabled = context.getEnvironment().getProperty( 53 | "spring.cloud.zookeeper.dependency.enabled", Boolean.class, false); 54 | if (dependenciesEnabled) { 55 | return ConditionOutcome.match( 56 | "Dependencies are not defined in configuration, but switch is turned on"); 57 | } 58 | return ConditionOutcome 59 | .noMatch("No dependencies have been passed for the service"); 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/discovery/dependency/DependencyEnvironmentPostProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.discovery.dependency; 18 | 19 | import java.util.Collections; 20 | 21 | import org.springframework.boot.SpringApplication; 22 | import org.springframework.boot.env.EnvironmentPostProcessor; 23 | import org.springframework.cloud.bootstrap.BootstrapConfigFileApplicationListener; 24 | import org.springframework.core.Ordered; 25 | import org.springframework.core.env.ConfigurableEnvironment; 26 | import org.springframework.core.env.MapPropertySource; 27 | import org.springframework.util.StringUtils; 28 | 29 | /** 30 | * EnvironmentPostProcessor that sets spring.application.name. Specifically, if 31 | * spring.application.name doesn't contain a / and spring.cloud.zookeeper.prefix has text, 32 | * it sets spring.application.name to 33 | * /${spring.cloud.zookeeper.prefix}/${spring.application.name} 34 | * 35 | * @author Spencer Gibb 36 | * @since 1.0.0 37 | */ 38 | public class DependencyEnvironmentPostProcessor 39 | implements EnvironmentPostProcessor, Ordered { 40 | 41 | // after ConfigFileEnvironmentPostProcessorr 42 | private int order = BootstrapConfigFileApplicationListener.DEFAULT_ORDER + 1; 43 | 44 | @Override 45 | public int getOrder() { 46 | return this.order; 47 | } 48 | 49 | @Override 50 | public void postProcessEnvironment(ConfigurableEnvironment environment, 51 | SpringApplication application) { 52 | String appName = environment.getProperty("spring.application.name"); 53 | if (StringUtils.hasText(appName) && !appName.contains("/")) { 54 | String prefix = environment.getProperty("spring.cloud.zookeeper.prefix"); 55 | if (StringUtils.hasText(prefix)) { 56 | StringBuilder prefixedName = new StringBuilder(); 57 | if (!prefix.startsWith("/")) { 58 | prefixedName.append("/"); 59 | } 60 | prefixedName.append(prefix); 61 | if (!prefix.endsWith("/")) { 62 | prefixedName.append("/"); 63 | } 64 | prefixedName.append(appName); 65 | MapPropertySource propertySource = new MapPropertySource( 66 | "zookeeperDependencyEnvironment", 67 | Collections.singletonMap("spring.application.name", 68 | (Object) prefixedName.toString())); 69 | environment.getPropertySources().addFirst(propertySource); 70 | } 71 | } 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/discovery/dependency/ZookeeperDependenciesAutoConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.discovery.dependency; 18 | 19 | import org.springframework.boot.autoconfigure.AutoConfigureAfter; 20 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 21 | import org.springframework.boot.context.properties.EnableConfigurationProperties; 22 | import org.springframework.cloud.zookeeper.ConditionalOnZookeeperEnabled; 23 | import org.springframework.cloud.zookeeper.ZookeeperAutoConfiguration; 24 | import org.springframework.context.annotation.Bean; 25 | import org.springframework.context.annotation.Configuration; 26 | 27 | /** 28 | * Provides AutoConfiguration for Zookeeper dependency set up in properties. 29 | * 30 | * @author Marcin Grzejszczak 31 | * @author Olga Maciaszek-Sharma 32 | * @since 1.0.0 33 | */ 34 | @Configuration(proxyBeanMethods = false) 35 | @EnableConfigurationProperties 36 | @ConditionalOnZookeeperEnabled 37 | @AutoConfigureAfter(ZookeeperAutoConfiguration.class) 38 | public class ZookeeperDependenciesAutoConfiguration { 39 | 40 | @Bean 41 | @ConditionalOnMissingBean 42 | public ZookeeperDependencies zookeeperDependencies() { 43 | return new ZookeeperDependencies(); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/discovery/watcher/DependencyRegistrationHookProvider.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.discovery.watcher; 18 | 19 | import java.io.IOException; 20 | 21 | /** 22 | * Implementations of this interface are required to register dependency registration 23 | * hooks on startup and their cleaning upon application context shutdown. 24 | * 25 | * @author Marcin Grzejszczak 26 | * @since 1.0.0 27 | */ 28 | public interface DependencyRegistrationHookProvider { 29 | 30 | /** 31 | * Register hooks upon dependencies registration. 32 | * @throws Exception if registration fails. 33 | */ 34 | void registerDependencyRegistrationHooks() throws Exception; 35 | 36 | /** 37 | * Unregister hooks upon dependencies registration. 38 | * @throws IOException if clearing fails. 39 | */ 40 | void clearDependencyRegistrationHooks() throws IOException; 41 | 42 | } 43 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/discovery/watcher/DependencyState.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.discovery.watcher; 18 | 19 | /** 20 | * Represents a dependency's Zookeeper connection state. 21 | * 22 | * @author Marcin Grzejszczak 23 | * @since 1.0.0 24 | */ 25 | public enum DependencyState { 26 | 27 | /** 28 | * valid states. 29 | */ 30 | CONNECTED, DISCONNECTED 31 | 32 | } 33 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/discovery/watcher/DependencyStateChangeListenerRegistry.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.discovery.watcher; 18 | 19 | import java.util.List; 20 | 21 | import org.apache.commons.logging.Log; 22 | import org.apache.commons.logging.LogFactory; 23 | import org.apache.curator.framework.CuratorFramework; 24 | import org.apache.curator.framework.state.ConnectionState; 25 | import org.apache.curator.x.discovery.ServiceCache; 26 | import org.apache.curator.x.discovery.details.ServiceCacheListener; 27 | 28 | /** 29 | * Informs all the DependencyWatcherListeners that a dependency's state has changed. 30 | * 31 | * @author Marcin Grzejszczak 32 | * @author Tomasz Nurkiewicz, 4financeIT 33 | * @since 1.0.0 34 | */ 35 | public class DependencyStateChangeListenerRegistry implements ServiceCacheListener { 36 | 37 | private static final Log log = LogFactory 38 | .getLog(DependencyStateChangeListenerRegistry.class); 39 | 40 | private final List listeners; 41 | 42 | private final String dependencyName; 43 | 44 | private final ServiceCache serviceCache; 45 | 46 | public DependencyStateChangeListenerRegistry( 47 | List listeners, String dependencyName, 48 | ServiceCache serviceCache) { 49 | this.listeners = listeners; 50 | this.dependencyName = dependencyName; 51 | this.serviceCache = serviceCache; 52 | } 53 | 54 | @Override 55 | public void cacheChanged() { 56 | DependencyState state = this.serviceCache.getInstances().isEmpty() 57 | ? DependencyState.DISCONNECTED : DependencyState.CONNECTED; 58 | logCurrentState(state); 59 | informListeners(state); 60 | } 61 | 62 | private void logCurrentState(DependencyState dependencyState) { 63 | log.info("Service cache state change for '" + this.dependencyName 64 | + "' instances, current service state: " + dependencyState); 65 | } 66 | 67 | private void informListeners(DependencyState state) { 68 | for (DependencyWatcherListener listener : this.listeners) { 69 | listener.stateChanged(this.dependencyName, state); 70 | } 71 | } 72 | 73 | @Override 74 | public void stateChanged(CuratorFramework client, ConnectionState newState) { 75 | // TODO do something or ignore for what is worth 76 | } 77 | 78 | } 79 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/discovery/watcher/DependencyWatcherAutoConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.discovery.watcher; 18 | 19 | import java.util.ArrayList; 20 | import java.util.List; 21 | 22 | import org.apache.curator.x.discovery.ServiceDiscovery; 23 | 24 | import org.springframework.beans.factory.annotation.Autowired; 25 | import org.springframework.boot.autoconfigure.AutoConfigureAfter; 26 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 27 | import org.springframework.boot.context.properties.EnableConfigurationProperties; 28 | import org.springframework.cloud.zookeeper.ConditionalOnZookeeperEnabled; 29 | import org.springframework.cloud.zookeeper.discovery.ZookeeperInstance; 30 | import org.springframework.cloud.zookeeper.discovery.dependency.ConditionalOnDependenciesPassed; 31 | import org.springframework.cloud.zookeeper.discovery.dependency.ZookeeperDependencies; 32 | import org.springframework.cloud.zookeeper.discovery.dependency.ZookeeperDependenciesAutoConfiguration; 33 | import org.springframework.cloud.zookeeper.discovery.watcher.presence.DefaultDependencyPresenceOnStartupVerifier; 34 | import org.springframework.cloud.zookeeper.discovery.watcher.presence.DependencyPresenceOnStartupVerifier; 35 | import org.springframework.context.annotation.Bean; 36 | import org.springframework.context.annotation.Configuration; 37 | 38 | /** 39 | * Provides hooks for observing dependency lifecycle in Zookeeper. Needs custom 40 | * dependencies to be set in order to work. 41 | * 42 | * @author Marcin Grzejszczak 43 | * @since 1.0.0 44 | * @see ZookeeperDependencies 45 | */ 46 | @Configuration(proxyBeanMethods = false) 47 | @EnableConfigurationProperties 48 | @ConditionalOnZookeeperEnabled 49 | @ConditionalOnDependenciesPassed 50 | @AutoConfigureAfter(ZookeeperDependenciesAutoConfiguration.class) 51 | public class DependencyWatcherAutoConfiguration { 52 | 53 | @Autowired(required = false) 54 | private List dependencyWatcherListeners = new ArrayList<>(); 55 | 56 | @Bean 57 | @ConditionalOnMissingBean 58 | public DependencyPresenceOnStartupVerifier dependencyPresenceOnStartupVerifier() { 59 | return new DefaultDependencyPresenceOnStartupVerifier(); 60 | } 61 | 62 | @Bean(destroyMethod = "clearDependencyRegistrationHooks") 63 | @ConditionalOnMissingBean 64 | public DependencyRegistrationHookProvider dependencyWatcher( 65 | ServiceDiscovery serviceDiscovery, 66 | DependencyPresenceOnStartupVerifier dependencyPresenceOnStartupVerifier, 67 | ZookeeperDependencies zookeeperDependencies) { 68 | return new DefaultDependencyWatcher(serviceDiscovery, 69 | dependencyPresenceOnStartupVerifier, this.dependencyWatcherListeners, 70 | zookeeperDependencies); 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/discovery/watcher/DependencyWatcherListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.discovery.watcher; 18 | 19 | /** 20 | * Performs logic upon change of state of a dependency {@link DependencyState} in the 21 | * service discovery system. 22 | * 23 | * @author Marcin Grzejszczak 24 | * @since 1.0.0 25 | * @see org.springframework.cloud.zookeeper.discovery.dependency.ZookeeperDependencies 26 | */ 27 | public interface DependencyWatcherListener { 28 | 29 | /** 30 | * Method executed upon state change of a dependency. 31 | * @param dependencyName - alias from microservice configuration 32 | * @param newState - new state of the dependency 33 | */ 34 | void stateChanged(String dependencyName, DependencyState newState); 35 | 36 | } 37 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/discovery/watcher/presence/DefaultDependencyPresenceOnStartupVerifier.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.discovery.watcher.presence; 18 | 19 | /** 20 | * By default passes logging dependency checker in order not to shutdown the application 21 | * if dependency is missing. 22 | * 23 | * @author Marcin Grzejszczak 24 | * @see LogMissingDependencyChecker 25 | * @version 1.0.0 26 | */ 27 | public class DefaultDependencyPresenceOnStartupVerifier 28 | extends DependencyPresenceOnStartupVerifier { 29 | 30 | public DefaultDependencyPresenceOnStartupVerifier() { 31 | super(new LogMissingDependencyChecker()); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/discovery/watcher/presence/DependencyPresenceOnStartupVerifier.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.discovery.watcher.presence; 18 | 19 | import org.apache.curator.x.discovery.ServiceCache; 20 | 21 | /** 22 | * Verifier that checks for presence of mandatory dependencies and delegates to an 23 | * optional presence checker verification of presence of optional dependencies. 24 | * 25 | * The default implementation of required dependencies will result in shutting down of the 26 | * application if the dependency is missing. 27 | * 28 | * @author Marcin Grzejszczak 29 | * @author Tomasz Szymanski, 4financeIT 30 | * @see FailOnMissingDependencyChecker 31 | * @version 1.0.0 32 | */ 33 | public abstract class DependencyPresenceOnStartupVerifier { 34 | 35 | private static final PresenceChecker MANDATORY_DEPENDENCY_CHECKER = new FailOnMissingDependencyChecker(); 36 | 37 | private final PresenceChecker optionalDependencyChecker; 38 | 39 | public DependencyPresenceOnStartupVerifier( 40 | PresenceChecker optionalDependencyChecker) { 41 | this.optionalDependencyChecker = optionalDependencyChecker; 42 | } 43 | 44 | @SuppressWarnings("unchecked") 45 | public void verifyDependencyPresence(String dependencyName, 46 | @SuppressWarnings("rawtypes") ServiceCache serviceCache, boolean required) { 47 | if (required) { 48 | MANDATORY_DEPENDENCY_CHECKER.checkPresence(dependencyName, 49 | serviceCache.getInstances()); 50 | } 51 | else { 52 | this.optionalDependencyChecker.checkPresence(dependencyName, 53 | serviceCache.getInstances()); 54 | } 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/discovery/watcher/presence/FailOnMissingDependencyChecker.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.discovery.watcher.presence; 18 | 19 | import java.util.List; 20 | 21 | import org.apache.curator.x.discovery.ServiceInstance; 22 | 23 | /** 24 | * Will result in throwing an exception if there are no running instances of the 25 | * dependency. 26 | * 27 | * @author Marcin Grzejszczak 28 | * @author Adam Chudzik, 4financeIT 29 | * @since 1.0.0 30 | */ 31 | public class FailOnMissingDependencyChecker implements PresenceChecker { 32 | 33 | @Override 34 | public void checkPresence(String dependencyName, 35 | List> serviceInstances) { 36 | if (serviceInstances.isEmpty()) { 37 | throw new NoInstancesRunningException(dependencyName); 38 | } 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/discovery/watcher/presence/LogMissingDependencyChecker.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.discovery.watcher.presence; 18 | 19 | import java.util.List; 20 | 21 | import org.apache.commons.logging.Log; 22 | import org.apache.commons.logging.LogFactory; 23 | import org.apache.curator.x.discovery.ServiceInstance; 24 | 25 | /** 26 | * Will log the missing microservice dependency. 27 | * 28 | * @author Marcin Grzejszczak 29 | * @author Tomasz Dziurko, 4financeIT 30 | * @since 1.0.0 31 | */ 32 | public class LogMissingDependencyChecker implements PresenceChecker { 33 | 34 | private static final Log log = LogFactory.getLog(LogMissingDependencyChecker.class); 35 | 36 | @Override 37 | public void checkPresence(String dependencyName, 38 | List> serviceInstances) { 39 | if (serviceInstances.isEmpty()) { 40 | log.warn("Microservice dependency with name [" + dependencyName 41 | + "] is missing."); 42 | } 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/discovery/watcher/presence/NoInstancesRunningException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.discovery.watcher.presence; 18 | 19 | /** 20 | * @author Marcin Grzejszczak 21 | * @since 1.0.0 22 | */ 23 | public class NoInstancesRunningException extends RuntimeException { 24 | 25 | public NoInstancesRunningException(String dependencyName) { 26 | super("Required microservice dependency with name [" + dependencyName 27 | + "] is missing"); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/discovery/watcher/presence/PresenceChecker.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.discovery.watcher.presence; 18 | 19 | import java.util.List; 20 | 21 | import org.apache.curator.x.discovery.ServiceInstance; 22 | 23 | /** 24 | * The implementation of this interface will be called upon checking if a dependency with 25 | * a given name is present upon startup within the provided service instances. 26 | * 27 | * @author Marcin Grzejszczak 28 | * @since 1.0.0 29 | */ 30 | public interface PresenceChecker { 31 | 32 | /** 33 | * Checks if a given dependency is present. 34 | * @param dependencyName Name of the dependency. 35 | * @param serviceInstances - instances to check the dependency for. 36 | */ 37 | void checkPresence(String dependencyName, List> serviceInstances); 38 | 39 | } 40 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/serviceregistry/ZookeeperAutoServiceRegistration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.serviceregistry; 18 | 19 | import org.apache.commons.logging.Log; 20 | import org.apache.commons.logging.LogFactory; 21 | 22 | import org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration; 23 | import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties; 24 | import org.springframework.cloud.zookeeper.discovery.ZookeeperDiscoveryProperties; 25 | 26 | /** 27 | * Zookeeper {@link AbstractAutoServiceRegistration} that uses 28 | * {@link ZookeeperServiceRegistry} to register and de-register instances. 29 | * 30 | * @author Spencer Gibb 31 | * @since 1.0.0 32 | */ 33 | public class ZookeeperAutoServiceRegistration 34 | extends AbstractAutoServiceRegistration { 35 | 36 | private static final Log log = LogFactory 37 | .getLog(ZookeeperAutoServiceRegistration.class); 38 | 39 | private ZookeeperRegistration registration; 40 | 41 | private ZookeeperDiscoveryProperties properties; 42 | 43 | public ZookeeperAutoServiceRegistration(ZookeeperServiceRegistry registry, 44 | ZookeeperRegistration registration, ZookeeperDiscoveryProperties properties) { 45 | this(registry, registration, properties, null); 46 | } 47 | 48 | public ZookeeperAutoServiceRegistration(ZookeeperServiceRegistry registry, 49 | ZookeeperRegistration registration, ZookeeperDiscoveryProperties properties, 50 | AutoServiceRegistrationProperties arProperties) { 51 | super(registry, arProperties); 52 | this.registration = registration; 53 | this.properties = properties; 54 | if (this.properties.getInstancePort() != null) { 55 | this.registration.setPort(this.properties.getInstancePort()); 56 | } 57 | } 58 | 59 | @Override 60 | protected ZookeeperRegistration getRegistration() { 61 | return this.registration; 62 | } 63 | 64 | @Override 65 | protected ZookeeperRegistration getManagementRegistration() { 66 | return null; 67 | } 68 | 69 | @Override 70 | protected void register() { 71 | if (!this.properties.isRegister()) { 72 | log.debug("Registration disabled."); 73 | return; 74 | } 75 | if (this.registration.getPort() == 0) { 76 | this.registration.setPort(getPort().get()); 77 | } 78 | super.register(); 79 | } 80 | 81 | @Override 82 | protected void deregister() { 83 | if (!this.properties.isRegister()) { 84 | return; 85 | } 86 | super.deregister(); 87 | } 88 | 89 | @Override 90 | protected boolean isEnabled() { 91 | return this.properties.isEnabled(); 92 | } 93 | 94 | @Override 95 | protected Object getConfiguration() { 96 | return this.properties; 97 | } 98 | 99 | } 100 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/serviceregistry/ZookeeperRegistration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.serviceregistry; 18 | 19 | import org.apache.curator.x.discovery.ServiceInstance; 20 | 21 | import org.springframework.cloud.client.serviceregistry.Registration; 22 | import org.springframework.cloud.zookeeper.discovery.ZookeeperInstance; 23 | 24 | /** 25 | * @author Spencer Gibb 26 | */ 27 | public interface ZookeeperRegistration extends Registration { 28 | 29 | ServiceInstance getServiceInstance(); 30 | 31 | void setPort(int port); 32 | 33 | } 34 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/serviceregistry/ZookeeperServiceRegistryAutoConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.serviceregistry; 18 | 19 | import org.apache.curator.x.discovery.ServiceDiscovery; 20 | import org.apache.curator.x.discovery.details.InstanceSerializer; 21 | import org.apache.curator.x.discovery.details.JsonInstanceSerializer; 22 | 23 | import org.springframework.beans.BeansException; 24 | import org.springframework.boot.autoconfigure.AutoConfigureBefore; 25 | import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; 26 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 27 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 28 | import org.springframework.cloud.client.serviceregistry.ServiceRegistryAutoConfiguration; 29 | import org.springframework.cloud.commons.util.InetUtils; 30 | import org.springframework.cloud.zookeeper.discovery.ConditionalOnZookeeperDiscoveryEnabled; 31 | import org.springframework.cloud.zookeeper.discovery.ZookeeperDiscoveryProperties; 32 | import org.springframework.cloud.zookeeper.discovery.ZookeeperInstance; 33 | import org.springframework.context.ApplicationContext; 34 | import org.springframework.context.ApplicationContextAware; 35 | import org.springframework.context.annotation.Bean; 36 | import org.springframework.context.annotation.Configuration; 37 | 38 | /** 39 | * @author Spencer Gibb 40 | */ 41 | @Configuration(proxyBeanMethods = false) 42 | @ConditionalOnZookeeperDiscoveryEnabled 43 | @ConditionalOnProperty(value = "spring.cloud.service-registry.enabled", matchIfMissing = true) 44 | @AutoConfigureBefore(ServiceRegistryAutoConfiguration.class) 45 | public class ZookeeperServiceRegistryAutoConfiguration 46 | implements ApplicationContextAware { 47 | 48 | private ApplicationContext context; 49 | 50 | @Override 51 | public void setApplicationContext(ApplicationContext context) throws BeansException { 52 | this.context = context; 53 | } 54 | 55 | @Bean 56 | @ConditionalOnBean(ServiceDiscovery.class) 57 | @SuppressWarnings("unchecked") 58 | public ZookeeperServiceRegistry zookeeperServiceRegistry() { 59 | return new ZookeeperServiceRegistry(this.context.getBean(ServiceDiscovery.class)); 60 | } 61 | 62 | @Bean 63 | @ConditionalOnMissingBean 64 | public InstanceSerializer instanceSerializer() { 65 | return new JsonInstanceSerializer<>(ZookeeperInstance.class); 66 | } 67 | 68 | @Bean 69 | @ConditionalOnMissingBean 70 | public ZookeeperDiscoveryProperties zookeeperDiscoveryProperties( 71 | InetUtils inetUtils) { 72 | return new ZookeeperDiscoveryProperties(inetUtils); 73 | } 74 | 75 | } 76 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/support/CuratorServiceDiscoveryAutoConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.support; 18 | 19 | import org.apache.curator.framework.CuratorFramework; 20 | import org.apache.curator.x.discovery.ServiceDiscovery; 21 | import org.apache.curator.x.discovery.ServiceDiscoveryBuilder; 22 | import org.apache.curator.x.discovery.details.InstanceSerializer; 23 | import org.apache.curator.x.discovery.details.JsonInstanceSerializer; 24 | 25 | import org.springframework.boot.autoconfigure.AutoConfigureBefore; 26 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 27 | import org.springframework.cloud.client.ConditionalOnDiscoveryEnabled; 28 | import org.springframework.cloud.zookeeper.discovery.ConditionalOnZookeeperDiscoveryEnabled; 29 | import org.springframework.cloud.zookeeper.discovery.ZookeeperDiscoveryAutoConfiguration; 30 | import org.springframework.cloud.zookeeper.discovery.ZookeeperDiscoveryProperties; 31 | import org.springframework.cloud.zookeeper.discovery.ZookeeperInstance; 32 | import org.springframework.cloud.zookeeper.serviceregistry.ZookeeperServiceRegistryAutoConfiguration; 33 | import org.springframework.context.annotation.Bean; 34 | import org.springframework.context.annotation.Configuration; 35 | 36 | /** 37 | * @author Spencer Gibb 38 | */ 39 | @Configuration(proxyBeanMethods = false) 40 | @ConditionalOnDiscoveryEnabled 41 | @ConditionalOnZookeeperDiscoveryEnabled 42 | @AutoConfigureBefore({ ZookeeperDiscoveryAutoConfiguration.class, 43 | ZookeeperServiceRegistryAutoConfiguration.class }) 44 | public class CuratorServiceDiscoveryAutoConfiguration { 45 | 46 | @Bean 47 | @ConditionalOnMissingBean(ServiceDiscoveryCustomizer.class) 48 | public DefaultServiceDiscoveryCustomizer defaultServiceDiscoveryCustomizer( 49 | CuratorFramework curator, ZookeeperDiscoveryProperties properties, 50 | InstanceSerializer serializer) { 51 | return new DefaultServiceDiscoveryCustomizer(curator, properties, serializer); 52 | } 53 | 54 | @Bean 55 | @ConditionalOnMissingBean 56 | public InstanceSerializer deprecatedInstanceSerializer() { 57 | return new JsonInstanceSerializer<>(ZookeeperInstance.class); 58 | } 59 | 60 | @Bean 61 | @ConditionalOnMissingBean 62 | public ServiceDiscovery curatorServiceDiscovery( 63 | ServiceDiscoveryCustomizer customizer) { 64 | return customizer 65 | .customize(ServiceDiscoveryBuilder.builder(ZookeeperInstance.class)); 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/support/DefaultServiceDiscoveryCustomizer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.support; 18 | 19 | import org.apache.curator.framework.CuratorFramework; 20 | import org.apache.curator.x.discovery.ServiceDiscovery; 21 | import org.apache.curator.x.discovery.ServiceDiscoveryBuilder; 22 | import org.apache.curator.x.discovery.details.InstanceSerializer; 23 | 24 | import org.springframework.cloud.zookeeper.discovery.ZookeeperDiscoveryProperties; 25 | import org.springframework.cloud.zookeeper.discovery.ZookeeperInstance; 26 | 27 | /** 28 | * @author Spencer Gibb 29 | */ 30 | public class DefaultServiceDiscoveryCustomizer implements ServiceDiscoveryCustomizer { 31 | 32 | protected CuratorFramework curator; 33 | 34 | protected ZookeeperDiscoveryProperties properties; 35 | 36 | protected InstanceSerializer instanceSerializer; 37 | 38 | public DefaultServiceDiscoveryCustomizer(CuratorFramework curator, 39 | ZookeeperDiscoveryProperties properties, 40 | InstanceSerializer instanceSerializer) { 41 | this.curator = curator; 42 | this.properties = properties; 43 | this.instanceSerializer = instanceSerializer; 44 | } 45 | 46 | @Override 47 | public ServiceDiscovery customize( 48 | ServiceDiscoveryBuilder builder) { 49 | // @formatter:off 50 | return builder 51 | .client(this.curator) 52 | .basePath(this.properties.getRoot()) 53 | .serializer(this.instanceSerializer) 54 | .build(); 55 | // @formatter:on 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/support/ServiceDiscoveryCustomizer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.support; 18 | 19 | import org.apache.curator.x.discovery.ServiceDiscovery; 20 | import org.apache.curator.x.discovery.ServiceDiscoveryBuilder; 21 | 22 | import org.springframework.cloud.zookeeper.discovery.ZookeeperInstance; 23 | 24 | /** 25 | * @author Spencer Gibb 26 | */ 27 | public interface ServiceDiscoveryCustomizer { 28 | 29 | ServiceDiscovery customize( 30 | ServiceDiscoveryBuilder builder); 31 | 32 | } 33 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/support/StatusConstants.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.support; 18 | 19 | import org.springframework.cloud.zookeeper.discovery.ZookeeperInstance; 20 | 21 | /** 22 | * @author Spencer Gibb 23 | */ 24 | public final class StatusConstants { 25 | 26 | private StatusConstants() { 27 | } 28 | 29 | /** 30 | * Key to the 31 | * {@link ZookeeperInstance#getMetadata()} 32 | * map. 33 | */ 34 | public static final String INSTANCE_STATUS_KEY = "instance_status"; 35 | 36 | /** 37 | * UP value for {@link StatusConstants#INSTANCE_STATUS_KEY} key. 38 | */ 39 | public static final String STATUS_UP = "UP"; 40 | 41 | /** 42 | * OUT_OF_SERVICE value for {@link StatusConstants#INSTANCE_STATUS_KEY} key. 43 | */ 44 | public static final String STATUS_OUT_OF_SERVICE = "OUT_OF_SERVICE"; 45 | 46 | } 47 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/main/resources/META-INF/spring.factories: -------------------------------------------------------------------------------- 1 | # Environment Post Processors 2 | org.springframework.boot.env.EnvironmentPostProcessor=\ 3 | org.springframework.cloud.zookeeper.discovery.dependency.DependencyEnvironmentPostProcessor 4 | 5 | org.springframework.cloud.bootstrap.BootstrapConfiguration=\ 6 | org.springframework.cloud.zookeeper.discovery.configclient.ZookeeperDiscoveryClientConfigServiceBootstrapConfiguration 7 | 8 | org.springframework.boot.BootstrapRegistryInitializer=\ 9 | org.springframework.cloud.zookeeper.discovery.configclient.ZookeeperConfigServerBootstrapper -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/main/resources/META-INF/spring/aot.factories: -------------------------------------------------------------------------------- 1 | org.springframework.aot.hint.RuntimeHintsRegistrar=\ 2 | org.springframework.cloud.zookeeper.discovery.ZookeeperDiscoveryHints -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports: -------------------------------------------------------------------------------- 1 | org.springframework.cloud.zookeeper.discovery.ZookeeperDiscoveryAutoConfiguration 2 | org.springframework.cloud.zookeeper.discovery.dependency.DependencyFeignClientAutoConfiguration 3 | org.springframework.cloud.zookeeper.discovery.dependency.DependencyRestTemplateAutoConfiguration 4 | org.springframework.cloud.zookeeper.discovery.dependency.ZookeeperDependenciesAutoConfiguration 5 | org.springframework.cloud.zookeeper.discovery.watcher.DependencyWatcherAutoConfiguration 6 | org.springframework.cloud.zookeeper.serviceregistry.ZookeeperAutoServiceRegistrationAutoConfiguration 7 | org.springframework.cloud.zookeeper.serviceregistry.ZookeeperServiceRegistryAutoConfiguration 8 | org.springframework.cloud.zookeeper.support.CuratorServiceDiscoveryAutoConfiguration 9 | org.springframework.cloud.zookeeper.discovery.ZookeeperDiscoveryClientConfiguration 10 | org.springframework.cloud.zookeeper.discovery.LoadBalancerZookeeperAutoConfiguration 11 | org.springframework.cloud.zookeeper.discovery.reactive.ZookeeperReactiveDiscoveryClientConfiguration -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/test/java/org/springframework/cloud/zookeeper/discovery/TestServiceRegistrar.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.discovery; 18 | 19 | import java.io.IOException; 20 | 21 | import org.apache.curator.framework.CuratorFramework; 22 | import org.apache.curator.x.discovery.ServiceDiscovery; 23 | import org.apache.curator.x.discovery.ServiceDiscoveryBuilder; 24 | import org.apache.curator.x.discovery.ServiceInstance; 25 | import org.apache.curator.x.discovery.UriSpec; 26 | 27 | public class TestServiceRegistrar { 28 | 29 | private final int serverPort; 30 | 31 | private final CuratorFramework curatorFramework; 32 | 33 | private final ServiceDiscovery serviceDiscovery; 34 | 35 | public TestServiceRegistrar(int serverPort, CuratorFramework curatorFramework) { 36 | this.serverPort = serverPort; 37 | this.curatorFramework = curatorFramework; 38 | this.serviceDiscovery = serviceDiscovery(); 39 | } 40 | 41 | public void start() { 42 | try { 43 | this.serviceDiscovery.start(); 44 | } 45 | catch (Exception e) { 46 | throw new RuntimeException(e); 47 | } 48 | } 49 | 50 | public ServiceInstance serviceInstance() { 51 | try { 52 | return ServiceInstance.builder() 53 | .uriSpec(new UriSpec("{scheme}://{address}:{port}/")) 54 | .address("localhost").port(this.serverPort).name("testInstance") 55 | .build(); 56 | } 57 | catch (Exception e) { 58 | throw new RuntimeException(e); 59 | } 60 | } 61 | 62 | public ServiceDiscovery serviceDiscovery() { 63 | return ServiceDiscoveryBuilder.builder(Void.class).basePath("/services") 64 | .client(this.curatorFramework).thisInstance(serviceInstance()).build(); 65 | } 66 | 67 | public void stop() { 68 | try { 69 | this.serviceDiscovery.close(); 70 | } 71 | catch (IOException e) { 72 | throw new RuntimeException(e); 73 | } 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/test/java/org/springframework/cloud/zookeeper/discovery/ZookeeperDiscoveryClientTests.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.discovery; 18 | 19 | import java.util.List; 20 | 21 | import org.apache.curator.x.discovery.ServiceDiscovery; 22 | import org.apache.zookeeper.KeeperException.NoNodeException; 23 | import org.junit.Test; 24 | 25 | import org.springframework.cloud.client.ServiceInstance; 26 | 27 | import static org.assertj.core.api.BDDAssertions.then; 28 | import static org.mockito.Mockito.mock; 29 | import static org.mockito.Mockito.when; 30 | 31 | /** 32 | * @author Marcin Grzejszczak 33 | */ 34 | public class ZookeeperDiscoveryClientTests { 35 | 36 | @Test 37 | public void should_return_an_empty_list_of_services_if_service_discovery_is_null() { 38 | // given: 39 | ServiceDiscovery serviceDiscovery = mock( 40 | ServiceDiscovery.class); 41 | ZookeeperDiscoveryClient zookeeperDiscoveryClient = new ZookeeperDiscoveryClient( 42 | serviceDiscovery, null, new ZookeeperDiscoveryProperties()); 43 | // when: 44 | List services = zookeeperDiscoveryClient.getServices(); 45 | // then: 46 | then(services).isEmpty(); 47 | } 48 | 49 | @Test 50 | public void getServicesShouldReturnEmptyWhenNoNodeException() throws Exception { 51 | // given: 52 | ServiceDiscovery serviceDiscovery = mock( 53 | ServiceDiscovery.class); 54 | when(serviceDiscovery.queryForNames()).thenThrow(new NoNodeException()); 55 | ZookeeperDiscoveryClient discoveryClient = new ZookeeperDiscoveryClient( 56 | serviceDiscovery, null, new ZookeeperDiscoveryProperties()); 57 | // when: 58 | List services = discoveryClient.getServices(); 59 | // then: 60 | then(services).isEmpty(); 61 | } 62 | 63 | @Test 64 | public void getInstancesshouldReturnEmptyWhenNoNodeException() throws Exception { 65 | // given: 66 | ServiceDiscovery serviceDiscovery = mock( 67 | ServiceDiscovery.class); 68 | when(serviceDiscovery.queryForInstances("myservice")) 69 | .thenThrow(new NoNodeException()); 70 | ZookeeperDiscoveryClient discoveryClient = new ZookeeperDiscoveryClient( 71 | serviceDiscovery, null, new ZookeeperDiscoveryProperties()); 72 | // when: 73 | List instances = discoveryClient.getInstances("myservice"); 74 | // then: 75 | then(instances).isEmpty(); 76 | } 77 | 78 | } 79 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/test/java/org/springframework/cloud/zookeeper/discovery/ZookeeperDiscoveryDisabledTests.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2024 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 | * https://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 | package org.springframework.cloud.zookeeper.discovery; 18 | 19 | import org.apache.curator.framework.CuratorFramework; 20 | import org.junit.Test; 21 | import org.junit.runner.RunWith; 22 | 23 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 24 | import org.springframework.boot.test.context.SpringBootTest; 25 | import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; 26 | import org.springframework.cloud.zookeeper.discovery.test.CommonTestConfig; 27 | import org.springframework.cloud.zookeeper.test.ZookeeperTestingServer; 28 | import org.springframework.context.annotation.Bean; 29 | import org.springframework.context.annotation.Configuration; 30 | import org.springframework.context.annotation.Import; 31 | import org.springframework.test.context.ContextConfiguration; 32 | import org.springframework.test.context.junit4.SpringRunner; 33 | 34 | import static org.mockito.Mockito.mock; 35 | 36 | /** 37 | * @author Marcin Grzejszczak 38 | */ 39 | @RunWith(SpringRunner.class) 40 | @SpringBootTest(classes = ZookeeperDiscoveryDisabledTests.SomeApp.class, 41 | webEnvironment = WebEnvironment.RANDOM_PORT, properties = { 42 | "spring.cloud.zookeeper.discovery.enabled=false", "debug=true" }) 43 | @ContextConfiguration(loader = ZookeeperTestingServer.Loader.class) 44 | public class ZookeeperDiscoveryDisabledTests { 45 | 46 | @Test 47 | public void should_start_the_context_with_discovery_disabled() { 48 | } 49 | 50 | @Configuration 51 | @EnableAutoConfiguration 52 | @Import(CommonTestConfig.class) 53 | static class SomeApp { 54 | 55 | @Bean 56 | CuratorFramework curator() { 57 | return mock(CuratorFramework.class); 58 | } 59 | 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/test/java/org/springframework/cloud/zookeeper/discovery/ZookeeperDiscoveryHealthIndicatorDisabledTests.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.discovery; 18 | 19 | import org.junit.Test; 20 | import org.junit.runner.RunWith; 21 | 22 | import org.springframework.beans.factory.annotation.Autowired; 23 | import org.springframework.boot.SpringBootConfiguration; 24 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 25 | import org.springframework.boot.test.context.SpringBootTest; 26 | import org.springframework.cloud.zookeeper.discovery.test.CommonTestConfig; 27 | import org.springframework.cloud.zookeeper.test.ZookeeperTestingServer; 28 | import org.springframework.context.annotation.Import; 29 | import org.springframework.test.context.ContextConfiguration; 30 | import org.springframework.test.context.junit4.SpringRunner; 31 | 32 | import static org.assertj.core.api.BDDAssertions.then; 33 | import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; 34 | 35 | /** 36 | * @author Spencer Gibb 37 | */ 38 | @RunWith(SpringRunner.class) 39 | @SpringBootTest(webEnvironment = RANDOM_PORT, properties = { "management.health.zookeeper.enabled=false", 40 | "spring.cloud.service-registry.auto-registration.enabled=false" }) 41 | @ContextConfiguration(loader = ZookeeperTestingServer.Loader.class) 42 | public class ZookeeperDiscoveryHealthIndicatorDisabledTests { 43 | 44 | @Autowired(required = false) 45 | private ZookeeperDiscoveryHealthIndicator healthIndicator; 46 | 47 | // Issue: #101 - ZookeeperDiscoveryHealthIndicator should be able to be disabled with 48 | // a property 49 | @Test 50 | public void healthIndicatorDisabled() { 51 | // when: 52 | // then: 53 | then(this.healthIndicator).isNull(); 54 | } 55 | 56 | @SpringBootConfiguration 57 | @EnableAutoConfiguration 58 | @Import(CommonTestConfig.class) 59 | static class Config { 60 | 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/test/java/org/springframework/cloud/zookeeper/discovery/ZookeeperDiscoveryPropertiesIntegrationTests.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.discovery; 18 | 19 | 20 | import org.junit.jupiter.api.Test; 21 | 22 | import org.springframework.beans.factory.annotation.Autowired; 23 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 24 | import org.springframework.boot.test.context.SpringBootTest; 25 | import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; 26 | import org.springframework.cloud.zookeeper.discovery.test.CommonTestConfig; 27 | import org.springframework.cloud.zookeeper.test.ZookeeperTestingServer; 28 | import org.springframework.context.annotation.Configuration; 29 | import org.springframework.context.annotation.Import; 30 | import org.springframework.test.context.ContextConfiguration; 31 | 32 | import static org.assertj.core.api.Assertions.assertThat; 33 | 34 | /** 35 | * @author wmz7year 36 | */ 37 | @SpringBootTest(properties = { "spring.application.name=testZookeeperDiscovery", 38 | "spring.cloud.service-registry.auto-registration.enabled=false", 39 | "spring.cloud.zookeeper.discovery.instance-id=zkpropstestid-123", 40 | "spring.cloud.zookeeper.discovery.preferIpAddress=true", 41 | "spring.cloud.zookeeper.discovery.instanceIpAddress=1.1.1.1" }, 42 | classes = ZookeeperDiscoveryPropertiesIntegrationTests.Config.class, 43 | webEnvironment = WebEnvironment.RANDOM_PORT) 44 | @ContextConfiguration(loader = ZookeeperTestingServer.Loader.class) 45 | public class ZookeeperDiscoveryPropertiesIntegrationTests { 46 | 47 | @Autowired 48 | private ZookeeperDiscoveryProperties discoveryProperties; 49 | 50 | @Test 51 | public void testPreferIpAddress() { 52 | assertThat(this.discoveryProperties.getInstanceId()) 53 | .isEqualTo("zkpropstestid-123"); 54 | assertThat(this.discoveryProperties.getInstanceHost()).isEqualTo("1.1.1.1"); 55 | } 56 | 57 | @Configuration 58 | @EnableAutoConfiguration 59 | @Import(CommonTestConfig.class) 60 | static class Config { 61 | 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/test/java/org/springframework/cloud/zookeeper/discovery/ZookeeperDiscoveryPropertiesTests.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.discovery; 18 | 19 | import java.util.Arrays; 20 | 21 | import org.junit.Test; 22 | import org.junit.runner.RunWith; 23 | import org.junit.runners.Parameterized; 24 | 25 | import org.springframework.cloud.commons.util.InetUtils; 26 | import org.springframework.cloud.commons.util.InetUtilsProperties; 27 | 28 | import static org.assertj.core.api.BDDAssertions.then; 29 | 30 | @RunWith(Parameterized.class) 31 | public class ZookeeperDiscoveryPropertiesTests { 32 | 33 | private String root; 34 | 35 | public ZookeeperDiscoveryPropertiesTests(String root) { 36 | this.root = root; 37 | } 38 | 39 | @Parameterized.Parameters(name = "With root {0}") 40 | public static Iterable rootVariations() { 41 | return Arrays.asList("es", "es/", "/es"); 42 | } 43 | 44 | @Test 45 | public void should_escape_root() { 46 | // given: 47 | ZookeeperDiscoveryProperties zookeeperDiscoveryProperties = new ZookeeperDiscoveryProperties( 48 | new InetUtils(new InetUtilsProperties())); 49 | // when: 50 | zookeeperDiscoveryProperties.setRoot(root); 51 | // then: 52 | then(zookeeperDiscoveryProperties.getRoot()).isEqualTo("/es"); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/test/java/org/springframework/cloud/zookeeper/discovery/ZookeeperDiscoverySecurePortTests.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.discovery; 18 | 19 | import org.junit.Test; 20 | import org.junit.runner.RunWith; 21 | 22 | import org.springframework.beans.factory.annotation.Autowired; 23 | import org.springframework.beans.factory.annotation.Value; 24 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 25 | import org.springframework.boot.test.context.SpringBootTest; 26 | import org.springframework.cloud.client.ServiceInstance; 27 | import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; 28 | import org.springframework.cloud.zookeeper.discovery.test.CommonTestConfig; 29 | import org.springframework.cloud.zookeeper.serviceregistry.ZookeeperRegistration; 30 | import org.springframework.cloud.zookeeper.test.ZookeeperTestingServer; 31 | import org.springframework.context.annotation.Configuration; 32 | import org.springframework.context.annotation.Import; 33 | import org.springframework.context.annotation.Profile; 34 | import org.springframework.test.annotation.DirtiesContext; 35 | import org.springframework.test.context.ActiveProfiles; 36 | import org.springframework.test.context.ContextConfiguration; 37 | import org.springframework.test.context.junit4.SpringRunner; 38 | 39 | import static org.assertj.core.api.BDDAssertions.then; 40 | import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; 41 | 42 | /** 43 | * @author Marcin Grzejszczak 44 | */ 45 | @RunWith(SpringRunner.class) 46 | @SpringBootTest(classes = ZookeeperDiscoverySecurePortTests.Config.class, properties = { 47 | "feign.hystrix.enabled=false", 48 | "spring.cloud.zookeeper.discovery.uriSpec={scheme}://{address}:{port}/contextPath", 49 | "spring.cloud.zookeeper.discovery.instance-ssl-port=8443" }, webEnvironment = RANDOM_PORT) 50 | @ActiveProfiles("loadbalancer") 51 | @DirtiesContext 52 | @ContextConfiguration(loader = ZookeeperTestingServer.Loader.class) 53 | public class ZookeeperDiscoverySecurePortTests { 54 | 55 | @Autowired 56 | private LoadBalancerClient loadBalancerClient; 57 | 58 | @Autowired 59 | private ZookeeperRegistration zookeeperRegistration; 60 | 61 | @Value("${spring.application.name}") 62 | private String springAppName; 63 | 64 | @Test 65 | public void isSecureIsTrue() { 66 | ServiceInstance instance = this.loadBalancerClient.choose(this.springAppName); 67 | then(instance.isSecure()).isTrue(); 68 | } 69 | 70 | @Test 71 | public void shouldSetServiceInstanceSslPort() { 72 | then(this.zookeeperRegistration.getServiceInstance().getSslPort()) 73 | .isEqualTo(8443); 74 | } 75 | 76 | @Configuration 77 | @EnableAutoConfiguration 78 | @Import(CommonTestConfig.class) 79 | @Profile("loadbalancer") 80 | static class Config { 81 | 82 | } 83 | 84 | } 85 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/test/java/org/springframework/cloud/zookeeper/discovery/ZookeeperLifecycleRegistrationDisabledTests.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.discovery; 18 | 19 | import java.util.List; 20 | 21 | import org.junit.Test; 22 | import org.junit.runner.RunWith; 23 | 24 | import org.springframework.beans.factory.annotation.Autowired; 25 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 26 | import org.springframework.boot.test.context.SpringBootTest; 27 | import org.springframework.cloud.client.ServiceInstance; 28 | import org.springframework.cloud.zookeeper.discovery.test.CommonTestConfig; 29 | import org.springframework.cloud.zookeeper.test.ZookeeperTestingServer; 30 | import org.springframework.context.annotation.Configuration; 31 | import org.springframework.context.annotation.Import; 32 | import org.springframework.test.context.ContextConfiguration; 33 | import org.springframework.test.context.junit4.SpringRunner; 34 | 35 | import static org.assertj.core.api.Assertions.assertThat; 36 | import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; 37 | 38 | /** 39 | * @author Spencer Gibb 40 | */ 41 | @RunWith(SpringRunner.class) 42 | @SpringBootTest(classes = ZookeeperLifecycleRegistrationDisabledTests.TestPropsConfig.class, properties = { 43 | "spring.application.name=myTestNotRegisteredService", 44 | "spring.cloud.zookeeper.discovery.register=false", 45 | "spring.cloud.zookeeper.dependency.enabled=false" }, webEnvironment = RANDOM_PORT) 46 | @ContextConfiguration(loader = ZookeeperTestingServer.Loader.class) 47 | public class ZookeeperLifecycleRegistrationDisabledTests { 48 | 49 | @Autowired 50 | private ZookeeperDiscoveryClient client; 51 | 52 | @Test 53 | public void contextLoads() { 54 | List instances = this.client 55 | .getInstances("myTestNotRegisteredService"); 56 | assertThat(instances.isEmpty()).as("service was registered").isTrue(); 57 | } 58 | 59 | @Configuration 60 | @EnableAutoConfiguration 61 | @Import({ CommonTestConfig.class }) 62 | static class TestPropsConfig { 63 | 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/test/java/org/springframework/cloud/zookeeper/discovery/configclient/ZookeeperConfigServerAutoConfigurationTests.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.discovery.configclient; 18 | 19 | import org.junit.After; 20 | import org.junit.Test; 21 | 22 | import org.springframework.boot.WebApplicationType; 23 | import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; 24 | import org.springframework.boot.builder.SpringApplicationBuilder; 25 | import org.springframework.cloud.config.server.config.ConfigServerProperties; 26 | import org.springframework.cloud.zookeeper.discovery.ZookeeperDiscoveryProperties; 27 | import org.springframework.cloud.zookeeper.test.ZookeeperTestingServer; 28 | import org.springframework.context.ConfigurableApplicationContext; 29 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; 30 | 31 | import static org.assertj.core.api.Assertions.assertThat; 32 | 33 | /** 34 | * @author Dave Syer 35 | */ 36 | public class ZookeeperConfigServerAutoConfigurationTests { 37 | 38 | private ConfigurableApplicationContext context; 39 | 40 | @After 41 | public void close() { 42 | if (this.context != null) { 43 | this.context.close(); 44 | } 45 | } 46 | 47 | @Test 48 | public void offByDefault() { 49 | this.context = new AnnotationConfigApplicationContext( 50 | ZookeeperConfigServerAutoConfiguration.class); 51 | assertThat(this.context 52 | .getBeanNamesForType(ZookeeperDiscoveryProperties.class).length) 53 | .isEqualTo(0); 54 | } 55 | 56 | @Test 57 | public void onWhenRequested() { 58 | setup("spring.cloud.config.server.prefix=/config"); 59 | assertThat(this.context 60 | .getBeanNamesForType(ZookeeperDiscoveryProperties.class).length) 61 | .isEqualTo(1); 62 | ZookeeperDiscoveryProperties properties = this.context 63 | .getBean(ZookeeperDiscoveryProperties.class); 64 | assertThat(properties.getMetadata()).containsEntry("configPath", "/config"); 65 | } 66 | 67 | private void setup(String... env) { 68 | this.context = new SpringApplicationBuilder( 69 | PropertyPlaceholderAutoConfiguration.class, 70 | ZookeeperConfigServerAutoConfiguration.class, 71 | ConfigServerProperties.class, ZookeeperDiscoveryProperties.class) 72 | .listeners(new ZookeeperTestingServer()) 73 | .web(WebApplicationType.NONE).properties(env).run(); 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/test/java/org/springframework/cloud/zookeeper/discovery/configclient/ZookeeperConfigServerBootstrapperNoConfigClientTests.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2020 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 | * https://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 | package org.springframework.cloud.zookeeper.discovery.configclient; 18 | 19 | import org.apache.catalina.webresources.TomcatURLStreamHandlerFactory; 20 | import org.apache.curator.test.TestingServer; 21 | import org.junit.Test; 22 | import org.junit.runner.RunWith; 23 | 24 | import org.springframework.boot.SpringBootConfiguration; 25 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 26 | import org.springframework.boot.builder.SpringApplicationBuilder; 27 | import org.springframework.cloud.test.ClassPathExclusions; 28 | import org.springframework.cloud.test.ModifiedClassPathRunner; 29 | import org.springframework.cloud.test.TestSocketUtils; 30 | import org.springframework.context.ConfigurableApplicationContext; 31 | 32 | @RunWith(ModifiedClassPathRunner.class) 33 | @ClassPathExclusions({ "spring-cloud-config-client-*.jar", "spring-cloud-config-server-*.jar" }) 34 | public class ZookeeperConfigServerBootstrapperNoConfigClientTests { 35 | 36 | @Test 37 | public void contextLoads() throws Exception { 38 | TestingServer testingServer = null; 39 | ConfigurableApplicationContext context = null; 40 | try { 41 | TomcatURLStreamHandlerFactory.disable(); 42 | int port = TestSocketUtils.findAvailableTcpPort(); 43 | testingServer = new TestingServer(port); 44 | context = new SpringApplicationBuilder(TestConfig.class).properties("--server.port=0", 45 | "spring.cloud.config.discovery.enabled=true", "spring.cloud.zookeeper.connect-string=localhost:" + port, 46 | "spring.cloud.service-registry.auto-registration.enabled=false") 47 | .run(); 48 | } 49 | finally { 50 | if (context != null) { 51 | context.close(); 52 | } 53 | if (testingServer != null) { 54 | testingServer.close(); 55 | } 56 | } 57 | } 58 | 59 | @SpringBootConfiguration 60 | @EnableAutoConfiguration 61 | static class TestConfig { 62 | 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/test/java/org/springframework/cloud/zookeeper/discovery/dependency/StubsConfigurationTests.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.discovery.dependency; 18 | 19 | import org.junit.Test; 20 | 21 | import static org.assertj.core.api.BDDAssertions.then; 22 | 23 | /** 24 | * @author Marcin Grzejszczak 25 | */ 26 | public class StubsConfigurationTests { 27 | 28 | @Test 29 | public void should_return_empty_colon_separated_dependency_notation_if_empty_path_has_been_provided() { 30 | // given: 31 | String path = ""; 32 | // when: 33 | StubsConfiguration stubsConfiguration = new StubsConfiguration(path); 34 | // then: 35 | then(stubsConfiguration.toColonSeparatedDependencyNotation()).isEqualTo(""); 36 | } 37 | 38 | @Test 39 | public void should_return_empty_colon_separated_dependency_notation_if_invalid_path_has_been_provided() { 40 | // given: 41 | String path = "pl/"; 42 | // when: 43 | StubsConfiguration stubsConfiguration = new StubsConfiguration(path); 44 | // then: 45 | then(stubsConfiguration.toColonSeparatedDependencyNotation()).isEqualTo(""); 46 | } 47 | 48 | @Test 49 | public void should_properly_parse_invalid_colon_separated_path_into_empty_notation() { 50 | // given: 51 | String path = "pl/a"; 52 | // when: 53 | StubsConfiguration stubsConfiguration = new StubsConfiguration(path); 54 | // then: 55 | then(stubsConfiguration.toColonSeparatedDependencyNotation()).isEqualTo(""); 56 | then(stubsConfiguration.getStubsGroupId()).isEqualTo(""); 57 | then(stubsConfiguration.getStubsArtifactId()).isEqualTo(""); 58 | then(stubsConfiguration.getStubsClassifier()).isEqualTo(""); 59 | } 60 | 61 | @Test 62 | public void should_parse_the_path_into_group_artifact_and_classifier() { 63 | // given: 64 | String path = "pl/a"; 65 | // when: 66 | StubsConfiguration stubsConfiguration = new StubsConfiguration( 67 | new StubsConfiguration.DependencyPath(path)); 68 | // then: 69 | then(stubsConfiguration.toColonSeparatedDependencyNotation()) 70 | .isEqualTo("pl:a:stubs"); 71 | then(stubsConfiguration.getStubsGroupId()).isEqualTo("pl"); 72 | then(stubsConfiguration.getStubsArtifactId()).isEqualTo("a"); 73 | then(stubsConfiguration.getStubsClassifier()).isEqualTo("stubs"); 74 | } 75 | 76 | @Test 77 | public void should_properly_set_group_artifact_and_classifier() { 78 | // when: 79 | StubsConfiguration stubsConfiguration = new StubsConfiguration("pl", "a", 80 | "superstubs"); 81 | // then: 82 | then(stubsConfiguration.toColonSeparatedDependencyNotation()) 83 | .isEqualTo("pl:a:superstubs"); 84 | then(stubsConfiguration.getStubsGroupId()).isEqualTo("pl"); 85 | then(stubsConfiguration.getStubsArtifactId()).isEqualTo("a"); 86 | then(stubsConfiguration.getStubsClassifier()).isEqualTo("superstubs"); 87 | } 88 | 89 | } 90 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/test/java/org/springframework/cloud/zookeeper/discovery/dependency/ZookeeperDependenciesTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.discovery.dependency; 18 | 19 | import java.util.LinkedHashMap; 20 | import java.util.Map; 21 | 22 | import org.junit.Test; 23 | 24 | import static org.assertj.core.api.BDDAssertions.then; 25 | 26 | public class ZookeeperDependenciesTest { 27 | 28 | @Test 29 | public void should_properly_sanitize_dependency_path() { 30 | // given: 31 | Map dependencies = new LinkedHashMap<>(); 32 | ZookeeperDependency cat = new ZookeeperDependency(); 33 | cat.setPath("/cats/cat"); 34 | dependencies.put("cat", cat); 35 | ZookeeperDependency dog = new ZookeeperDependency(); 36 | dog.setPath("dogs/dog"); 37 | dependencies.put("dog", dog); 38 | ZookeeperDependencies zookeeperDependencies = new ZookeeperDependencies(); 39 | zookeeperDependencies.setDependencies(dependencies); 40 | // when: 41 | zookeeperDependencies.init(); 42 | // then: 43 | then(zookeeperDependencies.getDependencies().get("cat").getPath()) 44 | .isEqualTo("/cats/cat"); 45 | then(zookeeperDependencies.getDependencies().get("dog").getPath()) 46 | .isEqualTo("/dogs/dog"); 47 | } 48 | 49 | @Test 50 | public void should_properly_sanitize_dependency_path_with_prefix() { 51 | // given: 52 | Map dependencies = new LinkedHashMap<>(); 53 | ZookeeperDependency cat = new ZookeeperDependency(); 54 | cat.setPath("/cats/cat"); 55 | dependencies.put("cat", cat); 56 | ZookeeperDependency dog = new ZookeeperDependency(); 57 | dog.setPath("dogs/dog"); 58 | dependencies.put("dog", dog); 59 | ZookeeperDependencies zookeeperDependencies = new ZookeeperDependencies(); 60 | zookeeperDependencies.setPrefix("animals/"); 61 | zookeeperDependencies.setDependencies(dependencies); 62 | // when: 63 | zookeeperDependencies.init(); 64 | // then: 65 | then(zookeeperDependencies.getDependencies().get("cat").getPath()) 66 | .isEqualTo("/animals/cats/cat"); 67 | then(zookeeperDependencies.getDependencies().get("dog").getPath()) 68 | .isEqualTo("/animals/dogs/dog"); 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/test/java/org/springframework/cloud/zookeeper/discovery/test/CommonTestConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.discovery.test; 18 | 19 | import org.apache.curator.test.TestingServer; 20 | 21 | import org.springframework.cloud.client.loadbalancer.LoadBalanced; 22 | import org.springframework.cloud.test.TestSocketUtils; 23 | import org.springframework.cloud.zookeeper.ZookeeperProperties; 24 | import org.springframework.context.annotation.Bean; 25 | import org.springframework.context.annotation.Configuration; 26 | import org.springframework.web.client.RestTemplate; 27 | 28 | /** 29 | * 30 | */ 31 | @Configuration 32 | public class CommonTestConfig { 33 | 34 | @Bean 35 | @LoadBalanced 36 | RestTemplate loadBalancedRestTemplate() { 37 | return new RestTemplate(); 38 | } 39 | 40 | @Bean(destroyMethod = "close") 41 | TestingServer testingServer() throws Exception { 42 | return new TestingServer(TestSocketUtils.findAvailableTcpPort()); 43 | } 44 | 45 | @Bean 46 | ZookeeperProperties zookeeperProperties(TestingServer testingServer) { 47 | ZookeeperProperties zookeeperProperties = new ZookeeperProperties(); 48 | zookeeperProperties.setConnectString("localhost:" + testingServer.getPort()); 49 | return zookeeperProperties; 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/test/java/org/springframework/cloud/zookeeper/discovery/test/TestLoadBalancedClient.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.discovery.test; 18 | 19 | import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties; 20 | import org.springframework.web.client.RestTemplate; 21 | 22 | /** 23 | * @author Marcin Grzejszczak 24 | */ 25 | public class TestLoadBalancedClient extends TestServiceRestClient { 26 | 27 | /** 28 | * Shared base path. 29 | */ 30 | public static final String BASE_PATH = new WebEndpointProperties().getBasePath(); 31 | 32 | private final String thisAppName; 33 | 34 | public TestLoadBalancedClient(RestTemplate restTemplate) { 35 | super(restTemplate); 36 | this.thisAppName = "someName"; 37 | } 38 | 39 | public TestLoadBalancedClient(RestTemplate restTemplate, String thisAppName) { 40 | super(restTemplate); 41 | this.thisAppName = thisAppName; 42 | } 43 | 44 | public String thisHealthCheck() { 45 | return this.restTemplate.getForObject( 46 | "http://" + this.thisAppName + BASE_PATH + "/health", String.class); 47 | } 48 | 49 | public Integer thisPort() { 50 | return this.restTemplate.getForObject("http://" + this.thisAppName + "/port", 51 | Integer.class); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/test/java/org/springframework/cloud/zookeeper/discovery/test/TestServiceRestClient.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.discovery.test; 18 | 19 | import org.apache.commons.logging.Log; 20 | import org.apache.commons.logging.LogFactory; 21 | 22 | import org.springframework.web.client.RestTemplate; 23 | 24 | /** 25 | * @author Marcin Grzejszczak 26 | */ 27 | public class TestServiceRestClient { 28 | 29 | private static final Log log = LogFactory.getLog(TestServiceRestClient.class); 30 | 31 | protected final RestTemplate restTemplate; 32 | 33 | public TestServiceRestClient(RestTemplate restTemplate) { 34 | this.restTemplate = restTemplate; 35 | } 36 | 37 | public T callService(String alias, String endpoint, Class clazz) { 38 | String url = "http://" + alias + "/" + endpoint; 39 | log.info("Calling [" + url + "]"); 40 | return this.restTemplate.getForObject(url, clazz); 41 | } 42 | 43 | public String callService(String alias, String endpoint) { 44 | return callService(alias, endpoint, String.class); 45 | } 46 | 47 | public String callOnUrl(String url, String endpoint) { 48 | if (!endpoint.startsWith("/")) { 49 | endpoint = "/" + endpoint; 50 | } 51 | 52 | return new RestTemplate().getForObject("http://" + url + endpoint, String.class); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/test/java/org/springframework/cloud/zookeeper/discovery/watcher/presence/DefaultDependencyPresenceOnStartupVerifierTests.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.discovery.watcher.presence; 18 | 19 | import java.util.Collections; 20 | 21 | import org.apache.curator.x.discovery.ServiceCache; 22 | import org.junit.Test; 23 | 24 | import static org.assertj.core.api.BDDAssertions.then; 25 | import static org.assertj.core.api.Fail.fail; 26 | import static org.mockito.BDDMockito.given; 27 | import static org.mockito.Mockito.mock; 28 | 29 | /** 30 | * @author Marcin Grzejszczak 31 | */ 32 | public class DefaultDependencyPresenceOnStartupVerifierTests { 33 | 34 | private static final String SERVICE_NAME = "service01"; 35 | 36 | @Test 37 | public void should_throw_exception_if_obligatory_dependencies_are_missing() { 38 | // given: 39 | DefaultDependencyPresenceOnStartupVerifier dependencyVerifier = new DefaultDependencyPresenceOnStartupVerifier(); 40 | ServiceCache serviceCache = mock(ServiceCache.class); 41 | given(serviceCache.getInstances()).willReturn(Collections.emptyList()); 42 | // when: 43 | try { 44 | dependencyVerifier.verifyDependencyPresence(SERVICE_NAME, serviceCache, true); 45 | fail("Should throw no instances running exception"); 46 | } 47 | catch (Exception e) { 48 | // then: 49 | then(e).isInstanceOf(NoInstancesRunningException.class); 50 | } 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/test/java/org/springframework/cloud/zookeeper/discovery/watcher/presence/DependencyPresenceOnStartupVerifierTests.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.discovery.watcher.presence; 18 | 19 | import java.util.Collections; 20 | 21 | import org.apache.curator.x.discovery.ServiceCache; 22 | import org.junit.Test; 23 | 24 | import static org.mockito.BDDMockito.given; 25 | import static org.mockito.BDDMockito.then; 26 | import static org.mockito.Mockito.mock; 27 | 28 | /** 29 | * @author Marcin Grzejszczak 30 | */ 31 | public class DependencyPresenceOnStartupVerifierTests { 32 | 33 | private static final String SERVICE_NAME = "service01"; 34 | 35 | @Test 36 | public void should_check_optional_dependency_using_optional_dependency_checker() { 37 | // given: 38 | PresenceChecker optionalDependencyChecker = mock(PresenceChecker.class); 39 | DependencyPresenceOnStartupVerifier dependencyVerifier = new DependencyPresenceOnStartupVerifier( 40 | optionalDependencyChecker) { 41 | }; 42 | ServiceCache serviceCache = mock(ServiceCache.class); 43 | given(serviceCache.getInstances()).willReturn(Collections.emptyList()); 44 | // when: 45 | dependencyVerifier.verifyDependencyPresence(SERVICE_NAME, serviceCache, false); 46 | // then: 47 | then(optionalDependencyChecker).should().checkPresence(SERVICE_NAME, 48 | serviceCache.getInstances()); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/test/resources/application-client.yml: -------------------------------------------------------------------------------- 1 | server.port: 0 2 | spring.application.name: client 3 | spring.cloud.zookeeper: 4 | dependencies: 5 | testInstance: 6 | path: /server 7 | logging: 8 | level: 9 | org.apache.zookeeper.ClientCnxn: ERROR -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/test/resources/application-dependencies.yml: -------------------------------------------------------------------------------- 1 | spring.application.name: nameWithoutAlias 2 | spring.cloud.zookeeper: 3 | dependencies: 4 | someAlias: 5 | path: nameWithoutAlias 6 | loadBalancerType: ROUND_ROBIN 7 | contentTypeTemplate: application/vnd.newsletter.$version+json 8 | version: v1 9 | headers: 10 | header1: 11 | - value1 12 | header2: 13 | - value2 14 | required: false 15 | stubs: org.springframework:foo:stubs 16 | testInstance2: 17 | path: somePath2 18 | loadBalancerType: ROUND_ROBIN 19 | contentTypeTemplate: application/vnd.newsletter.$version+json 20 | version: v1 21 | required: false 22 | aliasIsPath: '' 23 | anotherAlias: 'myPath' 24 | some-service: 25 | path: io/company/department/some-service 26 | management: 27 | security: 28 | enabled: false 29 | logging: 30 | level: 31 | org.apache.zookeeper.ClientCnxn: ERROR 32 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/test/resources/application-loadbalancer.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | application: 3 | name: loadBalancerApp 4 | cloud: 5 | zookeeper: 6 | discovery: 7 | metadata: 8 | testMetadataKey: testMetadataValue 9 | uriSpec: "{scheme}://{address}:{port}/contextPath" 10 | instance-id: loadbalancer-instance-id-123 11 | management: 12 | security: 13 | enabled: false 14 | logging: 15 | level: 16 | org.apache.zookeeper.ClientCnxn: ERROR 17 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/test/resources/application-loadbalancerclient.yml: -------------------------------------------------------------------------------- 1 | spring.application.name: loadbalancerclient 2 | spring.cloud.zookeeper: 3 | dependencies: 4 | someAlias: 5 | path: testInstance 6 | loadBalancerType: STICKY 7 | logging: 8 | level: 9 | org.apache.zookeeper.ClientCnxn: ERROR 10 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/test/resources/application-nestedstructure.yml: -------------------------------------------------------------------------------- 1 | spring.cloud.zookeeper.dependency.enabled: false 2 | spring.application.name: me 3 | management: 4 | security: 5 | enabled: false 6 | logging: 7 | level: 8 | org.apache.zookeeper.ClientCnxn: ERROR 9 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/test/resources/application-server.yml: -------------------------------------------------------------------------------- 1 | server.port: 0 2 | spring.application.name: server 3 | logging: 4 | level: 5 | org.apache.zookeeper.ClientCnxn: ERROR -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/test/resources/application-watcher.yml: -------------------------------------------------------------------------------- 1 | spring.application.name: some/name/without/alias 2 | spring.cloud.zookeeper: 3 | dependencies: 4 | someAlias: 5 | path: testInstance 6 | loadBalancerType: ROUND_ROBIN 7 | contentTypeTemplate: application/vnd.newsletter.$version+json 8 | version: v1 9 | headers: 10 | header1: 11 | - value1 12 | header2: 13 | - value2 14 | required: false 15 | testInstance2: 16 | path: somePath2 17 | loadBalancerType: ROUND_ROBIN 18 | contentTypeTemplate: application/vnd.newsletter.$version+json2 19 | version: v1 20 | required: false 21 | logging: 22 | level: 23 | org.apache.zookeeper.ClientCnxn: ERROR -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/test/resources/application.yml: -------------------------------------------------------------------------------- 1 | testInstance.loadbalancer.ServerListRefreshInterval: 100 2 | logging: 3 | level: 4 | org.apache.zookeeper.ClientCnxn: ERROR -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/test/resources/bootstrapper.yaml: -------------------------------------------------------------------------------- 1 | spring: 2 | config: 3 | import: "optional:configserver:" 4 | cloud: 5 | config: 6 | discovery: 7 | service-id: zookeeper-configserver 8 | enabled: true 9 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-discovery/src/test/resources/logback-test.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-sample/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | spring-cloud-zookeeper-sample 7 | jar 8 | Spring Cloud Zookeeper Sample 9 | Spring Cloud Zookeeper Sample 10 | 11 | 12 | org.springframework.cloud 13 | spring-cloud-zookeeper 14 | 4.3.1-SNAPSHOT 15 | .. 16 | 17 | 18 | 19 | true 20 | 21 | 22 | 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-maven-plugin 27 | 28 | 29 | 30 | maven-deploy-plugin 31 | 32 | true 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | org.springframework.cloud 41 | spring-cloud-starter-zookeeper-all 42 | 43 | 44 | org.springframework.boot 45 | spring-boot-starter-web 46 | 47 | 48 | org.springframework.boot 49 | spring-boot-starter-actuator 50 | 51 | 52 | org.springframework.cloud 53 | spring-cloud-starter-openfeign 54 | 55 | 56 | org.springframework.boot 57 | spring-boot-starter-test 58 | test 59 | 60 | 61 | org.junit.vintage 62 | junit-vintage-engine 63 | test 64 | 65 | 66 | org.apache.curator 67 | curator-test 68 | test 69 | 70 | 71 | org.springframework.cloud 72 | spring-cloud-zookeeper-core 73 | ${project.version} 74 | test-jar 75 | test 76 | 77 | 78 | org.springframework.cloud 79 | spring-cloud-test-support 80 | test 81 | 82 | 83 | 84 | 85 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-sample/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8080 3 | 4 | endpoints: 5 | restart: 6 | enabled: true 7 | shutdown: 8 | enabled: true 9 | health: 10 | sensitive: false 11 | 12 | logging.level: 13 | org.apache.zookeeper.ClientCnxn: ERROR 14 | 15 | management: 16 | security: 17 | enabled: false 18 | 19 | #spring.cloud.zookeeper.dependencies: 20 | # - testZookeeperApp: ~ -------------------------------------------------------------------------------- /spring-cloud-zookeeper-sample/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | application: 3 | name: testZookeeperApp 4 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-sample/src/test/java/org/springframework/cloud/zookeeper/sample/SampleApplicationTests.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2019 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 | * https://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 | package org.springframework.cloud.zookeeper.sample; 18 | 19 | import org.apache.curator.test.TestingServer; 20 | import org.junit.Test; 21 | 22 | import org.springframework.boot.builder.SpringApplicationBuilder; 23 | import org.springframework.boot.test.web.client.TestRestTemplate; 24 | import org.springframework.cloud.test.TestSocketUtils; 25 | import org.springframework.context.ConfigurableApplicationContext; 26 | import org.springframework.http.HttpStatus; 27 | import org.springframework.http.ResponseEntity; 28 | 29 | import static org.assertj.core.api.Assertions.assertThat; 30 | 31 | public class SampleApplicationTests { 32 | 33 | @Test 34 | public void contextLoads() throws Exception { 35 | int zkPort = TestSocketUtils.findAvailableTcpPort(); 36 | TestingServer server = new TestingServer(zkPort); 37 | 38 | int port = TestSocketUtils.findAvailableTcpPort(zkPort + 1); 39 | 40 | ConfigurableApplicationContext context = new SpringApplicationBuilder( 41 | SampleZookeeperApplication.class).run("--server.port=" + port, 42 | "--spring.config.use-legacy-processing=true", 43 | "--management.endpoints.web.exposure.include=*", 44 | "--spring.cloud.zookeeper.connect-string=localhost:" + zkPort); 45 | 46 | ResponseEntity response = new TestRestTemplate() 47 | .getForEntity("http://localhost:" + port + "/hi", String.class); 48 | assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); 49 | 50 | context.close(); 51 | server.close(); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-sample/src/test/resources/orderingtest-dev.properties: -------------------------------------------------------------------------------- 1 | my.prop=my value from local dev profile 2 | -------------------------------------------------------------------------------- /spring-cloud-zookeeper-sample/src/test/resources/orderingtest.properties: -------------------------------------------------------------------------------- 1 | spring.config.import=zookeeper: 2 | -------------------------------------------------------------------------------- /src/checkstyle/checkstyle-suppressions.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | --------------------------------------------------------------------------------