windows下安装consul、springboot整合consul

Spring Cloud Consul通过自动配置和绑定到Spring Environment和其他Spring编程模型习语,为Spring Boot应用程序提供Consul集成。通过一些简单的注解,可以快速启用和配置应用程序内的常用模式,并使用Hashicorp的Consul构建大型分布式系统。提供的模式包括服务发现、分布式配置和控制总线。

接下来这篇文章就介绍一下怎么使用consul来搭建一个集成了注册中心和配置中心服务,类似于nacos:nacos作为注册中心和配置中心

目录

1、初步学习并安装consul

第一步:进入spring官网:https://spring.io/

第二步:依次点击Projects >> Spring Cloud

第三步:找到并点击spring coud consul

第四步:安装consul

第五步、访问consul控制台

2、springboot整合consul

一、注册到consul

二、从consul拉取配置


1、初步学习并安装consul

第一步:进入spring官网:https://spring.io/

第二步:依次点击Projects >> Spring Cloud

第三步:找到并点击spring coud consul

然后看一下consul的特性

- 服务注册与发现

- 支持ribbon客户端负载均衡器

- 支持zuul网关服务

- 通过key/value存储实现分布式配置

- 控制总线

Spring Cloud Consul features:

  • Service Discovery: instances can be registered with the Consul agent and clients can discover the instances using Spring-managed beans

  • Supports Ribbon, the client side load-balancer via Spring Cloud Netflix

  • Supports Spring Cloud LoadBalancer - a client side load-balancer provided by the Spring Cloud project

  • Supports Zuul, a dynamic router and filter via Spring Cloud Netflix

  • Distributed Configuration: using the Consul Key/Value store

  • Control Bus: Distributed control events using Consul Events

第四步:安装consul

要使用consul,首先要先安装consul,安装consul很简单,下载consul.exe然后在命令窗口运行起来。

上一张图片的页面拉到最下面,点击蓝色链接进入consul官网

在consul官网的页面,找到并点击install。

点击install后跳转到的consul安装的页面

切换到windows,然后找到Binary download for Windows下面的download链接,点击下载consul。

下载完成后,会得到一个压缩文件,里面只有一个.exe的可运行文件(果然如传说的一样ovo)。

解压这个压缩文件,把解压得到的sonsul.exe复制到D盘program目录下,然后在当前目录的地址输入cmd打开命令窗口,输入以下命令之一。

consul agent -dev # 以开发模式运行consul agent -server # 以服务器模式运行

第五步、访问consul控制台

然后在浏览器的地址栏输入localhost:8500,即可访问consul的控制台了。

前面的consul特性的特性有一条,通过key/value存储来实现分布式配置,我们点击一下左边菜单栏的Key/Value,然后点击右上角的create按钮。

然后我们看到

存储配置文件时,其实这里的key就是我们的配置文件的文件名,当然也可以存字符串和其他的数据。

2、springboot整合consul

好了,接下来进入这篇文章最重要的部分,springboot整合consul

首先,创建一个springboot项目,就叫consul

一、注册到consul

修改application.yml配置文件

spring:application:name: consulcloud :consul :port: 8500host: localhostdiscovery:enabled: trueregister: truehostname: localhosthealth-check-critical-timeout: 30sservice-name: ${spring.application.name}instance-id: ${spring.application.name}:${server.port}
server:port: 8080

注意,这里不能设置profiles,否则启动时会报错

