西维蜀黍

【Microservices】微服务 - 服务注册(Service Registry)和服务发现(Service Discovery)

Service Discovery

Let’s imagine that you are writing some code that invokes a service that has a REST API or Thrift API. In order to make a request, your code needs to know the network location (IP address and port) of a service instance. In a traditional application running on physical hardware, the network locations of service instances are relatively static. For example, your code can read the network locations from a configuration file that is occasionally updated.

In a modern, cloud‑based microservices application, however, this is a much more difficult problem to solve as shown in the following diagram.

  ...


【ZooKeeper】Basic

ZooKeeper

Apache ZooKeeper is an open-source server for highly reliable distributed coordination of cloud applications. It is a project of the Apache Software Foundation.

ZooKeeper is essentially a service for distributed systems offering a hierarchical key-value store, which is used to provide a distributed configuration service, synchronization service, and naming registry for large distributed systems (see Use cases). ZooKeeper was a sub-project of Hadoop but is now a top-level Apache project in its own right.

Zookeeper 一个最常用的使用场景就是用于担任服务生产者和服务消费者的注册中心。 服务生产者将自己提供的服务注册到Zookeeper中心,服务的消费者在进行服务调用的时候先到Zookeeper中查找服务,获取到服务生产者的详细信息之后,再去调用服务生产者的内容与数据。

如下图所示,在 Dubbo架构中 Zookeeper 就担任了注册中心这一角色。

当然,在Dubbo中,我们利用ZooKeeper 中的瞬时节点(ephemeral node)了,即当服务的消费者 subscribe 了自己依赖的一个服务生产者的信息后,当服务生产者在Zookeeper中心更新了自己的节点信息时,服务的消费者可以实时的收到通知。

这意味着我们可以实现“优雅的服务注册与服务下线”,即服务消费者可以(通过被push的形式)实时的感知到服务生产者的节点变化。

  ...


【ZooKeeper】安装

Install on OS

Prerequisition

安装 ZooKeeper 之前需要先安装 JDK, 关于 JDK 的安装这里不再赘述。

  ...


【Java】安装

Ubuntu

Execute the following command to install the default Java Runtime Environment (JRE), which will install the JRE from OpenJDK 11:

$ apt install default-jre

The JRE will allow you to run almost all Java software.

Verify the installation with:

$ java -version

You’ll see the following output:

Outputopenjdk version "11.0.7" 2020-04-14
OpenJDK Runtime Environment (build 11.0.7+10-post-Ubuntu-2ubuntu218.04)
OpenJDK 64-Bit Server VM (build 11.0.7+10-post-Ubuntu-2ubuntu218.04, mixed mode, sharing)

You may need the Java Development Kit (JDK) in addition to the JRE in order to compile and run some specific Java-based software. To install the JDK, execute the following command, which will also install the JRE:

$ sudo apt install default-jdk
  ...


【Redis】Codis 安装

Prerequisition

安装Go

$ echo "export GOPATH=/home/parallels/go" > .zshrc
$ echo "export GOROOT=/usr/lib/go-1.14" > .zshrc

安装Java

安装Zookeeper

安装 autoconf

$ sudo apt-get install autoconf
  ...


【Golang】Golang 安装

Ubuntu

If you’re using Ubuntu 18.04 LTS or 19.10 on amd64, arm64, armhf or i386, then you can use the longsleep/golang-backports PPA and install Go 1.14.

$ sudo add-apt-repository ppa:longsleep/golang-backports
$ sudo apt update
$ sudo apt install golang-go
  ...


【Prometheus】Prometheus - Exporter - Redis Exporter

Build and run locally

$ git clone https://github.com/oliver006/redis_exporter.git
$ cd redis_exporter
$ go build .
$ nohup /home/sw/redis_exporter/redis_exporter &
  ...


【Prometheus】Advanced

Concepts

Data Model

Metric and Label

Every time series is uniquely identified by its metric name and optional key-value pairs called labels.

The metric name specifies the general feature of a system that is measured (e.g. http_requests_total - the total number of HTTP requests received). It may contain ASCII letters and digits, as well as underscores and colons. It must match the regex [a-zA-Z_:][a-zA-Z0-9_:]*.

Note: The colons are reserved for user defined recording rules. They should not be used by exporters or direct instrumentation.

Labels enable Prometheus’s dimensional data model: any given combination of labels for the same metric name identifies a particular dimensional instantiation of that metric (for example: all HTTP requests that used the method POST to the /api/tracks handler). The query language allows filtering and aggregation based on these dimensions. Changing any label value, including adding or removing a label, will create a new time series.

Label names may contain ASCII letters, numbers, as well as underscores. They must match the regex [a-zA-Z_][a-zA-Z0-9_]*. Label names beginning with __ are reserved for internal use.

Label values may contain any Unicode characters.

A label with an empty label value is considered equivalent to a label that does not exist.

  ...


【Linux】安装 oh-my-zsh

$ sudo apt install git
$ sudo apt install zsh
$ chsh -s $(which zsh)
$ sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

Reference

  ...


【Prometheus】Prometheus - Exporter

  • 所有的Exporter:https://prometheus.io/docs/instrumenting/exporters/#exporters-and-integrations
  • Redis Exporter:https://github.com/oliver006/redis_exporter
  • Golang Exporter:https://github.com/prometheus/mysqld_exporter
  ...