diff --git a/.env b/.env index dbb7c6e..17d1a18 100644 --- a/.env +++ b/.env @@ -24,6 +24,6 @@ NACOS_HOST=192.168.28.199 NACOS_PORT=8848 NACOS_USERNAME=nacos NACOS_PASSWORD=nacos -NACOS_NAMESPACE=e42b853c-5195-478b-b5e3-6d49f6a45053 -NACOS_GROUP=dev +NACOS_NAMESPACE_ID=e42b853c-5195-478b-b5e3-6d49f6a45053 +NACOS_GROUP=api NACOS_SERVICE_NAME=DataCampusService \ No newline at end of file diff --git a/app/Controller/CampusController.php b/app/Controller/CampusController.php deleted file mode 100644 index f3c119b..0000000 --- a/app/Controller/CampusController.php +++ /dev/null @@ -1,98 +0,0 @@ -service->list($request->all())); - } - - /** - * 获取省份列表. - */ - #[GetMapping(path: 'campus/provinces')] - public function provinces(): array - { - return AetherResponse::success($this->service->getProvinces()); - } - - /** - * 获取城市列表. - */ - #[GetMapping(path: 'campus/cities')] - public function cities(RequestInterface $request): array - { - return AetherResponse::success($this->service->getCitiesByProvince($request->input('province_id'))); - } - - /** - * 创建校区. - * @throws Throwable - */ - #[PostMapping(path: 'campus')] - public function create(): array - { - return AetherResponse::success($this->service->create($this->request->all()), '校区创建成功'); - } - - /** - * 获取校区详情. - */ - #[GetMapping(path: 'campus/{id}')] - public function get(int $id): array - { - return AetherResponse::success($this->service->detail($id)); - } - - /** - * 更新校区. - * @throws Throwable - */ - #[PutMapping(path: 'campus/{id}')] - public function update(int $id): array - { - return AetherResponse::success($this->service->update($id, $this->request->all()), '校区更新成功'); - } - - /** - * 删除校区. - * @throws Throwable - */ - #[DeleteMapping(path: 'campus/{id}')] - public function delete(int $id): array - { - return AetherResponse::success($this->service->delete($id), '校区删除成功'); - } - - protected function getService(): AetherCrudService - { - return $this->service; - } -} diff --git a/app/Controller/TeacherController.php b/app/Controller/TeacherController.php deleted file mode 100644 index f80f086..0000000 --- a/app/Controller/TeacherController.php +++ /dev/null @@ -1,81 +0,0 @@ -service->list($request->all())); - } - - /** - * 创建教师. - * @throws Throwable - */ - #[PostMapping(path: 'teacher')] - public function create(): array - { - return AetherResponse::success($this->service->create($this->request->all()), '教师创建成功'); - } - - /** - * 获取教师详情. - */ - #[GetMapping(path: 'teacher/{id}')] - public function get(int $id): array - { - return AetherResponse::success($this->service->detail($id)); - } - - /** - * 更新教师. - * @throws Throwable - */ - #[PutMapping(path: 'teacher/{id}')] - public function update(int $id): array - { - return AetherResponse::success($this->service->update($id, $this->request->all()), '教师更新成功'); - } - - /** - * 删除教师. - * @throws Throwable - */ - #[DeleteMapping(path: 'teacher/{id}')] - public function delete(int $id): array - { - return AetherResponse::success($this->service->delete($id), '教师删除成功'); - } - - protected function getService(): AetherCrudService - { - return $this->service; - } -} diff --git a/app/JsonRpc/Service/CampusService.php b/app/JsonRpc/Service/CampusService.php deleted file mode 100644 index d59f6e2..0000000 --- a/app/JsonRpc/Service/CampusService.php +++ /dev/null @@ -1,84 +0,0 @@ -status != 1) { - throw new BusinessException('校区不存在或已禁用', 10001); - } - return $campus->toArray(); - } - - public function getCampusesByIds(array $ids): array - { - return Campus::whereIn('id', $ids) - ->enabled() - ->get() - ->toArray(); - } - - public function getCampusesByParentId(int $parentId): array - { - return Campus::where('parent_id', $parentId) - ->enabled() - ->get() - ->toArray(); - } - - public function getProvinces(): array - { - return Campus::level(1) - ->enabled() - ->orderBy('name') - ->get(['id', 'name', 'province']) - ->toArray(); - } - - public function getCitiesByProvince(string $province): array - { - return Campus::level(2) - ->province($province) - ->enabled() - ->orderBy('name') - ->get(['id', 'name', 'city']) - ->toArray(); - } - - protected function getModel(): AetherModel - { - return $this->model; - } - - protected function getValidator(): AetherValidator - { - return $this->validator; - } -} diff --git a/app/JsonRpc/Service/TeacherService.php b/app/JsonRpc/Service/DataService.php similarity index 59% rename from app/JsonRpc/Service/TeacherService.php rename to app/JsonRpc/Service/DataService.php index 7c2d157..b66460d 100644 --- a/app/JsonRpc/Service/TeacherService.php +++ b/app/JsonRpc/Service/DataService.php @@ -4,29 +4,66 @@ declare(strict_types=1); namespace App\JsonRpc\Service; -use Aether\AetherCrudService; -use Aether\AetherModel; -use Aether\AetherValidator; use Aether\Exception\BusinessException; +use App\Model\Campus; use App\Model\Teacher; -use App\Validator\TeacherValidator; +use App\Validator\CampusValidator; use Hyperf\Di\Annotation\Inject; use Hyperf\RpcServer\Annotation\RpcService; -use MicroService\Contract\TeacherServiceInterface; +use MicroService\Contract\DataServiceInterface; -#[RpcService( - name: 'DataTeacher', - server: 'jsonrpc-http', - protocol: 'jsonrpc-http', - publishTo: 'nacos' -)] -class TeacherService extends AetherCrudService implements TeacherServiceInterface +#[RpcService(name: 'DataService', server: 'jsonrpc-http', protocol: 'jsonrpc-http', publishTo: 'nacos')] +class DataService implements DataServiceInterface { #[Inject] - protected Teacher $model; + protected Campus $campusModel; #[Inject] - protected TeacherValidator $validator; + protected CampusValidator $campusValidator; + + public function getCampusById(int $id): array + { + $campus = Campus::find($id); + if (! $campus || $campus->status != 1) { + throw new BusinessException('校区不存在或已禁用', 10001); + } + return $campus->toArray(); + } + + public function getCampusesByIds(array $ids): array + { + return Campus::whereIn('id', $ids) + ->enabled() + ->get() + ->toArray(); + } + + public function getCampusesByParentId(int $parentId): array + { + return Campus::where('parent_id', $parentId) + ->enabled() + ->get() + ->toArray(); + } + + public function getProvinces(): array + { + return Campus::level(1) + ->enabled() + ->orderBy('name') + ->get(['id', 'name', 'province']) + ->toArray(); + } + + public function getCitiesByProvince(string $province): array + { + return Campus::level(2) + ->province($province) + ->enabled() + ->orderBy('name') + ->get(['id', 'name', 'city']) + ->toArray(); + } public function getTeacherById(int $id): array { @@ -96,14 +133,4 @@ class TeacherService extends AetherCrudService implements TeacherServiceInterfac 'list' => $list, ]; } - - protected function getModel(): AetherModel - { - return $this->model; - } - - protected function getValidator(): AetherValidator - { - return $this->validator; - } } diff --git a/config/autoload/cache.php b/config/autoload/cache.php index 24d0e5b..70aca78 100644 --- a/config/autoload/cache.php +++ b/config/autoload/cache.php @@ -1,18 +1,21 @@ [ - 'driver' => Hyperf\Cache\Driver\RedisDriver::class, - 'packer' => Hyperf\Codec\Packer\PhpSerializerPacker::class, + 'driver' => RedisDriver::class, + 'packer' => PhpSerializerPacker::class, 'prefix' => 'c:', 'skip_cache_results' => [], ], diff --git a/config/autoload/devtool.php b/config/autoload/devtool.php index 3c2f607..5e7e4b9 100644 --- a/config/autoload/devtool.php +++ b/config/autoload/devtool.php @@ -1,44 +1,36 @@ [ 'amqp' => [ 'consumer' => [ - 'namespace' => 'App\\Amqp\\Consumer', + 'namespace' => 'App\Amqp\Consumer', ], 'producer' => [ - 'namespace' => 'App\\Amqp\\Producer', + 'namespace' => 'App\Amqp\Producer', ], ], 'aspect' => [ - 'namespace' => 'App\\Aspect', + 'namespace' => 'App\Aspect', ], 'command' => [ - 'namespace' => 'App\\Command', + 'namespace' => 'App\Command', ], 'controller' => [ - 'namespace' => 'App\\Controller', + 'namespace' => 'App\Controller', ], 'job' => [ - 'namespace' => 'App\\Job', + 'namespace' => 'App\Job', ], 'listener' => [ - 'namespace' => 'App\\Listener', + 'namespace' => 'App\Listener', ], 'middleware' => [ - 'namespace' => 'App\\Middleware', + 'namespace' => 'App\Middleware', ], 'Process' => [ - 'namespace' => 'App\\Processes', + 'namespace' => 'App\Processes', ], ], ]; diff --git a/config/autoload/exceptions.php b/config/autoload/exceptions.php index d331de6..437cccd 100644 --- a/config/autoload/exceptions.php +++ b/config/autoload/exceptions.php @@ -2,20 +2,11 @@ declare(strict_types=1); -use Aether\ApiExceptionHandler; -use Aether\GlobalExceptionHandler; use Aether\RpcExceptionHandler; -use Hyperf\HttpServer\Exception\Handler\HttpExceptionHandler; return [ 'handler' => [ - 'http' => [ - // HttpExceptionHandler::class, - // ApiExceptionHandler::class, - GlobalExceptionHandler::class, - ], 'jsonrpc-http' => [ - HttpExceptionHandler::class, RpcExceptionHandler::class, ], ], diff --git a/config/autoload/listeners.php b/config/autoload/listeners.php index 8a2c7a2..a25251b 100644 --- a/config/autoload/listeners.php +++ b/config/autoload/listeners.php @@ -1,15 +1,18 @@ [ 'handler' => [ - 'class' => Monolog\Handler\StreamHandler::class, + 'class' => StreamHandler::class, 'constructor' => [ 'stream' => BASE_PATH . '/runtime/logs/hyperf.log', - 'level' => Monolog\Logger::DEBUG, + 'level' => Logger::DEBUG, ], ], 'formatter' => [ - 'class' => Monolog\Formatter\LineFormatter::class, + 'class' => LineFormatter::class, 'constructor' => [ 'format' => null, 'dateFormat' => 'Y-m-d H:i:s', diff --git a/config/autoload/server.php b/config/autoload/server.php index ed76312..1664d4d 100644 --- a/config/autoload/server.php +++ b/config/autoload/server.php @@ -15,20 +15,6 @@ use function Hyperf\Support\env; return [ 'mode' => SWOOLE_PROCESS, 'servers' => [ - // [ - // 'name' => 'http', - // 'type' => Server::SERVER_HTTP, - // 'host' => '0.0.0.0', - // 'port' => 9501, - // 'sock_type' => SWOOLE_SOCK_TCP, - // 'callbacks' => [ - // Event::ON_REQUEST => [Hyperf\HttpServer\Server::class, 'onRequest'], - // ], - // 'options' => [ - // // Whether to enable request lifecycle event - // 'enable_request_lifecycle' => false, - // ], - // ], [ 'name' => 'jsonrpc-http', 'type' => Server::SERVER_HTTP, @@ -39,7 +25,7 @@ return [ Event::ON_REQUEST => [JsonRpcHttpServer::class, 'onRequest'], ], 'settings' => [ - // 'open_eof_split' => true, + 'open_eof_split' => true, 'package_eof' => "\r\n", 'package_max_length' => 1024 * 1024 * 2, ], diff --git a/config/autoload/services.php b/config/autoload/services.php index 8b0d1f9..785609a 100644 --- a/config/autoload/services.php +++ b/config/autoload/services.php @@ -1,62 +1,56 @@ true, - 'driver' => 'nacos', + 'enable' => [ + 'discovery' => true, + 'register' => true, + ], + 'consumers' => [ + // 数据服务消费者配置 + [ + 'name' => 'DataService', + 'service' => DataServiceInterface::class, + 'registry' => [ + 'protocol' => 'nacos', + 'address' => 'http://192.168.28.199:8848/', + ], + 'protocol' => 'jsonrpc-http', + 'options' => [ + 'timeout' => 0.5, + ], + ], + ], + 'providers' => [ + [ + 'id' => 'hyperf-gateway', + 'service' => 'hyperf-gateway', + 'protocol' => 'jsonrpc-http', + 'load_balancer' => 'random', + 'registry' => [ + 'protocol' => 'nacos', + 'address' => 'http://192.168.28.199:8848', + ], + ], + ], 'drivers' => [ 'nacos' => [ // nacos server url like https://nacos.hyperf.io, Priority is higher than host:port // 'url' => '', // The nacos host info - 'host' => env('NACOS_HOST', '127.0.0.1'), + 'host' => env('NACOS_HOST', '192.168.28.199'), 'port' => env('NACOS_PORT', 8848), // The nacos account info - 'username' => null, - 'password' => null, + // 'username' => null, + // 'password' => null, 'guzzle' => [ 'config' => null, ], - 'group_name' => 'api', - 'namespace_id' => env('NACOS_NAMESPACE', 'dev'), + 'group_name' => env('NACOS_GROUP', 'api'), + 'namespace_id' => env('NACOS_NAMESPACE_ID', 'dev'), 'heartbeat' => 5, ], ], - 'registries' => [ - [ - 'name' => 'DataCampus', - 'host' => env('NACOS_HOST', '192.168.28.199'), - 'port' => env('NACOS_PORT', 8848), - // 'username' => env('NACOS_USERNAME', 'nacos'), - // 'password' => env('NACOS_PASSWORD', 'nacos'), - 'namespace' => env('NACOS_NAMESPACE', 'e42b853c-5195-478b-b5e3-6d49f6a45053'), - 'group_name' => env('NACOS_GROUP', 'DEFAULT_GROUP'), - 'service_name' => 'DataCampus', - 'metadata' => [ - 'protocol' => 'jsonrpc-http', - 'server' => 'jsonrpc-http', - ], - 'heartbeat' => 5, - 'heartbeat_timeout' => 15, - ], - [ - 'name' => 'DataTeacherService', - 'host' => env('NACOS_HOST', '192.168.28.199'), - 'port' => env('NACOS_PORT', 8848), - // 'username' => env('NACOS_USERNAME', 'nacos'), - // 'password' => env('NACOS_PASSWORD', 'nacos'), - 'namespace' => env('NACOS_NAMESPACE', 'e42b853c-5195-478b-b5e3-6d49f6a45053'), - 'group_name' => env('NACOS_GROUP', 'DEFAULT_GROUP'), - 'service_name' => 'DataTeacherService', - 'metadata' => [ - 'protocol' => 'jsonrpc-http', - 'server' => 'jsonrpc-http', - ], - 'heartbeat' => 5, - 'heartbeat_timeout' => 15, - ], - ], ]; diff --git a/extend/MicroService/src/Contract/CampusServiceInterface.php b/extend/MicroService/src/Contract/CampusServiceInterface.php deleted file mode 100644 index c98e72a..0000000 --- a/extend/MicroService/src/Contract/CampusServiceInterface.php +++ /dev/null @@ -1,37 +0,0 @@ -