-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
feat: support use database as a registry #4595
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+2,685
−3
Merged
Changes from 2 commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
a9e0e3c
feat: support use database as a registry
Anilople 46df1c0
fix: test fail when missing bean RegistryService
Anilople 4a06884
Delete RegistryController.java
Anilople 5615639
feat: add spring.cloud.discovery.enabled=false
Anilople c84a4b5
refactor: healthCheckInterval -> healthCheckIntervalInSecond
Anilople aa61c4c
fix: 'com.ctrip.framework.apollo.biz.repository.RegistryRepository' t…
Anilople c2be03d
refactor: Registry -> ServiceRegistry. change all config prefix to ap…
Anilople 343d9f9
feat: add service registry config heartbeatIntervalInSecond
Anilople 1eed34a
feat: clear service instances 1 days ago
Anilople e4513c6
fix: move '@Transactional' from repository up to service
Anilople 4fa67b7
refactor: Change all to use jvm `LocalDateTime.now()`
Anilople bcc5575
fix test fail
Anilople 98b55f7
Update apolloconfigdb.sql
Anilople e6de6ae
refactor: split heartbeat and deregister
Anilople a1c1488
fix: catch Throwable instead of catch Exception
Anilople ef4fc4b
refactor: use stream and lambda instead of for each loop filter
Anilople fa0d4f7
delete USING BTREE
Anilople fbf7d2d
fix: label -> cluster
Anilople 5d57924
Update scripts/sql/apolloconfigdb.sql
Anilople 5bb73d1
Update scripts/sql/delta/v210-v220/apolloconfigdb-v210-v220.sql
Anilople 16f237d
test: add bean exist check
Anilople a9a05b9
feat: add Metadata column
Anilople c61f8ef
refactor: only use cluster in DatabaseDiscoveryClientImpl
Anilople ad2a1f0
feat: add metadata config in code
Anilople 4cb1443
Merge branch 'master' into feature/register-to-database
Anilople c358128
Merge branch 'master' into feature/register-to-database
Anilople 6fa748f
test: add tests of registry
Anilople b4fc7f0
Merge branch 'master' into feature/register-to-database
Anilople 9219f34
docs: Enable database-discovery to replace built-in eureka
Anilople 1baec92
Update CHANGES.md
Anilople 4c3e409
room -> cluster
Anilople 8eea742
room -> cluster
Anilople 02370fa
feat: when database fail, database-discovery still be useful. by deco…
Anilople 1d1d77e
test: fix when DatabaseDiscoveryClient wrap by decorator
Anilople File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
.../src/main/java/com/ctrip/framework/apollo/adminservice/controller/RegistryController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| /* | ||
| * Copyright 2022 Apollo Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| */ | ||
| package com.ctrip.framework.apollo.adminservice.controller; | ||
|
|
||
| import com.ctrip.framework.apollo.biz.entity.Registry; | ||
| import com.ctrip.framework.apollo.biz.service.RegistryService; | ||
| import java.time.Duration; | ||
| import java.util.List; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/registry") | ||
| @ConditionalOnBean(RegistryService.class) | ||
| public class RegistryController { | ||
|
|
||
| @Autowired | ||
| private RegistryService registryService; | ||
|
|
||
| @PostMapping("/clear/instances") | ||
| public List<Registry> clearInstances() { | ||
| return this.registryService.deleteTimeBefore(Duration.ofMinutes(10)); | ||
| } | ||
| } | ||
20 changes: 20 additions & 0 deletions
20
apollo-adminservice/src/main/resources/application-database-discovery.properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # | ||
| # Copyright 2022 Apollo Authors | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
| eureka.client.enabled=false | ||
Anilople marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| apollo.registry.client.enabled=true | ||
| apollo.registry.client.label=default | ||
Anilople marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
136 changes: 136 additions & 0 deletions
136
apollo-biz/src/main/java/com/ctrip/framework/apollo/biz/entity/Registry.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| /* | ||
| * Copyright 2022 Apollo Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| */ | ||
| package com.ctrip.framework.apollo.biz.entity; | ||
|
|
||
| import com.ctrip.framework.apollo.biz.registry.ServiceInstance; | ||
| import java.time.LocalDateTime; | ||
| import javax.persistence.Column; | ||
| import javax.persistence.Entity; | ||
| import javax.persistence.GeneratedValue; | ||
| import javax.persistence.GenerationType; | ||
| import javax.persistence.Id; | ||
| import javax.persistence.PrePersist; | ||
| import javax.persistence.Table; | ||
|
|
||
| /** | ||
| * use database as a registry instead of eureka, zookeeper, consul etc. | ||
| * <p> | ||
| * persist {@link ServiceInstance} | ||
| */ | ||
| @Entity | ||
| @Table(name = "Registry") | ||
| public class Registry { | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| @Column(name = "Id") | ||
| private long id; | ||
|
|
||
| @Column(name = "ServiceName", nullable = false) | ||
| private String serviceName; | ||
|
|
||
| /** | ||
| * @see ServiceInstance#getUri() | ||
| */ | ||
| @Column(name = "Uri", nullable = false) | ||
| private String uri; | ||
|
|
||
| /** | ||
| * @see ServiceInstance#getLabel() | ||
| */ | ||
| @Column(name = "Label", nullable = false) | ||
| private String label; | ||
|
|
||
| @Column(name = "DataChange_CreatedTime", nullable = false) | ||
| private LocalDateTime dataChangeCreatedTime; | ||
|
|
||
| /** | ||
| * modify by heartbeat | ||
| */ | ||
| @Column(name = "DataChange_LastTime", nullable = false) | ||
| private LocalDateTime dataChangeLastModifiedTime; | ||
|
|
||
| @PrePersist | ||
| protected void prePersist() { | ||
| if (this.dataChangeCreatedTime == null) { | ||
| dataChangeCreatedTime = LocalDateTime.now(); | ||
| } | ||
| if (this.dataChangeLastModifiedTime == null) { | ||
| dataChangeLastModifiedTime = dataChangeCreatedTime; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "Registry{" + | ||
| "id=" + id + | ||
| ", serviceName='" + serviceName + '\'' + | ||
| ", uri='" + uri + '\'' + | ||
| ", label='" + label + '\'' + | ||
| ", dataChangeCreatedTime=" + dataChangeCreatedTime + | ||
| ", dataChangeLastModifiedTime=" + dataChangeLastModifiedTime + | ||
| '}'; | ||
| } | ||
|
|
||
| public long getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public void setId(long id) { | ||
| this.id = id; | ||
| } | ||
|
|
||
| public String getServiceName() { | ||
| return serviceName; | ||
| } | ||
|
|
||
| public void setServiceName(String serviceName) { | ||
| this.serviceName = serviceName; | ||
| } | ||
|
|
||
| public String getUri() { | ||
| return uri; | ||
| } | ||
|
|
||
| public void setUri(String uri) { | ||
| this.uri = uri; | ||
| } | ||
|
|
||
| public String getLabel() { | ||
| return label; | ||
| } | ||
|
|
||
| public void setLabel(String label) { | ||
| this.label = label; | ||
| } | ||
|
|
||
| public LocalDateTime getDataChangeCreatedTime() { | ||
| return dataChangeCreatedTime; | ||
| } | ||
|
|
||
| public void setDataChangeCreatedTime(LocalDateTime dataChangeCreatedTime) { | ||
| this.dataChangeCreatedTime = dataChangeCreatedTime; | ||
| } | ||
|
|
||
| public LocalDateTime getDataChangeLastModifiedTime() { | ||
| return dataChangeLastModifiedTime; | ||
| } | ||
|
|
||
| public void setDataChangeLastModifiedTime(LocalDateTime dataChangeLastModifiedTime) { | ||
| this.dataChangeLastModifiedTime = dataChangeLastModifiedTime; | ||
| } | ||
| } |
30 changes: 30 additions & 0 deletions
30
...lo-biz/src/main/java/com/ctrip/framework/apollo/biz/registry/DatabaseDiscoveryClient.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| /* | ||
| * Copyright 2022 Apollo Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| */ | ||
| package com.ctrip.framework.apollo.biz.registry; | ||
|
|
||
| import com.ctrip.framework.apollo.biz.registry.configuration.support.ApolloRegistryDiscoveryProperties; | ||
| import com.ctrip.framework.apollo.biz.registry.configuration.support.ApolloRegistryClientProperties; | ||
| import java.util.List; | ||
|
|
||
| public interface DatabaseDiscoveryClient { | ||
|
|
||
| /** | ||
| * find by {@link ApolloRegistryClientProperties#getServiceName()}, | ||
| * then filter by label if {@link ApolloRegistryDiscoveryProperties#isFilterByLabel()} is true. | ||
| */ | ||
| List<ServiceInstance> getInstances(String serviceName); | ||
| } |
127 changes: 127 additions & 0 deletions
127
...iz/src/main/java/com/ctrip/framework/apollo/biz/registry/DatabaseDiscoveryClientImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| /* | ||
| * Copyright 2022 Apollo Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| */ | ||
| package com.ctrip.framework.apollo.biz.registry; | ||
|
|
||
| import com.ctrip.framework.apollo.biz.entity.Registry; | ||
| import com.ctrip.framework.apollo.biz.registry.configuration.support.ApolloRegistryDiscoveryProperties; | ||
| import com.ctrip.framework.apollo.biz.registry.configuration.support.ApolloRegistryClientProperties; | ||
| import com.ctrip.framework.apollo.biz.service.RegistryService; | ||
| import java.time.LocalDateTime; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| public class DatabaseDiscoveryClientImpl implements DatabaseDiscoveryClient { | ||
|
|
||
| private static final Logger log = LoggerFactory.getLogger(DatabaseDiscoveryClientImpl.class); | ||
|
|
||
| private final RegistryService registryService; | ||
|
|
||
| private final ApolloRegistryDiscoveryProperties discoveryProperties; | ||
|
|
||
| private final ServiceInstance self; | ||
|
|
||
| public DatabaseDiscoveryClientImpl( | ||
| RegistryService registryService, | ||
| ApolloRegistryDiscoveryProperties discoveryProperties, | ||
| ServiceInstance self) { | ||
| this.registryService = registryService; | ||
| this.discoveryProperties = discoveryProperties; | ||
| this.self = self; | ||
Anilople marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
| * find by {@link ApolloRegistryClientProperties#getServiceName()} | ||
| */ | ||
| @Override | ||
| public List<ServiceInstance> getInstances(String serviceName) { | ||
| final List<Registry> filterByLabel; | ||
| { | ||
| List<Registry> all = this.registryService.findByServiceName(serviceName); | ||
| if (this.discoveryProperties.isFilterByLabel()) { | ||
Anilople marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| filterByLabel = filterByLabel(all, this.self.getLabel()); | ||
| } else { | ||
| // get all | ||
| filterByLabel = all; | ||
| } | ||
| } | ||
| LocalDateTime healthCheckTime = this.registryService.getTimeBeforeSeconds( | ||
| this.discoveryProperties.getHealthCheckInterval() | ||
| ); | ||
| final List<Registry> filterByHealthCheck = filterByHealthCheck(filterByLabel, healthCheckTime, serviceName); | ||
nobodyiam marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // convert | ||
| List<ServiceInstance> registrationList = new ArrayList<>(filterByHealthCheck.size()); | ||
| for (Registry registry : filterByHealthCheck) { | ||
| ApolloRegistryClientProperties registration = convert(registry); | ||
| registrationList.add(registration); | ||
| } | ||
| return registrationList; | ||
| } | ||
|
|
||
| static ApolloRegistryClientProperties convert(Registry registry) { | ||
| ApolloRegistryClientProperties registration = new ApolloRegistryClientProperties(); | ||
| registration.setServiceName(registry.getServiceName()); | ||
| registration.setUri(registry.getUri()); | ||
| registration.setLabel(registry.getLabel()); | ||
| return registration; | ||
| } | ||
|
|
||
| static List<Registry> filterByLabel(List<Registry> list, String label) { | ||
| if (list.isEmpty()) { | ||
| return Collections.emptyList(); | ||
| } | ||
| List<Registry> listAfterFilter = new ArrayList<>(8); | ||
| for (Registry registry : list) { | ||
| if (Objects.equals(label, registry.getLabel())) { | ||
| listAfterFilter.add(registry); | ||
| } | ||
| } | ||
| return listAfterFilter; | ||
| } | ||
|
|
||
| static List<Registry> filterByHealthCheck( | ||
| List<Registry> list, | ||
| LocalDateTime healthCheckTime, | ||
| String serviceName | ||
| ) { | ||
| if (list.isEmpty()) { | ||
| return Collections.emptyList(); | ||
| } | ||
| List<Registry> listAfterFilter = new ArrayList<>(8); | ||
| for (Registry registry : list) { | ||
| LocalDateTime lastModifiedTime = registry.getDataChangeLastModifiedTime(); | ||
| if (lastModifiedTime.isAfter(healthCheckTime)) { | ||
| listAfterFilter.add(registry); | ||
| } | ||
| } | ||
|
|
||
| if (listAfterFilter.isEmpty()) { | ||
| log.error( | ||
| "there is no healthy instance of '{}'. And there are {} unhealthy instances", | ||
| serviceName, | ||
| list.size() | ||
| ); | ||
| } | ||
|
|
||
| return listAfterFilter; | ||
| } | ||
|
|
||
| } | ||
33 changes: 33 additions & 0 deletions
33
...lo-biz/src/main/java/com/ctrip/framework/apollo/biz/registry/DatabaseServiceRegistry.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /* | ||
| * Copyright 2022 Apollo Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| */ | ||
| package com.ctrip.framework.apollo.biz.registry; | ||
|
|
||
| /** | ||
| * @see org.springframework.cloud.client.serviceregistry.ServiceRegistry | ||
| */ | ||
| public interface DatabaseServiceRegistry { | ||
|
|
||
| /** | ||
| * register an instance to database | ||
| */ | ||
| void register(ServiceInstance instance); | ||
|
|
||
| /** | ||
| * remove an instance from database | ||
| */ | ||
| void deregister(ServiceInstance instance); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.