001/** 002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com). 003 * <p> 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * <p> 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * <p> 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package io.jboot.components.rpc.motan; 017 018import com.weibo.api.motan.common.MotanConstants; 019import com.weibo.api.motan.config.RefererConfig; 020import com.weibo.api.motan.config.ServiceConfig; 021import com.weibo.api.motan.util.MotanSwitcherUtil; 022import io.jboot.Jboot; 023import io.jboot.components.rpc.JbootrpcBase; 024import io.jboot.components.rpc.JbootrpcReferenceConfig; 025import io.jboot.components.rpc.JbootrpcServiceConfig; 026import io.jboot.utils.StrUtil; 027 028 029public class JbootMotanrpc extends JbootrpcBase { 030 031 private MotanrpcConfig defaultConfig = Jboot.config(MotanrpcConfig.class); 032 033 @Override 034 public void onStart() { 035 MotanUtil.initMotan(); 036 } 037 038 @Override 039 public <T> T onServiceCreate(Class<T> interfaceClass, JbootrpcReferenceConfig config) { 040 RefererConfig<T> referer = MotanUtil.toRefererConfig(config); 041 referer.setInterface(interfaceClass); 042 043 String directUrl = rpcConfig.getUrl(interfaceClass.getName()); 044 if (StrUtil.isNotBlank(directUrl)) { 045 referer.setDirectUrl(directUrl); 046 } 047 048 String consumer = rpcConfig.getConsumer(interfaceClass.getName()); 049 if (consumer != null) { 050 referer.setBasicReferer(MotanUtil.getBaseReferer(consumer)); 051 } 052 053 if (referer.getGroup() == null) { 054 referer.setGroup(rpcConfig.getGroup(interfaceClass.getName())); 055 } 056 057 if (referer.getVersion() == null) { 058 referer.setVersion(rpcConfig.getVersion(interfaceClass.getName())); 059 } 060 061 return referer.getRef(); 062 } 063 064 065 @Override 066 public <T> boolean serviceExport(Class<T> interfaceClass, Object object, JbootrpcServiceConfig config) { 067 068 synchronized (this) { 069 070 MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, false); 071 072 ServiceConfig<T> service = MotanUtil.toServiceConfig(config); 073 service.setInterface(interfaceClass); 074 service.setRef((T) object); 075 service.setShareChannel(true); 076 service.setExport(defaultConfig.getExport(interfaceClass.getName())); 077 service.setHost(defaultConfig.getHost(interfaceClass.getName())); 078 079 String provider = rpcConfig.getProvider(interfaceClass.getName()); 080 if (provider != null) { 081 service.setBasicService(MotanUtil.getBaseService(provider)); 082 } 083 084 if (service.getGroup() == null) { 085 service.setGroup(rpcConfig.getGroup(interfaceClass.getName())); 086 } 087 088 if (service.getVersion() == null) { 089 service.setVersion(rpcConfig.getVersion(interfaceClass.getName())); 090 } 091 092 service.export(); 093 MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true); 094 } 095 096 return true; 097 } 098 099 100}