我是服务的执政官-服务发现和注册工具consul简介
服務(wù)發(fā)現(xiàn)和注冊
我們有了兩個服務(wù)。服務(wù)A的IP地址是192.168.0.1,端口9001,服務(wù)B的IP地址192.168.0.2,端口9002。我們的客戶端需要調(diào)用服務(wù)A和服務(wù)B,我們只需要在配置文件中寫上服務(wù)A和服務(wù)B的IP地址即可。
此時,服務(wù)A的服務(wù)器負(fù)載有點高,我們需要臨時增加服務(wù)A的實例,IP192.168.0.3,端口9001。但是我們的客戶端要怎么才能調(diào)用新的實例?
常規(guī)來說,我們可以有以下幾種方法:
網(wǎng)絡(luò)代理方式
如果是http方式通信的服務(wù),可以增加一個nginx做反向代理,轉(zhuǎn)發(fā)到兩個服務(wù)A的實例上。
如果是RPC服務(wù)則可以增加一個LVS或HAProxy或者ESB之類的網(wǎng)絡(luò)代理,客戶端配置網(wǎng)絡(luò)代理地址。
服務(wù)B我們再來一套一樣的配置,這時候又來了服務(wù)C、服務(wù)D、服務(wù)E...,好吧我們好還要再多維護(hù)同樣多的網(wǎng)絡(luò)代理。此外,所有的服務(wù)調(diào)用服務(wù)調(diào)用都必須經(jīng)過網(wǎng)絡(luò)代理,我們還必須保證代理的高可用。最后,陷入運維災(zāi)難。DNS方式
給服務(wù)A配置一個域名,然后通過配置兩個A記錄分別指向兩個服務(wù)A的實例,客戶端只要配置服務(wù)A的域名即可。
這種方式也存在問題,首先DNS沒有辦法管理端口,我們的端口還是只能寫在每個客戶端的配置文件中。此外DNS輪詢負(fù)載均衡能力太弱,可能會導(dǎo)致客戶端負(fù)載的不均衡。
現(xiàn)在有了服務(wù)發(fā)現(xiàn)和注冊機(jī)制,我們可以更合理的解決這個問題。
服務(wù)發(fā)現(xiàn)和注冊,參考字面意思很容易理解,其核心部分可以理解為一個服務(wù)注冊表。服務(wù)啟動時,將自己的信息注冊到注冊表中。注冊表需要每一定時間訪問下已經(jīng)注冊的服務(wù),將沒有響應(yīng)的服務(wù)從注冊表中刪除。最終讓客戶端拿到正常運行的服務(wù)地址。
此時,我們的服務(wù)注冊表必須具備分布式、高可用、強(qiáng)一致性等特點。
目前,業(yè)界開源的服務(wù)發(fā)現(xiàn)和注冊產(chǎn)品有很多,比較流行的主要有:ZooKeeper,Etcd,Consul,Eureka等。
下面介紹下今天的主角,Consul。
Consul
Consul是一個服務(wù)發(fā)現(xiàn)和注冊的工具,其具有分布式、高擴(kuò)展性能特點。
Consul主要包含如下功能:
- 服務(wù)發(fā)現(xiàn): 支持 http 和 dns 兩種協(xié)議的服務(wù)注冊和發(fā)現(xiàn)方式。
- 監(jiān)控檢查: 支持多種方式的健康檢查。
- Key/Value存儲: 支持通過HTTP API實現(xiàn)分布式KV數(shù)據(jù)存儲。
- 多數(shù)據(jù)中心支持:支持任意數(shù)量數(shù)據(jù)中心。
上圖是官網(wǎng)提供的一個事例系統(tǒng)圖,圖中的Server是consul服務(wù)端高可用集群,Client是consul客戶端。consul客戶端不保存數(shù)據(jù),客戶端將接收到的請求轉(zhuǎn)發(fā)給響應(yīng)的Server端。Server之間通過局域網(wǎng)或廣域網(wǎng)通信實現(xiàn)數(shù)據(jù)一致性。每個Server或Client都是一個consul agent。Consul集群間使用了GOSSIP協(xié)議通信和raft一致性算法。
使用Consul也非常簡單,基本可以做到開箱即用。
下載應(yīng)用后可以通過簡單的腳本啟動服務(wù)端和客戶端:
$ consul usage: consul [--version] [--help] <command> [<args>]Available commands are: agent Runs a Consul agent configtest Validate config file event Fire a new event exec Executes a command on Consul nodes force-leave Forces a member of the cluster to enter the "left" state info Provides debugging information for operators join Tell Consul agent to join cluster keygen Generates a new encryption key keyring Manages gossip layer encryption keys kv Interact with the key-value store leave Gracefully leaves the Consul cluster and shuts down lock Execute a command holding a lock maint Controls node or service maintenance mode members Lists the members of a Consul cluster monitor Stream logs from a Consul agent operator Provides cluster-level tools for Consul operators reload Triggers the agent to reload configuration files rtt Estimates network round trip time between nodes version Prints the Consul version watch Watch for changes in Consul運行consul agent 加上相關(guān)的參數(shù),就可以啟動一個consul server 或者 client。
接著,我們可以通過http api注冊服務(wù),向/v1/catalog/register發(fā)送PUT動作的JSON報文:
{"Datacenter": "dc1", "Node": "foobar", "Address": "192.168.10.10", "Service": { //注冊的服務(wù)信息"ID": "redis1","Service": "redis","Tags": ["primary","v1"],"Address": "127.0.0.1","Port": 8000},"Check": { //注冊健康檢查"Node": "foobar","CheckID": "service:redis1","Name": "Redis health check","Notes": "Script based health check","Status": "passing","ServiceID": "redis1"} }注冊后,我們可以在consul自帶的WEB UI中看到剛剛注冊的服務(wù):
與Spring Cloud集成
如果你的服務(wù)正好使用spring boot構(gòu)建,不妨試試Spring Cloud Consul。
Spring Cloud Consul通過幾個簡單的注解,就可以集成諸多consul功能,讓你更方便的治理你的服務(wù)。
@SpringBootApplication @EnableDiscoveryClient //只要添加這個注解即可向配置號的consul注冊服務(wù) @RestController public class Application { @RequestMapping("/")public String home() {return "Hello world";}public static void main(String[] args) {new SpringApplicationBuilder(Application.class).web(true).run(args);} }同時我們可以配合spring boot actuator來注冊監(jiān)控檢查:
spring:cloud:consul:discovery:healthCheckPath: ${management.contextPath}/healthhealthCheckInterval: 15sSpring Cloud支持 Feign 或者 Spring RestTemplate 通過服務(wù)發(fā)現(xiàn)來調(diào)用服務(wù),也可以使用org.springframework.cloud.client.discovery.DiscoveryClient:
@Autowired //注入一個DiscoveryClient private DiscoveryClient discoveryClient;public String serviceUrl() {List<ServiceInstance> list = discoveryClient.getInstances("STORES");if (list != null && list.size() > 0 ) {return list.get(0).getUri();}return null; }consul除了可以用作服務(wù)治理的工具,還可以利用其KV存儲能力,實現(xiàn)分布式服務(wù)配置或分布式鎖等功能。各位感興趣的童鞋可以去consul官網(wǎng),學(xué)習(xí)更多的內(nèi)容。
最后,都看到這了,就順手點個贊吧~~~
參考資料:
https://www.nginx.com/blog/service-discovery-in-a-microservices-architecture/
https://highops.com/insights/service-discovery-6-questions-to-4-experts/
https://www.consul.io/
http://cloud.spring.io/spring-cloud-consul/
轉(zhuǎn)載于:https://www.cnblogs.com/lazio10000/p/6126754.html
總結(jié)
以上是生活随笔為你收集整理的我是服务的执政官-服务发现和注册工具consul简介的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 华为大数据解决方案 (文末附PPT下载)
- 下一篇: 图的十字链表