ruima/microservice-tool

ruima微服务框架的一些公共方法。

v0.1.1 2019-06-06 12:50 UTC

This package is auto-updated.

Last update: 2024-09-05 12:18:36 UTC


README

描述

这是一个微服务框架,使构建微服务API服务器变得容易。网关将用户认证信息(基本信息、角色信息、权限信息)缓存在Redis中。

此包做了什么?

  • 自动将请求分发到目标微服务
  • 使用Redis在网关中缓存用户认证信息
  • 网关中的认证
  • 微服务中间件中的API权限保护

依赖项

  • PHP >= 7.1.3
  • Lumen >= 5.8
  • Guzzle >= 6.3
  • illuminate/redis >= 5.8
  • predis/predis >= 1.1

通过Composer安装

$ composer require ruima/microservice-tool

或者,如果您喜欢,可以手动编辑 composer.json

{
    "require": {
        "ruima/microservice-tool": "^0.1.1"
    }
}

启动调度器

您需要将以下Cron条目添加到您的服务器。

* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1

修改服务环境(.env文件)

对于微服务

# (Require) the url of this service
APP_URL=http://your.app.url

# (Require) the url of gateway
MICROSERVICE_GATEWAY_URL = http://your.gateway.url

# (Require) the name of this microservice
MICROSERVICE_NAME = micro-data

# (Require) the type of this microservice 
# auth (basic microservice) | data (normal microservice)
MICROSERVICE_TYPE = data

# (Not Require) the description of this microservice
MICROSERVICE_DESCRIPTION = 'Data microservice Demo'

# (Not Require) the version of this microservice, default return lumen's version
MICROSERVICE_VERSION = ''

# (Not Require) the administrator's code when using permission middleware (the administrator can access all controller), default ADMIN
MICROSERVICE_ADMIN_CODE = ADMIN

对于认证微服务

您需要添加APP_KEY

APP_KEY=The only supported ciphers are AES-128-CBC or AES-256-CBC

对于网关

# The gateway use redis to cache the user auth info , you need to set the redis config
REDIS_HOST=redis
REDIS_PORT=6379
REDIS_PASSWORD=your_redis_password

# (Not Require) custom the microservices config list dir 
MICROSERVICE_CONFIG_DIR = /absolute/path/to/your/config/dir

# (Not Require) print how much time spend when gateway distribute request.
MICROSERVICE_GATEWAY_TIME_LOG = true

修改引导流程(bootstrap/app.php文件)

对于微服务

$app->singleton(
    'MicroserviceTool', function ($app) {
        return new Ruima\MicroserviceTool\Slaver($app);
    }
);

$app->routeMiddleware([
    'auth' => Ruima\MicroserviceTool\Middleware\FackAuth::class,
    'permission' => Ruima\MicroserviceTool\Middleware\Permission::class,
]);

$app->register(Ruima\MicroserviceTool\Provider\SlaverProvider::class);

对于网关

$app->register(Illuminate\Redis\RedisServiceProvider::class);

$app->register(Ruima\MicroserviceTool\Provider\MasterProvider::class);

已安装路由

对于微服务

对于网关

已安装计划任务和命令

对于微服务

对于网关

已安装方法

对于微服务

#use app('MicroserviceTool') to get the MicroserviceTool Object
app('MicroserviceTool')->{methods};

# return an Array for microservice infos
getSlaverInfo()

# send the request to tell gateway destory the user cache in gateway
destoryAuth() 

# return a String for target microservice url
getServerUrl(String $service_name) 

# return a http body which the target microserver callback
get(String $service_name, String $url = '/the/route/which/defind/in/web.php/or/api.php', Array $guzzle_config = []) 
post(String $service_name, String $url, Array $guzzle_config) 
delete(String $service_name, String $url, Array $guzzle_config) 
put(String $service_name, String $url, Array $guzzle_config) 

对于网关

 (Comming soon)

将请求分发到目标微服务

遵循以下规则

http://gateway.url/microservice.name/microservice.router.url

例如,有一个微服务,其中.env中的MICROSERVICE_NAME是"micro-auth",它有一个路由GET/auth-info。当发送请求时,使用以下URL,网关将自动处理。

http://loacal.gateway.com/micro-auth/auth-info

在微服务中使用授权

在认证微服务中

您需要添加一个路由"GET/auth-info",返回json格式的用户认证信息。当其他微服务需要认证信息时,网关将发送一个包含token的HTTP请求到"/auth-info",然后网关将使用token的最后64位作为key将响应缓存到Redis中。当认证信息更改时,您需要向网关发送一个请求"POST/distroy-auth",请求体为{ short_token (token的最后64位) }。如果short_token = "all",网关将清除所有缓存。

在API路由器中

$router->get('auth-info', 'AuthenticateController@getAuthInfo');

建议的用户认证信息格式

{
    "id": 1,
    "name": "admin",
    "username": "admin",
    "token": "9hkIE4iVo_60QbbB8t1Ho-jkde7gzLZDubcUifRrd4YINZJxnq9F3-c6nJFS6EAk",
    "roles": [
      "admin",
    ],
    "permissons": [
      "ADMIN",
    ]
}

在其他微服务中

在路由中使用中间件认证

在路由中

$router->get('/', [ 'middleware' => 'auth' , function () use ($router) {
    return $router->app->version();
}]);

在控制器中

public function index (Request $request) {
    $AUTH = $request->auth;
}
在路由中使用权限中间件

当使用权限中间件时,它将自动使用认证中间件,并检查$request->auth['permissions']是否有正确的权限。在多个权限的情况下,只要用户有一个权限,它就会通过权限检查。

$router->get('/', [ 'middleware' => 'permission:PA ADMIN RE' , function () use ($router) {
    return $router->app->version();
}]);