spring:profiles: dev # 这个代码加了会报错application:name: consulcloud :consul :port: 8500host: localhostdiscovery:enabled: trueregister: truehostname: localhosthealth-check-critical-timeout: 30sservice-name: ${spring.application.name}# instance-id: ${spring.application.name}:${server.port}
server:port: 8080
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jmxMBeanExporter' defined in class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/jmx/JmxEndpointAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.endpoint.jmx.JmxEndpointExporter]: Factory method 'jmxMBeanExporter' threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.cloud.client.serviceregistry.ServiceRegistryAutoConfiguration$ServiceRegistryEndpointConfiguration': Unsatisfied dependency expressed through field 'registration'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'consulRegistration' defined in class path resource [org/springframework/cloud/consul/serviceregistry/ConsulAutoServiceRegistrationAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is 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: nullat org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:655) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:635) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1336) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1176) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:556) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:897) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143) ~[spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) [spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) [spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) [spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]at com.example.consul.ConsulApplication.main(ConsulApplication.java:18) [classes/:na]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.endpoint.jmx.JmxEndpointExporter]: Factory method 'jmxMBeanExporter' threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.cloud.client.serviceregistry.ServiceRegistryAutoConfiguration$ServiceRegistryEndpointConfiguration': Unsatisfied dependency expressed through field 'registration'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'consulRegistration' defined in class path resource [org/springframework/cloud/consul/serviceregistry/ConsulAutoServiceRegistrationAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is 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: nullat org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:650) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]... 20 common frames omitted
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.cloud.client.serviceregistry.ServiceRegistryAutoConfiguration$ServiceRegistryEndpointConfiguration': Unsatisfied dependency expressed through field 'registration'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'consulRegistration' defined in class path resource [org/springframework/cloud/consul/serviceregistry/ConsulAutoServiceRegistrationAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is 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: nullat org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1420) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:408) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1336) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1176) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:556) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1109) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]at org.springframework.boot.actuate.endpoint.annotation.EndpointDiscoverer.lambda$createEndpointBean$1(EndpointDiscoverer.java:145) ~[spring-boot-actuator-2.3.2.RELEASE.jar:2.3.2.RELEASE]at org.springframework.boot.actuate.endpoint.annotation.EndpointDiscoverer$EndpointBean.getBean(EndpointDiscoverer.java:469) ~[spring-boot-actuator-2.3.2.RELEASE.jar:2.3.2.RELEASE]at org.springframework.boot.actuate.endpoint.annotation.EndpointDiscoverer.getFilterEndpoint(EndpointDiscoverer.java:329) ~[spring-boot-actuator-2.3.2.RELEASE.jar:2.3.2.RELEASE]at org.springframework.boot.actuate.endpoint.annotation.EndpointDiscoverer.isFilterMatch(EndpointDiscoverer.java:317) ~[spring-boot-actuator-2.3.2.RELEASE.jar:2.3.2.RELEASE]at org.springframework.boot.actuate.endpoint.annotation.EndpointDiscoverer.isEndpointFiltered(EndpointDiscoverer.java:292) ~[spring-boot-actuator-2.3.2.RELEASE.jar:2.3.2.RELEASE]at org.springframework.boot.actuate.endpoint.annotation.EndpointDiscoverer.isEndpointExposed(EndpointDiscoverer.java:265) ~[spring-boot-actuator-2.3.2.RELEASE.jar:2.3.2.RELEASE]at org.springframework.boot.actuate.endpoint.annotation.EndpointDiscoverer.convertToEndpoints(EndpointDiscoverer.java:181) ~[spring-boot-actuator-2.3.2.RELEASE.jar:2.3.2.RELEASE]at org.springframework.boot.actuate.endpoint.annotation.EndpointDiscoverer.discoverEndpoints(EndpointDiscoverer.java:125) ~[spring-boot-actuator-2.3.2.RELEASE.jar:2.3.2.RELEASE]at org.springframework.boot.actuate.endpoint.annotation.EndpointDiscoverer.getEndpoints(EndpointDiscoverer.java:117) ~[spring-boot-actuator-2.3.2.RELEASE.jar:2.3.2.RELEASE]at org.springframework.boot.actuate.autoconfigure.endpoint.jmx.JmxEndpointAutoConfiguration.jmxMBeanExporter(JmxEndpointAutoConfiguration.java:95) ~[spring-boot-actuator-autoconfigure-2.3.2.RELEASE.jar:2.3.2.RELEASE]at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_152]at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_152]at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_152]at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_152]at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]... 21 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'consulRegistration' defined in class path resource [org/springframework/cloud/consul/serviceregistry/ConsulAutoServiceRegistrationAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is 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: nullat org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:655) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:635) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1336) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1176) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:556) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1307) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1227) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]... 55 common frames omitted
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: nullat org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:650) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]... 68 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: nullat org.springframework.cloud.consul.serviceregistry.ConsulAutoRegistration.normalizeForDns(ConsulAutoRegistration.java:185) ~[spring-cloud-consul-discovery-2.2.8.RELEASE.jar:2.2.8.RELEASE]at org.springframework.cloud.consul.serviceregistry.ConsulAutoRegistration.getInstanceId(ConsulAutoRegistration.java:176) ~[spring-cloud-consul-discovery-2.2.8.RELEASE.jar:2.2.8.RELEASE]at org.springframework.cloud.consul.serviceregistry.ConsulAutoRegistration.registration(ConsulAutoRegistration.java:87) ~[spring-cloud-consul-discovery-2.2.8.RELEASE.jar:2.2.8.RELEASE]at org.springframework.cloud.consul.serviceregistry.ConsulAutoServiceRegistrationAutoConfiguration.consulRegistration(ConsulAutoServiceRegistrationAutoConfiguration.java:82) ~[spring-cloud-consul-discovery-2.2.8.RELEASE.jar:2.2.8.RELEASE]at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_152]at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_152]at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_152]at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_152]at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]... 69 common frames omitted

