SpringBoot使用Consul注册服务

引入依赖

springBoot版本 : 2.0.3.RELEASE

<properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>Finchley.SR1</spring-cloud.version>
</properties>

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

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

<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>

配置文件中增加配置

server:
    port: 8088

spring:
    application:
        name: cn-consul-test
    cloud:
        consul:
            discovery:
                instance-id: ${spring.application.name}-${spring.cloud.client.ip-address}-${server.port}
                prefer-ip-address: true
            host: 10.130.131.39
            port: 8500

注册相关

每个应用注册consul都会生成或指定一个唯一实例id
默认根据hostname+appName生成实例ID
同一个应用多个节点注册服务时,默认的ID生成规则会相互覆盖
解决服务注册覆盖方法有三种
1.指定实例ID(instance-id),规则中加入随机数:

${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}}

2.指定实例ID(instance-id),规则中加入ip:

${spring.application.name}-${spring.cloud.client.ip-address}-${server.port}

3.自定义实例ID,此方法会在生成实例ID后,再生成自定义实例ID。

import com.ecwid.consul.v1.ConsulClient;
import org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties;
import org.springframework.cloud.consul.discovery.HeartbeatProperties;
import org.springframework.cloud.consul.discovery.TtlScheduler;
import org.springframework.cloud.consul.serviceregistry.ConsulRegistration;
import org.springframework.cloud.consul.serviceregistry.ConsulServiceRegistry;

/**
* <p></p>
*
* @author 0.0
* @since 2020-08-30 11:23
*/
public class MyConsulServiceRegistry extends ConsulServiceRegistry {

   public MyConsulServiceRegistry(ConsulClient client, ConsulDiscoveryProperties properties, TtlScheduler ttlScheduler, HeartbeatProperties heartbeatProperties) {
       super(client, properties, ttlScheduler, heartbeatProperties);
   }

   @Override
   public void register(ConsulRegistration reg) {
       reg.getService().setId(reg.getService().getName() +"-" + reg.getService().getAddress() + ":" + reg.getService().getPort());
       super.register(reg);
   }
}
import com.ecwid.consul.v1.ConsulClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties;
import org.springframework.cloud.consul.discovery.HeartbeatProperties;
import org.springframework.cloud.consul.discovery.TtlScheduler;
import org.springframework.cloud.consul.serviceregistry.ConsulServiceRegistry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* <p></p>
*
* @author 0.0
* @since 2020-08-30 11:26
*/
@Configuration
public class MyConsulServiceRegistryConfig {

   @Bean
   public ConsulServiceRegistry getConsulServiceRegistry(ConsulClient consulClient, ConsulDiscoveryProperties properties,
           HeartbeatProperties heartbeatProperties) {
       return new MyConsulServiceRegistry(consulClient, properties, null, heartbeatProperties);
   }
}

删除consul注册的节点

首先获取节点ID
在这里插入图片描述
生成删除节点的请求:
curl -X PUT http://10.130.131.39:8500/v1/agent/service/deregister/instance-idxxx
如:curl -X PUT http://10.130.131.39:8500/v1/agent/service/deregister/cn-consul-test-DESKTOP-G56EDDL:8088

服务器上执行请求

SpringCloud使用consul做了啥

和zookeeper一样,服务注册是为了动态获取服务提供者并暴露给服务消费者(不同的是zookeeper+dubbo注册的服务更详细),节点动态扩充缩减增加了领用灵活性。

consul作为服务注册中间组件,接收并记录服务提供者信息(注册服务应用名,主机ip+端口等),而服务消费者则从consul获取服务提供者信息动态生成调用接口调用服务,形成基本的微服务调用。

应用使用过程中consul挂了,消费者还能调用服务提供者吗?可以!

consul提供的是存活的服务提供者列表,而消费者调用服务是根据服务提供者列表生成调用接口,本地也会缓存从consul获取到的服务提供者列表,一旦consul挂了,意味着消费者本地的服务提供者列表缓存无法再被更新了

服务提供者和消费者怎么区分的?不注册服务则只能做消费者,注册服务则即时服务提供者,也是服务消费者
spring.cloud.consul.discovery.register=false

猜你喜欢

转载自blog.csdn.net/u012098021/article/details/108464806