-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
301 additions
and
30 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1 +1,62 @@ | ||
# OpenSergo Java SDK | ||
# OpenSergo Java SDK | ||
|
||
|
||
## How to use | ||
|
||
### scene 1 : subscribe config-data | ||
``` java | ||
public static void main(String[] args) throws Exception { | ||
|
||
// instant OpenSergoClient | ||
OpenSergoClient openSergoClient = new OpenSergoClient("33.1.33.1", 10246); | ||
|
||
// registry SubscribeInfo of FaultToleranceRule | ||
// 1. instant SubscribeKey | ||
SubscribeKey subscribeKey = new SubscribeKey("default", "foo-app", ConfigKind.FAULT_TOLERANCE_RULE); | ||
// 2. construct SubscribeInfo | ||
OpensergoClientSubscribeInfo opensergoClientSubscribeInfo = new OpensergoClientSubscribeInfo(subscribeKey); | ||
opensergoClientSubscribeInfo.addSubscriber(new DefaultFaulttoleranceSubscriber()); | ||
// 2. registry | ||
openSergoClient.registerSubscribeInfo(opensergoClientSubscribeInfo); | ||
|
||
// start OpenSergoClient | ||
openSergoClient.start(); | ||
} | ||
``` | ||
|
||
### scene 2 : subscribe config-data with custom-logic when config-data changed. | ||
Add a subscriber by implements the function in `subscribe.Subscriber`. | ||
``` java | ||
public class TestSubscriber implements OpenSergoConfigSubscriber { | ||
@Override | ||
public boolean onConfigUpdate(SubscribeKey subscribeKey, Object data) { | ||
System.out.println(data); | ||
return true; | ||
} | ||
} | ||
``` | ||
And then registry it to `subscriber.SubscriberRegistry`. | ||
``` java | ||
public static void main(String[] args) throws Exception { | ||
|
||
// instant OpenSergoClient | ||
OpenSergoClient openSergoClient = new OpenSergoClient("33.1.33.1", 10246); | ||
|
||
// registry SubscribeInfo of FaultToleranceRule | ||
// 1. instant SubscribeKey | ||
SubscribeKey subscribeKey = new SubscribeKey("default", "foo-app", ConfigKind.FAULT_TOLERANCE_RULE); | ||
// 2. instant Subscriber | ||
TestSubscriber testSubscriber = new TestSubscriber(); | ||
// 3. construct SubscribeInfo | ||
OpensergoClientSubscribeInfo opensergoClientSubscribeInfo = new OpensergoClientSubscribeInfo(subscribeKey); | ||
opensergoClientSubscribeInfo.addSubscriber(testSubscriber); | ||
// 4. registry | ||
openSergoClient.registerSubscribeInfo(opensergoClientSubscribeInfo); | ||
|
||
openSergoClient.start(); | ||
|
||
// registry after OpenSergoClient started | ||
opensergoClientSubscribeInfo.addSubscriber(new OpensergoConfigDefaultSubscriber()); | ||
openSergoClient.registerSubscribeInfo(opensergoClientSubscribeInfo); | ||
} | ||
``` |
This file contains 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
This file contains 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
This file contains 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, OpenSergo 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 | ||
* | ||
* https://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 io.opensergo; | ||
|
||
/** | ||
* @author Jiangnan Jia | ||
**/ | ||
public enum OpensergoClientStatus { | ||
|
||
/* initial*/ | ||
INITIAL, | ||
STARTING, | ||
STARTED, | ||
INTERRUPTED, | ||
SHUTDOWN | ||
|
||
} |
68 changes: 68 additions & 0 deletions
68
src/main/java/io/opensergo/OpensergoClientSubscribeInfo.java
This file contains 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,68 @@ | ||
/* | ||
* Copyright 2022, OpenSergo 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 | ||
* | ||
* https://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 io.opensergo; | ||
|
||
import com.google.common.collect.Lists; | ||
import io.opensergo.subscribe.OpenSergoConfigSubscriber; | ||
import io.opensergo.subscribe.SubscribeKey; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* @author Jiangnan Jia | ||
**/ | ||
public class OpensergoClientSubscribeInfo { | ||
|
||
private SubscribeKey subscribeKey; | ||
private List<OpenSergoConfigSubscriber> subscriberList; | ||
|
||
public OpensergoClientSubscribeInfo(SubscribeKey subscribeKey) { | ||
this.subscribeKey = subscribeKey; | ||
this.subscriberList = Lists.newArrayList(); | ||
} | ||
public OpensergoClientSubscribeInfo(SubscribeKey subscribeKey, List<OpenSergoConfigSubscriber> subscriberList) { | ||
this.subscribeKey = subscribeKey; | ||
this.subscriberList = subscriberList; | ||
} | ||
|
||
public OpensergoClientSubscribeInfo addSubscriber(OpenSergoConfigSubscriber subscriber) { | ||
int sameClassNum = this.subscriberList.stream() | ||
.filter(item -> item.getClass() == subscriber.getClass()) | ||
.collect(Collectors.toList()) | ||
.size(); | ||
if (sameClassNum == 0) { | ||
this.subscriberList.add(subscriber); | ||
} | ||
return this; | ||
} | ||
|
||
public SubscribeKey getSubscribeKey() { | ||
return subscribeKey; | ||
} | ||
|
||
public List<OpenSergoConfigSubscriber> getSubscriberList() { | ||
return subscriberList; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "OpensergoClientSubscribeInfo{" + | ||
"subscribeKey=" + subscribeKey + | ||
", subscriberList=" + subscriberList + | ||
'}'; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/io/opensergo/subscribe/DefaultFaulttoleranceSubscriber.java
This file contains 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,27 @@ | ||
/* | ||
* Copyright 2022, OpenSergo 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 | ||
* | ||
* https://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 io.opensergo.subscribe; | ||
|
||
/** | ||
* @author Jiangnan Jia | ||
**/ | ||
public class DefaultFaulttoleranceSubscriber implements OpenSergoConfigSubscriber { | ||
@Override | ||
public boolean onConfigUpdate(SubscribeKey subscribeKey, Object data) { | ||
System.out.println(data); | ||
return true; | ||
} | ||
} |
Oops, something went wrong.