Skip to content

Commit 978b05b

Browse files
Merge pull request #1 from starkandwayne/solve-peer-issues
Solve peer issues
2 parents e0d781e + 409a6e3 commit 978b05b

File tree

13 files changed

+664
-11
lines changed

13 files changed

+664
-11
lines changed

pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</parent>
1111
<groupId>com.starkandwayne</groupId>
1212
<artifactId>service-registry</artifactId>
13-
<version>1.0.1-3.1.37</version>
13+
<version>1.2.0-3.1.37</version>
1414
<name>service-registry</name>
1515
<description>Service Registry for Stark And Wayne</description>
1616
<properties>
@@ -38,6 +38,7 @@
3838
<artifactId>spring-boot-starter-test</artifactId>
3939
<scope>test</scope>
4040
</dependency>
41+
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency>
4142
</dependencies>
4243
<dependencyManagement>
4344
<dependencies>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.starkandwayne.serviceregistry;
2+
3+
import org.springframework.core.env.ConfigurableEnvironment;
4+
5+
public class CloudFoundrySessionData {
6+
public String INSTANCE_GUID;
7+
public String INSTANCE_INDEX;
8+
public String INSTANCE_ADDR;
9+
public String INSTANCE_IP;
10+
public String INSTANCE_PORT;
11+
public String INTERNAL_IP;
12+
public String INTERNAL_PORT;
13+
public String INTSTANCE_TYPE;
14+
public String PEERS;
15+
16+
public static CloudFoundrySessionData GetEnvironment() {
17+
CloudFoundrySessionData cfd = new CloudFoundrySessionData();
18+
cfd.INSTANCE_INDEX = System.getenv("CF_INSTANCE_INDEX");
19+
cfd.INSTANCE_GUID = System.getenv("CF_INSTANCE_GUID");
20+
cfd.INSTANCE_ADDR = System.getenv("CF_INSTANCE_ADDR");
21+
cfd.INSTANCE_IP = System.getenv("CF_INSTANCE_IP");
22+
cfd.INSTANCE_PORT = System.getenv("CF_INSTANCE_PORT");
23+
cfd.INTERNAL_IP = System.getenv("CF_INSTANCE_INTERNAL_IP");
24+
cfd.INTERNAL_PORT = System.getenv("VCAP_APP_PORT");
25+
cfd.PEERS = System.getenv("PEERS");
26+
cfd.INTSTANCE_TYPE = System.getenv("PEERING_TYPE");
27+
return cfd;
28+
}
29+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package com.starkandwayne.serviceregistry;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import com.fasterxml.jackson.core.JsonProcessingException;
6+
import com.fasterxml.jackson.databind.ObjectMapper;
7+
import java.net.URI;
8+
import java.util.Objects;
9+
10+
import org.springframework.lang.Nullable;
11+
12+
@JsonIgnoreProperties(
13+
ignoreUnknown = true
14+
)
15+
public class ScsPeerInfo {
16+
private URI uri;
17+
@JsonProperty("nodeCount")
18+
private int count;
19+
@JsonProperty("service-instance-id")
20+
private String serviceInstanceId;
21+
@JsonProperty("issuer")
22+
private URI issuerUri;
23+
@JsonProperty("jwk-set-uri")
24+
private URI jwkSetUri;
25+
26+
public URI getUri() {
27+
return this.uri;
28+
}
29+
30+
public void setUri(URI uri) {
31+
this.uri = uri;
32+
}
33+
34+
public int getCount() {
35+
return this.count;
36+
}
37+
38+
public void setCount(int count) {
39+
this.count = count;
40+
}
41+
42+
public String getServiceInstanceId() {
43+
return this.serviceInstanceId;
44+
}
45+
46+
public void setServiceInstanceId(String serviceInstanceId) {
47+
this.serviceInstanceId = serviceInstanceId;
48+
}
49+
50+
public URI getIssuerUri() {
51+
return this.issuerUri;
52+
}
53+
54+
public void setIssuerUri(URI issuerUri) {
55+
this.issuerUri = issuerUri;
56+
}
57+
58+
public URI getJwkSetUri() {
59+
return this.jwkSetUri;
60+
}
61+
62+
public void setJwkSetUri(URI jwkSetUri) {
63+
this.jwkSetUri = jwkSetUri;
64+
}
65+
66+
public String toString() {
67+
return "ScsPeerInfo{uri=" + this.uri + ", count=" + this.count + ", serviceInstanceId='" + this.serviceInstanceId + '\'' + ", issuerUri=" + this.issuerUri + ", jwkSetUri=" + this.jwkSetUri + '}';
68+
}
69+
70+
public boolean equals(Object o) {
71+
if (this == o) {
72+
return true;
73+
} else if (o != null && this.getClass() == o.getClass()) {
74+
ScsPeerInfo peerInfo = (ScsPeerInfo)o;
75+
if (this.count != peerInfo.count) {
76+
return false;
77+
} else if (!Objects.equals(this.uri, peerInfo.uri)) {
78+
return false;
79+
} else if (!Objects.equals(this.serviceInstanceId, peerInfo.serviceInstanceId)) {
80+
return false;
81+
} else {
82+
return !Objects.equals(this.issuerUri, peerInfo.issuerUri) ? false : Objects.equals(this.jwkSetUri, peerInfo.jwkSetUri);
83+
}
84+
} else {
85+
return false;
86+
}
87+
}
88+
89+
public int hashCode() {
90+
int result = this.uri != null ? this.uri.hashCode() : 0;
91+
result = 31 * result + this.count;
92+
result = 31 * result + (this.serviceInstanceId != null ? this.serviceInstanceId.hashCode() : 0);
93+
result = 31 * result + (this.issuerUri != null ? this.issuerUri.hashCode() : 0);
94+
result = 31 * result + (this.jwkSetUri != null ? this.jwkSetUri.hashCode() : 0);
95+
return result;
96+
}
97+
98+
public static class Converter implements org.springframework.core.convert.converter.Converter<ScsPeerInfo, String> {
99+
private static final ObjectMapper JSON = new ObjectMapper();
100+
101+
public String convert(ScsPeerInfo source) {
102+
try {
103+
return JSON.writeValueAsString(source);
104+
} catch (JsonProcessingException var3) {
105+
throw new RuntimeException(var3);
106+
}
107+
}
108+
}
109+
}
Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,41 @@
11
package com.starkandwayne.serviceregistry;
22

3+
4+
import java.util.LinkedList;
5+
36
import org.springframework.boot.SpringApplication;
47
import org.springframework.boot.autoconfigure.SpringBootApplication;
8+
59
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
10+
import org.springframework.context.ConfigurableApplicationContext;
11+
import org.springframework.context.annotation.Bean;
12+
import org.springframework.web.client.RestTemplate;
13+
614

715
@SpringBootApplication
816
@EnableEurekaServer
917
public class ServiceRegistryApplication {
1018

19+
public static CloudFoundrySessionData SessionData;
20+
public static SpringApplication Application;
21+
private static ConfigurableApplicationContext ApplicationContext;
22+
public static LinkedList<ScsPeerInfo> CurrentLoadedPeers;
23+
24+
public static String ServerPort;
25+
public static String[] CurrentArgs;
26+
1127
public static void main(String[] args) {
12-
SpringApplication.run(ServiceRegistryApplication.class, args);
28+
29+
CurrentArgs = args;
30+
Application = new SpringApplication(ServiceRegistryApplication.class);
31+
ApplicationContext = Application.run(args);
32+
1333
}
1434

35+
36+
@Bean
37+
public RestTemplate restTemplate() {
38+
return new RestTemplate();
39+
}
40+
1541
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.starkandwayne.serviceregistry;
2+
3+
import java.io.IOException;
4+
import java.util.HashMap;
5+
import java.util.LinkedList;
6+
import java.util.List;
7+
import java.util.Map;
8+
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;
13+
import org.springframework.http.MediaType;
14+
import org.springframework.http.ResponseEntity;
15+
import org.springframework.web.bind.annotation.GetMapping;
16+
import org.springframework.web.bind.annotation.PostMapping;
17+
import org.springframework.web.bind.annotation.RequestBody;
18+
import org.springframework.web.bind.annotation.RequestMapping;
19+
import org.springframework.web.bind.annotation.RestController;
20+
21+
import com.fasterxml.jackson.core.JsonProcessingException;
22+
import com.fasterxml.jackson.databind.JsonSerializer;
23+
import com.fasterxml.jackson.databind.ObjectMapper;
24+
25+
@RestController
26+
@RequestMapping("/config")
27+
public class ServiceRegistryController {
28+
29+
@GetMapping(
30+
value = "/session-data",
31+
produces = {MediaType.APPLICATION_JSON_VALUE})
32+
public ResponseEntity<CloudFoundrySessionData> getCfSessionData() {
33+
return ResponseEntity.ok().body(CloudFoundrySessionData.GetEnvironment());
34+
}
35+
36+
@GetMapping(
37+
value = "/peers",
38+
produces = {MediaType.APPLICATION_JSON_VALUE})
39+
public ResponseEntity<LinkedList<ScsPeerInfo>> getPeers() {
40+
return ResponseEntity.ok().body(ServiceRegistryApplication.CurrentLoadedPeers);
41+
}
42+
43+
@PostMapping(
44+
value = "/peers",
45+
consumes = {MediaType.APPLICATION_JSON_VALUE},
46+
produces = {MediaType.APPLICATION_JSON_VALUE})
47+
public ResponseEntity<LinkedList<ScsPeerInfo>> postPeers(@RequestBody LinkedList<ScsPeerInfo> listpeers) {
48+
try
49+
{
50+
ServiceRegistryApplication.CurrentLoadedPeers = listpeers;
51+
System.out.println(ServiceRegistryApplication.CurrentLoadedPeers);
52+
UpdateEnvExcuteProcess(listpeers);
53+
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();
76+
}
77+
78+
79+
}
80+
}

0 commit comments

Comments
 (0)