Jin microservices is a complete microservice demo based on PHP language + hyperf microservices framework

Last update: Jun 30, 2022

介绍

Jin-microservices是基于 php 语言 + hyperf 微服务 框架的完整微服务demo。

github:https://github.com/Double-Jin/jin-microservices

gitee:https://gitee.com/ljj96/jin-microservices

关于 JM

作为php、go双修的开发者, go 语言的微服务体系已经基本掌握,go 语言相关的微服务的文章、开源项目在网上搜索一搜一大堆,这让会 go 语言的开发者能容易地上手并实现微服务,毕竟go语言是除java外最合适做微服务的语言这一。

php语言的优势在于web生态,开发的应用绝大多数为单体应用架构。近年来随着基于 swoole 扩展的 hyperf框架的出现,让php也能开发微服务架构,这里要感谢开源工作者。但用 "php + 微服务 "作为关键词时,搜索出来的文章、开源项目都是一些简单的案例,开发者并不能通过这些简单的案例来了解微服务,这让我有了想写本项目的原始动力。

前言

JM 是一款基于 php 语言 + hyperf 微服务 框架编写的完整微服务demo,与网上能找到的单一功能点简单实现的文章不同,JM从实际项目需求出发,力求做到git clone 项目下来后对着文件就能帮你构建微服务完整的知识体系,让你实际用hyperf开发微服务项目时能粘贴复制本项目的代码。

微服务架构并不是比单体架构先进的架构,只是在项目体量、项目开发者人数达到一定量级后的一种选择。切勿盲目鼓吹微服务,在团队开发、运维能力不足的情况下强行推进微服务架构恐怕会适得其反。

下面提到的组件并不是微服务架构才能使用,如elk、nacos、dtm这些,在单体应用里面也有合适的场景用到,取其精华来满足业务上的需要。如在生产上用到这些组件最好选择编译安装或购买云服务

功能亮点

  • 完整微服务架构
  • JsonRpc调用
  • 统一异常处理
  • 服务注册与服务发现
  • 消息队列
  • 链接追踪
  • 配置中心
  • 服务限流
  • 服务降级
  • 分布式日志
  • 分布式事务

准备

微服务是把单体应用进行分拆后的架构,分拆后带来的问题通过引用第三方组件来解决,安装部署这些组件的时候你将会遇到很多奇奇怪怪的问题。为减低难度,本项目大部分组件采用docker来安装,整体流程我已在不同的电脑上验证数遍,即便如此还是会存在如composer、github、http/tcp访问、端口、内存、docker版本等问题,同样的操作换了台电脑就可能出问题,这需要你跟据报错内容查找相关资料自行解决。

  • 8核16G电脑
  • 熟悉docker
  • 了解网络协议
  • 基本的运维能力

安装

使用

  • 目录结构

      |-- api-gateway //网关服务项目代码 
      |-- order-srv //订单服务项目代码
      |-- user-srv // 用户服务项目代码
      |-- README.md //说明文档
    
  • 完整微服务架构 完整的php微服务案例

  • JsonRpc调用

    • GET http://127.0.0.1:9501/User/UserInfo 通讯单一服务
    • GET http://127.0.0.1:9501/User/UserBonusList 通讯单一服务
    • GET http://127.0.0.1:9501/User/UserStoredList 通讯单一服务
    • GET http://127.0.0.1:9501/Order/OrderList 通讯多个服务
  • 统一异常处理

    • 封装AppServiceExceptionHandler.php 统一处理http请求异常
    • 封装RateLimitExceptionHandler.php 统一处理限流异常
    • 封装JsonRpcExceptionHandler.php 统一处理JsonPrc通讯异常
    • 封装DtmExceptionHandler.php 统一处理DTM事务中间件异常
  • 服务注册与服务发现 带你走进微服务

  • 消息队列

    • GET http://127.0.0.1:9501/User/UserRabbitMQ 调用投递用户消息队列接口
    • GET http://127.0.0.1:9501/Order/OrderRabbitMQ 调用投递订单消息队列接口 完整的php微服务案例
  • 链接追踪 带你走进微服务

  • 配置中心 带你走进微服务 带你走进微服务

  • 服务限流 GET http://127.0.0.1:9501/RateLimit/Test 带你走进微服务

  • 服务降级 GET http://127.0.0.1:9501/CircuitBreaker/Test 带你走进微服务

  • 分布式日志

    当系统变为集群后,应用日志在数十台甚至是上百台不同的服务器上,能实现日志的统一查找、分析和归档等功能便可称为分布式日志系统。

    生产上方案会有很多,如将日志直接输出来Elasticsearch,如使用云服务商提供的日志收集。本案例采用的是通过filebeat将日志同步到ELK中。 带你走进微服务

  • 分布式事务

    数据库事务可以确保该事务范围内的所有操作都可以全部成功或者全部失败。但对分布式系统来说,数据的操作来自多个不同的数据库,单个数据库事务的成功或失败不代表整个系统的数据一致性是对的,只能够通过分布式事务来解决。

    分布式事务就是指事务的发起者、资源及资源管理器和事务协调者分别位于分布式系统的不同节点之上。行业上常用的有二阶段提交、SAGA、TCC等方案,当了解原理后,你自行用http/tcp也能实现二阶段提交、SAGA、TCC。

    下面的接口通过DTM调度实现在一个SAGA案例。 POST http://127.0.0.1:9501/Order/CreateOrder 分布式事务 带你走进微服务 带你走进微服务 完整的php微服务案例

不足

  • 不支持gRpc的服务注册与服务发现
  • 配置中心组件只支持config调用,无法做到env的动态写入与框架重启,但可通过k8s实现

相关文档

GitHub

https://github.com/Double-Jin/jin-microservices
You might also like...

swoole and golang ipc, use goroutine complete swoole coroutine

swoole and golang ipc demo swoole process module exec go excutable file as sider car, use goroutine complete swoole coroutine hub.php ?php require '

Apr 17, 2022

Motan - a cross-language remote procedure call(RPC) framework for rapid development of high performance distributed services

Motan-PHP Overview Motan is a cross-language remote procedure call(RPC) framework for rapid development of high performance distributed services.

Jun 16, 2022

Framework X is a simple and fast micro framework based on PHP

Framework X is a simple and fast micro framework based on PHP

Framework X is a simple and fast micro framework based on PHP. I've created a simple CRUD application to understand how it works. I used twig and I created a custom middleware to handle PUT, DELETE methods.

Feb 6, 2022

Zephir is a compiled high level language aimed to the creation of C-extensions for PHP.

Zephir - is a high level programming language that eases the creation and maintainability of extensions for PHP. Zephir extensions are exported to C c

Jun 30, 2022

Phpactor Language Server

This package provides a platform for building a Language Server according to the Language Server Specification

Jun 9, 2022

FuelPHP v1.x is a simple, flexible, community driven PHP 5.3+ framework, based on the best ideas of other frameworks, with a fresh start! FuelPHP is fully PHP 7 compatible.

FuelPHP Version: 1.8.2 Website Release Documentation Release API browser Development branch Documentation Development branch API browser Support Forum

Jun 16, 2022

High performance HTTP Service Framework for PHP based on Workerman.

High performance HTTP Service Framework for PHP based on Workerman.

webman High performance HTTP Service Framework for PHP based on Workerman. Manual https://www.workerman.net/doc/webman Benchmarks https://www.techempo

Jul 4, 2022

Symprowire is a PHP MVC Framework based and built on Symfony, using the ProcessWire CMS as DBAL and Service Provider.

Symprowire - PHP MVC Framework for ProcessWire 3.x Symprowire is a PHP MVC Framework based and built on Symfony using ProcessWire 3.x as DBAL and Serv

Jan 16, 2022

This JSON marshaller is based on the one built into FEAST framework

This JSON marshaller is based on the one built into FEAST framework

