1
2

ミニ生命保険システムを作成 S02

Last updated at Posted at 2023-09-12

関連記事

はじめに

今回はProductを実装します。ただ先にアプリケーションを起動させるだけです。 ビジネスロジックはその後に実装する。

開発環境

  • macOS Ventura
  • Jdk 20
  • IntelliJ IDEA 2023.2 (Community Edition)

Service Discoveryを実装する

Spring Cloudを使うので、Service Discoveryのことも忘れて一緒に実装するわけにはいきません。

このサイトを開く: https://start.spring.io/ Dependenciesを追加します。
Eureka.png
GENERATEボタンを押すとプロジェクトが完成するので、ideaでインポートします。

Directory 構造

application configuration fileをyaml形式に変更した。

├── src
│   ├── main
│   │   ├── java
│   │   │   └── com
│   │   │       └── insurance
│   │   │           └── life
│   │   │               └── eureka
│   │   │                   └── EurekaApplication.java
│   │   └── resources
│   │       └── application.yaml
│   └── test
│       └── java
│           └── com
│               └── insurance
│                   └── life
│                       └── eureka
│                           └── EurekaApplicationTests.java

スタートアップクラス

EurekaApplication
package com.insurance.life.eureka;

@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {

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

}

Configuration File

application.yaml
spring:
  application:
    name: registry
server:
  port: 8761

eureka:
  environment: dev
  instance:
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
  client:
    register-with-eureka: false
    fetch-registry: false
    serviceUrl:
      defaultZone: http://127.0.0.1:8761/eureka/

EurekaApplicationを実行する。

Productを実装する

Product.png

スタートアップクラス

ProductApplication
package com.insurance.life.product;

@SpringBootApplication
@EnableDiscoveryClient
public class ProductApplication {

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

  • テスト用rest apiを追加する
ProductController
package com.insurance.life.product.controller;

@RestController
public class ProductController {

    @GetMapping("/ok")
    public String ok() {
        return "Product is ok   ";
    }
}

Configuration File

application.yaml

server:
  port: 8002

spring:
  application:
    name: product
  datasource:
    url: jdbc:h2:file:./db/product
    username: product
    password: product
  jpa:
    show-sql: true
    hibernate:
      ddl-auto: update
  h2:
    console:
      path: /h2-console
      enabled: true

eureka:
  instance:
    prefer-ip-address: true
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:8761/eureka/

ProductApplicationを実行する。

確認する

  • http://127.0.0.1:8761/ を開き、レジストされている Product サービスを確認する。
    EurekaRun.png

  • テスト用rest apiへのアクセス

% curl http://127.0.0.1:8002/ok
Product is ok%  

Source Code

git.png

終わり

最後まで読んでいただきありがとうございました。Productの初期化が正常に行われました。次回は、引き続き他のアプリケーションを実装していきます。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2