SpringCloud uses Consul as the service governance center

SpringCloud uses Consul as the service governance center

Preface

When we are developing a distributed architecture system, there is an indispensable tool that is the service governance component. We can use it to register, publish and call services, which can be understood as maintaining a roster of all our services. . Current mainstream service management center Zookeeper, Eureka, Nacosand so on, but here today, I want to tell you about another service governance component - Consul.

For the use of Nacos, please refer to my blog : Nacos Service Management Center and Configuration Center

text

Consul

Consul is an Spring Cloudintegrated open source distributed service registration discovery center. Written
by Golanguage. Support health check, multi-data center also supports kv storage, adopt Raftconsistency algorithm to ensure strong consistency and availability. And it is dockerperfectly compatible.

Consul itself is also a service governance center, in contrast to Eureka:

  • ConsulPart of the performance is sacrificed to ensure strong service consistency.
  • EurekaAs long as the service is registered to the master node, the service registration is considered successful, regardless of whether other nodes can be called normally.

Consul vs Eureka vs Zookeeper

Insert picture description here

CAPThe principles refer to data consistency, data availability, and partition tolerance, respectively . Here APand CPrefer to:

  • AP模式: At the expense of strong consistency, some nodes are down, and normal working nodes will not be affected.
  • CP模式: At the expense of data availability, in order to ensure data consistency, when a machine fails, the data of all nodes cannot be used.

The consensus algorithm makes a group of servers agree on a value, so the active feature is that each server can determine a value in the end. The same value can be achieved through the same data request will be processed by the same server. PaxosAnd Raftare selected by the masterimplemented consistency value of the multi-node.

Consul installation and startup

Step 1: Downloadconsul Choose the appropriate version
from the official website according to your own system . What I use here is Macthat you can brewdownload it directly.
Also friends who use Mac can refer to my blog: Use Mac system for Java programming

brew install consul

Step 2: Startconsul

./consul agent -dev -ui -client 0.0.0.0

Description of the parameters of the startup command:

  • agent: ConsulThe core command, the main functions are to maintain member information, run status detection, declare services, and process requests, etc.
  • -server: Is the representative servermode
  • -ui: On behalf of the webcontrol panel
  • -bootstrap-expect: Represents the number of clusters you want to create, the official recommendation is 3 or 5
  • -data-dir: Data storage directory
  • -node: Represents the current nodename
  • -client: It should be an address registered by the client service, which can be serverthe same as the current one or another host address. The system default is 127.0.0.1
  • -bind: Cluster communication address
  • -join: The address of the joined cluster

Step 3: Enter http://localhost:8500/ui/dc1/services to enter the Consul UI interface
Insert picture description here

SpringCloud uses Consul as the service governance center

Maven dependency

  • ConsulThere are certain requirements for the SpringBootand SpringCloudversion, so the demorelatively complete dependencies of the examples are shown here .
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.2.RELEASE</version>
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <lombok-version>1.18.2</lombok-version>
</properties>

<dependencies>
<!--actuator用于检查节点健康-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<!--Spring Cloud Consul-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>

<!--spring boot-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!--lombok-->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>${
    
    lombok-version}</version>
    <optional>true</optional>
</dependency>

</dependencies>

<!--加入Spring Cloud 的Dalston版本的配置依赖-->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Greenwich.SR4</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

application.properties: configuration class

server.port=8080
spring.application.name=springcloud-demo-producer

#consul
spring.cloud.consul.host=127.0.0.1
spring.cloud.consul.port=8500
#健康检查路径
spring.cloud.consul.discovery.health-check-path=/actuator/health
spring.cloud.consul.discovery.health-check-interval=15s
spring.cloud.consul.discovery.hostname=127.0.0.1
spring.cloud.consul.discovery.register=true
spring.cloud.consul.discovery.port=${
    
    server.port}


#consul service name
spring.cloud.consul.discovery.serviceName=springcloud-demo-producer
spring.cloud.consul.discovery.heartbeat.enabled=true

SpringCloudProducerApp: startup class

  • @EnableDiscoveryClient: Used to provide registration services to consulor zookeeperas a registration center
@Slf4j
@SpringBootApplication(exclude = {
    
    DataSourceAutoConfiguration.class})
@EnableDiscoveryClient
public class SpringCloudProducerApp extends SpringBootServletInitializer {
    
    
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    
    
        return application.sources(SpringCloudProducerApp.class);
    }
    /**
     * 项目的启动方法
     *
     * @param args
     */
    public static void main(String[] args) {
    
    
        SpringApplication.run(SpringCloudProducerApp.class, args);
        log.info("======服务已经启动========");
    }
}

verification

Start the project, enter http://localhost:8500/ui/dc1/servicesConsul UI interface to see the newly registered service
Insert picture description here

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_40990818/article/details/108878497