微服务 – Spring Cloud – consul 安装、服务注册、服务发现

233 次浏览次阅读
没有评论

微服务 – Spring Cloud – consul 安装、服务注册、服务发现

[TOC]

what is consul?

HashiCorp Consul is a service networking solution that enables teams to manage secure network connectivity between services and across on-prem and multi-cloud environments and runtimes. Consul offers service discovery, service mesh, traffic management, and automated updates to network infrastructure device. You can use these features individually or together in a single Consul deployment.

Consul 是一套开源的分布式服务发现和配置管理系统。由 HashiCorp 公司使用 Go 语言开发。

提供了微服务系统中的服务治理、配置中心、控制总线等。这些功能即可以单独使用,也可以一起使用构建全方位的服务网格。

功能

  • 服务发现 提供 http 和 dns 两种发现方式
  • 监控检测 支持多方式,http、tcp、docker、shell 脚本定制化
  • KV 存储 key、value 的存储方式
  • 多数据中心 consul 支持多数据中心
  • 可视化 web 界面

安装 Consul

1、拉取镜像

docker search consul
docker pull consul

2、运行镜像

docker run -d -p 8500:8500 --restart=always --name=consul consul:latest agent -server -bootstrap -ui -node=1 -client='0.0.0.0'

docker logs containId
  • agent: 表示启动 Agent 进程。
  • server:表示启动 Consul Server 模式
  • client:表示启动 Consul Cilent 模式。
  • bootstrap:表示这个节点是 Server-Leader,每个数据中心只能运行一台服务器。技术角度上讲 Leader 是通过 Raft 算法选举的,但是集群第一次启动时需要一个引导 Leader,在引导群集后,建议不要使用此标志。
  • ui:表示启动 Web UI 管理器,默认开放端口 8500,所以上面使用 Docker 命令把 8500 端口对外开放。
  • node:节点的名称,集群中必须是唯一的,默认是该节点的主机名。
  • client:consul 服务侦听地址,这个地址提供 HTTP、DNS、RPC 等服务,默认是 127.0.0.1 所以不对外提供服务,如果你要对外提供服务改成 0.0.0.0
  • join:表示加入到某一个集群中去。如:-json=192.168.0.11。

3、测试访问

localhost:8500
微服务 – Spring Cloud – consul 安装、服务注册、服务发现

服务注册

1、新建 子模块 payment 服务

2、引入 consul pom 依赖

<!-- 整合 consul 客户端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>

3、application.yaml 配置文件

server:
  port: 8006

spring:
  application:
    name: consul-provier-payment

  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        service-name: ${spring.application.name}

4、主启动类 ConsulMain8006.java

@SpringBootApplication
@EnableDiscoveryClient
public class ConsulMain8006 {public static void main(String[] args) {SpringApplication.run(ConsulMain8006.class, args);
    }

}

4、编写业务控制体,提供一个接口,供消费者服务通过 consul 服务发现调用

PaymentController.java

@RestController
public class PaymentController {@Value("${server.port}")
    private String serverPort;

    @GetMapping(value = "/payment/cs")
    public String paymentConsul() {return "SpringCloud with consul: " + serverPort + "\t" + UUID.randomUUID().toString();}

}

5、启动 然后去 consul 注册中心查看 consul-provier-payment 服务是否注册进来
微服务 – Spring Cloud – consul 安装、服务注册、服务发现

服务发现

采用还是 RestTemplate

1、新建子模块 order 服务,并把 order 服务注册到 consul 服务中心

2、order 模块 pom 引入 consul 依赖

<!-- 整合 consul 客户端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>

2、配置文件 application.yaml

server:
  port: 80

spring:
  application:
    name: consul-consumer-order

  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        service-name: ${spring.application.name}

3、RestTemplate 配置类文件 ApplicationContextConfig.java

@Configuration
public class ApplicationContextConfig {

    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate() {return new RestTemplate();
    }

}

4、业务接口 控制器 负责 通过 consul 发现 payment 服务, 并调用 payment 服务中的 /payment/cs 接口

@RestController
public class OrderController {

    private final static String PAYMENT_SERVICE = "http://consul-provier-payment";

    @Resource
    private RestTemplate restTemplate;

    @GetMapping(value = "/comsumer/payment/cs")
    public String paymentInfo() {String result = restTemplate.getForObject(PAYMENT_SERVICE + "/payment/cs", String.class);
        return result;
    }

}

5、主启动类

@SpringBootApplication
@EnableDiscoveryClient
public class ConsulOrderMain80 {public static void main(String[] args) {SpringApplication.run(ConsulOrderMain80.class, args);
    }

}

6、运行 order 服务,会发现 order 服务已经注册到 consul 注册中心

访问 http://localhost/comsumer/payment/cs 接口 实现了服务发现调用

微服务 – Spring Cloud – consul 安装、服务注册、服务发现

微服务 – Spring Cloud – consul 安装、服务注册、服务发现

正文完
 0
评论(没有评论)