十六:Spring Cloud 之Consul服务注册中心(单机版)

版权声明:本文为博主原创文章,觉得稍有帮助,可点赞、转载注明出处。 https://blog.csdn.net/chenghuaying/article/details/82822648

1. 简介

Consul is a distributed service mesh to connect, secure, and configure services across any runtime platform and public or private cloud

  • 服务注册中心,需要单独安装

2. Consul 安装

2.1 下载

以Windows为例,Linux可以参考同样文章

点击https://www.consul.io/downloads.html下载Consul,Windows平台是 consul.exe可执行文件

2.2 配置环境变量

将consul.exe文件所在的文件路径添加到Path中,以便在命令行中可以直接使用consul命令· 这里我放置的路径是:D:\java_install_tools,环境变量配置信息:
在这里插入图片描述

2.3 验证

命令行输入:consul、或者consul --version,出现以下结果:
在这里插入图片描述

2.4 启动

命令行输入以下命令:consul agent -dev
在这里插入图片描述

3. 代码实现

3.1涉及的模块

  • consul-service:服务提供者

3.2 源代码

3.2.1 Github地址

https://github.com/andyChenHuaYing/spring-cloud-demo

3.3 consul-service

3.3.1整体步骤

1.pom.xml引入使用consul的依赖
2. application.yml指定consul服务注册中心地址以及健康检查endpoint
3. ConsulServiceApplication@EnableDiscoveryClient注解开启使用注册中心功能
4. 查看consul注册中心自带的Web UI界面查看服务注册情况

3.3.2 pom文件添加依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring-cloud-finchley-demo</artifactId>
        <groupId>org.oscar.scd</groupId>
        <version>1.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>consul-service</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>
</project>

3.3.3 application.yml

spring:
  application:
    name: consul-service
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        health-check-interval: 10s
        instance-id: consul-service
        health-check-path: /actuator/health
server:
  port: 8780

3.3.4 ConsulServiceApplication

package org.oscar.scd.consul.service;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@EnableDiscoveryClient
@SpringBootApplication
public class ConsulServiceApplication {
    @Value("${spring.application.name}")
    private String appName;

    @RequestMapping("/appName")
    public String getAppName() {
        return this.appName;
    }

    public static void main(String[] args) {
        SpringApplication.run(ConsulServiceApplication.class, args);
    }
}

4. 验证

4.1 创建SpringBoot启动类

4.1.1 ConsulServiceApplication

最简单的方式创建一个Spring Boot启动类即可

4.2 启动SpringBoot启动类

  • ConsulServiceApplication

4.3 访问注册中心

访问:http://localhost:8500 查看服务注册信息
在这里插入图片描述

5. 思考

  • 与eureka有什么不同
  • 如何搭建HA
  • 有哪些特性

6. 补充

6.1 资料

https://www.consul.io/

二:Spring Cloud 之Eureka服务注册中心(单机版)

四:对微服务所需的服务发现的理解

猜你喜欢

转载自blog.csdn.net/chenghuaying/article/details/82822648
今日推荐