In the Spring Cloud system, Eureka is closed source, and is it good to use Consul as a registry?

Time:2022-8-5

1. SpringCloud technology stack

Developing distributed systems can be challenging, and complexity has moved from the application layer to the network layer and requires more interaction between services. Making your code "cloud-native" requires solving 12-factors such as external configuration, service statelessness, logging, and connecting to backup services. The Spring Cloud project suite includes getting your application running in the cloud Many services required.

12-factor (12-factor for cloud-native applications):

SpringCloud Architecture:

1.1 SpringCloud technology stack

The SpringCloud technology stack is very rich, which is one of the reasons why SpringCloud is so popular in the field of microservices. The technology stack is as shown above, in service registration and configuration, service invocation, microservice gateway, message components, link tracking, configuration center , security control, limit flow and many other aspects of the technology stack are relatively complete, and Alibaba has also released a set of SpringCloud Alibaba version, which mainly integrates the mainstream technology stack in Alibaba.

1.2 Introduction to SpringCloud Classic Technology

Microservice projects have been very popular in recent years, and the related technical solutions that have been launched are also very active. However, some technical components in the SpringCloud technology stack are gradually being phased out or closed source, but they are replaced by better technical solutions. In the near future, there is a high probability that those technologies that are closed source or will be eliminated will not be used in the project, so we can directly learn better alternative technical solutions when we study.

Eureka closed source:

The above English probably means:Eureka 2.0The open source work has been discontinued, relying on the open source libraryEureka 2.xBranched projects or related code are at your own risk.

Eureka is mainly responsible for service registration and discovery in microservice projects. There are many alternative technical solutions, and many solutions are better than Eureka, such as Consul, Nacos, etc.

Hystrix stopped updating:

Hystrix mainly does service fuse and downgrade in the project, but it is officially announced that it will not be under development and is currently under maintenance, but the official said that the 1.5.18 version of Hystrix is ​​stable enough to meet the needs of Netflix's existing applications.

There are also many alternative products about Hystrix, such as the officially recommended onesresilience4jresilience4jis a lightweight circuit breaker framework, butresilience4jAt present, the frequency of use in China is not high, and the functions are not strong enough. We recommend using more powerful functions.SpringCloud Alibaba Sentinel

Zuul Obsolete:

Zuulis a microservice gateway technology, butZuul1.xIt uses a blocking API, does not support long connections, does not provide asynchronous, and has low performance in high concurrency scenarios.SpringCloudThe official website launched a new microservice gateway technologySpringCloud Gateway,CompareZuulIt has more powerful performance, richer functions, and supports a variety of features such as asynchronous.

SpringCloud Config has poor practicality:

SpringCloud ConfigMainly used to manage the configuration file of the project, every time you want to useSpringCloud ConfigWhen it is necessary to go through a wave of operation and configuration, it can be usedSpringCloud ConfigImplementing configuration management, and using it alone cannot support real-time configuration refresh, it is really a headache to use in a project.

There are currently many technologies that can replaceSpringCloud Config, such as Ctrip'sApolloSpringCloud Alibaba Nacos, the function is more thanSpringCloud ConfigStrong, and supports real-time refresh configuration.

SpringCloud Bus has poor practicality:

SpringCloud Bus is a service message bus, which mainly implements informing multiple services to perform a task, generally used with SpringCloud Config. This function is actually not used, because many task components basically have the message notification function, such as Nacos and Apollo, which can implement all service subscriptions to perform related operations.

1.3 SpringCloud project scenario

Microservice technology has been widely used in many large factories at home and abroad, so how to use microservice technology in the project? Let's take the future of Didi Express business to explain the combination of microservice technology and business application.

The taxi business is as shown above:

1: When you take a taxi, you will choose a model. When you choose a model, our calling process is: Gateway->Driver (load the driver list)
2: After selecting the model, confirm the taxi, which is equivalent to placing an order. The calling process is: Gateway->Order (order)->Driver (driver status change)
3: After the taxi is over, the user enters payment, and the calling process is: Gateway->Pay (payment)->Driver (update driver status)
                                                    ->Order (update order status)

2、SpringCloud Consul

we knowEureka 2.XEncountered difficulties and stopped development, so we need to find other alternative technologies to replace Eureka. In this section, we will explain a new component, Consul.

2.1 Introduction to Consul