通过debug发现最后获取到的instanceId为null,这个问题找了半天才发现原因的,注意避坑!

 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

最后启动项目,访问consul发现注册到了consul上,并且服务名为${spring.application.name}-${server.port},这也是不设置instance-id时默认的服务名。

二、从consul拉取配置

上面已经让我们的项目注册到consul了,接下来修改一下配置文件,从consul拉取配置。

spring:application:name: consulcloud :consul :port: 8500host: localhostdiscovery:enabled: trueregister: truehostname: localhosthealth-check-critical-timeout: 30sservice-name: ${spring.application.name}config:enabled: trueprefix: config # 配置文件所在文件夹,默认值就是configname: consul-dev.yaml # 配置文件名,也就是keyserver:port: 8080

接着,在consul里创建一个config文件夹,在config文件夹下面创建一个consul-dev.yaml

user:name: heyunlin

注意,文件夹和文件的区别在于key的结尾有没有/

 最后,创建一个控制器,获取配置文件consul-dev.yaml中的配置

package com.example.consul.controller;import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** @author heyunlin* @version 1.0*/
@RestController
@RequestMapping(path = "/config")
public class ConfigController {@Value("${user.name}")String username;@GetMapping("/username")public String getPort() {return username;}}

重启项目,访问http://localhost:8080/config/username发现成功获取并返回了字符串heyunlin

至此,springboot整合consul就算完成了。文章涉及的代码已经上传到git,按需获取

consul使用案例https://gitee.com/he-yunlin/consul.git

好了,文章就分享到这里了,看完不要忘了点赞+收藏哦~

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/4839.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

检测到错误页面web应用服务器版本信息泄露

详细描述 Web服务器未能正确处理异常请求导致Web服务器版本信息泄露,攻击者收集到服务器信息后可进行进一步针对性攻击。 解决办法 临时修复建议如下: 1、关闭web服务器错误提示。 2、关闭运行平台的错误提示。 3、建立错误机制,不要把真实…

MySQL八股学习总览-from 小林coding

MySQL八股学习总览-from 小林coding MySql执行流程连接MySQL服务器查询缓存解析SQL执行SQL预处理器优化器执行器 MySql执行流程 连接MySQL服务器 经过如下的命令,就可以与MySQL服务器建立起连接,三次握手 mysql -h$ip -u$user -p服务端查询多少个客户端连接 show processlis…

干货分享:商城系统开发方式

商城系统是一种为了满足电子商务需求而开发的系统,它能够实现在线购物、支付、订单管理等功能。在当今互联网时代,商城系统的开发方式多种多样。那么,商城系统开发方式有哪些呢? 1、完全独立自主开发 完全独立自主开发是指企业根…

【C++】仿函数(less)

