Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
D
DS_Project_Part1
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ali.saeed
DS_Project_Part1
Commits
35652575
Commit
35652575
authored
Jan 30, 2024
by
Ali Saeed
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update onElectedToBeLeader function to register as coordinator and store it's IP address
parent
22c7eed5
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
20 additions
and
1 deletion
+20
-1
ServiceRegistry.java
...src/main/java/Registration_Discovery/ServiceRegistry.java
+20
-1
No files found.
registration-discovery-leader-reelection-cordinator-workers/src/main/java/Registration_Discovery/ServiceRegistry.java
View file @
35652575
...
...
@@ -14,8 +14,8 @@ public class ServiceRegistry implements Watcher {
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
ServiceRegistry
.
class
);
private
static
final
String
REGISTRY_ZNODE
=
"/service_registry"
;
private
static
final
String
COORDINATOR_ZNODE
=
"/cordinator"
;
private
final
ZooKeeper
zooKeeper
;
private
String
currentZnode
=
null
;
private
static
List
<
String
>
allServiceAddresses
=
null
;
...
...
@@ -26,6 +26,7 @@ public class ServiceRegistry implements Watcher {
public
ServiceRegistry
(
ZooKeeper
zooKeeper
)
{
this
.
zooKeeper
=
zooKeeper
;
createServiceRegistryZnode
();
createCoordinatorZnode
();
}
private
void
createServiceRegistryZnode
()
{
...
...
@@ -39,6 +40,17 @@ public class ServiceRegistry implements Watcher {
e
.
printStackTrace
();
}
}
private
void
createCoordinatorZnode
()
{
try
{
if
(
zooKeeper
.
exists
(
COORDINATOR_ZNODE
,
false
)
==
null
)
{
zooKeeper
.
create
(
COORDINATOR_ZNODE
,
new
byte
[]{},
ZooDefs
.
Ids
.
OPEN_ACL_UNSAFE
,
CreateMode
.
PERSISTENT
);
}
}
catch
(
KeeperException
e
)
{
e
.
printStackTrace
();
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
public
void
registerToCluster
(
String
metadata
)
throws
KeeperException
,
InterruptedException
{
if
(
this
.
currentZnode
!=
null
)
{
...
...
@@ -51,6 +63,12 @@ public class ServiceRegistry implements Watcher {
// System.out.println("Registered to service registry");
logger
.
debug
(
"Registered to service registry"
);
}
public
void
registerToClusterAsCoordinator
(
String
metadata
)
throws
KeeperException
,
InterruptedException
{
zooKeeper
.
create
(
COORDINATOR_ZNODE
+
"/C_"
,
metadata
.
getBytes
(),
ZooDefs
.
Ids
.
OPEN_ACL_UNSAFE
,
CreateMode
.
EPHEMERAL_SEQUENTIAL
);
logger
.
debug
(
"Register to cluster as coordinator"
);
}
public
void
registerForUpdates
()
{
try
{
...
...
@@ -62,6 +80,7 @@ public class ServiceRegistry implements Watcher {
}
}
public
void
unregisterFromCluster
()
{
try
{
if
(
currentZnode
!=
null
&&
zooKeeper
.
exists
(
currentZnode
,
false
)
!=
null
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment