CAP原理

一致性(consistency)所有节点在同一时间具有相同的数据

可用性(AVailability)保证每个请求不管成功或者失败都有响应

分区容错(Partition tolerance )系统中任意信息的丢失或失败不会影响系统的继续运作

只能满足其2,无法满足其3

CP主流

保证一致性和分区容错

AP其次

保证可用性和分区容错

注册配置中心软件:


Consul:

方便部署

采用Raft算法实现,有服务发现,key/value存储,可以做配置中心使用,有健康检查,同时提供了web管理界面

Consul角色

-dev 开发环境下启动,提供了基本的服务

-client 客服端,无状态,将http和DNS请求转发到服务端集群

-server 服务端,保存配置信息,搭建高可用集群

Consul内部端口:


Consul工作原理:


php接入consul:   consul service

<?php
namespace app\service;
use think\facade\Config;
class Consul
{
    private $httpUrl;
    public function __construct(){
          //  'host'=>'localhost',
         //   'port'=>'8500'
        $consulConfig=Config::get('common.consul');
        $this->httpUrl='http://'.$consulConfig['host'].':'.$consulConfig['port'].'/';
    }
    //服务注册
    //agent/service/register
    public function registerService($data){
        $url=$this->httpUrl.'v1/agent/service/register';
        return $this->curlPUT($url,$data);
    }
    //服务信息
    public function serviceInfo($serviceId){
        $url=$this->httpUrl.'v1/health/service/'.$serviceId;
        return $this->curlGET($url);
    }

    public static function curlGet($httpUrl) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $httpUrl);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-type:application/json"]);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $res = curl_exec($ch);
        if ($res === false) {
            var_dump(curl_error($ch));
        }
        curl_close($ch);
        return $res;
    }

  public static function curlPut($httpUrl, $data) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $httpUrl);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-type:application/json"]);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        $res = curl_exec($ch);
        if ($res === false) {
            var_dump(curl_error($ch));
        }
        curl_close($ch);
        return $res;
    }

consul 的 服务注册及服务发现

<?php

namespace app\controller;

use app\BaseController;
use app\service\Consul;


class ConsulDemo extends BaseController
{
//注册服务
    public function regDemo(){
        $data=[
            'ID'=>'demoService',
            'Name'=>'demoService',
            'Tags'=>['core.demo'],
            'Address'=>'thinkphp6.sc',//访问地址
            'Port'=>8087,//检查接口
            'Check'=>[
                'HTTP'=>'http://thinkphp6.sc:80',
                'Interval'=>'5s'//回调时间
            ]
        ];
        $consul =new Consul();
        $rs= $consul->registerService($data);
        var_dump($rs);
    }
    //服务发现,获取服务信息
    public function serviceInfo(){
        $serviceId='demoService';
        $consul=new Consul();
        $rs=$consul->serviceInfo($serviceId);
        echo $rs;

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值