hmily整合brpc 测试 (#223)

* brpc demo 添加

* hmily 接入 brpc 测试

* Revert "hmily 接入 brpc 测试"

This reverts commit 65bd3d6fe2c7209b2a44b8698ab80ff10058869b.

* Revert "Revert "hmily 接入 brpc 测试""

This reverts commit 89aee1f827cd0694d86ada93f8b3a0d8bbe0063a.

* 添加 apache lincese ,修改注释

* 解决依赖问题
This commit is contained in:
liuyueve 2020-10-12 12:04:07 +08:00 committed by GitHub
parent 8f7d3f5443
commit d08ca9e600
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
28 changed files with 672 additions and 40 deletions

View File

@ -71,6 +71,7 @@
<spring.version>5.0.7.RELEASE</spring.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
<dubbo.version>2.6.2</dubbo.version>
<brpc.version>3.0.2</brpc.version>
<apache.dubbo.version>2.7.1</apache.dubbo.version>
<sofa.rpc.version>5.5.3</sofa.rpc.version>
<mongo.driver.version>3.8.0</mongo.driver.version>
@ -208,6 +209,23 @@
<version>${apache.dubbo.version}</version>
</dependency>
<!-- baidu-brpc config-->
<dependency>
<groupId>com.baidu</groupId>
<artifactId>brpc-java</artifactId>
<version>${brpc.version}</version>
</dependency>
<dependency>
<groupId>com.baidu</groupId>
<artifactId>brpc-spring</artifactId>
<version>${brpc.version}</version>
</dependency>
<dependency>
<groupId>com.baidu</groupId>
<artifactId>brpc-spring-boot-starter</artifactId>
<version>${brpc.version}</version>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-rpc-all</artifactId>

View File

@ -12,6 +12,14 @@
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.dromara</groupId>
<artifactId>hmily-demo-common</artifactId>
</dependency>
<dependency>
<groupId>com.baidu</groupId>
<artifactId>brpc-java-naming-zookeeper</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
@ -19,8 +27,13 @@
</dependency>
<dependency>
<groupId>org.dromara</groupId>
<artifactId>hmily-tac-p6spy</artifactId>
<version>2.1.2-SNAPSHOT</version>
<artifactId>hmily-spring-boot-starter-brpc</artifactId>
<exclusions>
<exclusion>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

View File

@ -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;
@ -65,4 +66,5 @@ public class HmilyTacDatasourceConfig {
hikariDataSource.setMaxLifetime(1800000);
return new HmilyP6Datasource(hikariDataSource);
}
}

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>hmily-demo-brpc</artifactId>
<groupId>org.dromara</groupId>
<version>1.0.0-SNAPSHOT</version>
<version>2.0.2-RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>hmily-demo-brpc-inventory</artifactId>
@ -13,14 +13,22 @@
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
<groupId>org.dromara</groupId>
<artifactId>hmily-demo-common</artifactId>
</dependency>
<dependency>
<groupId>org.dromara</groupId>
<artifactId>hmily-tac-p6spy</artifactId>
<version>2.1.2-SNAPSHOT</version>
<artifactId>hmily-brpc</artifactId>
<version>${hmily.version}</version>
</dependency>
<dependency>
<groupId>com.baidu</groupId>
<artifactId>brpc-java-naming-zookeeper</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

View File

@ -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;

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>hmily-demo-brpc</artifactId>
<groupId>org.dromara</groupId>
<version>1.0.0-SNAPSHOT</version>
<version>2.0.2-RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>hmily-demo-brpc-order</artifactId>
@ -16,6 +16,14 @@
</properties>
<dependencies>
<dependency>
<groupId>org.dromara</groupId>
<artifactId>hmily-demo-common</artifactId>
</dependency>
<dependency>
<groupId>com.baidu</groupId>
<artifactId>brpc-java-naming-zookeeper</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
@ -23,8 +31,14 @@
</dependency>
<dependency>
<groupId>org.dromara</groupId>
<artifactId>hmily-tac-p6spy</artifactId>
<version>2.1.2-SNAPSHOT</version>
<artifactId>hmily-spring-boot-starter-brpc</artifactId>
<version>${hmily.version}</version>
<exclusions>
<exclusion>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.springfox</groupId>

View File

@ -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.

View File

@ -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;
@ -64,4 +69,10 @@ public class HmilyTacDatasourceConfig {
hikariDataSource.setMaxLifetime(1800000);
return new HmilyP6Datasource(hikariDataSource);
}
@Bean("hmilyInterceptor")
public Interceptor interceptor() {
return new BrpcHmilyTransactionInterceptor();
}
}

View File

@ -37,3 +37,4 @@ brpc:
ioThreadNum: 1
protocol-type: 30
max-try-times: 1
interceptor-bean-names: hmilyInterceptor

View File

