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 047a8c6
Show file tree
Hide file tree
Showing 10 changed files with 237 additions and 242 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
Loading

0 comments on commit 047a8c6

Please sign in to comment.