Skip to content

Commit

Permalink
Merge pull request #154 from cmgyqjj/feature_Use_Spring_IOC_to_refact…
Browse files Browse the repository at this point in the history
…or_RpcProxyManager

feature:Use_Spring_IOC_to_refactor_RpcProxyManager
  • Loading branch information
crossoverJie authored Sep 21, 2024
2 parents 508b916 + 211ebf2 commit 1128d0c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.crossoverjie.cim.route.config;

import com.crossoverjie.cim.common.core.proxy.RpcProxyManager;
import com.crossoverjie.cim.common.metastore.MetaStore;
import com.crossoverjie.cim.common.metastore.ZkConfiguration;
import com.crossoverjie.cim.common.metastore.ZkMetaStoreImpl;
import com.crossoverjie.cim.common.route.algorithm.RouteHandle;
import com.crossoverjie.cim.common.route.algorithm.consistenthash.AbstractConsistentHash;
import com.crossoverjie.cim.server.api.ServerApi;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import org.I0Itec.zkclient.ZkClient;
Expand Down Expand Up @@ -35,7 +38,7 @@
public class BeanConfig {


@Autowired
@Resource
private AppConfiguration appConfiguration;


Expand Down Expand Up @@ -105,4 +108,9 @@ public RouteHandle buildRouteHandle() throws Exception {
}

}

@Bean
public ServerApi serverApi(OkHttpClient okHttpClient) {
return RpcProxyManager.create(ServerApi.class, okHttpClient);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.crossoverjie.cim.route.service.UserInfoCacheService;
import com.crossoverjie.cim.server.api.ServerApi;
import com.crossoverjie.cim.server.api.vo.req.SendMsgReqVO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.Response;
Expand Down Expand Up @@ -46,15 +47,18 @@
@Service
public class AccountServiceRedisImpl implements AccountService {

@Autowired
@Resource
private RedisTemplate<String, String> redisTemplate;

@Autowired
@Resource
private UserInfoCacheService userInfoCacheService;

@Autowired
@Resource
private OkHttpClient okHttpClient;

@Resource
private ServerApi serverApi;

@Override
public RegisterInfoResVO register(RegisterInfoResVO info) {
String key = ACCOUNT_PREFIX + info.getUserId();
Expand Down Expand Up @@ -153,7 +157,7 @@ public void pushMsg(CIMServerResVO cimServerResVO, long sendUserId, ChatReqVO gr
CIMUserInfo cimUserInfo = userInfoCacheService.loadUserInfoByUserId(sendUserId);

String url = "http://" + cimServerResVO.getIp() + ":" + cimServerResVO.getHttpPort();
ServerApi serverApi = RpcProxyManager.create(ServerApi.class, okHttpClient);

SendMsgReqVO vo = new SendMsgReqVO(cimUserInfo.getUserName() + ":" + groupReqVO.getMsg(), groupReqVO.getUserId());
serverApi.sendMsg(vo, url);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.crossoverjie.cim.server.config;

import com.crossoverjie.cim.common.constant.Constants;
import com.crossoverjie.cim.common.core.proxy.RpcProxyManager;
import com.crossoverjie.cim.common.metastore.MetaStore;
import com.crossoverjie.cim.common.metastore.ZkConfiguration;
import com.crossoverjie.cim.common.metastore.ZkMetaStoreImpl;
import com.crossoverjie.cim.common.protocol.CIMRequestProto;
import com.crossoverjie.cim.route.api.RouteApi;
import jakarta.annotation.Resource;
import okhttp3.OkHttpClient;
import org.I0Itec.zkclient.ZkClient;
import org.apache.curator.retry.ExponentialBackoffRetry;
Expand All @@ -24,6 +27,9 @@
@Configuration
public class BeanConfig {

@Resource
private AppConfiguration appConfiguration;

/**
* http client
* @return okHttp
Expand Down Expand Up @@ -57,4 +63,9 @@ public CIMRequestProto.CIMReqProtocol heartBeat() {
.build();
return heart;
}

@Bean
public RouteApi routeApi(OkHttpClient okHttpClient) {
return RpcProxyManager.create(RouteApi.class, appConfiguration.getRouteUrl(), okHttpClient);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.crossoverjie.cim.server.config.AppConfiguration;
import com.crossoverjie.cim.server.util.SessionSocketHolder;
import io.netty.channel.socket.nio.NioSocketChannel;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.Response;
Expand All @@ -26,12 +27,15 @@
@Slf4j
public class RouteHandler {

@Autowired
@Resource
private OkHttpClient okHttpClient;

@Autowired
@Resource
private AppConfiguration configuration;

@Resource
private RouteApi routeApi;

/**
* 用户下线
*
Expand All @@ -57,7 +61,6 @@ public void userOffLine(CIMUserInfo userInfo, NioSocketChannel channel) {
* @throws IOException
*/
public void clearRouteInfo(CIMUserInfo userInfo) {
RouteApi routeApi = RpcProxyManager.create(RouteApi.class, configuration.getRouteUrl(), okHttpClient);
ChatReqVO vo = new ChatReqVO(userInfo.getUserId(), userInfo.getUserName());
routeApi.offLine(vo);
}
Expand Down

0 comments on commit 1128d0c

Please sign in to comment.