diff --git a/hmily-bom/pom.xml b/hmily-bom/pom.xml index ba4aa8b5..7a49b195 100644 --- a/hmily-bom/pom.xml +++ b/hmily-bom/pom.xml @@ -71,6 +71,7 @@ 5.0.7.RELEASE Finchley.RELEASE 2.6.2 + 3.0.2 2.7.1 5.5.3 3.8.0 @@ -208,6 +209,23 @@ ${apache.dubbo.version} + + + com.baidu + brpc-java + ${brpc.version} + + + com.baidu + brpc-spring + ${brpc.version} + + + com.baidu + brpc-spring-boot-starter + ${brpc.version} + + com.alipay.sofa sofa-rpc-all diff --git a/hmily-demo/hmily-demo-brpc/hmily-demo-brpc-account/pom.xml b/hmily-demo/hmily-demo-brpc/hmily-demo-brpc-account/pom.xml index d1ed2b1b..7c1be988 100644 --- a/hmily-demo/hmily-demo-brpc/hmily-demo-brpc-account/pom.xml +++ b/hmily-demo/hmily-demo-brpc/hmily-demo-brpc-account/pom.xml @@ -12,6 +12,14 @@ jar + + org.dromara + hmily-demo-common + + + com.baidu + brpc-java-naming-zookeeper + org.projectlombok lombok @@ -19,8 +27,13 @@ org.dromara - hmily-tac-p6spy - 2.1.2-SNAPSHOT + hmily-spring-boot-starter-brpc + + + com.ctrip.framework.apollo + apollo-client + + diff --git a/hmily-demo/hmily-demo-brpc/hmily-demo-brpc-account/src/main/java/org/dromara/hmily/demo/brpc/account/config/HmilyTacDatasourceConfig.java b/hmily-demo/hmily-demo-brpc/hmily-demo-brpc-account/src/main/java/org/dromara/hmily/demo/brpc/account/config/HmilyTacDatasourceConfig.java index ad26697f..c9d200e9 100644 --- a/hmily-demo/hmily-demo-brpc/hmily-demo-brpc-account/src/main/java/org/dromara/hmily/demo/brpc/account/config/HmilyTacDatasourceConfig.java +++ b/hmily-demo/hmily-demo-brpc/hmily-demo-brpc-account/src/main/java/org/dromara/hmily/demo/brpc/account/config/HmilyTacDatasourceConfig.java @@ -18,6 +18,7 @@ package org.dromara.hmily.demo.brpc.account.config; import com.zaxxer.hikari.HikariDataSource; + import javax.sql.DataSource; import org.dromara.hmily.tac.p6spy.HmilyP6Datasource; @@ -33,9 +34,9 @@ import org.springframework.context.annotation.Primary; */ @Configuration public class HmilyTacDatasourceConfig { - + private final DataSourceProperties dataSourceProperties; - + /** * Instantiates a new Hmily tac datasource config. * @@ -44,7 +45,7 @@ public class HmilyTacDatasourceConfig { public HmilyTacDatasourceConfig(DataSourceProperties dataSourceProperties) { this.dataSourceProperties = dataSourceProperties; } - + /** * Data source data source. * @@ -65,4 +66,5 @@ public class HmilyTacDatasourceConfig { hikariDataSource.setMaxLifetime(1800000); return new HmilyP6Datasource(hikariDataSource); } + } diff --git a/hmily-demo/hmily-demo-brpc/hmily-demo-brpc-account/src/main/resources/application.yml b/hmily-demo/hmily-demo-brpc/hmily-demo-brpc-account/src/main/resources/application.yml index 562b8163..ac3d8173 100644 --- a/hmily-demo/hmily-demo-brpc/hmily-demo-brpc-account/src/main/resources/application.yml +++ b/hmily-demo/hmily-demo-brpc/hmily-demo-brpc-account/src/main/resources/application.yml @@ -41,4 +41,4 @@ brpc: server: port: 8003 workThreadNum: 1 - ioThreadNum: 1 \ No newline at end of file + ioThreadNum: 1 diff --git a/hmily-demo/hmily-demo-brpc/hmily-demo-brpc-inventory/pom.xml b/hmily-demo/hmily-demo-brpc/hmily-demo-brpc-inventory/pom.xml index e87e2588..1d4a17fc 100644 --- a/hmily-demo/hmily-demo-brpc/hmily-demo-brpc-inventory/pom.xml +++ b/hmily-demo/hmily-demo-brpc/hmily-demo-brpc-inventory/pom.xml @@ -5,7 +5,7 @@ hmily-demo-brpc org.dromara - 1.0.0-SNAPSHOT + 2.0.2-RELEASE 4.0.0 hmily-demo-brpc-inventory @@ -13,14 +13,22 @@ - org.projectlombok - lombok - provided + org.dromara + hmily-demo-common org.dromara - hmily-tac-p6spy - 2.1.2-SNAPSHOT + hmily-brpc + ${hmily.version} + + + com.baidu + brpc-java-naming-zookeeper + + + org.projectlombok + lombok + provided diff --git a/hmily-demo/hmily-demo-brpc/hmily-demo-brpc-inventory/src/main/java/org/dromara/hmily/demo/brpc/inventory/service/InventoryServiceImpl.java b/hmily-demo/hmily-demo-brpc/hmily-demo-brpc-inventory/src/main/java/org/dromara/hmily/demo/brpc/inventory/service/InventoryServiceImpl.java index c2f39ada..335074a3 100644 --- a/hmily-demo/hmily-demo-brpc/hmily-demo-brpc-inventory/src/main/java/org/dromara/hmily/demo/brpc/inventory/service/InventoryServiceImpl.java +++ b/hmily-demo/hmily-demo-brpc/hmily-demo-brpc-inventory/src/main/java/org/dromara/hmily/demo/brpc/inventory/service/InventoryServiceImpl.java @@ -29,7 +29,6 @@ import org.dromara.hmily.demo.common.inventory.mapper.InventoryMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; diff --git a/hmily-demo/hmily-demo-brpc/hmily-demo-brpc-order/pom.xml b/hmily-demo/hmily-demo-brpc/hmily-demo-brpc-order/pom.xml index 475140b2..69bae4b4 100644 --- a/hmily-demo/hmily-demo-brpc/hmily-demo-brpc-order/pom.xml +++ b/hmily-demo/hmily-demo-brpc/hmily-demo-brpc-order/pom.xml @@ -5,7 +5,7 @@ hmily-demo-brpc org.dromara - 1.0.0-SNAPSHOT + 2.0.2-RELEASE 4.0.0 hmily-demo-brpc-order @@ -16,6 +16,14 @@ + + org.dromara + hmily-demo-common + + + com.baidu + brpc-java-naming-zookeeper + org.projectlombok lombok @@ -23,8 +31,14 @@ org.dromara - hmily-tac-p6spy - 2.1.2-SNAPSHOT + hmily-spring-boot-starter-brpc + ${hmily.version} + + + com.ctrip.framework.apollo + apollo-client + + io.springfox diff --git a/hmily-demo/hmily-demo-brpc/hmily-demo-brpc-order/src/main/java/org/dromara/hmily/demo/brpc/order/BrpcHmilyOrderApplication.java b/hmily-demo/hmily-demo-brpc/hmily-demo-brpc-order/src/main/java/org/dromara/hmily/demo/brpc/order/BrpcHmilyOrderApplication.java index e7fbd785..9e3cc8a0 100644 --- a/hmily-demo/hmily-demo-brpc/hmily-demo-brpc-order/src/main/java/org/dromara/hmily/demo/brpc/order/BrpcHmilyOrderApplication.java +++ b/hmily-demo/hmily-demo-brpc/hmily-demo-brpc-order/src/main/java/org/dromara/hmily/demo/brpc/order/BrpcHmilyOrderApplication.java @@ -20,7 +20,6 @@ package org.dromara.hmily.demo.brpc.order; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.annotation.ImportResource; /** * DubboTccOrderApplication. diff --git a/hmily-demo/hmily-demo-brpc/hmily-demo-brpc-order/src/main/java/org/dromara/hmily/demo/brpc/order/configuration/HmilyTacDatasourceConfig.java b/hmily-demo/hmily-demo-brpc/hmily-demo-brpc-order/src/main/java/org/dromara/hmily/demo/brpc/order/configuration/HmilyTacDatasourceConfig.java index 536b1114..a603f7ee 100644 --- a/hmily-demo/hmily-demo-brpc/hmily-demo-brpc-order/src/main/java/org/dromara/hmily/demo/brpc/order/configuration/HmilyTacDatasourceConfig.java +++ b/hmily-demo/hmily-demo-brpc/hmily-demo-brpc-order/src/main/java/org/dromara/hmily/demo/brpc/order/configuration/HmilyTacDatasourceConfig.java @@ -17,8 +17,13 @@ package org.dromara.hmily.demo.brpc.order.configuration; +import com.baidu.brpc.interceptor.Interceptor; import com.zaxxer.hikari.HikariDataSource; + import javax.sql.DataSource; + +import org.dromara.hmily.brpc.interceptor.BrpcHmilyTransactionInterceptor; + import org.dromara.hmily.tac.p6spy.HmilyP6Datasource; import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; import org.springframework.context.annotation.Bean; @@ -32,9 +37,9 @@ import org.springframework.context.annotation.Primary; */ @Configuration public class HmilyTacDatasourceConfig { - + private final DataSourceProperties dataSourceProperties; - + /** * Instantiates a new Hmily tac datasource config. * @@ -43,7 +48,7 @@ public class HmilyTacDatasourceConfig { public HmilyTacDatasourceConfig(DataSourceProperties dataSourceProperties) { this.dataSourceProperties = dataSourceProperties; } - + /** * Data source data source. * @@ -64,4 +69,10 @@ public class HmilyTacDatasourceConfig { hikariDataSource.setMaxLifetime(1800000); return new HmilyP6Datasource(hikariDataSource); } + + @Bean("hmilyInterceptor") + public Interceptor interceptor() { + return new BrpcHmilyTransactionInterceptor(); + } + } diff --git a/hmily-demo/hmily-demo-brpc/hmily-demo-brpc-order/src/main/resources/application.yml b/hmily-demo/hmily-demo-brpc/hmily-demo-brpc-order/src/main/resources/application.yml index ecaf4c02..c774a437 100644 --- a/hmily-demo/hmily-demo-brpc/hmily-demo-brpc-order/src/main/resources/application.yml +++ b/hmily-demo/hmily-demo-brpc/hmily-demo-brpc-order/src/main/resources/application.yml @@ -37,3 +37,4 @@ brpc: ioThreadNum: 1 protocol-type: 30 max-try-times: 1 + interceptor-bean-names: hmilyInterceptor diff --git a/hmily-demo/hmily-demo-brpc/pom.xml b/hmily-demo/hmily-demo-brpc/pom.xml index f21cf448..7a934a17 100644 --- a/hmily-demo/hmily-demo-brpc/pom.xml +++ b/hmily-demo/hmily-demo-brpc/pom.xml @@ -17,25 +17,18 @@ - 3.0.1 - 2.6.1 + 3.0.2 - - - org.dromara - hmily-demo-common - ${project.version} - - - com.baidu - brpc-spring-boot-starter - ${brpc.version} - - - com.baidu - brpc-java-naming-zookeeper - ${brpc.version} - - + + + + com.baidu + brpc-java-naming-zookeeper + ${brpc.version} + + + + + diff --git a/hmily-demo/pom.xml b/hmily-demo/pom.xml index 720bc9ef..87571d6d 100644 --- a/hmily-demo/pom.xml +++ b/hmily-demo/pom.xml @@ -19,6 +19,7 @@ hmily-demo-motan hmily-demo-common hmily-demo-sofa + hmily-demo-brpc hmily-demo-tars @@ -26,7 +27,7 @@ UTF-8 1.8 - 2.1.1 + 2.1.2-SNAPSHOT Dalston.SR1 2.6.1 2.1.3.RELEASE @@ -58,6 +59,12 @@ ${hmily.version} + + org.dromara + hmily-brpc + ${hmily.version} + + org.dromara hmily-springcloud @@ -76,6 +83,12 @@ ${hmily.version} + + org.dromara + hmily-spring-boot-starter-brpc + ${hmily.version} + + org.dromara hmily-spring-boot-starter-springcloud diff --git a/hmily-rpc/hmily-brpc/pom.xml b/hmily-rpc/hmily-brpc/pom.xml new file mode 100644 index 00000000..e8fe0aa4 --- /dev/null +++ b/hmily-rpc/hmily-brpc/pom.xml @@ -0,0 +1,56 @@ + + + + + org.dromara + hmily-rpc + 2.1.2-SNAPSHOT + + 4.0.0 + hmily-brpc + jar + + + + com.baidu + brpc-java + + + org.dromara + hmily-spring + ${project.version} + + + + org.dromara + hmily-tcc + ${project.version} + + + + org.dromara + hmily-tac-p6spy + ${project.version} + + + com.baidu + brpc-spring + + + + + hmily-brpc + + + org.apache.maven.plugins + maven-compiler-plugin + + ${jdk.version} + ${jdk.version} + ${project.build.sourceEncoding} + + + + + diff --git a/hmily-rpc/hmily-brpc/src/main/java/org/dromara/hmily/brpc/field/BrpcRefererAnnotationField.java b/hmily-rpc/hmily-brpc/src/main/java/org/dromara/hmily/brpc/field/BrpcRefererAnnotationField.java new file mode 100644 index 00000000..e31a8d55 --- /dev/null +++ b/hmily-rpc/hmily-brpc/src/main/java/org/dromara/hmily/brpc/field/BrpcRefererAnnotationField.java @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.dromara.hmily.brpc.field; + +import com.baidu.brpc.spring.annotation.RpcProxy; +import org.dromara.hmily.core.field.AnnotationField; +import org.dromara.hmily.spi.HmilySPI; + +import java.lang.reflect.Field; + +/** + * The type brpc referer annotation field. + * + * @author liu·yu + */ +@HmilySPI(value = "brpc") +public class BrpcRefererAnnotationField implements AnnotationField { + @Override + public boolean check(final Field field) { + RpcProxy rpcProxy = field.getAnnotation(RpcProxy.class); + return rpcProxy != null; + } +} diff --git a/hmily-rpc/hmily-brpc/src/main/java/org/dromara/hmily/brpc/interceptor/BrpcHmilyTransactionInterceptor.java b/hmily-rpc/hmily-brpc/src/main/java/org/dromara/hmily/brpc/interceptor/BrpcHmilyTransactionInterceptor.java new file mode 100644 index 00000000..79fc6dcd --- /dev/null +++ b/hmily-rpc/hmily-brpc/src/main/java/org/dromara/hmily/brpc/interceptor/BrpcHmilyTransactionInterceptor.java @@ -0,0 +1,130 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.dromara.hmily.brpc.interceptor; + +import java.lang.reflect.Type; + +import com.baidu.brpc.RpcContext; +import org.dromara.hmily.common.utils.IdWorkerUtils; +import org.dromara.hmily.repository.spi.entity.HmilyInvocation; + +import com.baidu.brpc.exceptions.RpcException; +import com.baidu.brpc.interceptor.AbstractInterceptor; +import com.baidu.brpc.interceptor.InterceptorChain; +import com.baidu.brpc.protocol.Request; +import com.baidu.brpc.protocol.Response; +import org.dromara.hmily.annotation.Hmily; +import org.dromara.hmily.common.enums.HmilyActionEnum; +import org.dromara.hmily.common.enums.HmilyRoleEnum; +import org.dromara.hmily.common.exception.HmilyRuntimeException; +import org.dromara.hmily.common.utils.LogUtil; +import org.dromara.hmily.core.context.HmilyContextHolder; +import org.dromara.hmily.core.context.HmilyTransactionContext; +import org.dromara.hmily.core.holder.HmilyTransactionHolder; +import org.dromara.hmily.core.mediator.RpcMediator; +import org.dromara.hmily.repository.spi.entity.HmilyParticipant; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.lang.reflect.Method; +import java.util.Objects; +import java.util.Optional; + +/** + * The hmily brpc transaction interceptor. + * + * @author liu·yu + */ +public class BrpcHmilyTransactionInterceptor extends AbstractInterceptor { + + private static final Logger LOGGER = LoggerFactory.getLogger(BrpcHmilyTransactionInterceptor.class); + + @Override + public void aroundProcess(final Request request, final Response response, final InterceptorChain chain) throws RpcException { + final HmilyTransactionContext context = HmilyContextHolder.get(); + if (Objects.isNull(context)) { + chain.intercept(request, response); + return; + } + Method method = request.getRpcMethodInfo().getMethod(); + try { + Hmily hmily = method.getAnnotation(Hmily.class); + if (Objects.isNull(hmily)) { + chain.intercept(request, response); + return; + } + } catch (Exception ex) { + LogUtil.error(LOGGER, "hmily find method error {} ", ex::getMessage); + chain.intercept(request, response); + return; + } + Long participantId = context.getParticipantId(); + HmilyParticipant hmilyParticipant = buildParticipant(context, request); + Optional.ofNullable(hmilyParticipant).ifPresent(participant -> context.setParticipantId(participant.getParticipantId())); + if (context.getRole() == HmilyRoleEnum.PARTICIPANT.getCode()) { + context.setParticipantRefId(participantId); + } + RpcMediator.getInstance().transmit(RpcContext.getContext()::setRequestKvAttachment, context); + if (request.getKvAttachment() == null) { + request.setKvAttachment(RpcContext.getContext().getRequestKvAttachment()); + } else { + request.getKvAttachment().putAll(RpcContext.getContext().getRequestKvAttachment()); + } + try { + chain.intercept(request, response); + if (context.getRole() == HmilyRoleEnum.PARTICIPANT.getCode()) { + HmilyTransactionHolder.getInstance().registerParticipantByNested(participantId, hmilyParticipant); + } else { + HmilyTransactionHolder.getInstance().registerStarterParticipant(hmilyParticipant); + } + } catch (Exception e) { + throw new HmilyRuntimeException("rpc invoke exception{}", e); + } + } + + private HmilyParticipant buildParticipant(final HmilyTransactionContext context, final Request request) { + if (HmilyActionEnum.TRYING.getCode() != context.getAction()) { + return null; + } + HmilyParticipant hmilyParticipant = new HmilyParticipant(); + hmilyParticipant.setParticipantId(IdWorkerUtils.getInstance().createUUID()); + hmilyParticipant.setTransId(context.getTransId()); + hmilyParticipant.setTransType(context.getTransType()); + + Class clazz = request.getRpcMethodInfo().getMethod().getDeclaringClass(); + String methodName = request.getRpcMethodInfo().getMethodName(); + Class[] converter = converterParamsClass(request.getRpcMethodInfo().getInputClasses()); + Object[] args = request.getArgs(); + + HmilyInvocation invocation = new HmilyInvocation(clazz, methodName, converter, args); + + hmilyParticipant.setConfirmHmilyInvocation(invocation); + hmilyParticipant.setCancelHmilyInvocation(invocation); + + return hmilyParticipant; + } + + private Class[] converterParamsClass(final Type[] types) { + Class[] classes = new Class[types.length]; + for (int i = 0; i < types.length; i++) { + classes[i] = (Class) types[i]; + } + return classes; + } + +} diff --git a/hmily-rpc/hmily-brpc/src/main/java/org/dromara/hmily/brpc/loadbalance/HmilyFairStrategy.java b/hmily-rpc/hmily-brpc/src/main/java/org/dromara/hmily/brpc/loadbalance/HmilyFairStrategy.java new file mode 100644 index 00000000..b5eff571 --- /dev/null +++ b/hmily-rpc/hmily-brpc/src/main/java/org/dromara/hmily/brpc/loadbalance/HmilyFairStrategy.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.dromara.hmily.brpc.loadbalance; + +import com.baidu.brpc.client.CommunicationClient; +import com.baidu.brpc.loadbalance.FairStrategy; +import com.baidu.brpc.protocol.Request; + +import java.util.List; +import java.util.Set; + +/** + * The hmily brpc fair strategy load balance. + * + * @author liu·yu + */ +public class HmilyFairStrategy extends FairStrategy { + + @Override + public CommunicationClient selectInstance(final Request request, final List instances, final Set selectedInstances) { + CommunicationClient client = super.selectInstance(request, instances, selectedInstances); + return HmilyLoadBalanceUtils.doSelect(client, instances); + } +} diff --git a/hmily-rpc/hmily-brpc/src/main/java/org/dromara/hmily/brpc/loadbalance/HmilyLoadBalanceUtils.java b/hmily-rpc/hmily-brpc/src/main/java/org/dromara/hmily/brpc/loadbalance/HmilyLoadBalanceUtils.java new file mode 100644 index 00000000..ecd04483 --- /dev/null +++ b/hmily-rpc/hmily-brpc/src/main/java/org/dromara/hmily/brpc/loadbalance/HmilyLoadBalanceUtils.java @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.dromara.hmily.brpc.loadbalance; + +import com.baidu.brpc.client.CommunicationClient; +import com.google.common.collect.Maps; +import org.dromara.hmily.common.enums.HmilyActionEnum; +import org.dromara.hmily.core.context.HmilyContextHolder; +import org.dromara.hmily.core.context.HmilyTransactionContext; + +import java.util.List; +import java.util.Map; +import java.util.Objects; + +/** + * The hmily brpc load balance utils referer annotation field . + * + * @author liu·yu + */ +public class HmilyLoadBalanceUtils { + + private static final Map URL_MAP = Maps.newConcurrentMap(); + + /** + * do select client. + * + * @param defaultClient default client + * @param instances all client + * @return client + */ + public static CommunicationClient doSelect(final CommunicationClient defaultClient, + final List instances) { + final HmilyTransactionContext hmilyTransactionContext = HmilyContextHolder.get(); + if (Objects.isNull(hmilyTransactionContext)) { + return defaultClient; + } + //if try + String key = defaultClient.getCommunicationOptions().getClientName(); + if (hmilyTransactionContext.getAction() == HmilyActionEnum.TRYING.getCode()) { + URL_MAP.put(key, defaultClient.getServiceInstance().getIp()); + return defaultClient; + } + final String ip = URL_MAP.get(key); + URL_MAP.remove(key); + if (Objects.nonNull(ip)) { + for (CommunicationClient client : instances) { + if (Objects.equals(client.getServiceInstance().getIp(), ip)) { + return client; + } + } + } + return defaultClient; + } + +} diff --git a/hmily-rpc/hmily-brpc/src/main/java/org/dromara/hmily/brpc/loadbalance/HmilyRandomStrategy.java b/hmily-rpc/hmily-brpc/src/main/java/org/dromara/hmily/brpc/loadbalance/HmilyRandomStrategy.java new file mode 100644 index 00000000..306ec24e --- /dev/null +++ b/hmily-rpc/hmily-brpc/src/main/java/org/dromara/hmily/brpc/loadbalance/HmilyRandomStrategy.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.dromara.hmily.brpc.loadbalance; + +import com.baidu.brpc.client.CommunicationClient; +import com.baidu.brpc.loadbalance.RandomStrategy; +import com.baidu.brpc.protocol.Request; + +import java.util.List; +import java.util.Set; + +/** + * The hmily brpc random strategy load balance. + * + * @author liu·yu + */ +public class HmilyRandomStrategy extends RandomStrategy { + + @Override + public CommunicationClient selectInstance(final Request request, final List instances, final Set selectedInstances) { + CommunicationClient client = super.selectInstance(request, instances, selectedInstances); + return HmilyLoadBalanceUtils.doSelect(client, instances); + } +} diff --git a/hmily-rpc/hmily-brpc/src/main/java/org/dromara/hmily/brpc/loadbalance/HmilyRobinStrategy.java b/hmily-rpc/hmily-brpc/src/main/java/org/dromara/hmily/brpc/loadbalance/HmilyRobinStrategy.java new file mode 100644 index 00000000..41a79327 --- /dev/null +++ b/hmily-rpc/hmily-brpc/src/main/java/org/dromara/hmily/brpc/loadbalance/HmilyRobinStrategy.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.dromara.hmily.brpc.loadbalance; + +import com.baidu.brpc.client.CommunicationClient; +import com.baidu.brpc.loadbalance.FairStrategy; +import com.baidu.brpc.protocol.Request; + +import java.util.List; +import java.util.Set; + +/** + * The hmily brpc robin strategy load balance. + * + * @author liu·yu + */ +public class HmilyRobinStrategy extends FairStrategy { + + @Override + public CommunicationClient selectInstance(final Request request, final List instances, final Set selectedInstances) { + CommunicationClient client = super.selectInstance(request, instances, selectedInstances); + return HmilyLoadBalanceUtils.doSelect(client, instances); + } +} diff --git a/hmily-rpc/hmily-brpc/src/main/java/org/dromara/hmily/brpc/loadbalance/HmilyWeightStrategy.java b/hmily-rpc/hmily-brpc/src/main/java/org/dromara/hmily/brpc/loadbalance/HmilyWeightStrategy.java new file mode 100644 index 00000000..d4dff3b0 --- /dev/null +++ b/hmily-rpc/hmily-brpc/src/main/java/org/dromara/hmily/brpc/loadbalance/HmilyWeightStrategy.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.dromara.hmily.brpc.loadbalance; + +import com.baidu.brpc.client.CommunicationClient; +import com.baidu.brpc.loadbalance.FairStrategy; +import com.baidu.brpc.protocol.Request; + +import java.util.List; +import java.util.Set; + +/** + * The hmily brpc weight strategy load balance. + * + * @author liu·yu + */ +public class HmilyWeightStrategy extends FairStrategy { + + @Override + public CommunicationClient selectInstance(final Request request, final List instances, final Set selectedInstances) { + CommunicationClient client = super.selectInstance(request, instances, selectedInstances); + return HmilyLoadBalanceUtils.doSelect(client, instances); + } +} diff --git a/hmily-rpc/hmily-brpc/src/main/java/org/dromara/hmily/brpc/parameter/BrpcParameterLoader.java b/hmily-rpc/hmily-brpc/src/main/java/org/dromara/hmily/brpc/parameter/BrpcParameterLoader.java new file mode 100644 index 00000000..4f878659 --- /dev/null +++ b/hmily-rpc/hmily-brpc/src/main/java/org/dromara/hmily/brpc/parameter/BrpcParameterLoader.java @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.dromara.hmily.brpc.parameter; + +import com.baidu.brpc.RpcContext; +import org.dromara.hmily.core.context.HmilyContextHolder; +import org.dromara.hmily.core.context.HmilyTransactionContext; +import org.dromara.hmily.core.mediator.RpcMediator; +import org.dromara.hmily.core.mediator.RpcParameterLoader; +import org.dromara.hmily.spi.HmilySPI; + +import java.util.Map; +import java.util.Optional; + +/** + * The hmily brpc parameter loader. + * + * @author liu·yu + */ +@HmilySPI(value = "brpc") +public class BrpcParameterLoader implements RpcParameterLoader { + @Override + public HmilyTransactionContext load() { + return Optional.ofNullable(RpcMediator.getInstance().acquire(k -> { + Map attachment = RpcContext.getContext() + .getRequestKvAttachment(); + if (attachment != null) { + return String.valueOf(attachment.get(k)); + } + return null; + })).orElse(HmilyContextHolder.get()); + } +} diff --git a/hmily-rpc/hmily-brpc/src/main/resources/META-INF/hmily/org.dromara.hmily.core.field.AnnotationField b/hmily-rpc/hmily-brpc/src/main/resources/META-INF/hmily/org.dromara.hmily.core.field.AnnotationField new file mode 100644 index 00000000..82d9b6c8 --- /dev/null +++ b/hmily-rpc/hmily-brpc/src/main/resources/META-INF/hmily/org.dromara.hmily.core.field.AnnotationField @@ -0,0 +1 @@ +org.dromara.hmily.brpc.field.BrpcRefererAnnotationField \ No newline at end of file diff --git a/hmily-rpc/hmily-brpc/src/main/resources/META-INF/hmily/org.dromara.hmily.core.mediator.RpcParameterLoader b/hmily-rpc/hmily-brpc/src/main/resources/META-INF/hmily/org.dromara.hmily.core.mediator.RpcParameterLoader new file mode 100644 index 00000000..9ca4b4eb --- /dev/null +++ b/hmily-rpc/hmily-brpc/src/main/resources/META-INF/hmily/org.dromara.hmily.core.mediator.RpcParameterLoader @@ -0,0 +1 @@ +org.dromara.hmily.brpc.parameter.BrpcParameterLoader \ No newline at end of file diff --git a/hmily-rpc/hmily-brpc/src/main/resources/META-INF/services/com.baidu.brpc.interceptor.Interceptor b/hmily-rpc/hmily-brpc/src/main/resources/META-INF/services/com.baidu.brpc.interceptor.Interceptor new file mode 100644 index 00000000..11ab877d --- /dev/null +++ b/hmily-rpc/hmily-brpc/src/main/resources/META-INF/services/com.baidu.brpc.interceptor.Interceptor @@ -0,0 +1 @@ +org.dromara.hmily.brpc.interceptor.BrpcHmilyTransactionInterceptor \ No newline at end of file diff --git a/hmily-rpc/hmily-brpc/src/main/resources/META-INF/services/com.baidu.brpc.loadbalance.LoadBalanceStrategy b/hmily-rpc/hmily-brpc/src/main/resources/META-INF/services/com.baidu.brpc.loadbalance.LoadBalanceStrategy new file mode 100644 index 00000000..39879c90 --- /dev/null +++ b/hmily-rpc/hmily-brpc/src/main/resources/META-INF/services/com.baidu.brpc.loadbalance.LoadBalanceStrategy @@ -0,0 +1,4 @@ +org.dromara.hmily.brpc.loadbalance.HmilyFairStrategy +org.dromara.hmily.brpc.loadbalance.HmilyRandomStrategy +org.dromara.hmily.brpc.loadbalance.HmilyRobinStrategy +org.dromara.hmily.brpc.loadbalance.HmilyWeightStrategy \ No newline at end of file diff --git a/hmily-rpc/pom.xml b/hmily-rpc/pom.xml index 273bfe92..5baae1af 100644 --- a/hmily-rpc/pom.xml +++ b/hmily-rpc/pom.xml @@ -36,6 +36,7 @@ hmily-motan hmily-springcloud hmily-sofa-rpc + hmily-brpc hmily-tars diff --git a/hmily-spring-boot-starter/hmily-spring-boot-starter-brpc/pom.xml b/hmily-spring-boot-starter/hmily-spring-boot-starter-brpc/pom.xml new file mode 100644 index 00000000..131cf6a0 --- /dev/null +++ b/hmily-spring-boot-starter/hmily-spring-boot-starter-brpc/pom.xml @@ -0,0 +1,54 @@ + + + + + + hmily-spring-boot-starter + org.dromara + 2.1.2-SNAPSHOT + + 4.0.0 + + hmily-spring-boot-starter-brpc + + + + + org.dromara + hmily-spring-boot-starter-parent + ${project.version} + + + + org.dromara + hmily-brpc + ${project.version} + + + + com.baidu + brpc-spring-boot-starter + + + + + \ No newline at end of file diff --git a/hmily-spring-boot-starter/hmily-spring-boot-starter-brpc/src/main/resources/META-INF/spring.provides b/hmily-spring-boot-starter/hmily-spring-boot-starter-brpc/src/main/resources/META-INF/spring.provides new file mode 100644 index 00000000..8bbf105f --- /dev/null +++ b/hmily-spring-boot-starter/hmily-spring-boot-starter-brpc/src/main/resources/META-INF/spring.provides @@ -0,0 +1 @@ +provides: hmily-spring-boot-starter-parent