​


一 简介

​微服务注册中心:Consul——概念与基础操作​​介绍了consul的安装和基本操作,本篇开始在consul上进行服务注册与发现,语言使用Java,框架使用Spring Boot整合Consul。

二 Spring Boot整合Consul

通常demo比较好找,导入后观察需要引入哪些依赖,然后启动。最多微调版本和配置问题即可。

2.1 网上demo

打脸来的如此迅速,百度上搜了一堆,最后发现都是官方文档的翻译,版本不清,代码不全,尝试几个未果。再次感叹现在很多文章的质量。

2.2 官方文档

不得已还是先尝试查看官方文档,​​Spring Cloud Consul​​是基于3.0.2版本,给出的集成demo。不过很遗憾,官方给出的sample地址,访问一直是404状态。

微服务注册中心:Consul——服务注册_spring

上面Consul Sample的查看结果:

微服务注册中心:Consul——服务注册_spring_02

2.3 spring-cloud的github

考虑下一种渠道,寻找官方​​github​​。官方的pom.xml配置建议如下:

<project>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>{spring-boot-version}</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

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

事实上,springboot的版本选择过程中也遇到不少问题,几个典型的错误如下(为了节省空间,只截取了关键错误信息):

2.3.1 启动报错信息1

Description:

An attempt was made to call a method that does not exist. The attempt was made from the following location:

org.springframework.cloud.client.discovery.health.DiscoveryCompositeHealthIndicator.<init>(DiscoveryCompositeHealthIndicator.java:41)

The following method did not exist:

org.springframework.boot.actuate.health.CompositeHealthIndicator.<init>(Lorg/springframework/boot/actuate/health/HealthAggregator;)V

The method's class, org.springframework.boot.actuate.health.CompositeHealthIndicator, is available from the following locations:

jar:file:/Users/lijingyong/.m2/repository/org/springframework/boot/spring-boot-actuator/2.2.6.RELEASE/spring-boot-actuator-2.2.6.RELEASE.jar!/org/springframework/boot/actuate/health/CompositeHealthIndicator.class

It was loaded from the following location:

file:/Users/lijingyong/.m2/repository/org/springframework/boot/spring-boot-actuator/2.2.6.RELEASE/spring-boot-actuator-2.2.6.RELEASE.jar


Action:

Correct the classpath of your application so that it contains a single, compatible version of org.springframework.boot.actuate.health.CompositeHealthIndicator


Process finished with exit code 1


2.3.2 报错2

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cloud.consul.serviceregistry.ConsulAutoRegistration]: Factory method 'consulRegistration' threw exception; nested exception is java.lang.IllegalArgumentException: Consul service ids must not be empty, must start with a letter, end with a letter or digit, and have as interior characters only letters, digits, and hyphen: null
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:651) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
... 96 common frames omitted
Caused by: java.lang.IllegalArgumentException: Consul service ids must not be empty, must start with a letter, end with a letter or digit, and have as interior characters only letters, digits, and hyphen: null
at org.springframework.cloud.consul.serviceregistry.ConsulAutoRegistration.normalizeForDns(ConsulAutoRegistration.java:178) ~[spring-cloud-consul-discovery-2.2.1.RELEASE.jar:2.2.1.RELEASE]

2.4 一个可用demo

根据官方说明,并参考另一个demo:https://github.com/RobbieXie/springboot-consul 后,整理如下,不过是使用的较旧版本springboot,可以根据需要更新到适合的版本信息。

关键配置和代码:

2.4.1 pom.xml

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

<groupId>org.example</groupId>
<artifactId>springboot-consul</artifactId>
<description>Spring Boot整合consul示例</description>
<version>1.0-SNAPSHOT</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

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

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

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

<dependency>
<groupId>com.orbitz.consul</groupId>
<artifactId>consul-client</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.ecwid.consul</groupId>
<artifactId>consul-api</artifactId>
<version>1.3.0</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>bintray</name>
<url>http://jcenter.bintray.com</url>
</repository>
</repositories>
</project>

2.4.2 应用启动类 SpringBootConsulApplication

package com.flamingskys.learn.springboot.consul;

import com.flamingskys.learn.springboot.consul.service.ConsulService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import javax.annotation.PreDestroy;

@SpringBootApplication
@EnableAutoConfiguration
public class SpringBootConsulApplication {

@Value("${consul.instanceId}")
private String instanceId;

@Bean
public String getInstanceId(){
return instanceId;
}

@Autowired
private ConsulService consulService;

public static void main(String[] args) {
SpringApplication.run(SpringBootConsulApplication.class, args);
}

@PreDestroy
public void beforeExit(){
System.out.println("-----------------------------BEFORE EXIT--------------------------------");
consulService.deregisterService(instanceId);
}
}

2.4.3 资源配置application.properties

spring.application.name=first-consul-client

consul.host=127.0.0.1
consul.port=8500
consul.instanceId=${spring.application.name}:${spring.application.instanceid:${random.value}}

spring.cloud.consul.discovery.heartbeat.enabled=true


应用启动后,查看console上注册的服务列表,first-consul-client就是我们的服务:

微服务注册中心:Consul——服务注册_spring boot_03


完整代码已上传到了​​gitee​​。可关注公众号:程序员架构进阶 随时获取更新。