国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

windows下安裝consul、springboot整合consul

這篇具有很好參考價(jià)值的文章主要介紹了windows下安裝consul、springboot整合consul。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

Spring Cloud Consul通過自動(dòng)配置和綁定到Spring Environment和其他Spring編程模型習(xí)語,為Spring Boot應(yīng)用程序提供Consul集成。通過一些簡單的注解,可以快速啟用和配置應(yīng)用程序內(nèi)的常用模式,并使用Hashicorp的Consul構(gòu)建大型分布式系統(tǒng)。提供的模式包括服務(wù)發(fā)現(xiàn)、分布式配置和控制總線。

接下來這篇文章就介紹一下怎么使用consul來搭建一個(gè)集成了注冊中心和配置中心服務(wù),類似于nacos:nacos作為注冊中心和配置中心

目錄

1、初步學(xué)習(xí)并安裝consul

第一步:進(jìn)入spring官網(wǎng):https://spring.io/

第二步:依次點(diǎn)擊Projects >> Spring Cloud

第三步:找到并點(diǎn)擊spring coud consul

第四步:安裝consul

第五步、訪問consul控制臺(tái)

2、springboot整合consul

一、注冊到consul

二、從consul拉取配置


1、初步學(xué)習(xí)并安裝consul

第一步:進(jìn)入spring官網(wǎng):https://spring.io/

第二步:依次點(diǎn)擊Projects >> Spring Cloud

windows下安裝consul、springboot整合consul,consul,spring boot

第三步:找到并點(diǎn)擊spring coud consul

windows下安裝consul、springboot整合consul,consul,spring boot

然后看一下consul的特性

- 服務(wù)注冊與發(fā)現(xiàn)

- 支持ribbon客戶端負(fù)載均衡器

- 支持zuul網(wǎng)關(guān)服務(wù)

- 通過key/value存儲(chǔ)實(shí)現(xiàn)分布式配置

- 控制總線

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然后在命令窗口運(yùn)行起來。

上一張圖片的頁面拉到最下面,點(diǎn)擊藍(lán)色鏈接進(jìn)入consul官網(wǎng)

windows下安裝consul、springboot整合consul,consul,spring boot

在consul官網(wǎng)的頁面,找到并點(diǎn)擊install。

windows下安裝consul、springboot整合consul,consul,spring boot

點(diǎn)擊install后跳轉(zhuǎn)到的consul安裝的頁面

切換到windows,然后找到Binary download for Windows下面的download鏈接,點(diǎn)擊下載consul。

windows下安裝consul、springboot整合consul,consul,spring boot

下載完成后,會(huì)得到一個(gè)壓縮文件,里面只有一個(gè).exe的可運(yùn)行文件(果然如傳說的一樣ovo)。

解壓這個(gè)壓縮文件,把解壓得到的sonsul.exe復(fù)制到D盤program目錄下,然后在當(dāng)前目錄的地址輸入cmd打開命令窗口,輸入以下命令之一。

consul agent -dev # 以開發(fā)模式運(yùn)行

consul agent -server # 以服務(wù)器模式運(yùn)行

第五步、訪問consul控制臺(tái)

然后在瀏覽器的地址欄輸入localhost:8500,即可訪問consul的控制臺(tái)了。

windows下安裝consul、springboot整合consul,consul,spring boot

前面的consul特性的特性有一條,通過key/value存儲(chǔ)來實(shí)現(xiàn)分布式配置,我們點(diǎn)擊一下左邊菜單欄的Key/Value,然后點(diǎn)擊右上角的create按鈕。

windows下安裝consul、springboot整合consul,consul,spring boot

然后我們看到

windows下安裝consul、springboot整合consul,consul,spring boot

存儲(chǔ)配置文件時(shí),其實(shí)這里的key就是我們的配置文件的文件名,當(dāng)然也可以存字符串和其他的數(shù)據(jù)。

2、springboot整合consul

好了,接下來進(jìn)入這篇文章最重要的部分,springboot整合consul

首先,創(chuàng)建一個(gè)springboot項(xiàng)目,就叫consul

一、注冊到consul

修改application.yml配置文件

spring:
  application:
    name: consul
  cloud :
    consul :
      port: 8500
      host: localhost
      discovery:
        enabled: true
        register: true
        hostname: localhost
        health-check-critical-timeout: 30s
        service-name: ${spring.application.name}
        instance-id: ${spring.application.name}:${server.port}
server:
  port: 8080

注意,這里不能設(shè)置profiles,否則啟動(dòng)時(shí)會(huì)報(bào)錯(cuò)

