【consul】使用學習html
轉載:http://www.javashuo.com/article/p-btqukkxh-s.html java
一、下載 consulweb
https://www.consul.iospring
二、Window系統使用apache
開發模式啓動,啓動後之前配置會丟失json
consul agent -dev
導出配置bootstrap
consul kv export > kv.json
導入配置服務器
consul kv import @kv.json
三、將 Spring Boot 爲服務註冊到服務代理mybatis
bootstrap.yml 文件app
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 Key / Value < config < application
若是在 config 目錄下有應用名目錄 consul-client 也就是 spring.application.name=consul-client 指定的目錄下data中有相同的配置,就會覆蓋 application 下的配置
pom 依賴
<?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>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>consulclient</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>consulclient</name>
<description>Demo project for Spring Boot</description>
<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>
@EnableDiscoveryClient 在Edgware版本是可選的。
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.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController // @EnableDiscoveryClient 在Edgware版本是可選的 public class ConsulclientApplication { public static void main(String[] args) { SpringApplication.run(ConsulclientApplication.class, args); } @Value("${ycx.info}") private String info; @RequestMapping("/consul") public Object index() { return info; } }
bootstrap.yml 參考配置
spring: application: name: ${project.name} profiles: active: dev cloud: consul: host: dev.consul.ycx port: 8500 config: # enabled爲true表示啓用配置管理功能 enabled: true # watch選項爲配置監視功能,主要監視配置的改變 watch: enabled: true delay: 10000 wait-time: 30 # 表示若是沒有發現配置,是否拋出異常,true爲是,false爲否,當爲false時,consul會打印warn級別的日誌信息 fail-fast: false # 表示使用的配置格式 format: yaml # 服務發現配置 discovery: # 啓用服務發現 enabled: true # 啓用服務註冊 register: true # 服務中止時取消註冊 deregister: true # 表示註冊時使用IP而不是hostname prefer-ip-address: true # 執行監控檢查的頻率 health-check-interval: 15s # 設置健康檢查失敗多長時間後,取消註冊 health-check-critical-timeout: 15s # 健康檢查的路徑 health-check-path: /actuator/health # 服務註冊標識,格式爲:應用名稱+服務器IP+端口 instance-id: ${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}}
Key / Values < 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 # 邏輯已刪除值(默認爲 1) logic-not-delete-value: 1 # 邏輯未刪除值(默認爲 0) feign: client: config: default: connect-timeout: 20000 read-timeout: 20000