@ -17,25 +17,18 @@
</modules>
<properties>
<brpc.version>3.0.1</brpc.version>
<springfox.version>2.6.1</springfox.version>
<brpc.version>3.0.2</brpc.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.dromara</groupId>
<artifactId>hmily-demo-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.baidu</groupId>
<artifactId>brpc-spring-boot-starter</artifactId>
<version>${brpc.version}</version>
</dependency>
<dependency>
<groupId>com.baidu</groupId>
<artifactId>brpc-java-naming-zookeeper</artifactId>
<version>${brpc.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>

View File

@ -19,6 +19,7 @@
<module>hmily-demo-motan</module>
<module>hmily-demo-common</module>
<module>hmily-demo-sofa</module>
<module>hmily-demo-brpc</module>
<!-- <module>hmily-demo-grpc</module>-->
<module>hmily-demo-tars</module>
<!-- <module>hmily-demo-brpc</module>-->
@ -26,7 +27,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jdk.version>1.8</jdk.version>
<hmily.version>2.1.1</hmily.version>
<hmily.version>2.1.2-SNAPSHOT</hmily.version>
<spring-cloud.version>Dalston.SR1</spring-cloud.version>
<springfox.version>2.6.1</springfox.version>
<spring.data.mongodb.version>2.1.3.RELEASE</spring.data.mongodb.version>
@ -58,6 +59,12 @@
<version>${hmily.version}</version>
</dependency>
<dependency>
<groupId>org.dromara</groupId>
<artifactId>hmily-brpc</artifactId>
<version>${hmily.version}</version>
</dependency>
<dependency>
<groupId>org.dromara</groupId>
<artifactId>hmily-springcloud</artifactId>
@ -76,6 +83,12 @@
<version>${hmily.version}</version>
</dependency>
<dependency>
<groupId>org.dromara</groupId>
<artifactId>hmily-spring-boot-starter-brpc</artifactId>
<version>${hmily.version}</version>
</dependency>
<dependency>
<groupId>org.dromara</groupId>
<artifactId>hmily-spring-boot-starter-springcloud</artifactId>

View File

@ -0,0 +1,56 @@
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.dromara</groupId>
<artifactId>hmily-rpc</artifactId>
<version>2.1.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>hmily-brpc</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.baidu</groupId>
<artifactId>brpc-java</artifactId>
</dependency>
<dependency>
<groupId>org.dromara</groupId>
<artifactId>hmily-spring</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.dromara</groupId>
<artifactId>hmily-tcc</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.dromara</groupId>
<artifactId>hmily-tac-p6spy</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.baidu</groupId>
<artifactId>brpc-spring</artifactId>
</dependency>
</dependencies>
<build>
<finalName>hmily-brpc</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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<CommunicationClient> instances, final Set<CommunicationClient> selectedInstances) {
CommunicationClient client = super.selectInstance(request, instances, selectedInstances);
return HmilyLoadBalanceUtils.doSelect(client, instances);
}
}

View File

@ -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<String, String> 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<CommunicationClient> 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;
}
}

View File

@ -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<CommunicationClient> instances, final Set<CommunicationClient> selectedInstances) {
CommunicationClient client = super.selectInstance(request, instances, selectedInstances);
return HmilyLoadBalanceUtils.doSelect(client, instances);
}
}

View File

@ -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<CommunicationClient> instances, final Set<CommunicationClient> selectedInstances) {
CommunicationClient client = super.selectInstance(request, instances, selectedInstances);
return HmilyLoadBalanceUtils.doSelect(client, instances);
}
}

View File

@ -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<CommunicationClient> instances, final Set<CommunicationClient> selectedInstances) {
CommunicationClient client = super.selectInstance(request, instances, selectedInstances);
return HmilyLoadBalanceUtils.doSelect(client, instances);
}
}

View File

@ -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<String, Object> attachment = RpcContext.getContext()
.getRequestKvAttachment();
if (attachment != null) {
return String.valueOf(attachment.get(k));
}
return null;
})).orElse(HmilyContextHolder.get());
}
}

View File

@ -0,0 +1 @@
org.dromara.hmily.brpc.field.BrpcRefererAnnotationField

View File

@ -0,0 +1 @@
org.dromara.hmily.brpc.parameter.BrpcParameterLoader

View File

@ -0,0 +1 @@
org.dromara.hmily.brpc.interceptor.BrpcHmilyTransactionInterceptor

View File

@ -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

View File

@ -36,6 +36,7 @@
<module>hmily-motan</module>
<module>hmily-springcloud</module>
<module>hmily-sofa-rpc</module>
<module>hmily-brpc</module>
<module>hmily-tars</module>
</modules>

View File

@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ /*
~ * 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.
~ */
-->
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>hmily-spring-boot-starter</artifactId>
<groupId>org.dromara</groupId>
<version>2.1.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>hmily-spring-boot-starter-brpc</artifactId>
<dependencies>
<dependency>
<groupId>org.dromara</groupId>
<artifactId>hmily-spring-boot-starter-parent</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.dromara</groupId>
<artifactId>hmily-brpc</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.baidu</groupId>
<artifactId>brpc-spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1 @@
provides: hmily-spring-boot-starter-parent