Tag Archives: swoole

Error while injecting dependencies into App\Controller\IndexController: No entry or class found for

An error occurs when a service consumer class is manually created for the hyperf 2.2.0 microservice.

In the documentation on the official website, when manually creating a consumer class, its recommended configuration of services.php is as follows.

<?php
return [
    'consumers' => [
        [
            'name' => 'CalculatorService',
            'registry' => [
                'protocol' => 'consul',
                'address' => 'http://127.0.0.1:8500',
            ],
            'nodes' => [
                ['host' => '127.0.0.1', 'port' => 9504],
            ],
        ]
    ],
];

Later, I saw the video on the official website jsonrpc. The recommendation in the video is as follows.

<?php
return [
    'consumers' => [
        [
            'name' => 'CalculatorService',
            'nodes' => [
                ['host' => '127.0.0.1', 'port' => 9504],
            ],
        ]
    ],
];

I wonder if the above writing is only applicable to the 1. X.x era?When I upgrade to 2.2.0, it is reported above

[ERROR] Error while injecting dependencies into App\Controller\IndexController: No entry or class found for ‘App\Rpc\CalculatorServiceInterface'[101]

After tossing for a long time, I found that there are few services.php configurations

    'service' => \App\Rpc\CalculatorServiceInterface::class,

So, don’t lose service. The complete services.php is like this

return [
    'consumers' => [
        [
            // name needs to be the same as the name attribute of the service provider
            'name' => 'CalculatorService',
            // service interface name, optional, default value is equal to the value configured by name, if name is defined directly as an interface class then this line can be ignored, if name is a string then you need to configure service to correspond to the interface class
            'service' => \App\Rpc\CalculatorServiceInterface::class,
            // corresponding to the container object ID, optional, default value is equal to the service configuration value, used to define the dependency injection key
            'id' => \App\Rpc\CalculatorServiceInterface::class,
            // Service protocol of the service provider, optional, default value is jsonrpc-http
            // optional jsonrpc-http jsonrpc jsonrpc-tcp-length-check
            'protocol' => 'jsonrpc-http',
            // load balancing algorithm, optional, default value is random
// 'load_balancer' => 'random',
            // which service center this consumer will get the node information from, if not configured it will not get the node information from the service center
// 'registry' => [
// 'protocol' => 'consul',
// 'address' => 'http://127.0.0.1:8500',
// ],
            // If the above registry configuration is not specified, i.e., the node is consumed directly for the specified node, and the node information of the service provider is configured with the following nodes parameter
            'nodes' => [
                ['host' => '127.0.0.1', 'port' => 9504],
            ],
        ]
    ],
];

Run later and run through