Dubbo Main启动方式配置

Dubbo Main启动方式配置

Dubbo Main启动方式配置

Dubbo Main启动服务容器是一个standalone的启动程序,因为后台服务不需要Tomcat或JBoss等Web容器的功能,如果硬要用Web容器去加载服务提供方,增加复杂性,也浪费资源。
服务容器只是一个简单的Main方法,并加载一个简单的Spring容器,用于暴露服务。
服务容器的加载内容可以扩展,内置了spring, jetty, log4j等加载,可通过Container扩展点进行扩展,参见:Container
Spring Container

    如不配置dubbo.spring.config 会自动加载META-INF/spring目录下的所有Spring配置。

    手动配置:(配在java命令-D参数或者dubbo.properties中)

dubbo.spring.config=classpath*:*.xml ----配置spring配置加载位置  如

 
  1. dubbo.container=log4j,spring
  2. dubbo.application.name=hello-core-service
  3. dubbo.application.owner=coret
  4. dubbo.registry.address=zookeeper://127.0.0.1:2181
  5. dubbo.protocol.name=dubbo
  6. dubbo.protocol.port=56432
  7. dubbo.service.loadbalance=roundrobin
  8. dubbo.spring.config=classpath:dubbo-provider.xml
  9. dubbo.log4j.level=DEBUG
  10. dubbo.log4j.level=INFO

Jetty Container

启动一个内嵌Jetty,用于汇报状态。
配置:(配在java命令-D参数或者dubbo.properties中)
dubbo.jetty.port=8080 ----配置jetty启动端口
dubbo.jetty.directory=/foo/bar ----配置可通过jetty直接访问的目录,用于存放静态文件
dubbo.jetty.page=log,status,system ----配置显示的页面,缺省加载所有页面

Log4j Container

自动配置log4j的配置,在多进程启动时,自动给日志文件按进程分目录。
配置:(配在java命令-D参数或者dubbo.properties中)
dubbo.log4j.file=/foo/bar.log ----配置日志文件路径
dubbo.log4j.level=WARN ----配置日志级别

dubbo.log4j.subdirectory=20880 ----配置日志子目录,用于多进程启动,避免冲突

服务启动

 
  1. public class CoreLauncher {
  2.     private static Logger logger =  LoggerFactory.getLogger(CoreLauncher.class);
  3.     /**
  4.      * @param args
  5.      */
  6.     public static void main(String[] args) {
  7.         getLocalip();
  8.         logger.info("开始启动asset");
  9.         //第一种   通过其他容器启动如 tomcat ,jboss等 以web服务的形式启动
  10.         // 第二种  通过自定义main函数
  11.     /*  ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
  12.                 "classpath:dubbo-provider.xml");
  13.         
  14.         context.start();
  15.         
  16.         synchronized (CoreLauncher.class) {
  17.             while (true) {
  18.                 try {
  19.                     CoreLauncher.class.wait();
  20.                 } catch (Throwable e) {
  21.                 }
  22.             }
  23.         }
  24.         */
  25.         //第三种  通过dubbo.container.main  要在classpath目录下配置 dubbo.properties 相关配置
  26.         com.alibaba.dubbo.container.Main.main(args);
  27.     }
  28.     private static void getLocalip() {
  29.         try {
  30.             System.out.println("服务暴露的ip: "
  31.                     + java.net.InetAddress.getLocalHost().getHostAddress());
  32.         } catch (Exception e) {
  33.             logger.error(e.getMessage(), e);
  34.         }
  35.     }
  36. }