Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature:Use_Spring_IOC_to_refactor_RpcProxyManager #154

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading