微服務註冊中心: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":{}}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章