Standalone JSON marshaller based off the one built into FEAST framework at feast/framework on packagist or feastframework/framework on github

Feb 14, 2022
Comments
  • 1. 在api网关层无法读取nacos配置

    您好,冒昧打扰,请见谅。 我是在虚拟机上操作的,MYSQL和REDIS我是原生安装,其余的都是按照您的文档使用docker安装。安装完每个服务的url都可以正常访问,但在启动php bin/hyperf.php start 时,报了:The config of nacos_config read failed from Nacos 错误。 其余配置和状态信息截图如下,希望能获取到指点,谢谢! 1655433853416 1655433888441 1655433940910 1655434017487

    Reviewed by zhengzhishanliang at 2022-06-17 02:47
PhpBoot is an easy and powerful PHP framework for building RESTful/Microservices APIs.
PhpBoot is an easy and powerful PHP framework for building RESTful/Microservices APIs.

?? tiny & fast PHP framework for building Microservices/RESTful APIs, with useful features: IOC, Hook, ORM, RPC, Swagger, Annotation, Parameters binding, Validation, etc.

Jun 26, 2022
🚀 PHP Microservice Full Coroutine Framework
🚀 PHP Microservice Full Coroutine Framework

PHP microservice coroutine framework 中文说明 Introduction Swoft is a PHP microservices coroutine framework based on the Swoole extension. Like Go, Swoft

Jun 29, 2022
An issue tracking tool based on hyperf+reactjs for small and medium-sized enterprises, open-source and free, similar to Jira.
An issue tracking tool based on hyperf+reactjs for small and medium-sized enterprises, open-source and free, similar to Jira.

介绍 本项目以 actionview 为蓝本,使用 Hyperf 框架进行重写。 本项目为 Hyperf 框架的 DEMO 项目 原 ActionView 介绍 English | 中文 一个类Jira的问题需求跟踪工具,前端基于reactjs+redux、后端基于php laravel-frame

May 30, 2022
🚀Hyperf is an extremely performant and flexible PHP CLI framework
🚀Hyperf is an extremely performant and flexible PHP CLI framework

Hyperf is an extremely performant and flexible PHP CLI framework, powered by a state-of-the-art coroutine server and a large number of battle-tested components. Aside from decisively beating PHP-FPM frameworks in benchmarks, Hyperf is unique in its focus on flexibility and composition.

Jun 26, 2022
Hyperf is an extremely performant and flexible PHP CLI framework

?? A coroutine framework that focuses on hyperspeed and flexibility. Building microservice or middleware with ease.

Jul 4, 2022
🎲 This project provides an integration for the Doctrine ORM and the Hyperf framework

Hyperf ?? Doctrine This project provides an integration for the Doctrine ORM and the Hyperf framework. Install composer require leocavalcante/hyperf-d

Mar 24, 2022
Production ready Dockerfile for Octane powered Laravelish web services and microservices

Laravel Octane Dockerfile A pretty configurable and production ready multi-stage Dockerfile for Octane powered Laravelish web services and microservic

Jun 30, 2022
DoraRPC is an RPC For the PHP MicroService by The Swoole

Dora RPC 简介(Introduction) Dora RPC 是一款基础于Swoole定长包头通讯协议的最精简的RPC, 用于复杂项目前后端分离,分离后项目都通过API工作可更好的跟踪、升级、维护及管理。 问题提交: Issue For complex projects separation

May 29, 2022
Files Course Laravel Microservice E-mail

Curso Laravel Microservices com RabbitMQ (micro e-mail) Saiba Mais Sobre o Curso Requisitos Este micro e-mail depende do microservice 01, portanto, pr

Oct 21, 2021
🚀 Developing Rocketseat's Next Level Week (NLW#05) Application using PHP/Swoole + Hyperf

Inmana PHP ?? Developing Rocketseat 's Next Level Week (NLW#05) Application using Swoole + Hyperf. This is the app of the Elixir track. I know PHP/Swo

Jun 1, 2022