yangyoupeng

添加dubbogeneric服务支持

......@@ -32,6 +32,7 @@ import com.zhaoonline.support.gateway.config.GWConstants;
import com.zhaoonline.support.gateway.config.GateWayConfiguration;
import com.zhaoonline.support.gateway.listener.RuntimeStatusEvent;
import com.zhaoonline.support.gateway.service.AbstractService;
import com.zhaoonline.support.gateway.service.DubboGenericService;
import com.zhaoonline.support.gateway.service.DubboService;
import com.zhaoonline.support.gateway.service.HTTPService;
import com.zhaoonline.support.gateway.service.ServiceManager;
......@@ -142,11 +143,21 @@ public class TransforFilter extends ZuulFilter implements InitializingBean,Dispo
if(serviceInfo.getServiceType().equalsIgnoreCase(AbstractService.TYPE_DUBBO)){
return toDubboService(context,request, serviceInfo);
}
if(serviceInfo.getServiceType().equalsIgnoreCase(AbstractService.TYPE_DUBBO_GENERIC)){
return toDubboGenericService(context,request, serviceInfo);
}
handerUnkownServiceType(context,serviceInfo);
return null;
}
private HttpResponse toDubboGenericService(RequestContext context,HttpServletRequest request, ServiceInfo serviceInfo) {
DubboGenericService service = ServiceManager.INSTANCE.buildDubboGenericServiceFrom(serviceInfo,this.commonConfig);
checkServiceBuilded(service,context);
return doForward(context,request, service);
}
private HttpResponse toDubboService(RequestContext context,HttpServletRequest request, ServiceInfo serviceInfo) {
DubboService service = ServiceManager.INSTANCE.buildDubboServiceFrom(serviceInfo,this.commonConfig);
checkServiceBuilded(service,context);
......
......@@ -11,6 +11,7 @@ import org.slf4j.LoggerFactory;
import com.alibaba.dubbo.rpc.service.EchoService;
import com.zhaoonline.support.gateway.config.GateWayConfiguration;
import com.zhaoonline.support.gateway.service.AbstractService;
import com.zhaoonline.support.gateway.service.DubboGenericService;
import com.zhaoonline.support.gateway.service.DubboService;
import com.zhaoonline.support.gateway.service.ServiceInfo;
......@@ -30,6 +31,12 @@ public class ServiceLifeCycleChecker implements LifeCycleChecker {
DubboService dubboservice= (DubboService)service;
echoService =(EchoService) dubboservice.getAutomicReference().get();
}
if(service instanceof DubboGenericService ){
DubboGenericService dubboservice= (DubboGenericService)service;
echoService =(EchoService) dubboservice.getAutomicReference().get();
}
evictTime=Seconds.seconds(config.getInteger("dubbo.service.evict.time", 3600));
}
@Override
......
......@@ -24,6 +24,7 @@ public abstract class AbstractService<T,R> {
public static final String TYPE_HTTP="HTTP";
public static final String TYPE_DUBBO = "DUBBO";
public static final String TYPE_DUBBO_GENERIC = "GENERIC";
public abstract void init() throws Exception;
......
......@@ -24,8 +24,9 @@ public class ServiceInfo {
private String serviceName;
private String serviceInterface;
/**
* key:paramName
* key:paramindex
* value:type
*存储参数的类型,形如{1:"java.lang.String",2:"java.lang.Long"}
*/
private Map<Integer, String> interParams;
/**
......
......@@ -85,6 +85,53 @@ public enum ServiceManager{
}
return cachedService;
}
public DubboGenericService buildDubboGenericServiceFrom(ServiceInfo serviceInfo,
GateWayConfiguration config) {
String serviceName=serviceInfo.getServiceName();
DubboGenericService cachedService = (DubboGenericService) dubboServiceCache.get(serviceName);
if(cachedService == null){
LOG.info("no cache for dubbo service [{}],create new instance for service",serviceInfo.getServiceName());
return createNewDubboGenericService(serviceInfo, config, serviceName);
}else{
LOG.info("get dubbo service from cache with key [{}]",serviceName);
if(cachedService.isClosed()){
LOG.info("the cached dubbo service interface {} is closed by {},just return null",serviceInfo.getServiceInterface(),LifeCycleCheckerManager.class.getName());
dubboServiceCache.remove(serviceName);
cachedService=null;
return createNewDubboGenericService(serviceInfo, config, serviceName);
}
}
return cachedService;
}
/**
* Method name: createNewDubboGenericService <BR>
* Description: dubboGeneric服务是没有echo方法的 <BR>
* Remark: <BR>
* @param serviceInfo
* @param config
* @param serviceName
* @return DubboGenericService<BR>
*/
private DubboGenericService createNewDubboGenericService(ServiceInfo serviceInfo, GateWayConfiguration config,
String serviceName) {
DubboGenericService dubboGenericService=new DubboGenericService(serviceInfo);
try {
dubboGenericService.init();
// monitorStatus(dubboGenericService,config);
} catch (Exception e) {
LOG.info("fail to init the dubbo service interface [{}],cause by [{}]",serviceInfo,e.getMessage());
e.printStackTrace();
return null;
}
dubboServiceCache.put(serviceName,dubboGenericService);
return dubboGenericService;
}
private DubboService createNewDubboService(ServiceInfo serviceInfo, GateWayConfiguration config,
String serviceName) {
......@@ -102,6 +149,11 @@ public enum ServiceManager{
return dubboSerivce;
}
private void monitorStatus(AbstractService abstractService,GateWayConfiguration config) {
LOG.info("assign the dubboservice {} to be status monitored by {}",abstractService.getServicInfo(),LifeCycleCheckerManager.class.getName());
lifeCycleCheckerManager.submit(new ServiceLifeCycleChecker(abstractService,config));
}
private void monitorStatus(DubboService dubboSerivce,GateWayConfiguration config) {
LOG.info("assign the dubboservice {} to be status monitored by {}",dubboSerivce.getServicInfo(),LifeCycleCheckerManager.class.getName());
lifeCycleCheckerManager.submit(new ServiceLifeCycleChecker(dubboSerivce,config));
......@@ -124,6 +176,7 @@ public enum ServiceManager{
}
return true;
}
}
......
......@@ -39,7 +39,7 @@ $(document).ready(function(){
})
})
}
if($(this).val() === "dubbo") {
if($(this).val() === "dubbo" || $(this).val() === "generic") {
$("#dubboInfo").slideToggle()
$("[name='hostportType']").each(function(){
$(this).val("zookeeper")
......@@ -64,7 +64,7 @@ function saveInfo(){
alert("DUBBO服务servieName必须填写")
return false;
}
if(serviceType == 'dubbo'){
if(serviceType == 'dubbo' || serviceType == 'generic' ){
if(isEmpty(serviceInterface)){
alert("DUBBO服务serviceInterface必须填写")
return false;
......@@ -96,7 +96,7 @@ function saveInfo(){
if(hostportType === "http" && hostportType !== serviceType){
hostportTypeValidate=false;
}
if(hostportType === "zookeeper" && serviceType !== "dubbo"){
if(hostportType === "zookeeper" && serviceType !== "dubbo" && serviceType !== "generic"){
hostportTypeValidate=false;
}
})
......@@ -132,7 +132,7 @@ function retriveData(){
var serviceType=$("#serviceType").val();
var serviceName=$("#serviceName").val();
if(serviceType == "dubbo"){
if(serviceType == "dubbo" || serviceType == "generic"){
var serviceInterface=$("#serviceInterface").val().trim();
var serviceMethod=$("#serviceMethod").val().trim();
......@@ -231,6 +231,7 @@ function isEmpty(value) {
<div class="controls">
<select id="serviceType" data-rel="chosen">
<option value="http">HTTP</option>
<option value="generic">GENRIC</option>
<option value="dubbo">DUBBO</option>
</select>
<span class="help-inline">类型DUBBO或者HTTP</span>
......
package com.zhaoonline.support.gateway.service;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.servlet.http.HttpServletResponse;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import com.alibaba.dubbo.registry.support.AbstractRegistryFactory;
import com.alibaba.dubbo.rpc.service.EchoService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.netflix.zuul.context.RequestContext;
import com.zhaoonline.support.gateway.config.GWConstants;
import com.zhaoonline.support.gateway.utils.Utils;
/**
* class name:TestDUBBOGenericService <BR>
* class description: 测试dubbo的genericService<BR>
* Remark: <BR>
* @version 1.00 2016年11月29日
* @author zhaoonline)yangyoupeng
*/
public class TestDubboGenericService {
@Test
public void testConctruct(){
ServiceInfo servicInfo=new ServiceInfo();
servicInfo.setValid(true);
servicInfo.setServiceType("generic");
servicInfo.setServiceName("dubboTest");
DubboGenericService dubboservice=new DubboGenericService(servicInfo);
}
@Test
public void testSmalllestEntry(){
Map<Integer, String> src=new HashMap<>();
src.put(4,"aaa");
src.put(2,"bbb");
Entry<Integer, String> smallestKeyEntry= DubboGenericService.smallestEntryByKey(src);
Assert.assertEquals(Integer.valueOf(2), smallestKeyEntry.getKey());
Map<Integer, String> src2=new HashMap<>();
Entry<Integer, String> smallestKeyEntry2= DubboGenericService.smallestEntryByKey(src2);
Assert.assertNull(smallestKeyEntry2);
Map<Integer, String> src3=new HashMap<>();
src3.put(4,"aaa");
Entry<Integer, String> smallestKeyEntry3= DubboGenericService.smallestEntryByKey(src3);
Assert.assertEquals(Integer.valueOf(4), smallestKeyEntry3.getKey());
}
@Test
public void testGetSortedValueByKey(){
Map<Integer, String> src=new HashMap<>();
src.put(4,"aaa");
src.put(2,"bbb");
src.put(3,"ccc");
String[] sortedValue= DubboGenericService.getSortedValueByKey(src);
Assert.assertEquals(3,sortedValue.length);
Assert.assertEquals("bbb",sortedValue[0]);
Assert.assertEquals("ccc",sortedValue[1]);
Assert.assertEquals("aaa",sortedValue[2]);
}
/**
* Method name: testInvokeGenericDubboService <BR>
* Description: DubboGenericService访问dubbo。Logger日志能够看到返回值<BR>
* Remark: <BR>
* @throws Exception void<BR>
*/
@Test
public void testInvokeGenericDubboService() throws Exception{
RequestContext context = RequestContext.getCurrentContext();
HttpServletResponse response=new MockHttpServletResponse();
context.setResponse(response);
ServiceInfo servicInfo=new ServiceInfo();
servicInfo.setValid(true);
servicInfo.setServiceType("DUBBO");
servicInfo.setServiceName("dubboTest");
servicInfo.setServiceMethod("find");
String serviceClass="com.zhaoonline.user.api.user.IUserBankService";
servicInfo.setServiceInterface(serviceClass);
HostPort hostport=new HostPort();
hostport.setProtocol("zookeeper");
hostport.setHost("192.168.0.205");
hostport.setPort(2181);
List<HostPort> hostPorts=Arrays.asList(hostport);
servicInfo.setHostports(hostPorts);
Long bankId=530L;
Map<String, Object> inputData = new HashMap<String, Object>();
inputData.put("para1", bankId);
String innerJson=Utils.toJson(inputData);
Map<Integer, String> interParams=new HashMap<Integer, String>();
interParams.put(1, "java.lang.Long");
Map<String, Integer> paramsMapping=new HashMap<>();
paramsMapping.put("para1", 1);
Map<String, String> attachments=new HashMap<String, String>();
attachments.put("RPC_APP_ID", "tK9BjsZnZFOwcTaMxtP");
servicInfo.setAttachments(attachments);
servicInfo.setInterParams(interParams);
servicInfo.setParamsMapping(paramsMapping);
DubboGenericService dubboservice=new DubboGenericService(servicInfo);
MockHttpServletRequest request2=new MockHttpServletRequest();
request2.setContentType("application/json");
request2.setMethod("POST");
request2.setParameter(GWConstants.URL_SERVICEMETHOD,"find");
request2.setContent(innerJson.getBytes());
dubboservice.init();
dubboservice.forward(request2);
System.out.println(context.getResponseBody());
// dubboservice.getAutomicReference().get().$invoke("$echo", new String[]{"java.lang.String"}, new Object[]{"OK"})
//泛化引用的是没有echo方法。
EchoService echoService= (EchoService )dubboservice.getAutomicReference().get();
String status = (String) echoService.$echo("OK");
System.out.println("------echo result------"+status);
// Assert.assertEquals("OK", status);
dubboservice.destory();
AbstractRegistryFactory.destroyAll();
//dubbo服务中定时器,所以必须要System.exit
System.exit(0);
}
}