MSAユーザサービスの作成[1]プロジェクトの作成とh 2のバインド


🔨ユーザー・サービス・アイテムの作成
@SpringBootApplication
@EnableDiscoveryClient
public class UserServiceApplication {

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

}
プロジェクトが作成されると、@EnableDiscoveryClientによってEurekaクライアントとして登録されます.
server:
  port: 0 #랜덤으로 포트 설정

spring:
  application:
    name: user-service  #Eureka에 등록되는 서비스 이름
eureka:
  instance:
    instance-id: ${spring.application.name}:${spring.application.instance_id:${random.value}}  #포트가 중복으로 설정되어 구분하기 위한 인스턴스 아이디 값 설정
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://127.0.0.1:8761/eureka
ymlで設定します.
🔨健康診断コントローラの作成
@RestController
@RequestMapping("/")
public class UserController {

    @GetMapping("/health_check")
    public String status(){
        return "It's Working in User Service";
    }
}
サービスステータスをチェックするための健康診断URLを作成します.

サービスを実行することで、Eurekaでサービスを確認できます.

実際の健康診断も確認できます.
🔨yml設定データのインポート
server:
  port: 0 #랜덤으로 포트 설정

spring:
  application:
    name: user-service  #Eureka에 등록되는 서비스 이름
eureka:
  instance:
    instance-id: ${spring.application.name}:${spring.application.instance_id:${random.value}}  #포트가 중복으로 설정되어 구분하기 위한 인스턴스 아이디 값 설정
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://127.0.0.1:8761/eureka

greeting:
  message: Welcome to the MSA
ymlの設定に挨拶文を追加します.
@RestController
@RequestMapping("/")
@RequiredArgsConstructor
public class UserController {

    private final Environment env;

    ...

    @GetMapping("/welcome")
    public String welcome(){
        return env.getProperty("greeting.message");
    }
}
これはymlに指定した値をenvにインポートする方法です.

@Value()のインポート方法
@Component
@Data
public class Greeting {
    @Value("${greeting.message}")
    private String message;
}
ymlで設定した内容をGreeting classとして作成し、@Componentでbeanに登録し、@Valueで値を入力します.
@RestController
@RequestMapping("/")
@RequiredArgsConstructor
public class UserController {

    private final Environment env;
    private final Greeting greeting;

    ...

    @GetMapping("/welcome")
    public String welcome(){
        //return env.getProperty("greeting.message");
        return greeting.getMessage();
    }
}
メッセージは、以前のenvを使用することなく、オブジェクト自体に直接ロードできます.

同じ作業であることを確認できます.
h 2連動
<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>1.3.176</version>
    <scope>runtime</scope>
</dependency>
h 2の追加
h 2の政策変更は、1.3.176で行い、迅速に行うことができる.
server:
  port: 0 #랜덤으로 포트 설정

spring:
  application:
    name: user-service  #Eureka에 등록되는 서비스 이름
  h2:
    console:
      enabled: true
      settings:
        web-allow-others: true
      path: /h2-console
...
ymlファイルにh 2設定を追加します.


正常に動作していることを確認できます.