• 【consul】使用学习


    【consul】使用学习

    转载:https://www.cnblogs.com/yangchongxing/p/10653791.html 

    1、下载 consul

    https://www.consul.io

    2、Window系统使用

    开发模式启动,启动后以前配置会丢失

    consul agent -dev

    导出配置

    consul kv export > kv.json

    导入配置

    consul kv import @kv.json

     3、将 Spring Boot 为服务注册到服务代理

    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 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 
  • 相关阅读:
    Sign APK without putting keystore info in build.gradle
    Sign APK without putting keystore info in build.gradle
    Sketch教程
    Sketch教程
    简要分析unity3d中剪不断理还乱的yield
    简要分析unity3d中剪不断理还乱的yield
    iOS开发系列--地图与定位总结
    iOS开发系列--地图与定位总结
    Launch Screen在iOS7/8中的实现
    linux 命令随笔 ls cd pwd mkdir rm mv cp cat nl
  • 原文地址:https://www.cnblogs.com/yangchongxing/p/10653791.html
Copyright © 2020-2023  润新知