C中的仿函数 class Solution { public:struct cmp{bool operator()(const pair<string,int>&kv1,const pair<string,int>&kv2){if(kv1.second<kv2.second) return true;if(kv1.secondkv2.second&&kv1.first>kv2.first) return true;return …

TCP/IP详解

目录 一、OSI参考模型 1.图示 2.OSI七层模型各自作用 3.七层通信过程 二、IP协议 1.IPv4首部 2.IPv6首部 三、TCP协议 1.tcp首部格式 2.握手挥手图示 3.握手流程 4.为什么要三次握手&#xff1f; 5.四次挥手流程 6.为什么要四次分手&#xff1f; 7.为什么要等待…

后端Long类型传到前端精度丢失的问题

问题出现&#xff1a;后端的Java Bean的id属性是用的Long类型对应数据库主键使用bigint类型&#xff0c;当使用JSON方式传递该数据给前端时&#xff0c;前端接收到的数据末尾会变成0。&#xff08;发生的精度丢失问题&#xff09; 问题原因&#xff1a;Java中的long能表示的范围…

数据排布与跨距对齐

1 数据排布 1.1 数据排布的概念 在深度学习框架中&#xff0c;特征图通常以四维数组的形式呈现&#xff0c;这四个维度分别是&#xff1a;批量大小N&#xff0c;特征图通道数C&#xff0c;特征图高度H&#xff0c;特征图宽度W。数据排布&#xff08;Layout&#xff09;指的就…

http1.0、http1.1 http 2.0

HTTP/1.0是无状态、无连接的应用层协议。 无连接 无连接&#xff1a;每次请求都要建立连接&#xff0c;需要使用 keep-alive 参数建立长连接、HTTP1.1默认长连接keep-alive   无法复用连接&#xff0c;每次发送请求都要进行TCP连接&#xff0c;TCP的连接释放都比较费事&…

【优选算法题练习】day6

文章目录 一、76. 最小覆盖子串1.题目简介2.解题思路3.代码4.运行结果 二、704. 二分查找1.题目简介2.解题思路3.代码4.运行结果 三、34. 在排序数组中查找元素的第一个和最后一个位置1.题目简介2.解题思路3.代码4.运行结果 总结 一、76. 最小覆盖子串 1.题目简介 76. 最小覆…

魔术之舞:用Python编织无懈可击的WebUI自动化测试奇迹

文末附有精心准备的WebUI自动化测试30道面试题链接~ 一、引言 A. 引入WebUI自动化测试的重要性和挑战 Web应用程序的快速发展和普及使得Web用户界面&#xff08;WebUI&#xff09;自动化测试变得异常重要。随着Web应用程序的复杂性和功能需求的增加&#xff0c;传统的手动测试…

2023秋招,网络安全面试题

Hello&#xff0c;各位小伙伴&#xff0c;我作为一名网络安全工程师曾经在秋招中斩获&#x1f51f;个offer&#x1f33c;&#xff0c;并在国内知名互联网公司任职过的职场老油条&#xff0c;希望可以将我的面试的网络安全大厂面试题和好运分享给大家~ 转眼2023年秋招已经到了金…

04.MySQL——用户管理

用户管理 用户管理的价值 用户 用户信息 MySQL中的用户&#xff0c;都存储在系统数据库mysql的user表中 use mysql;select host,user,authentication_string from user;host&#xff1a; 表示这个用户可以从哪个主机登陆&#xff0c;如果是localhost&#xff0c;表示只能从…

Qt 获得QTableview所选中的行的某一列数据

1、点击QtableView控件-》右键-》跳到槽-》选择 2、编写槽函数信息 void XXX::on_tableView_CalTable_clicked(const QModelIndex &index) {int rowindex.row();//获得当前行索引int colindex.column();//获得当前列索引QModelIndex index1 CalViewModel->index(row,2)…

Enterprise:通过 App search 摄入数据

App Search 是 Elastic Enterprise Search 的一部分&#xff0c;Elastic Enterprise Search 是由 Elasticsearch 提供支持的内容搜索工具集合。 最初由 App Search 引入的一些功能&#xff08;例如网络爬虫&#xff09;现在可以直接通过企业搜索使用。 将这些功能与其他企业搜…

SpringCloud系列:负载均衡组件-Ribbon

作者平台&#xff1a; | CSDN&#xff1a;blog.csdn.net/qq_41153943 | 掘金&#xff1a;juejin.cn/user/651387… | 知乎&#xff1a;www.zhihu.com/people/1024… | GitHub&#xff1a;github.com/JiangXia-10… 本文一共4529字&#xff0c;预计阅读12分钟 前言 前面几…

idea 有时提示找不到类或者符号,日志报java: 找不到符号的解决

解决一&#xff1a; idea maven编译成功&#xff0c;运行失败提示找不到符号&#xff0c;主要是get和set方法找不到符号&#xff0c;此时就是idea的lombok版本冲突 IDEA版本导致的Lombok失效&#xff0c;需要更新lombok版本到1.18.14及之后版本得到解决 <dependency>&…

科技云报道:数字化转型完成后,制造业如何走向“数智”时代?

科技云报道原创。 随着我国数字化转型行动的深入推进和智能制造工程的大力实施&#xff0c;制造业正朝着“数智”时代迈进&#xff0c;生成式AI被视为推动制造业智能化发展的关键驱动力。 据预测&#xff0c;到2027年&#xff0c;将有30%的制造业采用生成式AI来提升产品研发效…

【C++修炼之路】类和对象(下)—— 完结篇

&#x1f451;作者主页&#xff1a;安 度 因 &#x1f3e0;学习社区&#xff1a;StackFrame &#x1f4d6;专栏链接&#xff1a;C修炼之路 文章目录 一、再谈构造函数1、初始化列表2、explicit 关键字 二、static 成员1、概念2、特性 三、友元1、友元函数2、友元类 四、内部类五…

zabbix监控linux主机、监控windows10主机

目录 一、环境准备 1、关闭防火墙 2、准备三台服务器、添加主机声明 3、修改主机名 4、此篇接着上一篇zabbix监控自己的环境下操作&#xff0c;server&#xff08;192.168.147.135&#xff09;已经配置好 二、源码安装zabbix 1、下载包、安装依赖包、联网同步清华时间 2…

十分钟让你了解 Linux ABI

getline() 提供了一种更灵活的方法&#xff0c;可以在不破坏系统的情况下将用户数据读入程序。 在 C 语言中读取字符串是一件非常危险的事情。当读取用户输入时&#xff0c;程序员可能会尝试使用 C 标准库中的 gets 函数。它的用法非常简单&#xff1a; char *gets(char *stri…