Skip to content

Commit

Permalink
服务调用包
Browse files Browse the repository at this point in the history
  • Loading branch information
lihuimin authored and lihuimin committed Dec 6, 2017
1 parent 5985651 commit b113d7a
Show file tree
Hide file tree
Showing 7 changed files with 795 additions and 0 deletions.
59 changes: 59 additions & 0 deletions dapeng-service-invoke/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?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>
<artifactId>dapeng-parent</artifactId>
<groupId>com.isuwang</groupId>
<version>1.2.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>dapeng-service-invoke</artifactId>

<properties>
</properties>


<dependencies>
<dependency>
<groupId>com.isuwang</groupId>
<artifactId>dapeng-remoting-netty</artifactId>
<version>1.2.2</version>
</dependency>

<dependency>
<groupId>com.isuwang</groupId>
<artifactId>dapeng-registry-zookeeper</artifactId>
<version>1.2.2</version>
</dependency>



<dependency>
<groupId>com.isuwang</groupId>
<artifactId>dapeng-core</artifactId>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3.1</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.12</version>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>16.0.1</version>
</dependency>



</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package com.isuwang.service.invoke;

import com.google.common.collect.TreeMultimap;
import com.isuwang.dapeng.core.SoaException;
import com.isuwang.dapeng.core.metadata.*;
import com.isuwang.dapeng.registry.ServiceInfo;
import com.isuwang.dapeng.remoting.fake.metadata.MetadataClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.xml.bind.JAXB;
import java.io.StringReader;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
* Created by Tony_PC on 2016/6/20.
*/
public class ApiServices {

private static final Logger LOGGER = LoggerFactory.getLogger(ApiServices.class);

private static Map<String, Service> services = new TreeMap<>();

private static Map<String, Service> fullNameService = new TreeMap<>();

public static TreeMultimap<String, String> urlMappings = TreeMultimap.create();

private static ZookeeperWatcher zookeeperWatcher;

public static void init() {
reloadServices();
}

public static void reloadServices() {

final Map<String, Service> services = new TreeMap<>();
urlMappings.clear();

Map<String, List<ServiceInfo>> servicesInfo = zookeeperWatcher.getAvailableServices();

Set<String> serviceKeys = servicesInfo.keySet();

for (String key : serviceKeys) {
String serviceName = key;
List<ServiceInfo> serviceInfoList = servicesInfo.get(key);
for (ServiceInfo serviceInfo : serviceInfoList) {
String version = serviceInfo.getVersionName();
String metadata = "";
try {
metadata = new MetadataClient(serviceName, version).getServiceMetadata();
if (metadata != null) {
try (StringReader reader = new StringReader(metadata)) {
Service serviceData = JAXB.unmarshal(reader, Service.class);
String serviceKey = getKey(serviceData);
if (!services.containsKey(serviceKey)) {
services.put(serviceKey, serviceData);
String fullNameKey = getFullNameKey(serviceData);
fullNameService.put(fullNameKey, serviceData);
loadResource(serviceData, services);
}
} catch (Exception e) {
LOGGER.error("JAXB 解析Service 出错");
}
}
} catch (SoaException e) {
LOGGER.error("生成SERVICE出错", e);
}
}
}
ApiServices.services = services;
LOGGER.info("size of urlMapping: " + urlMappings.size());
}

public void destory() {
services.clear();
}

public static void loadResource(Service service, Map<String, Service> services) {

//将service和service中的方法、结构体、枚举和字段名分别设置对应的url,以方便搜索
urlMappings.put(service.getName(), "api/service/" + service.name + "/" + service.meta.version + ".htm");
List<Method> methods = service.getMethods();
for (int i = 0; i < methods.size(); i++) {
Method method = methods.get(i);
urlMappings.put(method.name, "api/method/" + service.name + "/" + service.meta.version + "/" + method.name + ".htm");
}

List<Struct> structs = service.getStructDefinitions();
for (int i = 0; i < structs.size(); i++) {
Struct struct = structs.get(i);
urlMappings.put(struct.name, "api/struct/" + service.name + "/" + service.meta.version + "/" + struct.namespace + "." + struct.name + ".htm");

List<Field> fields = struct.getFields();
for (int j = 0; j < fields.size(); j++) {
Field field = fields.get(j);
urlMappings.put(field.name, "api/struct/" + service.name + "/" + service.meta.version + "/" + struct.namespace + "." + struct.name + ".htm");
}
}

List<TEnum> tEnums = service.getEnumDefinitions();
for (int i = 0; i < tEnums.size(); i++) {
TEnum tEnum = tEnums.get(i);
urlMappings.put(tEnum.name, "api/enum/" + service.name + "/" + service.meta.version + "/" + tEnum.namespace + "." + tEnum.name + ".htm");
}

}

public static Service getService(String name, String version) {

if (name.contains("."))
return fullNameService.get(getKey(name, version));
else
return services.get(getKey(name, version));
}

private static String getKey(Service service) {
return getKey(service.getName(), service.getMeta().version);
}

private static String getFullNameKey(Service service) {
return getKey(service.getNamespace() + "." + service.getName(), service.getMeta().version);
}

private static String getKey(String name, String version) {
return name + ":" + version;
}

public Map<String, Service> getServices() {
return services;
}

public ZookeeperWatcher getZookeeperWatcher() {
return zookeeperWatcher;
}

public void setZookeeperWatcher(ZookeeperWatcher zookeeperWatcher) {
this.zookeeperWatcher = zookeeperWatcher;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.isuwang.service.invoke;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.isuwang.dapeng.core.SoaHeader;
import com.isuwang.dapeng.core.SoaSystemEnvProperties;
import com.isuwang.service.invoke.entity.BaseRequest;
import com.isuwang.dapeng.registry.RegistryAgent;
import com.isuwang.dapeng.registry.RegistryAgentProxy;
import com.isuwang.dapeng.remoting.BaseClient;
import com.isuwang.dapeng.remoting.fake.json.JSONPost;
import com.isuwang.dapeng.remoting.filter.LoadBalanceFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ServiceLoader;

/**
* Created by lihuimin on 2017/12/6.
*/
public class BaseController {

private static final Logger LOGGER = LoggerFactory.getLogger(BaseController.class);

private static JSONPost jsonPost;

static{
if (!SoaSystemEnvProperties.SOA_REMOTING_MODE.equals("local")) {
try {

ServiceLoader<RegistryAgent> registryAgentLoader = ServiceLoader.load(RegistryAgent.class,BaseClient.class.getClassLoader());
for (RegistryAgent registryAgent : registryAgentLoader) {
RegistryAgentProxy.setCurrentInstance(RegistryAgentProxy.Type.Client,registryAgent);
RegistryAgentProxy.getCurrentInstance(RegistryAgentProxy.Type.Client).start();
ApiServices.init();
new ZookeeperWatcher(true).init();
}
} catch (Exception e) {
LOGGER.error("Load registry error", e);
}
} else {
LOGGER.info("soa remoting mode is {},client not load registry", SoaSystemEnvProperties.SOA_REMOTING_MODE);
}

}


public static String invoke(BaseRequest baseRequest){
JsonObject jsonObjectParameter = new JsonParser().parse(baseRequest.getJsonParameter()).getAsJsonObject();
com.isuwang.dapeng.core.metadata.Service service = ApiServices.getService(baseRequest.getServiceName(), baseRequest.getVersionName());

String callerInfo = LoadBalanceFilter.getCallerInfo(baseRequest.getServiceName() , baseRequest.getVersionName(), baseRequest.getVersionName());

SoaHeader header = new SoaHeader();
header.setServiceName(baseRequest.getServiceName());
header.setVersionName(baseRequest.getVersionName());
header.setMethodName(baseRequest.getMethodName());

String parameter = jsonObjectParameter.toString();

if (callerInfo == null && SoaSystemEnvProperties.SOA_REMOTING_MODE.equals("local")) {
jsonPost = new JSONPost(SoaSystemEnvProperties.SOA_SERVICE_IP, SoaSystemEnvProperties.SOA_SERVICE_PORT, true);
} else if (callerInfo != null) {
String[] infos = callerInfo.split(":");
jsonPost = new JSONPost(infos[0], Integer.valueOf(infos[1]), true);
} else {
return String.format("{\"responseCode\":\"%s\", \"responseMsg\":\"%s\", \"success\":\"%s\"}", "Err-Core-098", "无可用的服务实例", "{}");
}
try {
return jsonPost.callServiceMethod(header, parameter, service);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
}
return null;
}

}
Loading

0 comments on commit b113d7a

Please sign in to comment.