Consul のインストールと起動
公式サイトから Consul をダウンロードします。
Windows 環境で開発用にエージェントを起動するには以下のコマンドを使用します。このモードでは、再起動時に設定が失われます。
consul agent -dev
キー/バリュー(KV)ストアの内容を JSON ファイルにエクスポートするには:
consul kv export > kv.json
エクスポートした設定を再びインポートするには:
consul kv import @kv.json
Spring Boot アプリケーションのサービス登録
Spring Boot アプリケーションを Consul にサービスとして登録するには、bootstrap.yml に以下のように設定します。
server:
port: 0
spring:
application:
name: consul-client
cloud:
consul:
host: dev.consul.ycx
port: 8500
config:
format: yaml
discovery:
prefer-ip-address: true
health-check-interval: 3s
health-check-path: /actuator/health
instance-id: ${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}}
Consul の KV ストアにおいて、config/アプリ名 配下に YAML 形式の設定を格納すると、デフォルトの application 設定を上書きできます。
Maven 依存関係
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
<groupId>com.example</groupId>
<artifactId>consulclient</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
アプリケーションクラス
Spring Cloud Edgware リリース以降では @EnableDiscoveryClient アノテーションは省略可能です。
package com.example.consulclient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class ConsulclientApplication {
public static void main(String[] args) {
SpringApplication.run(ConsulclientApplication.class, args);
}
@Value("${ycx.info}")
private String info;
@GetMapping("/consul")
public String getInfo() {
return info;
}
}
詳細な bootstrap.yml 設定例
spring:
application:
name: ${project.name}
profiles:
active: dev
cloud:
consul:
host: dev.consul.ycx
port: 8500
config:
enabled: true
watch:
enabled: true
delay: 10000
wait-time: 30
fail-fast: false
format: yaml
discovery:
enabled: true
register: true
deregister: true
prefer-ip-address: true
health-check-interval: 15s
health-check-critical-timeout: 15s
health-check-path: /actuator/health
instance-id: ${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}}
KV ストアの設定例(YAML)
config/consul-client または config/application に以下の内容を登録することで、アプリケーションの設定を動的に管理できます。
server:
port: 0
logging:
level:
root: INFO
spring:
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
default-property-inclusion: non_null
mybatis-plus:
mapper-locations: classpath*:mapper/*.xml
configuration:
auto-mapping-behavior: partial
auto-mapping-unknown-column-behavior: warning
global-config:
db-config:
logic-delete-value: -1
logic-not-delete-value: 1
feign:
client:
config:
default:
connect-timeout: 20000
read-timeout: 20000