(4) SpringBoot 服务注册 (Consul)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liuxiaoxiaosmile/article/details/82970870

1. 依赖

1)提供健康检查,度量等接口,比如/info,/health等

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

2)提供服务注册和发现功能

<dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>

2. 建立bootstrap.yml配置文件(经测试,consul相关配置必须在bootstrap.yml中),内容如下

spring:
  cloud:
    consul:
       host: 10.40.66.144
       port: 80
       discovery:
         tags: v1
         healthCheckPath: /${spring.application.name}/info
         healthCheckInterval: 15s
         instanceId: ${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}}


#健康检查info返回内容,默认空对象{}
info:
  app:
    name: ${spring.application.name}
    port: ${server.port}

#spring.cloud.consul.discovery.enabled=false      关闭服务发现
#spring.cloud.consul.discovery.register=false     关闭服务注册
#management.health.consul.enabled=false           关闭健康检查
#${spring.application.name}  服务名称
#${server.port} 服务端口
#instanceId: ${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}}
application.yml
server:
  port: 8080
  sessionTimeout: 30
  contextPath: /${spring.application.name}/        #服务访问根路径,即服务名称,必须以/.../两个斜杠开头和结尾

spring:
  application:
    name: example                                  #服务名称
    type: ethan-example
  profiles:
      active: prod

3. 本实例说明

服务启动后即自动向consul地址:10.40.66.144:80注册服务,服务名称为example,端口为8080,实例名称为格式类似 example-13be030adf4a6bb498d967830bc7ee05,并通过http://service-ip:service-port/example/info健康检查。

猜你喜欢

转载自blog.csdn.net/liuxiaoxiaosmile/article/details/82970870