Skip to content

Commit

Permalink
fix:fix config refresh.
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyeBeFreeman committed Jun 25, 2024
1 parent a066de2 commit ef0d1e7
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 168 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
Expand All @@ -37,42 +36,42 @@
@SpringBootApplication
public class ProviderApplication {

public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}

// @RefreshScope
@RestController
public static class EchoController {
@RefreshScope
@RestController
public static class EchoController {

private static final Logger LOG = LoggerFactory.getLogger(ProviderApplication.class);
private static final Logger LOG = LoggerFactory.getLogger(ProviderApplication.class);

@Value("${name:}")
private String name;
@Value("${name:}")
private String name;

@Value("${server.port}")
private String port;
@Value("${server.port}")
private String port;

@Value("${spring.cloud.client.ip-address:127.0.0.1}")
private String ip;
@Value("${spring.cloud.client.ip-address:127.0.0.1}")
private String ip;

@GetMapping("/circuitBreak")
public String circuitBreak() {
LOG.info("Quickstart Callee Service [{}:{}] is called right.", ip, port);
return String.format("Quickstart Callee Service [%s:%s] is called right.", ip, port);
}
@GetMapping("/circuitBreak")
public String circuitBreak() {
LOG.info("Quickstart Callee Service [{}:{}] is called right.", ip, port);
return String.format("Quickstart Callee Service [%s:%s] is called right.", ip, port);
}

@GetMapping("/echo/{string}")
public String echo(@PathVariable String string) {
return "Hello, I'm provider, receive msg : "
+ string
+ ", my metadata : "
+ " name config : "
+ name
+ " "
+ port;
}
@GetMapping("/echo/{string}")
public String echo(@PathVariable String string) {
return "Hello, I'm provider, receive msg : "
+ string
+ ", my metadata : "
+ " name config : "
+ name
+ " "
+ port;
}

}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -39,42 +38,42 @@
@SpringBootApplication
public class ProviderApplication {

public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}

// @RefreshScope
@RestController
public static class EchoController {
@RefreshScope
@RestController
public static class EchoController {

private static final Logger LOG = LoggerFactory.getLogger(ProviderApplication.class);
private static final Logger LOG = LoggerFactory.getLogger(ProviderApplication.class);

@Value("${name:}")
private String name;
@Value("${name:}")
private String name;

@Value("${server.port}")
private String port;
@Value("${server.port}")
private String port;

@Value("${spring.cloud.client.ip-address:127.0.0.1}")
private String ip;
@Value("${spring.cloud.client.ip-address:127.0.0.1}")
private String ip;

@GetMapping("/circuitBreak")
public ResponseEntity<String> circuitBreak() throws InterruptedException {
LOG.info("Quickstart Callee Service [{}:{}] is called wrong.", ip, port);
return new ResponseEntity<>("failed for call quickstart callee service.", HttpStatus.BAD_GATEWAY);
}
@GetMapping("/circuitBreak")
public ResponseEntity<String> circuitBreak() throws InterruptedException {
LOG.info("Quickstart Callee Service [{}:{}] is called wrong.", ip, port);
return new ResponseEntity<>("failed for call quickstart callee service.", HttpStatus.BAD_GATEWAY);
}

@GetMapping("/echo/{string}")
public String echo(@PathVariable String string) {
return "Hello, I'm provider, receive msg : "
+ string
+ ", my metadata : "
+ " name config : "
+ name
+ " "
+ port;
}
@GetMapping("/echo/{string}")
public String echo(@PathVariable String string) {
return "Hello, I'm provider, receive msg : "
+ string
+ ", my metadata : "
+ " name config : "
+ name
+ " "
+ port;
}

}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.context.config.annotation.RefreshScope;
Expand All @@ -37,34 +39,35 @@
@SpringBootApplication
public class ConsumerApplication {

public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}

@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}

@RestController
@RefreshScope
public static class EchoController {
@RestController
@RefreshScope
@ImportAutoConfiguration(RefreshAutoConfiguration.class)
public static class EchoController {

private Registration registration;
private Registration registration;

private RestTemplate template;
private RestTemplate template;

@Value("${server.port}")
private int port;
@Value("${server.port}")
private int port;

@Value("${custom.config:none}")
private String customConfig;
@Value("${custom.config:none}")
private String customConfig;

public EchoController(RestTemplate restTemplate, Registration registration) {
this.template = restTemplate;
this.registration = registration;
}
public EchoController(RestTemplate restTemplate, Registration registration) {
this.template = restTemplate;
this.registration = registration;
}

// @GetMapping("/echo/{str}")
// public ResponseEntity<String> rest(@PathVariable String str) {
Expand All @@ -75,18 +78,18 @@ public EchoController(RestTemplate restTemplate, Registration registration) {
// return new ResponseEntity<>(content, HttpStatus.OK);
// }

@GetMapping("/echo/{str}")
public ResponseEntity<String> rest(@PathVariable String str) {
ResponseEntity<String> response = template.getForEntity("http://service-provider-2023/echo/" + str,
String.class);
return response;
}
@GetMapping("/echo/{str}")
public ResponseEntity<String> rest(@PathVariable String str) {
ResponseEntity<String> response = template.getForEntity("http://service-provider-2023/echo/" + str,
String.class);
return response;
}

@GetMapping("/custom/config")
public ResponseEntity<String> getCustomConfig() {
return new ResponseEntity<>(String.valueOf(customConfig), HttpStatus.OK);
}
@GetMapping("/custom/config")
public ResponseEntity<String> getCustomConfig() {
return new ResponseEntity<>(String.valueOf(customConfig), HttpStatus.OK);
}

}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ spring.cloud.nacos.discovery.enabled=true
spring.cloud.nacos.config.enabled=true
spring.cloud.nacos.username=nacos
spring.cloud.nacos.password=nacos

spring.cloud.loadbalancer.nacos.enabled=true
# use feign client in GraalVM environment need to set below config
spring.cloud.refresh.enabled=false
spring.main.allow-circular-references=true
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
server.port=65001
spring.application.name=service-provider-2022
spring.application.name=service-provider-2023
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
spring.cloud.nacos.config.enabled=true
Expand All @@ -9,10 +9,8 @@ spring.cloud.nacos.discovery.enabled=true
#spring.cloud.nacos.discovery.ip-type=IPv4
#only register IPv6 instance
#spring.cloud.nacos.discovery.ip-type=IPv6

spring.cloud.nacos.username=nacos
spring.cloud.nacos.password=nacos

management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
spring.main.allow-circular-references=true
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
Expand All @@ -37,42 +36,42 @@
@SpringBootApplication
public class ProviderApplication {

public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}

// @RefreshScope
@RestController
public static class EchoController {
@RefreshScope
@RestController
public static class EchoController {

private static final Logger LOG = LoggerFactory.getLogger(ProviderApplication.class);
private static final Logger LOG = LoggerFactory.getLogger(ProviderApplication.class);

@Value("${name:}")
private String name;
@Value("${name:}")
private String name;

@Value("${server.port}")
private String port;
@Value("${server.port}")
private String port;

@Value("${spring.cloud.client.ip-address:127.0.0.1}")
private String ip;
@Value("${spring.cloud.client.ip-address:127.0.0.1}")
private String ip;

@GetMapping("/circuitBreak")
public String circuitBreak() {
LOG.info("Quickstart Callee Service [{}:{}] is called right.", ip, port);
return String.format("Quickstart Callee Service [%s:%s] is called right.", ip, port);
}
@GetMapping("/circuitBreak")
public String circuitBreak() {
LOG.info("Quickstart Callee Service [{}:{}] is called right.", ip, port);
return String.format("Quickstart Callee Service [%s:%s] is called right.", ip, port);
}

@GetMapping("/echo/{string}")
public String echo(@PathVariable String string) {
return "Hello, I'm provider, receive msg : "
+ string
+ ", my metadata : "
+ " name config : "
+ name
+ " "
+ port;
}
@GetMapping("/echo/{string}")
public String echo(@PathVariable String string) {
return "Hello, I'm provider, receive msg : "
+ string
+ ", my metadata : "
+ " name config : "
+ name
+ " "
+ port;
}

}
}

}
Loading

0 comments on commit ef0d1e7

Please sign in to comment.