spring:
  profiles: dev # 這個(gè)代碼加了會(huì)報(bào)錯(cuò)
  application:
    name: consul
  cloud :
    consul :
      port: 8500
      host: localhost
      discovery:
        enabled: true
        register: true
        hostname: localhost
        health-check-critical-timeout: 30s
        service-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: null
	at 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: null
	at 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: null
	at 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: null
	at 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: null
	at 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: null
	at 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發(fā)現(xiàn)最后獲取到的instanceId為null,這個(gè)問題找了半天才發(fā)現(xiàn)原因的,注意避坑!

?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

最后啟動(dòng)項(xiàng)目,訪問consul發(fā)現(xiàn)注冊到了consul上,并且服務(wù)名為${spring.application.name}-${server.port},這也是不設(shè)置instance-id時(shí)默認(rèn)的服務(wù)名。

二、從consul拉取配置

上面已經(jīng)讓我們的項(xiàng)目注冊到consul了,接下來修改一下配置文件,從consul拉取配置。

spring:
  application:
    name: consul
  cloud :
    consul :
      port: 8500
      host: localhost
      discovery:
        enabled: true
        register: true
        hostname: localhost
        health-check-critical-timeout: 30s
        service-name: ${spring.application.name}
      config:
        enabled: true
        prefix: config # 配置文件所在文件夾,默認(rèn)值就是config
        name: consul-dev.yaml # 配置文件名,也就是key

server:
  port: 8080

接著,在consul里創(chuàng)建一個(gè)config文件夾,在config文件夾下面創(chuàng)建一個(gè)consul-dev.yaml

user:
  name: heyunlin

注意,文件夾和文件的區(qū)別在于key的結(jié)尾有沒有/

windows下安裝consul、springboot整合consul,consul,spring boot

?最后,創(chuàng)建一個(gè)控制器,獲取配置文件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;
    }

}

重啟項(xiàng)目,訪問http://localhost:8080/config/username發(fā)現(xiàn)成功獲取并返回了字符串heyunlin

windows下安裝consul、springboot整合consul,consul,spring boot

至此,springboot整合consul就算完成了。文章涉及的代碼已經(jīng)上傳到git,按需獲取

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

好了,文章就分享到這里了,看完不要忘了點(diǎn)贊+收藏哦~文章來源地址http://www.zghlxwxcb.cn/news/detail-589241.html

