Skip to content

Commit 409a6e3

Browse files
Support for actuator
1 parent c556dcb commit 409a6e3

File tree

6 files changed

+71
-132
lines changed

6 files changed

+71
-132
lines changed

src/main/java/com/starkandwayne/serviceregistry/ServiceRegistryApplication.java

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package com.starkandwayne.serviceregistry;
22

3-
import com.starkandwayne.serviceregistry.peers.Peers;
43

5-
import java.util.Properties;
4+
import java.util.LinkedList;
65

7-
import org.springframework.boot.ApplicationArguments;
86
import org.springframework.boot.SpringApplication;
97
import org.springframework.boot.autoconfigure.SpringBootApplication;
10-
import org.springframework.boot.web.client.RestTemplateBuilder;
8+
119
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
1210
import org.springframework.context.ConfigurableApplicationContext;
1311
import org.springframework.context.annotation.Bean;
@@ -21,7 +19,8 @@ public class ServiceRegistryApplication {
2119
public static CloudFoundrySessionData SessionData;
2220
public static SpringApplication Application;
2321
private static ConfigurableApplicationContext ApplicationContext;
24-
public static Peers CurrentLoadedPeers;
22+
public static LinkedList<ScsPeerInfo> CurrentLoadedPeers;
23+
2524
public static String ServerPort;
2625
public static String[] CurrentArgs;
2726

@@ -33,16 +32,6 @@ public static void main(String[] args) {
3332

3433
}
3534

36-
public static void RestartApplication() {
37-
38-
Thread thread = new Thread(() -> {
39-
ApplicationContext.close();
40-
Application = new SpringApplication(ServiceRegistryApplication.class);
41-
ApplicationContext = Application.run(CurrentArgs);
42-
});
43-
thread.setDaemon(false);
44-
thread.start();
45-
}
4635

4736
@Bean
4837
public RestTemplate restTemplate() {
Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package com.starkandwayne.serviceregistry;
22

3+
import java.io.IOException;
34
import java.util.HashMap;
45
import java.util.LinkedList;
56
import java.util.List;
67
import java.util.Map;
78

9+
import org.springframework.core.env.ConfigurableEnvironment;
10+
import org.springframework.core.env.MapPropertySource;
11+
import org.springframework.core.env.MutablePropertySources;
12+
import org.springframework.core.env.StandardEnvironment;
813
import org.springframework.http.MediaType;
914
import org.springframework.http.ResponseEntity;
1015
import org.springframework.web.bind.annotation.GetMapping;
@@ -13,13 +18,12 @@
1318
import org.springframework.web.bind.annotation.RequestMapping;
1419
import org.springframework.web.bind.annotation.RestController;
1520

21+
import com.fasterxml.jackson.core.JsonProcessingException;
1622
import com.fasterxml.jackson.databind.JsonSerializer;
1723
import com.fasterxml.jackson.databind.ObjectMapper;
18-
import com.starkandwayne.serviceregistry.peers.PeerData;
19-
import com.starkandwayne.serviceregistry.peers.Peers;
2024

2125
@RestController
22-
@RequestMapping("/cf-config")
26+
@RequestMapping("/config")
2327
public class ServiceRegistryController {
2428

2529
@GetMapping(
@@ -32,22 +36,45 @@ public ResponseEntity<CloudFoundrySessionData> getCfSessionData() {
3236
@GetMapping(
3337
value = "/peers",
3438
produces = {MediaType.APPLICATION_JSON_VALUE})
35-
public ResponseEntity<Peers> getPeers() {
39+
public ResponseEntity<LinkedList<ScsPeerInfo>> getPeers() {
3640
return ResponseEntity.ok().body(ServiceRegistryApplication.CurrentLoadedPeers);
3741
}
3842

3943
@PostMapping(
4044
value = "/peers",
4145
consumes = {MediaType.APPLICATION_JSON_VALUE},
4246
produces = {MediaType.APPLICATION_JSON_VALUE})
43-
public ResponseEntity<Peers> postPeers(@RequestBody LinkedList<PeerData> listpeers) {
44-
Peers peers = new Peers();
45-
46-
peers.CurrentPeers = listpeers;
47-
System.out.println(peers.CurrentPeers);
48-
ServiceRegistryApplication.CurrentLoadedPeers = peers;
47+
public ResponseEntity<LinkedList<ScsPeerInfo>> postPeers(@RequestBody LinkedList<ScsPeerInfo> listpeers) {
48+
try
49+
{
50+
ServiceRegistryApplication.CurrentLoadedPeers = listpeers;
4951
System.out.println(ServiceRegistryApplication.CurrentLoadedPeers);
50-
// ServiceRegistryApplication.RestartApplication();
52+
UpdateEnvExcuteProcess(listpeers);
5153
return ResponseEntity.ok().body(ServiceRegistryApplication.CurrentLoadedPeers);
54+
}
55+
catch (Exception ex)
56+
{
57+
return ResponseEntity.badRequest().body(listpeers);
58+
}
59+
60+
61+
}
62+
63+
public void UpdateEnvExcuteProcess(LinkedList<ScsPeerInfo> listpeers) throws IOException {
64+
ObjectMapper objectMapper = new ObjectMapper();
65+
String json;
66+
try {
67+
json = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(listpeers);
68+
ConfigurableEnvironment environment = new StandardEnvironment();
69+
MutablePropertySources propertySources = environment.getPropertySources();
70+
Map<String, Object> myMap = new HashMap<>();
71+
myMap.put("PATH", json);
72+
propertySources.addFirst(new MapPropertySource("MY_MAP", myMap));
73+
} catch (JsonProcessingException e) {
74+
75+
e.printStackTrace();
5276
}
53-
}
77+
78+
79+
}
80+
}

src/main/java/com/starkandwayne/serviceregistry/environment/ExternalPeerInfoCollector.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.fasterxml.jackson.core.type.TypeReference;
44
import com.fasterxml.jackson.databind.ObjectMapper;
55
import com.starkandwayne.serviceregistry.ScsPeerInfo;
6+
import com.starkandwayne.serviceregistry.ServiceRegistryApplication;
7+
68
import java.net.URI;
79
import java.time.Duration;
810
import java.time.temporal.ChronoUnit;
@@ -55,6 +57,14 @@ public ExternalPeerInfoCollector(ConfigurableEnvironment environment, ObjectMapp
5557

5658
public void afterPropertiesSet() throws Exception {
5759
String peersSetByBrokerString = this.environment.getProperty("scs.service-registry.peers");
60+
if (ServiceRegistryApplication.CurrentLoadedPeers != null)
61+
if ( ServiceRegistryApplication.CurrentLoadedPeers.size() > 0) {
62+
ServiceRegistryApplication.CurrentLoadedPeers.forEach((peer) -> {
63+
this.peerInfoCompletionService.submit(() -> {
64+
return peer;
65+
});
66+
});
67+
}
5868
if (!StringUtils.isEmpty(peersSetByBrokerString)) {
5969
List<PeerConfigUserInput> peersSetByBroker = this.objectMapper.readValue(peersSetByBrokerString, new TypeReference<List<PeerConfigUserInput>>(){});
6070
this.unresolvedPeerCount.set(peersSetByBroker.size());

src/main/java/com/starkandwayne/serviceregistry/peers/PeerData.java

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/main/java/com/starkandwayne/serviceregistry/peers/Peers.java

Lines changed: 0 additions & 88 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
eureka:
2+
instance:
3+
hostname: ${vcap.application.uris[0]}
4+
secure-port: 443
5+
secure-port-enabled: true
6+
non-secure-port-enabled: false
7+
8+
# FIXME: remove the following two settings when this issue is resolved and released: https://github.com/spring-cloud/spring-cloud-netflix/issues/3655
9+
secure-health-check-url: https://${eureka.instance.hostname}/actuator/health
10+
status-page-url: https://${eureka.instance.hostname}/actuator/info
11+
client:
12+
# Get the new eureka server to fetch registry from the existing one at
13+
# startup, so the new eureka can start "hot" and won't enforce a warmup
14+
# period.
15+
fetch-registry: true
16+
server:
17+
# try to sync up with the existing eureka for 0-downtime upgrades
18+
registry-sync-retries: 1

0 commit comments

Comments
 (0)