Spring Cloud - 21. Spring Cloud Config 연동 Actuactor

목차

참고

Spring Cloud Config Server

Spring Cloud Config Server 의존성 추가

// 외부 Config를 가져오기 위한 Dependency 설정
implementation 'org.springframework.cloud:spring-cloud-config-server'
implementation 'org.springframework.cloud:spring-cloud-starter-bootstrap'
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

@EnableConfigServer 어노테이션을 추가하여 Config Server를 만들어 준다.

@SpringBootApplication
@EnableConfigServer
public class ConfigServiceApplication {

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

}
server:
port: 8888

spring:
application:
name: config-service
cloud:
config:
server:
git:
uri: file:///Users/dongwoo-yang/dev/study/Spring-Cloud/gateway/git-local-repository
default-label: master
spring:
cloud:
config:
uri: http://127.0.0.1:8888
name: ecommerce
@RestController
public class UserController {
private Environment env;
private UserService userService;
private Greeting greeting;

public UserController(Environment env, UserService userService) {
this.env = env;
this.userService = userService;
}

@GetMapping("/health_check")
public String status() {
return String.format("It's Working in User Service"
+ ", Port(local.server.port) = " + env.getProperty("local.server.port") + "\n"
+ ", Port(server.port) = " + env.getProperty("server.port") + "\n"
+ ", token secret = " + env.getProperty("token.secret") + "\n"
+ ", token expiration time = " + env.getProperty("token.expiration_time"));
}

@GetMapping("/welcome")
public String welcome() {
return env.getProperty("greeting.message");
}

@GetMapping("/welcome_value")
public String welcomeValue() {
return greeting.getMessage();
}

@PostMapping("/users")
public ResponseEntity createUser(@RequestBody RequestUser user) {
ModelMapper modelMapper = new ModelMapper();
modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
UserDto userDto = modelMapper.map(user, UserDto.class);
userService.createUser(userDto);

ResponseUser responseUser = modelMapper.map(userDto, ResponseUser.class);
return ResponseEntity.status(HttpStatus.CREATED).body(responseUser);
}

@GetMapping("/users")
public ResponseEntity<List<ResponseUser>> getUsers() {
Iterable<UserEntity> userList = userService.getUserByAll();
List<ResponseUser> result = new ArrayList<>();

userList.forEach(v -> {
result.add(new ModelMapper().map(v, ResponseUser.class));
});

return ResponseEntity.status(HttpStatus.OK).body(result);
}

@GetMapping("/users/{userId}")
public ResponseEntity<ResponseUser> getUsers(@PathVariable("userId") String userId) {
UserDto userDto = userService.getUserByUserId(userId);

ResponseUser returnValue = new ModelMapper().map(userDto, ResponseUser.class);

return ResponseEntity.status(HttpStatus.OK).body(returnValue);
}
}

Spring Cloud Config Client

// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-actuator
implementation 'org.springframework.boot:spring-boot-starter-actuator'
server:
port: 0

spring:
application:
name: user-service
h2:
console:
enabled: true
settings:
web-allow-others: true
datasource:
username: sa
password:
driver-class-name: org.h2.Driver
url: jdbc:h2:mem:testdb

eureka:
instance:
instanceId: ${spring.application.name}:${spring.application.instance_id:${random.value}}
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:8761/eureka

greeting:
message: Welcome to the Simple E-commerce.

management:
endpoints:
web:
exposure:
include: refresh, health, beans

#token:
# expiration_time: 84600000
# secret: user_token
Share