consul Service Center

consul Service Center

The version used

  • server 1.5.0

1 Introduction

Features

  • Registration Service Center - Service Provider
  • Service Configuration Center - pull configuration before the application is started

1.1 springcloud the demo demo

Local installation 1.2 consul docker

docker run -d --rm --name consul -p 8400:8400 -p 8500:8500 -p 8600:53/udp -h node1 progrium/consul -server -bootstrap

2. springcloud registration services

2.1. Adding maven dependence
2.2. Springboot configuration application.yml Registration Service

2.1. Adding maven dependent

<!--consul服务注册功能-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-consul-all</artifactId>
</dependency>
<!--使用config, 解析yml-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

2.2. Springboot configuration bootstrap.yml Registration Service

spring:
    application:
        name: zuul-service
    cloud:
        consul:
            host: 192.168.9.233
            port: 8500
            discovery: # consul 注册中心
                prefer-ip-address: true #  ip 偏好; 这个必须配
                service-name: zuul-service #注册在consul上面的名字,在consul的调用中,是通过此名字调用的
                register-health-check: true #健康检查,保证服务处于启动状态,建议开启
                instance-id: ${spring.application.name}:${spring.cloud.client.ip-address} # 服务id
                health-check-url: http://${spring.cloud.client.ip-address}:${server.port}/actuator/health # 健康检查的URI

2.3 spring-cloud-starter-consul-discovery Detailed configuration properties

Version: spring-cloud-consul-discovery -2.0.0.RELEASE
configuration information categories: org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties
inlet Method: click specific attributes from entering bootstrap.yml idea, attention class inherit properties of the
recommended tips: open the idea of structure panel view
Spring Cloud Consul of official documents

spring:
  cloud:
    consul:
      host: 192.168.9.233 # ip
      port: 8500 # 端口
      discovery: # consul 注册中心
        service-name: zuul-service #注册在consul上面的名字,在consul的调用中,是通过此名字调用的
        register-health-check: true #健康检查,保证服务处于启动状态,建议开启
        instance-id: ${spring.application.name}:${spring.cloud.client.ip-address} # 服务id
        health-check-url: http://${spring.cloud.client.ip-address}:${server.port}/actuator/health # 健康检查的URI
        tags: # 标签,这个属性是个list,所以用yml的list的格式注入
          - gateway
          - common
          - other
          - firstboot
        enabled: true # 服务发现是否启动
        management-tags: # 注册管理服务时使用的标记,默认是management
          - sss
        # health-check-path: /health/check # 要调用以进行运行状况检查的备用服务器路径,已经使用health-check-url覆盖了,所以这属性暂时找不到有什么用处
        health-check-interval: 10s # 字符串,执行运行状况检查的频率(例如10秒),默认为10秒。
        health-check-timeout: 10s # 健康检查超时(例如10秒)。
        health-check-critical-timeout: 30s # 注销关键服务的时间, 要求consul的版本在1.0.7或者更高
        prefer-ip-address: true #  ip 偏好; 这个必须配 在注册过程中使用ip地址而不是主机名
        prefer-agent-address: false # 来源,我们将如何确定使用的地址
        # ip-address: 192.168.9.233 # 访问服务时要使用的IP地址(还必须设置要使用的首选IP地址)
        # hostname: # Hostname to use when accessing server
        # port: 8500 # Port to register the service under (defaults to listening port)
        # management-port: # Port to register the management service under (defaults to management port)
        # catalog-services-watch-delay: 1000 # 查看consul的服务更新间隔,默认1s
        # catalog-services-watch-timeout: 2 # 查看consul服务更新间隔的超时时间, 默认两秒
        query-passing: true # 这推动健康检查 pass 传递到服务器。
        deregister: false # 在consul禁用自动注销登记服务。
        fail-fast: true # *服务登记期间抛出异常如果这是真的,否则,日志警告(默认值为true)。
        # health-check-tls-skip-verify: true # 跳过证书校验, 如果不是则进行证书校验
      config:   # consul 配置中心
        prefix: config
        enabled: true
        format: YAML
        data-key: settings.yml

3. Configure centers (on premise registration services)

3.1. Springboot profile configuration consul configuration center path and the format
3.2 Add and profile springboot configured path on the consul of key / value of the control panel to yaml format
3.3. The configuration stored in the corresponding item in the directory path / line substituting _

3.1. Springboot profile configuration path and configuration center consul of format

spring:
    application:
        name: zuul-service
    cloud:
        consul:
            host: 192.168.9.233
            port: 8500
            discovery: # consul 注册中心
                prefer-ip-address: true #  ip 偏好; 这个必须配
                service-name: zuul-service #注册在consul上面的名字,在consul的调用中,是通过此名字调用的
                register-health-check: true #健康检查,保证服务处于启动状态,建议开启
                instance-id: ${spring.application.name}:${spring.cloud.client.ip-address} # 服务id
                health-check-url: http://${spring.cloud.client.ip-address}:${server.port}/actuator/health # 健康检查的URI
            config: # consul 配置中心
                prefix: config
                enabled: true
                format: YAML
                data-key: settings.yml

3.3. The configuration stored in the corresponding item in the directory path / line substituting _

This is settings.yml, is in the path consul config/<service-name>,<profile>/settings.yml

zuul:
    routes:
        console:
            path: /console/**
            serviceId: console-service
            strip-prefix: false
        order:
            path: /order/**
            serviceId: order-service
            strip-prefix: false
    add-host-header: true
    add-proxy-headers: true
env:
    version: 1.0
    author: suveng

4. springcloud cancellation of service

4.1. Maven remove dependence
4.2. Delete the configuration, application.yml and bootstarp.yml of
4.3. Clear Failure Service consul line

Clear lines of service failure

  1. Delete consul's request interface
# 删除无效服务
http://<server_ip>:<port>/v1/agent/service/deregister/<service_id>  put请求

# 删除无效节点
http://<server_ip>:<port>/v1/agent/force-leave/<node_id>
  1. Configuring the time of registration

After the Spring Cloud Consul application offline, automatically remove invalid health check service
consul delete unwanted services

data

Spring Boot Consul integrated distribution center, equipped with pit, see comment

Published 161 original articles · won praise 140 · views 470 000 +

Guess you like

Origin blog.csdn.net/qq_37933685/article/details/103591848