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

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"系列文章:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://xie.infoq.cn/article/2636d77e2fe433e8144680eac","title":"","type":null},"content":[{"type":"text","text":"微服务架构:网关概念与 zuul","attrs":{}}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://xie.infoq.cn/article/4154bb63f82f17b02e2dca2fb","title":"","type":null},"content":[{"type":"text","text":"微服务网关:Spring Cloud Gateway —— Zuul","attrs":{}}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://xie.infoq.cn/article/96b51e801ac0fa2b8b59f9991","title":"","type":null},"content":[{"type":"text","text":"微服务网关:Spring Cloud Config- 配置中心","attrs":{}}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://xie.infoq.cn/article/8f23ba021e573e876df221c6f","title":"","type":null},"content":[{"type":"text","text":"微服务网关方案:Kong & Nacos","attrs":{}}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://xie.infoq.cn/article/4db8d97e7ca4f1ab679e4d0f3","title":"","type":null},"content":[{"type":"text","text":"Nacos 实践","attrs":{}}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://xie.infoq.cn/article/68c7c9ec4d99aab16315736d2","title":"","type":null},"content":[{"type":"text","text":"微服务网关:Nacos 源码实践(二)","attrs":{}}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://xie.infoq.cn/article/61b260b61df452bd6fa47cbbb","title":"","type":null},"content":[{"type":"text","text":"微服务注册中心:Consul——概念与基础操作","attrs":{}}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"一 简介","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}},{"type":"link","attrs":{"href":"https://xie.infoq.cn/article/61b260b61df452bd6fa47cbbb","title":"","type":null},"content":[{"type":"text","text":"微服务注册中心:Consul——概念与基础操作","attrs":{}}]},{"type":"text","text":"介绍了consul的安装和基本操作,本篇开始在consul上进行服务注册与发现,语言使用Java,框架使用Spring Boot整合Consul。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"二 Spring Boot整合Consul","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 通常demo比较好找,导入后观察需要引入哪些依赖,然后启动。最多微调版本和配置问题即可。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"2.1 网上demo","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 打脸来的如此迅速,百度上搜了一堆,最后发现都是官方文档的翻译,版本不清,代码不全,尝试几个未果。再次感叹现在很多文章的质量。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"2.2 官方文档","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 不得已还是先尝试查看官方文档,","attrs":{}},{"type":"link","attrs":{"href":"https://spring.io/projects/spring-cloud-consul#overview","title":"","type":null},"content":[{"type":"text","text":"Spring Cloud Consul","attrs":{}}]},{"type":"text","text":"是基于3.0.2版本,给出的集成demo。不过很遗憾,官方给出的sample地址,访问一直是404状态。","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/fd/fd306824a7e60df56d2bba55e444872a.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上面Consul Sample的查看结果:","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/a1/a17ed430d36696c87ec5534a9ba90edb.png","alt":null,"title":"","style":[{"key":"width","value":"100%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"2.3 spring-cloud的github","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 考虑下一种渠道,寻找官方","attrs":{}},{"type":"link","attrs":{"href":"https://github.com/spring-cloud/spring-cloud-consul","title":"","type":null},"content":[{"type":"text","text":"github","attrs":{}}]},{"type":"text","text":"。官方的pom.xml配置建议如下:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"\n\n org.springframework.boot\n spring-boot-starter-parent\n {spring-boot-version}\n \n \n\n \n \n org.springframework.cloud\n spring-cloud-starter-consul-discovery\n \n \n org.springframework.boot\n spring-boot-starter-test\n test\n \n \n \n \n \n org.springframework.cloud\n spring-cloud-dependencies\n ${spring-cloud.version}\n pom\n import\n \n \n \n \n \n \n org.springframework.boot\n spring-boot-maven-plugin\n \n \n \n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"事实上,springboot的版本选择过程中也遇到不少问题,几个典型的错误如下(为了节省空间,只截取了关键错误信息):","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"2.3.1 启动报错信息1","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"Description:\n\nAn attempt was made to call a method that does not exist. The attempt was made from the following location:\n\n org.springframework.cloud.client.discovery.health.DiscoveryCompositeHealthIndicator.(DiscoveryCompositeHealthIndicator.java:41)\n\nThe following method did not exist:\n\n org.springframework.boot.actuate.health.CompositeHealthIndicator.(Lorg/springframework/boot/actuate/health/HealthAggregator;)V\n\nThe method's class, org.springframework.boot.actuate.health.CompositeHealthIndicator, is available from the following locations:\n\n 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\n\nIt was loaded from the following location:\n\n file:/Users/lijingyong/.m2/repository/org/springframework/boot/spring-boot-actuator/2.2.6.RELEASE/spring-boot-actuator-2.2.6.RELEASE.jar\n\n\nAction:\n\nCorrect the classpath of your application so that it contains a single, compatible version of org.springframework.boot.actuate.health.CompositeHealthIndicator\n\n\nProcess finished with exit code 1\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"2.3.2 报错2","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"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\n\tat org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]\n\tat org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:651) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]\n\t... 96 common frames omitted\nCaused 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\n\tat org.springframework.cloud.consul.serviceregistry.ConsulAutoRegistration.normalizeForDns(ConsulAutoRegistration.java:178) ~[spring-cloud-consul-discovery-2.2.1.RELEASE.jar:2.2.1.RELEASE]\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"2.4 一个可用demo","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 根据官方说明,并参考另一个demo:https://github.com/RobbieXie/springboot-consul 后,整理如下,不过是使用的较旧版本springboot,可以根据需要更新到适合的版本信息。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"关键配置和代码:","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"2.4.1 pom.xml","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"\n\n 4.0.0\n\n org.example\n springboot-consul\n Spring Boot整合consul示例\n 1.0-SNAPSHOT\n\n \n org.springframework.boot\n spring-boot-starter-parent\n 1.5.9.RELEASE\n \n \n\n \n UTF-8\n UTF-8\n 1.8\n \n\n \n \n org.springframework.boot\n spring-boot-starter-web\n \n\n \n org.springframework.boot\n spring-boot-starter-test\n test\n \n\n \n com.orbitz.consul\n consul-client\n 1.0.0\n \n \n com.ecwid.consul\n consul-api\n 1.3.0\n \n \n\n \n \n \n org.springframework.boot\n spring-boot-maven-plugin\n \n \n \n\n \n \n \n false\n \n central\n bintray\n http://jcenter.bintray.com\n \n \n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"2.4.2 应用启动类 SpringBootConsulApplication","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"package com.flamingskys.learn.springboot.consul;\n\nimport com.flamingskys.learn.springboot.consul.service.ConsulService;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.beans.factory.annotation.Value;\nimport org.springframework.boot.SpringApplication;\nimport org.springframework.boot.autoconfigure.EnableAutoConfiguration;\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\nimport org.springframework.context.annotation.Bean;\n\nimport javax.annotation.PreDestroy;\n\n@SpringBootApplication\n@EnableAutoConfiguration\npublic class SpringBootConsulApplication {\n\n @Value(\"${consul.instanceId}\")\n private String instanceId;\n\n @Bean\n public String getInstanceId(){\n return instanceId;\n }\n\n @Autowired\n private ConsulService consulService;\n\n public static void main(String[] args) {\n SpringApplication.run(SpringBootConsulApplication.class, args);\n }\n\n @PreDestroy\n public void beforeExit(){\n System.out.println(\"-----------------------------BEFORE EXIT--------------------------------\");\n consulService.deregisterService(instanceId);\n }\n}","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"2.4.3 资源配置application.properties","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"spring.application.name=first-consul-client\n\nconsul.host=127.0.0.1\nconsul.port=8500\nconsul.instanceId=${spring.application.name}:${spring.application.instanceid:${random.value}}\n\nspring.cloud.consul.discovery.heartbeat.enabled=true","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"应用启动后,查看console上注册的服务列表,first-consul-client就是我们的服务:","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/64/647ffe8e17dcc26ffefadbc53e6b2a6a.png","alt":null,"title":"","style":[{"key":"width","value":"100%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"完整代码已上传到了","attrs":{}},{"type":"link","attrs":{"href":"https://gitee.com/flamingskyline/spring-boot-integration","title":"","type":null},"content":[{"type":"text","text":"gitee","attrs":{}}]},{"type":"text","text":"。可关注公众号:程序员架构进阶 随时获取更新。","attrs":{}}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章