Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
R
Registration and Discovery Zookeeper
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
mohamadbashar.disoki
Registration and Discovery Zookeeper
Commits
ef49db2f
Commit
ef49db2f
authored
Jan 16, 2024
by
Mohamad Bashar Desoki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
logging - file appender
parent
fc503baf
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
75 additions
and
20 deletions
+75
-20
README.md
README.md
+1
-0
app.log
logs/app.log
+4
-0
Application.java
src/main/java/Registration_Discovery/Application.java
+13
-3
LeaderElection.java
src/main/java/Registration_Discovery/LeaderElection.java
+10
-2
OnElectionAction.java
src/main/java/Registration_Discovery/OnElectionAction.java
+2
-0
OnElectionCallback.java
src/main/java/Registration_Discovery/OnElectionCallback.java
+2
-0
ServiceRegistry.java
src/main/java/Registration_Discovery/ServiceRegistry.java
+13
-3
logback.xml
src/main/resources/logback.xml
+30
-12
No files found.
README.md
View file @
ef49db2f
...
...
@@ -2,6 +2,7 @@
*topics*
*
[
Logback vs SLF4J vs Log4J2 - what is the difference?
](
https://www.youtube.com/watch?v=SWHYrCXIL38&ab_channel=JavaBrains
)
*
[
SLF4J Logback Tutorial
](
https://mkyong.com/logging/slf4j-logback-tutorial/
)
*
Introduction to Service Registry & Service Discovery
*
Implement Service Registry with Zookeeper
*
Integrate into our leader-worker architecture
\ No newline at end of file
logs/app.log
0 → 100644
View file @
ef49db2f
2024-01-16 10:35:09,211 INFO Registration_Discovery.Application [main] Connected
2024-01-16 10:35:19,177 DEBUG Registration_Discovery.Application [main-EventThread] Successfully connected to Zookeeper
2024-01-16 10:35:19,277 INFO Registration_Discovery.LeaderElection [main] I'm a leader
2024-01-16 10:35:19,296 INFO Registration_Discovery.ServiceRegistry [main] The cluster addresses are: []
src/main/java/Application.java
→
src/main/java/
Registration_Discovery/
Application.java
View file @
ef49db2f
package
Registration_Discovery
;
import
org.apache.zookeeper.KeeperException
;
import
org.apache.zookeeper.WatchedEvent
;
import
org.apache.zookeeper.Watcher
;
...
...
@@ -9,6 +11,8 @@ import java.io.IOException;
public
class
Application
implements
Watcher
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
Application
.
class
);
private
static
final
String
address
=
"172.29.3.101:2181"
;
private
static
final
int
SESSION_TIMEOUT
=
3000
;
//dead client
private
static
final
int
DEFAULT_PORT
=
8080
;
...
...
@@ -20,6 +24,8 @@ public class Application implements Watcher {
Application
application
=
new
Application
();
ZooKeeper
zooKeeper
=
application
.
connectToZookeeper
();
logger
.
info
(
"Connected"
);
ServiceRegistry
serviceRegistry
=
new
ServiceRegistry
(
zooKeeper
);
OnElectionAction
onElectionAction
=
new
OnElectionAction
(
serviceRegistry
,
currentServerPort
);
...
...
@@ -31,6 +37,7 @@ public class Application implements Watcher {
application
.
run
();
application
.
close
();
}
public
ZooKeeper
connectToZookeeper
()
throws
IOException
{
...
...
@@ -53,14 +60,17 @@ public class Application implements Watcher {
switch
(
watchedEvent
.
getType
())
{
case
None:
if
(
watchedEvent
.
getState
()
==
Event
.
KeeperState
.
SyncConnected
)
{
System
.
out
.
println
(
"Successfully connected to Zookeeper"
);
// System.out.println("Successfully connected to Zookeeper");
logger
.
debug
(
"Successfully connected to Zookeeper"
);
}
else
if
(
watchedEvent
.
getState
()
==
Event
.
KeeperState
.
Disconnected
)
{
synchronized
(
zooKeeper
)
{
System
.
out
.
println
(
"Disconnected from Zookeeper"
);
// System.out.println("Disconnected from Zookeeper");
logger
.
debug
(
"Disconnected from Zookeeper"
);
zooKeeper
.
notifyAll
();
}
}
else
if
(
watchedEvent
.
getState
()
==
Event
.
KeeperState
.
Closed
)
{
System
.
out
.
println
(
"Closed Successfully"
);
// System.out.println("Closed Successfully");
logger
.
debug
(
"Closed Successfully"
);
}
break
;
}
...
...
src/main/java/LeaderElection.java
→
src/main/java/
Registration_Discovery/
LeaderElection.java
View file @
ef49db2f
package
Registration_Discovery
;
import
org.apache.zookeeper.*
;
import
org.apache.zookeeper.data.Stat
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
java.util.Collections
;
import
java.util.List
;
public
class
LeaderElection
implements
Watcher
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
LeaderElection
.
class
);
private
static
final
String
ELECTION_NAMESPACE
=
"/election"
;
private
String
currentZnodeName
;
...
...
@@ -38,19 +43,22 @@ public class LeaderElection implements Watcher {
String
smallestChild
=
children
.
get
(
0
);
//the first element
if
(
smallestChild
.
equals
(
currentZnodeName
))
{
System
.
out
.
println
(
"I'm a leader"
);
// System.out.println("I'm a leader");
logger
.
info
(
"I'm a leader"
);
onElectionCallback
.
onElectedToBeLeader
();
return
;
}
else
{
System
.
out
.
println
(
"I'm not a leader"
);
logger
.
info
(
"I'm not a leader"
);
int
predecessorIndex
=
children
.
indexOf
(
currentZnodeName
)
-
1
;
predecessorName
=
children
.
get
(
predecessorIndex
);
predecessorStat
=
zooKeeper
.
exists
(
ELECTION_NAMESPACE
+
"/"
+
predecessorName
,
this
);
}
}
onElectionCallback
.
onWorker
();
System
.
out
.
println
(
"Watching znode "
+
predecessorName
);
// System.out.println("Watching znode " + predecessorName);
logger
.
info
(
"Watching znode {}"
,
predecessorName
);
System
.
out
.
println
();
}
...
...
src/main/java/OnElectionAction.java
→
src/main/java/
Registration_Discovery/
OnElectionAction.java
View file @
ef49db2f
package
Registration_Discovery
;
import
org.apache.zookeeper.KeeperException
;
import
java.net.InetAddress
;
...
...
src/main/java/OnElectionCallback.java
→
src/main/java/
Registration_Discovery/
OnElectionCallback.java
View file @
ef49db2f
package
Registration_Discovery
;
public
interface
OnElectionCallback
{
void
onElectedToBeLeader
();
...
...
src/main/java/ServiceRegistry.java
→
src/main/java/
Registration_Discovery/
ServiceRegistry.java
View file @
ef49db2f
package
Registration_Discovery
;
import
org.apache.zookeeper.*
;
import
org.apache.zookeeper.data.Stat
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.List
;
public
class
ServiceRegistry
implements
Watcher
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
ServiceRegistry
.
class
);
private
static
final
String
REGISTRY_ZNODE
=
"/service_registry"
;
private
final
ZooKeeper
zooKeeper
;
...
...
@@ -31,12 +38,14 @@ public class ServiceRegistry implements Watcher {
public
void
registerToCluster
(
String
metadata
)
throws
KeeperException
,
InterruptedException
{
if
(
this
.
currentZnode
!=
null
)
{
System
.
out
.
println
(
"Already registered to service registry"
);
// System.out.println("Already registered to service registry");
logger
.
debug
(
"Already registered to service registry"
);
return
;
}
this
.
currentZnode
=
zooKeeper
.
create
(
REGISTRY_ZNODE
+
"/n_"
,
metadata
.
getBytes
(),
ZooDefs
.
Ids
.
OPEN_ACL_UNSAFE
,
CreateMode
.
EPHEMERAL_SEQUENTIAL
);
System
.
out
.
println
(
"Registered to service registry"
);
// System.out.println("Registered to service registry");
logger
.
debug
(
"Registered to service registry"
);
}
public
void
registerForUpdates
()
{
...
...
@@ -79,7 +88,8 @@ public class ServiceRegistry implements Watcher {
}
this
.
allServiceAddresses
=
Collections
.
unmodifiableList
(
addresses
);
System
.
out
.
println
(
"The cluster addresses are: "
+
this
.
allServiceAddresses
);
// System.out.println("The cluster addresses are: " + this.allServiceAddresses);
logger
.
info
(
"The cluster addresses are: {}"
,
this
.
allServiceAddresses
);
}
@Override
...
...
src/main/resources/logback.xml
View file @
ef49db2f
<?xml version="1.0" encoding="UTF-8"?>
<Configuration
status=
"WARN"
>
<Appenders>
<Console
name=
"Console"
target=
"SYSTEM_OUT"
>
<PatternLayout
pattern=
"%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"
/>
</Console>
</Appenders>
<Loggers>
<Root
level=
"WARN"
>
<AppenderRef
ref=
"Console"
/>
</Root>
</Loggers>
</Configuration>
\ No newline at end of file
<configuration>
<property
name=
"HOME_LOG"
value=
"logs/app.log"
/>
<appender
name=
"CONSOLE"
class=
"ch.qos.logback.core.ConsoleAppender"
>
<layout
class=
"ch.qos.logback.classic.PatternLayout"
>
<Pattern>
%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>
<appender
name=
"FILE"
class=
"ch.qos.logback.core.FileAppender"
>
<file>
${HOME_LOG}
</file>
<append>
true
</append>
<immediateFlush>
true
</immediateFlush>
<encoder>
<pattern>
%d %p %c{1.} [%t] %m%n
</pattern>
</encoder>
</appender>
<!-- Log Level: ERROR, WARN, INFO, DEBUG, TRACE-->
<logger
name=
"Registration_Discovery"
level=
"DEBUG"
additivity=
"false"
>
<appender-ref
ref=
"CONSOLE"
/>
<appender-ref
ref=
"FILE"
/>
</logger>
<root
level=
"error"
>
<appender-ref
ref=
"CONSOLE"
/>
</root>
</configuration>
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