1
- package github .javaguide .provider ;
1
+ package github .javaguide .provider . impl ;
2
2
3
- import github .javaguide .entity . RpcServiceProperties ;
3
+ import github .javaguide .config . RpcServiceConfig ;
4
4
import github .javaguide .enums .RpcErrorMessageEnum ;
5
5
import github .javaguide .exception .RpcException ;
6
6
import github .javaguide .extension .ExtensionLoader ;
7
+ import github .javaguide .provider .ServiceProvider ;
7
8
import github .javaguide .registry .ServiceRegistry ;
8
9
import github .javaguide .remoting .transport .netty .server .NettyRpcServer ;
9
10
import lombok .extern .slf4j .Slf4j ;
20
21
* @createTime 2020年05月13日 11:23:00
21
22
*/
22
23
@ Slf4j
23
- public class ServiceProviderImpl implements ServiceProvider {
24
+ public class ZkServiceProviderImpl implements ServiceProvider {
24
25
25
26
/**
26
27
* key: rpc service name(interface name + version + group)
@@ -30,47 +31,38 @@ public class ServiceProviderImpl implements ServiceProvider {
30
31
private final Set <String > registeredService ;
31
32
private final ServiceRegistry serviceRegistry ;
32
33
33
-
34
- public ServiceProviderImpl () {
34
+ public ZkServiceProviderImpl () {
35
35
serviceMap = new ConcurrentHashMap <>();
36
36
registeredService = ConcurrentHashMap .newKeySet ();
37
37
serviceRegistry = ExtensionLoader .getExtensionLoader (ServiceRegistry .class ).getExtension ("zk" );
38
38
}
39
39
40
40
@ Override
41
- public void addService (Object service , Class <?> serviceClass , RpcServiceProperties rpcServiceProperties ) {
42
- String rpcServiceName = rpcServiceProperties . toRpcServiceName ();
41
+ public void addService (RpcServiceConfig rpcServiceConfig ) {
42
+ String rpcServiceName = rpcServiceConfig . getRpcServiceName ();
43
43
if (registeredService .contains (rpcServiceName )) {
44
44
return ;
45
45
}
46
46
registeredService .add (rpcServiceName );
47
- serviceMap .put (rpcServiceName , service );
48
- log .info ("Add service: {} and interfaces:{}" , rpcServiceName , service .getClass ().getInterfaces ());
47
+ serviceMap .put (rpcServiceName , rpcServiceConfig . getService () );
48
+ log .info ("Add service: {} and interfaces:{}" , rpcServiceName , rpcServiceConfig . getService () .getClass ().getInterfaces ());
49
49
}
50
50
51
51
@ Override
52
- public Object getService (RpcServiceProperties rpcServiceProperties ) {
53
- Object service = serviceMap .get (rpcServiceProperties . toRpcServiceName () );
52
+ public Object getService (String rpcServiceName ) {
53
+ Object service = serviceMap .get (rpcServiceName );
54
54
if (null == service ) {
55
55
throw new RpcException (RpcErrorMessageEnum .SERVICE_CAN_NOT_BE_FOUND );
56
56
}
57
57
return service ;
58
58
}
59
59
60
60
@ Override
61
- public void publishService (Object service ) {
62
- this .publishService (service , RpcServiceProperties .builder ().group ("" ).version ("" ).build ());
63
- }
64
-
65
- @ Override
66
- public void publishService (Object service , RpcServiceProperties rpcServiceProperties ) {
61
+ public void publishService (RpcServiceConfig rpcServiceConfig ) {
67
62
try {
68
63
String host = InetAddress .getLocalHost ().getHostAddress ();
69
- Class <?> serviceRelatedInterface = service .getClass ().getInterfaces ()[0 ];
70
- String serviceName = serviceRelatedInterface .getCanonicalName ();
71
- rpcServiceProperties .setServiceName (serviceName );
72
- this .addService (service , serviceRelatedInterface , rpcServiceProperties );
73
- serviceRegistry .registerService (rpcServiceProperties .toRpcServiceName (), new InetSocketAddress (host , NettyRpcServer .PORT ));
64
+ this .addService (rpcServiceConfig );
65
+ serviceRegistry .registerService (rpcServiceConfig .getRpcServiceName (), new InetSocketAddress (host , NettyRpcServer .PORT ));
74
66
} catch (UnknownHostException e ) {
75
67
log .error ("occur exception when getHostAddress" , e );
76
68
}
0 commit comments