參考:https://www.jianshu.com/p/4a9b7680b47b
參考:https://www.cnblogs.com/gzyy1987/p/4703478.html
定義一個註解類,方便使用註解的方式自動注入服務類。
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface RemoteService {
String value() default "" ;
}
定義數據交互的端口直接在構造函數中傳入端口就可以了。這樣方式最爲簡單。@RemoteService爲自定義的註解值,方便使用註解的方式定義服務。注意實現類要繼承自UnicastRemoteObject類。可以設置一個類繼承自UnicastRemoteObject類,並在這個類中實現數據端口的定義【super(10087)】。
@Service
@RemoteService
public class CityServiceImple extends UnicastRemoteObject implements CityService {
private static final long serialVersionUID = 1L;
@Autowired
private CityDao cityDao;
@Autowired
private DataSourceHelper dataSourceHelper;
@Autowired
private DataInfoService dataInfoService;
public CityServiceImple() throws Exception {
super(10087);
}
@Override
public City selectByPrimaryKey(Integer companyid,Long cityid) throws RemoteException {
String key = dataInfoService.getDataSourceKey(companyid, DataSourceEnum.Comm.name(), OperationType.Read);
DynamicDataSource.setDataSourceKey(dataSourceHelper.getDataSourceKey(key));
return cityDao.selectByPrimaryKey(cityid);
}
@Override
public List<City> getAll(Integer companyid) throws RemoteException {
String key = dataInfoService.getDataSourceKey(companyid, DataSourceEnum.Comm.name(), OperationType.Read);
DynamicDataSource.setDataSourceKey(dataSourceHelper.getDataSourceKey(key));
return cityDao.getAll();
}
}
注意服務類要實現 繼承Remote。
public interface CityService extends Remote{
City selectByPrimaryKey(Integer companyid,Long cityid) throws java.rmi.RemoteException;
List<City> getAll(Integer companyid) throws java.rmi.RemoteException;
}
下面是啓動服務的源碼
public static void main(String[] args) {
logger.debug("RMI服務端啓動......");
System.out.println("RMI服務端啓動");
try {
ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
int port = Integer.parseInt(PropertyUtils.getValue("rmi.port"));
Registry registry = LocateRegistry.createRegistry(port);
String rmiIP=PropertyUtils.getValue("rmi.ip");
System.setProperty("java.rmi.server.hostname",rmiIP);
// 在端口上創建一個遠程對象註冊表
/*try{
registry = LocateRegistry.getRegistry(port);
}catch(RemoteException e){
System.out.println("RMI服務不存在");
registry = LocateRegistry.createRegistry(port);
logger.debug("RMI服務不存在");
}*/
Map<String, Object> beans = ctx.getBeansWithAnnotation(RemoteService.class);
for (Entry<String, Object> item : beans.entrySet()) {
String name =
(AopUtils.getTargetClass(item.getValue()).getInterfaces()[0]).getName();
String bindstr = String.format("rmi://%s:%d/%s", rmiIP,port, name);
System.out.println(bindstr);
//registry.rebind(bindstr, (Remote) item.getValue());
Naming.bind(bindstr, (Remote) item.getValue());
}
System.out.println("RMI服務端啓動完成");
Scanner sc = new Scanner(System.in);
while (true) {
String o = sc.nextLine();
if (o.equals("shutdown")) {
UnicastRemoteObject.unexportObject(registry, true);
System.out.println("RMI服務關閉");
break;
}
if (o.equals("start")) {
registry = BindRMI(port, ctx);
}
}
sc.close();
} catch (Exception e) {
e.printStackTrace();
logger.error(e.getMessage());
}
}
下面是直接啓動jar文件的shell
#!/bin/bash
#description: 啓動重啓server服務
#端口號,根據此端口號確定PID
#******************************************需要配置*************************************************************************#
PORT=10086
#啓動命令所在目錄
#******************************************需要配置*************************************************************************#
HOME='/usr/local/src'
#查詢出監聽了PORT端口TCP協議的程序
pid=`netstat -anp|grep $PORT|awk '{printf $7}'|cut -d/ -f1`
start(){
if [ -n "$pid" ]; then
echo "server already start,pid:$pid"
return 0
fi
#進入命令所在目錄
#******************************************需要配置*************************************************************************#
cd $HOME
nohup java -jar $HOME/RMIService.jar > /logs/rmiervice.log 2>&1 & #啓動聊天服務器 把日誌輸出到HOME目錄的server.log文件中
echo "start at port:$PORT"
}
stop(){
if [ -z "$pid" ]; then
echo "not find program on port:$PORT"
return 0
fi
#結束程序,使用訊號2,如果不行可以嘗試訊號9強制結束
kill -9 $pid
rm -rf $pid
echo "kill program use signal 2,pid:$pid"
}
status(){
if [ -z "$pid" ]; then
echo "not find program on port:$PORT"
else
echo "program is running,pid:$pid"
fi
}
case $1 in
start)
start
;;
stop)
stop
;;
restart)
$0 stop
sleep 2
$0 start
;;
status)
status
;;
*)
echo "Usage: {start|stop|status}"
;;
esac
exit 0
自啓動使用supervisor就可以了。只需要在ini文件中配置command時增加 start參數即可。參考:https://mp.csdn.net/postedit/84630869
[program:rmiservice]
directory=/usr/local/src/
command=/usr/local/src/rmiservice.sh start run
environment=JAVA_HOME="/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64",JAVA_BIN="/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64/bin"
user=root
autostart=true
autorestart=true
stdout_logfile=/var/log/supervisor/%(program_name)s.log
stderr_logfile=/var/log/supervisor/%(program_name)s.log