@少帮主 你好,想跟你请教个问题:
一些最最基础的服务,可能每个系统都会使用到,而且不经常变化。
如何在zbus启动的时候注入service?
明白!
SET MAIN_OPTS=-p 15555 -store dummy -openBrowser true -track 127.0.0.1:16666;127.0.0.1:16667 -serviceBase ./zbusservice
对应的jar包放到zbusservice目录下面,启动即可。
package org.zbus.serviceprovider;
import java.util.concurrent.ExecutorService;import java.util.concurrent.LinkedBlockingQueue;import java.util.concurrent.ThreadPoolExecutor;import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.zbus.client.service.ServiceConfig;import org.zbus.client.service.ServiceHandler;import org.zbus.client.service.ServiceProvider;import org.zbus.remoting.Message;
public class ServiceP1Test implements ServiceProvider, ServiceHandler { private static final Logger log = LoggerFactory .getLogger(ServiceP1Test.class); private ExecutorService executor = new ThreadPoolExecutor(4, 16, 120, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
public ServiceP1Test() {
}
public ServiceConfig getConfig() { ServiceConfig config = new ServiceConfig(); config.setMq("ServiceP1Test"); config.setServiceHandler(this); return config; }
private void handleMessage(Message req) { // 填写业务逻辑 System.out.println(req); System.out.println("ServiceP1Test server !提供服务了! biz"); }
public Message handleRequest(final Message msg) { Message result = new Message(); try { log.info(msg.toString()); executor.submit(new Runnable() { public void run() { handleMessage(msg); } });
} catch (Throwable e) { log.error(e.getMessage(), e); result.setStatus("error"); result.setBody(e.getMessage()); return result; } result.setStatus("200"); result.setBody("ServiceP1Test Server time: "+System.currentTimeMillis()); return result; }
本质是一样的呢,
public static void loadStartupService(String serviceBase, int serverPort){try{Class<?> loaderClass = Class.forName("org.zbus.client.service.ServiceLoader");Method m = loaderClass.getMethod("load", String.class, String.class);String brokerAddress = String.format("127.0.0.1:%d", serverPort);m.invoke(null, serviceBase, brokerAddress); } catch(Exception e){//ignorelog.debug("loading service error, ignore");}}
http://git.oschina.net/rushmore/zbus/blob/master/src/main/java/org/zbus/server/ServerHelper.java
@红薯 插入链接白屏
SingleBrokerConfig config = new SingleBrokerConfig(); config.setBrokerAddress("127.0.0.1:15555"); Broker broker = new SingleBroker(config); final ServiceLoader serviceLoader = new ServiceLoader(broker); ServiceProvider sp = new ServiceProviderExample();
serviceLoader.loadService(sp); //注册自身到zbus下面。
通过程序注册,搞清楚了。
插件模式下,还真没有搞明白。
支持动态加载JAVA服务,类似tomcat的work目录模式。启动zbus的时候可以指定serviceBase,请看下示例代码呢
http://git.oschina.net/rushmore/zbus/blob/master/src/test/java/org/zbus/ServiceProviderExample.java
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
暂无简介
文章 0 评论 0
接受
发布评论
评论(4)
明白!
SET MAIN_OPTS=-p 15555 -store dummy -openBrowser true -track 127.0.0.1:16666;127.0.0.1:16667 -serviceBase ./zbusservice
对应的jar包放到zbusservice目录下面,启动即可。
package org.zbus.serviceprovider;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zbus.client.service.ServiceConfig;
import org.zbus.client.service.ServiceHandler;
import org.zbus.client.service.ServiceProvider;
import org.zbus.remoting.Message;
public class ServiceP1Test implements ServiceProvider, ServiceHandler {
private static final Logger log = LoggerFactory
.getLogger(ServiceP1Test.class);
private ExecutorService executor = new ThreadPoolExecutor(4, 16, 120,
TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
public ServiceP1Test() {
}
public ServiceConfig getConfig() {
ServiceConfig config = new ServiceConfig();
config.setMq("ServiceP1Test");
config.setServiceHandler(this);
return config;
}
private void handleMessage(Message req) {
// 填写业务逻辑
System.out.println(req);
System.out.println("ServiceP1Test server !提供服务了! biz");
}
public Message handleRequest(final Message msg) {
Message result = new Message();
try {
log.info(msg.toString());
executor.submit(new Runnable() {
public void run() {
handleMessage(msg);
}
});
} catch (Throwable e) {
log.error(e.getMessage(), e);
result.setStatus("error");
result.setBody(e.getMessage());
return result;
}
result.setStatus("200");
result.setBody("ServiceP1Test Server time: "+System.currentTimeMillis());
return result;
}
}
本质是一样的呢,
public static void loadStartupService(String serviceBase, int serverPort){
try{
Class<?> loaderClass = Class.forName("org.zbus.client.service.ServiceLoader");
Method m = loaderClass.getMethod("load", String.class, String.class);
String brokerAddress = String.format("127.0.0.1:%d", serverPort);
m.invoke(null, serviceBase, brokerAddress);
} catch(Exception e){
//ignore
log.debug("loading service error, ignore");
}
}
http://git.oschina.net/rushmore/zbus/blob/master/src/main/java/org/zbus/server/ServerHelper.java
@红薯 插入链接白屏
SingleBrokerConfig config = new SingleBrokerConfig();
config.setBrokerAddress("127.0.0.1:15555");
Broker broker = new SingleBroker(config);
final ServiceLoader serviceLoader = new ServiceLoader(broker);
ServiceProvider sp = new ServiceProviderExample();
serviceLoader.loadService(sp); //注册自身到zbus下面。
通过程序注册,搞清楚了。
插件模式下,还真没有搞明白。
支持动态加载JAVA服务,类似tomcat的work目录模式。启动zbus的时候可以指定serviceBase,请看下示例代码呢
http://git.oschina.net/rushmore/zbus/blob/master/src/test/java/org/zbus/ServiceProviderExample.java