-
Notifications
You must be signed in to change notification settings - Fork 782
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
21a5218
commit 2fee25c
Showing
25 changed files
with
495 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>cn.dynamictp</groupId> | ||
<artifactId>dynamic-tp-adapter</artifactId> | ||
<version>1.0.9</version> | ||
</parent> | ||
<artifactId>dynamic-tp-adapter-brpc</artifactId> | ||
|
||
<properties> | ||
<brpc.version>2022.2.0</brpc.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>cn.dynamictp</groupId> | ||
<artifactId>dynamic-tp-adapter-common</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.baidu.cloud</groupId> | ||
<artifactId>spring-cloud-starter-baidu-starlight</artifactId> | ||
<version>${brpc.version}</version> | ||
<scope>compile</scope> | ||
<optional>true</optional> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
81 changes: 81 additions & 0 deletions
81
...ter/adapter-brpc/src/main/java/com/dtp/apapter/brpc/client/StarlightClientDtpAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package com.dtp.apapter.brpc.client; | ||
|
||
import cn.hutool.core.collection.CollUtil; | ||
import com.baidu.cloud.starlight.api.rpc.StarlightClient; | ||
import com.baidu.cloud.starlight.api.rpc.threadpool.ThreadPoolFactory; | ||
import com.baidu.cloud.starlight.core.rpc.SingleStarlightClient; | ||
import com.baidu.cloud.starlight.springcloud.client.cluster.SingleStarlightClientManager; | ||
import com.dtp.adapter.common.AbstractDtpAdapter; | ||
import com.dtp.common.ApplicationContextHolder; | ||
import com.dtp.common.dto.ExecutorWrapper; | ||
import com.dtp.common.properties.DtpProperties; | ||
import com.dtp.common.util.ReflectionUtil; | ||
import com.google.common.collect.Lists; | ||
import com.google.common.collect.Maps; | ||
import lombok.extern.slf4j.Slf4j; | ||
import lombok.val; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
/** | ||
* StarlightClientDtpAdapter related | ||
* | ||
* @author yanhom | ||
* @since 1.1.0 | ||
*/ | ||
@Slf4j | ||
public class StarlightClientDtpAdapter extends AbstractDtpAdapter { | ||
|
||
private static final String NAME = "brpcClientTp"; | ||
|
||
private static final String THREAD_POOL_FIELD = "threadPoolOfAll"; | ||
|
||
@Override | ||
public void refresh(DtpProperties dtpProperties) { | ||
refresh(NAME, dtpProperties.getBrpcTp(), dtpProperties.getPlatforms()); | ||
} | ||
|
||
@Override | ||
protected void initialize() { | ||
super.initialize(); | ||
|
||
SingleStarlightClientManager sscManager = null; | ||
Map<String, StarlightClient> scBeans = Maps.newHashMap(); | ||
try { | ||
sscManager = ApplicationContextHolder.getBean(SingleStarlightClientManager.class); | ||
scBeans = ApplicationContextHolder.getBeansOfType(StarlightClient.class); | ||
} catch (Exception e) { | ||
log.warn("getBean error, msg: {}", e.getMessage()); | ||
} | ||
|
||
List<StarlightClient> starlightClients = Lists.newArrayList(); | ||
if (CollUtil.isNotEmpty(scBeans)) { | ||
starlightClients.addAll(scBeans.values()); | ||
} | ||
if (Objects.nonNull(sscManager) && CollUtil.isNotEmpty(sscManager.allSingleClients())) { | ||
starlightClients.addAll(sscManager.allSingleClients().values()); | ||
} | ||
if (CollUtil.isEmpty(starlightClients)) { | ||
log.warn("Cannot find beans of type StarlightClient."); | ||
return; | ||
} | ||
|
||
starlightClients.forEach(v -> { | ||
val threadPoolFactory = (ThreadPoolFactory) ReflectionUtil.getFieldValue(SingleStarlightClient.class, | ||
THREAD_POOL_FIELD, v); | ||
if (Objects.isNull(threadPoolFactory)) { | ||
return; | ||
} | ||
String bizThreadPoolName = v.remoteURI().getParameter("biz_thread_pool_name") + "#client"; | ||
val executor = threadPoolFactory.defaultThreadPool(); | ||
if (Objects.nonNull(executor)) { | ||
val executorWrapper = new ExecutorWrapper(bizThreadPoolName, executor); | ||
initNotifyItems(bizThreadPoolName, executorWrapper); | ||
executors.put(bizThreadPoolName, executorWrapper); | ||
} | ||
}); | ||
log.info("DynamicTp adapter, brpc client executors init end, executors: {}", executors); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
...ter/adapter-brpc/src/main/java/com/dtp/apapter/brpc/server/StarlightServerDtpAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.dtp.apapter.brpc.server; | ||
|
||
import com.baidu.cloud.starlight.api.common.URI; | ||
import com.baidu.cloud.starlight.api.rpc.StarlightServer; | ||
import com.baidu.cloud.starlight.api.rpc.threadpool.ThreadPoolFactory; | ||
import com.baidu.cloud.starlight.api.transport.ServerPeer; | ||
import com.baidu.cloud.starlight.core.rpc.DefaultStarlightServer; | ||
import com.baidu.cloud.starlight.core.rpc.ServerProcessor; | ||
import com.baidu.cloud.starlight.transport.netty.NettyServer; | ||
import com.dtp.adapter.common.AbstractDtpAdapter; | ||
import com.dtp.common.ApplicationContextHolder; | ||
import com.dtp.common.dto.ExecutorWrapper; | ||
import com.dtp.common.properties.DtpProperties; | ||
import com.dtp.common.util.ReflectionUtil; | ||
import lombok.extern.slf4j.Slf4j; | ||
import lombok.val; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* StarlightServerDtpAdapter related | ||
* | ||
* @author yanhom | ||
* @since 1.1.0 | ||
*/ | ||
@Slf4j | ||
public class StarlightServerDtpAdapter extends AbstractDtpAdapter { | ||
|
||
private static final String NAME = "brpcServerTp"; | ||
|
||
private static final String URI_FIELD = "uri"; | ||
|
||
private static final String SERVER_PEER_FIELD = "serverPeer"; | ||
|
||
private static final String THREAD_POOL_FACTORY_FIELD = "threadPoolFactory"; | ||
|
||
@Override | ||
public void refresh(DtpProperties dtpProperties) { | ||
refresh(NAME, dtpProperties.getBrpcTp(), dtpProperties.getPlatforms()); | ||
} | ||
|
||
@Override | ||
protected void initialize() { | ||
super.initialize(); | ||
|
||
val bean = ApplicationContextHolder.getBean(StarlightServer.class); | ||
if (!(bean instanceof DefaultStarlightServer)) { | ||
return; | ||
} | ||
val starlightServer = (DefaultStarlightServer) bean; | ||
val uri = (URI) ReflectionUtil.getFieldValue(DefaultStarlightServer.class, | ||
URI_FIELD, starlightServer); | ||
val serverPeer = (ServerPeer) ReflectionUtil.getFieldValue(DefaultStarlightServer.class, | ||
SERVER_PEER_FIELD, starlightServer); | ||
|
||
if (Objects.isNull(uri) || Objects.isNull(serverPeer) || !(serverPeer instanceof NettyServer)) { | ||
return; | ||
} | ||
val processor = (ServerProcessor) serverPeer.getProcessor(); | ||
if (Objects.isNull(processor)) { | ||
return; | ||
} | ||
val threadPoolFactory = (ThreadPoolFactory) ReflectionUtil.getFieldValue(ServerProcessor.class, | ||
THREAD_POOL_FACTORY_FIELD, processor); | ||
if (Objects.isNull(threadPoolFactory)) { | ||
return; | ||
} | ||
String bizThreadPoolName = uri.getParameter("biz_thread_pool_name") + "#server"; | ||
val executor = threadPoolFactory.defaultThreadPool(); | ||
if (Objects.nonNull(executor)) { | ||
val executorWrapper = new ExecutorWrapper(bizThreadPoolName, executor); | ||
initNotifyItems(bizThreadPoolName, executorWrapper); | ||
executors.put(bizThreadPoolName, executorWrapper); | ||
} | ||
log.info("DynamicTp adapter, brpc server executors init end, executors: {}", executors); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
example/example-adapter/src/main/java/com/dtp/example/adapter/brpc/BrpcClientService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.dtp.example.adapter.brpc; | ||
|
||
import com.baidu.cloud.starlight.springcloud.client.annotation.RpcProxy; | ||
import org.springframework.stereotype.Service; | ||
|
||
/** | ||
* BrpcClientService related | ||
* | ||
* @author yanhom | ||
* @since 1.1.0 | ||
*/ | ||
@Service | ||
public class BrpcClientService { | ||
|
||
/** | ||
* 使用注解引用服务,指定服务端IP Port,采用brpc协议调用 | ||
*/ | ||
@RpcProxy(remoteUrl = "localhost:8777", protocol = "brpc") | ||
private UserService userService; | ||
|
||
/** | ||
* 使用注解引用服务,指定服务端IP Port,采用springrest(http)协议调用 | ||
*/ | ||
@RpcProxy(remoteUrl = "localhost:8777", protocol = "springrest") | ||
private UserService restUserService; | ||
|
||
public String getUserName(Long userId) { | ||
return userService.getUserName(userId); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
example/example-adapter/src/main/java/com/dtp/example/adapter/brpc/UserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.dtp.example.adapter.brpc; | ||
|
||
/** | ||
* UserService related | ||
* | ||
* @author yanhom | ||
* @since 1.1.0 | ||
*/ | ||
public interface UserService { | ||
|
||
String getUserName(Long userId); | ||
} |
Oops, something went wrong.