Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
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
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
cd946aba
Commit
cd946aba
authored
Jan 07, 2015
by
Paulo Pires
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Updated discovery source code example.
parent
2c6211e6
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
30 additions
and
37 deletions
+30
-37
README.md
examples/hazelcast/README.md
+30
-37
No files found.
examples/hazelcast/README.md
View file @
cd946aba
...
...
@@ -211,8 +211,6 @@ kubecfg resize hazelcast 4
### Hazelcast Discovery Source
```
java
import
static
com
.
github
.
pires
.
hazelcast
.
Constants
.
hazelcastPodLabelKey
;
import
static
com
.
github
.
pires
.
hazelcast
.
Constants
.
hazelcastPodLabelValue
;
import
com.hazelcast.config.Config
;
import
com.hazelcast.config.GroupConfig
;
import
com.hazelcast.config.JoinConfig
;
...
...
@@ -221,15 +219,14 @@ import com.hazelcast.config.NetworkConfig;
import
com.hazelcast.config.SSLConfig
;
import
com.hazelcast.config.TcpIpConfig
;
import
com.hazelcast.core.Hazelcast
;
import
io.fabric8.kubernetes.api.Kubernetes
;
import
io.fabric8.kubernetes.api.Kubernetes
Client
;
import
io.fabric8.kubernetes.api.KubernetesFactory
;
import
io.fabric8.kubernetes.api.model.Pod
Schema
;
import
io.fabric8.kubernetes.api.model.Pod
;
import
java.util.List
;
import
java.util.UUID
;
import
java.util.concurrent.CopyOnWriteArrayList
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.boot.CommandLineRunner
;
import
org.springframework.stereotype.Controller
;
...
...
@@ -242,46 +239,45 @@ public class HazelcastDiscoveryController implements CommandLineRunner {
private
static
final
Logger
log
=
LoggerFactory
.
getLogger
(
HazelcastDiscoveryController
.
class
);
// TODO load this from env vars
private
static
final
String
HC_GROUP_NAME
=
"someGroup"
;
private
static
final
String
HC_GROUP_PASSWORD
=
"someSecret"
;
private
static
final
int
HC_PORT
=
5701
;
private
static
final
String
HAZELCAST_LABEL_NAME
=
"name"
;
private
static
final
String
HAZELCAST_LABEL_VALUE
=
"hazelcast"
;
@Value
(
"#{systemEnvironment.KUBERNETES_RO_SERVICE_HOST}"
)
private
String
kubeMasterHost
;
@Value
(
"#{systemEnvironment.KUBERNETES_RO_SERVICE_PORT}"
)
private
String
kubeMasterPort
;
private
String
getKubeApi
()
{
return
"http://"
+
kubeMasterHost
+
":"
+
kubeMasterPort
;
private
static
String
getEnvOrDefault
(
String
var
,
String
def
)
{
final
String
val
=
System
.
getenv
(
var
);
return
(
val
==
null
||
val
.
isEmpty
())
?
def
:
val
;
}
@Override
public
void
run
(
String
...
args
)
{
log
.
info
(
"Asking k8s registry at {}.."
,
getKubeApi
());
KubernetesFactory
kubernetesFactory
=
new
KubernetesFactory
(
getKubeApi
());
final
List
<
PodSchema
>
hazelcastPods
=
retrieveHazelcastPods
(
kubernetesFactory
.
createKubernetes
());
log
.
info
(
"Found {} pods running Hazelcast."
,
hazelcastPods
.
size
());
final
String
kubeApiHost
=
getEnvOrDefault
(
"KUBERNETES_RO_SERVICE_HOST"
,
"localhost"
);
final
String
kubeApiPort
=
getEnvOrDefault
(
"KUBERNETES_RO_SERVICE_PORT"
,
"8080"
);
final
String
kubeUrl
=
"http://"
+
kubeApiHost
+
":"
+
kubeApiPort
;
log
.
info
(
"Asking k8s registry at {}.."
,
kubeUrl
);
final
KubernetesClient
kube
=
new
KubernetesClient
(
new
KubernetesFactory
(
kubeUrl
));
final
List
<
Pod
>
hazelcastPods
=
new
CopyOnWriteArrayList
<>();
kube
.
getPods
().
getItems
().
parallelStream
().
filter
(
pod
->
pod
.
getLabels
().
get
(
HAZELCAST_LABEL_NAME
).
equals
(
HAZELCAST_LABEL_VALUE
)).
forEach
(
hazelcastPods:
:
add
);
log
.
info
(
"Found {} pods running Hazelcast."
,
hazelcastPods
.
size
());
if
(!
hazelcastPods
.
isEmpty
())
{
runHazelcast
(
hazelcastPods
);
}
}
public
List
<
PodSchema
>
retrieveHazelcastPods
(
final
Kubernetes
kubernetes
)
{
final
List
<
PodSchema
>
hazelcastPods
=
new
CopyOnWriteArrayList
<>();
kubernetes
.
getPods
().
getItems
().
parallelStream
().
filter
(
pod
->
pod
.
getLabels
().
get
(
hazelcastPodLabelKey
).
equals
(
hazelcastPodLabelValue
)).
forEach
(
hazelcastPods:
:
add
);
return
hazelcastPods
;
}
private
void
runHazelcast
(
final
List
<
PodSchema
>
hazelcastPods
)
{
private
void
runHazelcast
(
final
List
<
Pod
>
hazelcastPods
)
{
// configure Hazelcast instance
final
Config
cfg
=
new
Config
();
cfg
.
setInstanceName
(
UUID
.
randomUUID
().
toString
());
// group configuration
final
String
HC_GROUP_NAME
=
getEnvOrDefault
(
"HC_GROUP_NAME"
,
"someGroup"
);
final
String
HC_GROUP_PASSWORD
=
getEnvOrDefault
(
"HC_GROUP_PASSWORD"
,
"someSecret"
);
final
int
HC_PORT
=
Integer
.
parseInt
(
getEnvOrDefault
(
"HC_PORT"
,
"5701"
));
cfg
.
setGroupConfig
(
new
GroupConfig
(
HC_GROUP_NAME
,
HC_GROUP_PASSWORD
));
// network configuration initialization
final
NetworkConfig
netCfg
=
new
NetworkConfig
();
...
...
@@ -292,10 +288,9 @@ public class HazelcastDiscoveryController implements CommandLineRunner {
mcCfg
.
setEnabled
(
false
);
// tcp
final
TcpIpConfig
tcpCfg
=
new
TcpIpConfig
();
hazelcastPods
.
stream
().
filter
(
pod
->
pod
.
getCurrentState
().
getPodIP
()
!=
null
).
forEach
(
pod
->
{
tcpCfg
.
addMember
(
pod
.
getCurrentState
().
getPodIP
());
});
hazelcastPods
.
parallelStream
().
forEach
(
pod
->
{
tcpCfg
.
addMember
(
pod
.
getCurrentState
().
getPodIP
());
});
tcpCfg
.
setEnabled
(
true
);
// network join configuration
final
JoinConfig
joinCfg
=
new
JoinConfig
();
...
...
@@ -306,11 +301,9 @@ public class HazelcastDiscoveryController implements CommandLineRunner {
netCfg
.
setSSLConfig
(
new
SSLConfig
().
setEnabled
(
false
));
// set it all
cfg
.
setNetworkConfig
(
netCfg
);
// run
Hazelcast
.
newHazelcastInstance
(
cfg
);
}
}
```
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