Consul is an open source tool launched by HashiCorp, which is used to realize service discovery and configuration of distributed systems. Compared with other distributed service registration and discovery solutions, Consul's solution is more "one-stop", with built-in service registration and discovery framework, distributed consensus protocol implementation, health check, Key/Value storage, and multi-data center solutions. Need to rely on other tools (such as ZooKeeper, etc.). It is also simpler to use. Consul is written in Go language, so it is naturally portable (supports Linux, Windows and Mac OS X); the installation package contains only one executable file, which is convenient for deployment and works seamlessly with lightweight containers such as Docker.

Let's compare the current mainstream technologies for service registration and discovery:

Contrast euerka Consul zookeeper etcd
Service Health Check Configurable support Service status, memory, hard disk, etc. (Weak) long connection, keepalive connection heartbeat
Multiple data centers support
kv storage service support support support
consistency raft paxos raft
cap ap cp cp cp
Using the interface (multilingual capability) http Support http and dns client http/grpc
watch support Supports long polling/most increments Full/support long polling support Support long polling
self-monitoring metrics metrics metrics
Safety acl /https acl https support (weak)
spring cloud integration supported supported supported supported

characteristic:

  • service discovery
  • health examination
  • Key/Value storage
  • Multiple data centers
  • active community

Advantages of Consul:

  • Using the Raft algorithm to ensure consistency is more straightforward than the complex Paxos algorithm. In comparison, zookeeper uses Paxos, while etcd uses Raft.
  • Supports multiple data centers, and the services of the internal and external networks are monitored on different ports. A multi-data center cluster can avoid the single point of failure of a single data center, and its deployment needs to consider network latency, fragmentation, etc. Neither zookeeper nor etcd provide support for multi-datacenter functionality.
  • Support health check. etcd does not provide this functionality.
  • Support http and dns protocol interface. The integration of zookeeper is more complicated, etcd only supports the http protocol.
  • The official web management interface is provided, etcd does not have this function.
  • Comprehensive comparison, Consul, as a new star in service registration and configuration management, is worthy of attention and research.
  • Provides rest api for easy integration:https://www.consul.io/api-doc…

Consul role

  • client: Client, stateless, forwards HTTP and DNS interface requests to server clusters in the LAN.
  • server: server, saves configuration information, high-availability cluster, communicates with local clients in the local area network, and communicates with other data centers through the WAN. The number of servers in each data center is recommended to be 3 or 5.

The Consul client and server also support the use of the Kua Center, which further improves its high availability.

2.2 Consul infrastructure

Glossary: ​​Terminology

See official documentation:https://www.consul.io/docs/in…

  • agent

Each member of the consul cluster must run an agent, which can be started by the consul agent command. The agent can run in server state or client state. Naturally, nodes running in server state are called server nodes; nodes running in client state are called client nodes.

  • server node

Responsible for the complex work of forming a cluster (electing the server to elect a leader by itself, maintaining state, forwarding requests to the leader), as well as the services provided by consul (responding to RPC requests), and storing and replicating data. Considering fault tolerance and convergence, it is generally appropriate to deploy 3 to 5.

  • client node

Responsible for forwarding all RPCs to the server node. It is stateless and lightweight, so a large number of client nodes can be deployed.

  • data center

While the definition of a data center may seem obvious, there are subtle details that must be considered. We define a data center as a private, low-latency, and high-bandwidth network environment. This does not include communication over the public internet, but for our purposes, multiple Availability Zones within a single EC2 region will be considered part of a single datacenter.

in addition:

The server elects a leader by itself. Although Consul can run on one server, it is recommended to use 3 to 5 servers to avoid data loss in case of failure. It is recommended to configure one server cluster per data center.

Components that need to discover other services in the infrastructure can query any Consul server or agent, and the agent will automatically forward the request to the server.

Each data center runs a Consul server cluster. When a service discovery and configuration request across data centers is created, the local Consul Server forwards the request to the remote data center and returns the result.

How to implement service registration and discovery

Consul plays the function of service registration and discovery in the project. Let's explain how it works:

1: When the Producer starts, it will send a post request to Consul and transmit its own IP and Port to Consul.
2: After Consul receives the registration of the Producer, it will send a health check request to the Producer every 10s (default) to check whether the Producer is healthy.
3: When the Consumer initiates a request to the Producer in the form of Http, it will first obtain a temporary table for storing the IP and Port of the service from Consul, and then send the request after obtaining the IP and Port of the Producer from the table.
4: The temporary table is updated every 10s and only contains Producers that have passed the health check.

2.2 Consul installation

Consul is different from Eureka and needs to be installed separately. Visit the official website of Consul to download the latest version of Consul. The current latest version is1.9.0,We useconsul_1.9.0_windows_amd64Version.

download

download link:https://www.consul.io/downloa…; How to download on linux can be viewed by clicking the corresponding tab.