到了這里,關(guān)于windows下安裝consul、springboot整合consul的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 基于spring-boot-starter-data-elasticsearch整合elasticsearch于window系統(tǒng)

    基于spring-boot-starter-data-elasticsearch整合elasticsearch于window系統(tǒng)

    在配置環(huán)境中會(huì)需要到elasticsearch和kibana,這兩個(gè)和spring-boot-starter-data-elasticsearch都必須得保持版本一致,我們可以通過查看他maven的配置: 比如我使用的是?spring-boot-starter-data-elasticsearch:2.7.3 查看到他的maven的結(jié)構(gòu)后可以看到他們都指向了elasticsearch的一個(gè)版本7.17.4,因此在電

    2024年04月25日
    瀏覽(17)
  • springboot3整合consul實(shí)現(xiàn)服務(wù)注冊和配置管理快速入門

    springboot3整合consul實(shí)現(xiàn)服務(wù)注冊和配置管理快速入門

    服務(wù)注冊: 配置管理: 注冊中心的比較: 在微服務(wù)的世界中,服務(wù)注冊是必不可少的?,F(xiàn)在比較流行的也就是Consul和Nacos,Zookeeper沒有管理界面,一般不建議使用,而Eureka已經(jīng)處于停更,并且本身就存在很多bug,一般不建議使用! 我之前寫過一篇spring boot整合nacos實(shí)現(xiàn)服務(wù)注

    2024年04月16日
    瀏覽(23)
  • spring boot 服務(wù)健康檢測返回OUT_OF_SERVICE,導(dǎo)致服務(wù)無法成功注冊到consul

    spring boot 服務(wù)健康檢測返回OUT_OF_SERVICE,導(dǎo)致服務(wù)無法成功注冊到consul

    健康檢測接口返回OUT_OF_SERVICE 從日志啟動(dòng)看,沒有任何報(bào)錯(cuò)信息;而且jvm進(jìn)程也啟動(dòng)成功。 關(guān)鍵的一點(diǎn)信息是,服務(wù)的swagger地址訪問也正常。 但是,consul上的服務(wù)狀態(tài)就是不健康。 當(dāng)然,重啟大法不好使。 增加配置項(xiàng): management.endpoint.health.show-details: always 可以看出,ela

    2024年02月14日
    瀏覽(21)
  • Springboot學(xué)習(xí):安裝spring boot helper插件的相關(guān)問題

    Springboot學(xué)習(xí):安裝spring boot helper插件的相關(guān)問題

    在idea中安裝在線插件spring boot helper后,在構(gòu)建spring boot項(xiàng)目發(fā)現(xiàn)IDE嚴(yán)重報(bào)錯(cuò):spring boot helper不是JetBrains的插件,解決方法是: 卸載剛才安裝的插件,注意需要卸載插件后點(diǎn)擊“應(yīng)用”,然后重啟idea即可卸載成功,不然會(huì)卸載不成功。 需要說明的是:安裝自己搜索的“spring

    2024年02月14日
    瀏覽(20)
  • Springboot 實(shí)踐(10)spring cloud 與consul配置運(yùn)用之服務(wù)的注冊與發(fā)現(xiàn)

    ????????前文講解,完成了springboot、spring security、Oauth2.0的繼承,實(shí)現(xiàn)了對系統(tǒng)資源的安全授權(quán)、允許獲得授權(quán)的用戶訪問,也就是實(shí)現(xiàn)了單一系統(tǒng)的全部技術(shù)開發(fā)內(nèi)容。 ????????Springboot是微服務(wù)框架,單一系統(tǒng)只能完成指定系統(tǒng)的功能;那么多個(gè)單一系統(tǒng)是如何實(shí)現(xiàn)

    2024年02月12日
    瀏覽(21)
  • spring boot es | spring boot 整合elasticsearch | spring boot整合多數(shù)據(jù)源es

    spring boot es | spring boot 整合elasticsearch | spring boot整合多數(shù)據(jù)源es

    目錄 Spring Boot與ES版本對應(yīng) Maven依賴 配置類 使用方式 @Test中注入方式 @Component中注入方式 查詢文檔 實(shí)體類 通過ElasticsearchRestTemplate查詢 通過JPA查詢 保存文檔 參考鏈接 項(xiàng)目組件版本: Spring Boot:2.2.13.RELEASE Elasticsearch:6.8.0 JDK:1.8.0_66 Tips: 主要看第3列和第5列,根據(jù)ES版本選擇

    2023年04月18日
    瀏覽(27)
  • Spring Boot進(jìn)階(54):Windows 平臺(tái)安裝 MongoDB數(shù)據(jù)庫 | 超級(jí)詳細(xì),建議收藏

    Spring Boot進(jìn)階(54):Windows 平臺(tái)安裝 MongoDB數(shù)據(jù)庫 | 超級(jí)詳細(xì),建議收藏

    ????????MongoDB是一種流行的文檔型NoSQL數(shù)據(jù)庫,它具有高性能、高可用性、可伸縮性等優(yōu)點(diǎn),因此被廣泛應(yīng)用于Web應(yīng)用程序、分布式系統(tǒng)、云計(jì)算等領(lǐng)域。在Windows系統(tǒng)中安裝和使用MongoDB也非常簡單,只需要按照官方文檔提供的步驟進(jìn)行操作即可。在本篇文章中,我們將介

    2024年02月16日
    瀏覽(31)
  • 【Spring Boot】Spring Boot整合多數(shù)據(jù)源

    在實(shí)際的開發(fā)工作中,我們經(jīng)常會(huì)遇到需要整合多個(gè)數(shù)據(jù)源的情況,比如同時(shí)連接多個(gè)數(shù)據(jù)庫、讀寫分離、跨數(shù)據(jù)庫查詢等。本文將介紹如何使用Spring Boot來實(shí)現(xiàn)多數(shù)據(jù)源的整合,對于剛剛接觸開發(fā)的小伙伴可能有一些幫助。 在一個(gè)應(yīng)用程序中使用多個(gè)數(shù)據(jù)源意味著我們需要

    2024年02月10日
    瀏覽(27)
  • 六、Spring/Spring Boot整合ActiveMQ

    六、Spring/Spring Boot整合ActiveMQ

    2.1 applicationContext.xml 路徑:src/main/resources/applicationContext.xml 2.2 生產(chǎn)者 2.3 消費(fèi)者 3.1 applicationContext.xml 路徑:src/main/resources/applicationContext.xml 3.2 生產(chǎn)者 和隊(duì)列的代碼一樣,只是在配置文件里面改了destination 3.3 消費(fèi)者 和隊(duì)列的代碼一樣,只是在配置文件里面改了destination 實(shí)現(xiàn)

    2024年02月22日
    瀏覽(15)
  • Spring Boot進(jìn)階(96):輕松上手:實(shí)戰(zhàn)Spring Boot整合Docker

    Spring Boot進(jìn)階(96):輕松上手:實(shí)戰(zhàn)Spring Boot整合Docker

    ??Docker 是一個(gè)開源的應(yīng)用程序容器化工具,它可以將應(yīng)用程序和依賴組件打包到一個(gè)容器中,實(shí)現(xiàn)應(yīng)用程序的快速部署和運(yùn)行。Spring Boot 是一個(gè)快速開發(fā)應(yīng)用程序的框架,使用 Spring Boot 可以快速構(gòu)建各種各樣的應(yīng)用程序。本文將介紹如何使用 Spring Boot 整合 Docker,實(shí)現(xiàn)應(yīng)用

    2024年02月07日
    瀏覽(26)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包