SpringCloud使用Consul作为注册中心

SpringCloud使用Consul作为注册中心

使用的版本:consul_1.9.5

使用的SpringCloud的版本:Dalston.SR1

作者: 王鹏霄

管理依赖版本文件:

 <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

        </dependencies>
    </dependencyManagement>

根据需要引入其对应的项目依赖

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.4.RELEASE</version>
		<relativePath/>
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>1.8</java.version>
	</properties>

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

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

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

consul agent -dev启动consul

使用@EnableDiscoveryClient注解使服务具有服务注册的能力,启动应用,注册日志信息
Registering service with consul: NewService{id='consul-client-2001', name='consul-client', tags=[], address='S883FC7EBTTYTNY22', port=2001, enableTagOverride=null, check=Check{script='null', interval=10s, ttl=null, http=http://S883FC7EBTTYTNY22:2001/health, tcp=null, timeout=null, deregisterCriticalServiceAfter=null, tlsSkipVerify=null}, checks=null}

consul报错信息如下:

HTTP GET http://S883FC7EBTTYTNY22:2001/health: 503  Output: {"status":"DOWN"}

解决: 升级SpringCloud版本为H版, Hoxton.SR3

如果有使用到fegin进行服务调用,需要将fegin替换为openfegin

注册信息如下

Registering service with consul: NewService{id='consul-server-2001', name='consul-server', tags=[secure=false], address='S883FC7EBTTYTNY22', meta=null, port=2001, enableTagOverride=null, check=Check{script='null', interval='10s', ttl='null', http='http://S883FC7EBTTYTNY22:2001/actuator/health', method='null', header={}, tcp='null', timeout='null', deregisterCriticalServiceAfter='null', tlsSkipVerify=null, status='null'}, checks=null}

使用Client可以正常调用Server端

在yml文件中添加下面配置指定不同

spring:
  application:
    name: consul-client
  cloud:
    consul:
      port: 8500
      host: localhost
    discovery:
      heartbeat: true
      instance-id: ${spring.application.name}
      service-name: name-${spring.application.name}
可以验证其方法调用根据 service-name

新建服务端

指定相同 instance-id: ${spring.application.name}

后者服务会取代前者,无法正常进行服务负载

替换如下配置使instance-id 唯一

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

代码地址: wang125631/SpringCloud-Component

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值