Download address of historical version:https://releases.hashicorp.co…

The downloaded file isconsul_1.9.0_windows_amd64.zip, we unzip this file, there is a file insideconsul.exe, we add the directory where the file is located to the environment variable path.

Installation starts

1. Run as consul server, refer to the following commands:

consul agent -server -bootstrap-expect 1 -data-dir /root/consul/data  -node=n1 -ui -client=0.0.0.0 -bind=192.168.200.129
  • -server defines the agent to run in server mode
  • -bootstrap-expect 1 The number of server nodes expected to be provided in a datacenter. When this value is provided, Consul will wait until the specified number of servers is reached before booting the entire cluster. Here we only start one server for the convenience of demonstration
  • The -data-dir parameter sets Consul's own maintained data storage path
  • -node=n1 The name of the node in the cluster, which must be unique in a cluster, the default is the host name of the node
  • -bind=127.0.0.1 This address is used for communication within the cluster. All nodes in the cluster must be reachable to the address. The default is 0.0.0.0
  • -datacenter=dc1 specifies the name of the current data center, this parameter can not be set
  • -ui specifies that it can be presented in UI. The current UI access address is: http://localhost:8500
  • -client=0.0.0.0 consul service listening address, this address provides services such as HTTP, DNS, RPC, etc. The default is 127.0.0.1, so it does not provide external services. If you want to provide external services, change it to 0.0.0.0, our current configuration is address outside.

See the official documentation for complete startup options parameters:https://www.consul.io/docs/ag…

To access the console UI after startup:http://localhost:8500/

Services:Service Information.

Nodes: Node information, Consul supports clustering.

Key/Value: Stored dynamic configuration information.

ACL: permission information.

Intentions: Manage consul by means of commands.

2. Run as consul client, the reference command is as follows:

consul agent -data-dir C:\developer\consul_1.9.6_windows_amd64\data\client -node=n2 -ui -client=0.0.0.0 -bind=192.168.200.10 -join 192.168.200.129

Through the above two operations, a complete data center is started.

3. For convenience, you can use the following command to start an agent server,

consul agent -dev

2.3 Using Consul in the project

If you want to use Consul as the service registry in the project, you only need to introduce the following dependency package and add it to the startup class@EnableDiscoveryClientannotation, and inapplication.ymlAdd the Consul service address to:

1) Add dependencies

in the projecthailtaxi-gatewayAdd dependencies:

<!--consul-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-consul-discovery</artifactId>
    <version>2.2.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    <version>2.2.10.RELEASE</version>
</dependency>

2) add@EnableDiscoveryClientannotation

existhailtaxi-gatewaystartup classGatewayApplicationadd on@EnableDiscoveryClientannotation:

3) Configure Consul service information

existapplication.ymlAdd Consul service information in:

The configuration is as follows:

spring:
  application:
    name: hailtaxi-gateway
  cloud:
    #ConsulConfiguration
    consul:
      host: 127.0.0.1
      port: 8500
      discovery:
        #Register to the service name in Consul
        service-name: ${spring.application.name}
        #The instance ID of the registered service, it is best not to repeat, here is the method suggested by the official website with random numbers Default: application name: port
        #instance-id: ${spring.application.name}:${vcap.application.instance_id:${spring.application.i nstance_id:${random.value}}}
        # The custom instance id is: application name: ip: port
        instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
        prefer-ip-address: true
        # Enable service registration
        register: true
        # Enable service discovery
        enabled: true
        Health check failed after #2 minutes unregister
        health-check-critical-timeout: 2m
        #consul Polling Period for Health Checks
        health-check-interval: 10s

4) Consul service data

This article was released by Chuanzhi Education Erudition Valley-Wild Architect teaching and research team, please indicate the source for reprinting!

If this article is helpful to you, please follow and like it; if you have any suggestions, you can also leave a comment or private message, your support is the driving force for me to persist in creating

Recommended Today

[Causal inference paper] China's new crown mortality rate is higher? – Simpson's paradox of Covid-19 mortality

Simpson’s paradox in Covid-19 case fatality rates: a mediation analysis of age-related causal effects Authors: Julius von Kügelgen , Luigi Gresele , and Bernhard Schölkopf IEEE Trans on AI 2021, Max Planck Institute for Intelligent Systems, Germany, University of Cambridge Paper link:https://ieeexplore.ieee.org/abstract/document/9404149 Link to this article:https://www.cnblogs.com/zihaojun/p/15737080.html Table of contents Simpson’s paradox in Covid-19 case fatality […]