Skip to content

Commit

Permalink
Support mosn. (sofastack#466)
Browse files Browse the repository at this point in the history
* support mosn

* first router to mosn, support mosn

* move mesh router package to mesh

* add switcher
  • Loading branch information
leizhiyuan authored Dec 29, 2018
1 parent d063253 commit 0169fbc
Show file tree
Hide file tree
Showing 31 changed files with 2,076 additions and 0 deletions.
6 changes: 6 additions & 0 deletions all/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@
<artifactId>sofa-rpc-registry-nacos</artifactId>
<version>${sofa.rpc.version}</version>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-rpc-registry-mesh</artifactId>
<version>${sofa.rpc.version}</version>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-rpc-remoting-bolt</artifactId>
Expand Down Expand Up @@ -379,6 +384,7 @@
<include>com.alipay.sofa:sofa-rpc-registry-local</include>
<include>com.alipay.sofa:sofa-rpc-registry-zk</include>
<include>com.alipay.sofa:sofa-rpc-registry-nacos</include>
<include>com.alipay.sofa:sofa-rpc-registry-mesh</include>
<include>com.alipay.sofa:sofa-rpc-remoting-bolt</include>
<include>com.alipay.sofa:sofa-rpc-remoting-http</include>
<include>com.alipay.sofa:sofa-rpc-remoting-resteasy</include>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.alipay.sofa.rpc.client.ProviderInfo;
import com.alipay.sofa.rpc.client.Router;
import com.alipay.sofa.rpc.common.RpcConstants;
import com.alipay.sofa.rpc.common.utils.CommonUtils;
import com.alipay.sofa.rpc.common.utils.StringUtils;
import com.alipay.sofa.rpc.config.ConsumerConfig;
import com.alipay.sofa.rpc.core.request.SofaRequest;
Expand Down Expand Up @@ -65,6 +66,12 @@ public boolean needToLoad(ConsumerBootstrap consumerBootstrap) {

@Override
public List<ProviderInfo> route(SofaRequest request, List<ProviderInfo> providerInfos) {

//has address. FIXME
if (CommonUtils.isNotEmpty(providerInfos)) {
return providerInfos;
}

AddressHolder addressHolder = consumerBootstrap.getCluster().getAddressHolder();
if (addressHolder != null) {
List<ProviderInfo> current = addressHolder.getProviderInfos(RpcConstants.ADDRESS_DEFAULT_GROUP);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ public class RpcConstants {
* zookeeper注册中心
*/
public static final String REGISTRY_PROTOCOL_ZK = "zooKeeper";

/**
* mesh注册中心
*/
public static final String REGISTRY_PROTOCOL_MESH = "mesh";

/**
* xml文件注册中心
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,9 @@ public class SofaOptions {
* TR心跳间隔
*/
public static final String CONFIG_RPC_HEART_BEAT_INTERVAL = "rpc_tr_heart_beat_interval";

// ========== mesh相关 ===========

public static final String CONFIG_RPC_MESH_SWITCH = "rpc_mesh_switch";

}
1 change: 1 addition & 0 deletions extension-impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<module>registry-local</module>
<module>registry-zk</module>
<module>registry-nacos</module>
<module>registry-mesh</module>
<module>remoting-bolt</module>
<module>remoting-http</module>
<module>remoting-resteasy</module>
Expand Down
95 changes: 95 additions & 0 deletions extension-impl/registry-mesh/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?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">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-rpc-extension-impl</artifactId>
<version>5.5.0-SNAPSHOT</version>
</parent>

<artifactId>sofa-rpc-registry-mesh</artifactId>

<dependencies>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-rpc-extension-common</artifactId>
<version>${project.parent.version}</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<sourceDirectory>src/main/java</sourceDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<includes>
<include>**/**</include>
</includes>
</resource>
</resources>
<testSourceDirectory>src/test/java</testSourceDirectory>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>false</filtering>
<includes>
<include>**/**</include>
</includes>
</testResource>
</testResources>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<configuration>
<skip>${module.install.skip}</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>${module.deploy.skip}</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>${skipTests}</skipTests>
<includes>
<!-- 这里需要根据自己的需要指定要跑的单元测试 -->
<include>**/*Test.java</include>
</includes>
<!-- 如无特殊需求,将forkMode设置为once -->
<forkMode>once</forkMode>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* 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 com.alipay.sofa.rpc.client.router;

import com.alipay.sofa.rpc.bootstrap.ConsumerBootstrap;
import com.alipay.sofa.rpc.client.AddressHolder;
import com.alipay.sofa.rpc.client.ProviderInfo;
import com.alipay.sofa.rpc.client.Router;
import com.alipay.sofa.rpc.common.RpcConstants;
import com.alipay.sofa.rpc.common.utils.StringUtils;
import com.alipay.sofa.rpc.config.ConsumerConfig;
import com.alipay.sofa.rpc.config.RegistryConfig;
import com.alipay.sofa.rpc.core.request.SofaRequest;
import com.alipay.sofa.rpc.ext.Extension;
import com.alipay.sofa.rpc.filter.AutoActive;

import java.util.List;

/**
* 从mesh获取路由,只为 bolt 开启。
* <p>
*
* @author <a href="mailto:[email protected]">zhiyuan.lzy</a>
*/
@Extension(value = "mesh", order = -19000)
@AutoActive(consumerSide = true)
public class MeshRouter extends Router {

/**
* 路由路径:注册中心
*
* @since 5.5.0
*/
public static final String RPC_MESH_ROUTER = "MESH";

/**
* 服务消费者配置
*/
protected ConsumerBootstrap consumerBootstrap;

@Override
public void init(ConsumerBootstrap consumerBootstrap) {
this.consumerBootstrap = consumerBootstrap;
}

@Override
public boolean needToLoad(ConsumerBootstrap consumerBootstrap) {
ConsumerConfig consumerConfig = consumerBootstrap.getConsumerConfig();
// 不是直连,且从注册中心订阅配置
final boolean isDirect = StringUtils.isNotBlank(consumerConfig.getDirectUrl());
final List<RegistryConfig> registrys = consumerConfig.getRegistry();
boolean isMesh = false;

if (registrys != null) {
for (RegistryConfig registry : registrys) {
if (registry.getProtocol().equalsIgnoreCase(RpcConstants.REGISTRY_PROTOCOL_MESH)) {
isMesh = true;
break;
}
}
}

boolean isBolt = consumerConfig.getProtocol().equalsIgnoreCase(RpcConstants.PROTOCOL_TYPE_BOLT);

return !isDirect && isMesh && isBolt;
}

@Override
public List<ProviderInfo> route(SofaRequest request, List<ProviderInfo> providerInfos) {
AddressHolder addressHolder = consumerBootstrap.getCluster().getAddressHolder();
if (addressHolder != null) {
List<ProviderInfo> current = addressHolder.getProviderInfos(RpcConstants.ADDRESS_DEFAULT_GROUP);
if (providerInfos != null) {
providerInfos.addAll(current);
} else {
providerInfos = current;
}
}
recordRouterWay(RPC_MESH_ROUTER);
return providerInfos;
}

}
Loading

0 comments on commit 0169fbc

Please sign in to comment.