dubbo服务运行的三种方式
dubbo服务运行,也就是让生产服务的进程一直启动。如果生产者进程挂掉,也就不存在生产者,消费者不能进行消费。
Dubbo服务运行的三种方式如下:
1、使用Servlet容器运行(Tomcat、Jetty等) -不可用
缺点:增加复杂性(端口、管理)
浪费资源(内存)
需要占用多个端口和内存,这种方式是不可取的
2、自己建立Main方法类运行(Srping容器)
缺点:Dobbo本省提供的高级特性没用上,自己的启动类可能有缺陷
3、使用Dubbo框架提供的Main方法类来运行(Spring 容器) 建议使用
优点:框架本身提供(com.alibaba.dubbo.container.Main),可是现优雅关机(ShutdownHook)。
这个不存在缓存,生产者开启或者关闭之后dubbo-admin可以马上感知到。
下面测试方法2和方法3。
1.自建类发布服务:(简单-重要)
pom.xml如下:
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>cn.qlq</groupId>
- <artifactId>zookeper</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <dependencies>
- <dependency>
- <groupId>org.apache.zookeeper</groupId>
- <artifactId>zookeeper</artifactId>
- <version>3.4.8</version>
- </dependency>
- <dependency>
- <groupId>org.apache.curator</groupId>
- <artifactId>curator-framework</artifactId>
- <version>2.12.0</version>
- </dependency>
- <dependency>
- <groupId>org.apache.curator</groupId>
- <artifactId>curator-recipes</artifactId>
- <version>2.12.0</version>
- </dependency>
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>dubbo</artifactId>
- <version>2.6.6</version>
- </dependency>
- <dependency>
- <groupId>org.javassist</groupId>
- <artifactId>javassist</artifactId>
- <version>3.20.0-GA</version>
- </dependency>
- <dependency>
- <groupId>io.netty</groupId>
- <artifactId>netty-all</artifactId>
- <version>4.1.34.Final</version>
- </dependency>
- <dependency>
- <groupId>com.101tec</groupId>
- <artifactId>zkclient</artifactId>
- <version>0.2</version>
- </dependency>
- <dependency>
- <groupId>org.apache.curator</groupId>
- <artifactId>curator-framework</artifactId>
- <version>2.12.0</version>
- </dependency>
- <dependency>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- <version>1.2.16</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-api</artifactId>
- <version>1.7.25</version>
- </dependency>
- <dependency>
- <groupId>commons-lang</groupId>
- <artifactId>commons-lang</artifactId>
- <version>2.6</version>
- </dependency>
- </dependencies>
- <build>
- <!-- 配置了很多插件 -->
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>3.5.1</version>
- <configuration>
- <source>1.7</source>
- <target>1.7</target>
- <encoding>UTF-8</encoding>
- </configuration>
- </plugin>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
- </project>
1.1 xml配置格式的发布
provider.xml
- <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xmlns="http://www.springframework.org/schema/beans"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
- <!-- 提供方应用信息,用于计算依赖关系 -->
- <dubbo:application name="hello-world-app">
- <!-- 关闭QOS:Quality of Service -->
- <dubbo:parameter key="qos.enable" value="false" />
- <dubbo:parameter key="qos.accept.foreign.ip" value="true" />
- <dubbo:parameter key="qos.port" value="2222" />
- </dubbo:application>
- <!-- 使用multicast广播注册中心暴露服务地址,IP设置: 224.0.0.0 - 239.255.255.255 -->
- <dubbo:registry address="zookeeper://127.0.0.1:2181" />
- <!-- 用dubbo协议在20880端口暴露服务 -->
- <dubbo:protocol name="dubbo" port="20880" />
- <!-- 声明需要暴露的服务接口 -->
- <dubbo:service interface="dubbo.DubboService" ref="demoService" />
- <!-- 和本地bean一样实现服务 -->
- <bean id="demoService" class="dubbo.DubboServiceImpl" />
- </beans>
provider.java发布服务:
- package dubbo;
- import java.io.IOException;
- import java.util.concurrent.CountDownLatch;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class Provider {
- public static void main(String[] args) throws IOException, InterruptedException {
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "provider.xml" });
- context.start();
- CountDownLatch countDownLatch = new CountDownLatch(1);
- countDownLatch.await();
- }
- }
启动之后查看dubbo-admin管理界面:
2.API格式的发布
- package dubbo.api;
- import java.util.concurrent.CountDownLatch;
- import com.alibaba.dubbo.config.ApplicationConfig;
- import com.alibaba.dubbo.config.ProtocolConfig;
- import com.alibaba.dubbo.config.RegistryConfig;
- import com.alibaba.dubbo.config.ServiceConfig;
- import dubbo.DubboService;
- import dubbo.DubboServiceImpl;
- public class Provider {
- public static void main(String[] args) throws InterruptedException {
- CountDownLatch countDownLatch = new CountDownLatch(1);
- // 服务实现
- DubboService dubboService = new DubboServiceImpl();
- // 当前应用配置
- ApplicationConfig application = new ApplicationConfig();
- application.setName("provider");
- // 连接注册中心配置
- RegistryConfig registry = new RegistryConfig();
- registry.setAddress("zookeeper://127.0.0.1:2181");
- registry.setUsername("aaa");
- registry.setPassword("bbb");
- // 服务提供者协议配置
- ProtocolConfig protocol = new ProtocolConfig();
- protocol.setName("dubbo");
- protocol.setPort(12345);
- protocol.setThreads(200);
- // 注意:ServiceConfig为重对象,内部封装了与注册中心的连接,以及开启服务端口
- // 服务提供者暴露服务配置
- ServiceConfig<DubboService> service = new ServiceConfig<DubboService>(); // 此实例很重,封装了与注册中心的连接,请自行缓存,否则可能造成内存和连接泄漏
- service.setApplication(application);
- service.setRegistry(registry); // 多个注册中心可以用setRegistries()
- service.setProtocol(protocol); // 多个协议可以用setProtocols()
- service.setInterface(DubboService.class);
- service.setRef(dubboService);
- service.setVersion("1.0.0");
- // 暴露及注册服务
- service.export();
- countDownLatch.await();
- }
- }
3.注解方式的发布
参考:https://www.cnblogs.com/qlqwjy/p/10539824.html
补充:自建类打成jar包的方式运行:
修改pom.xml
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>cn.qlq</groupId>
- <artifactId>zookeper</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <dependencies>
- <dependency>
- <groupId>org.apache.zookeeper</groupId>
- <artifactId>zookeeper</artifactId>
- <version>3.4.8</version>
- </dependency>
- <dependency>
- <groupId>org.apache.curator</groupId>
- <artifactId>curator-framework</artifactId>
- <version>2.12.0</version>
- </dependency>
- <dependency>
- <groupId>org.apache.curator</groupId>
- <artifactId>curator-recipes</artifactId>
- <version>2.12.0</version>
- </dependency>
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>dubbo</artifactId>
- <version>2.6.6</version>
- </dependency>
- <dependency>
- <groupId>org.javassist</groupId>
- <artifactId>javassist</artifactId>
- <version>3.20.0-GA</version>
- </dependency>
- <dependency>
- <groupId>io.netty</groupId>
- <artifactId>netty-all</artifactId>
- <version>4.1.34.Final</version>
- </dependency>
- <dependency>
- <groupId>com.101tec</groupId>
- <artifactId>zkclient</artifactId>
- <version>0.2</version>
- </dependency>
- <dependency>
- <groupId>org.apache.curator</groupId>
- <artifactId>curator-framework</artifactId>
- <version>2.12.0</version>
- </dependency>
- <dependency>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- <version>1.2.16</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-api</artifactId>
- <version>1.7.25</version>
- </dependency>
- <dependency>
- <groupId>commons-lang</groupId>
- <artifactId>commons-lang</artifactId>
- <version>2.6</version>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.9</version>
- </dependency>
- </dependencies>
- <build>
- <!-- 配置了很多插件 -->
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>3.5.1</version>
- <configuration>
- <source>1.7</source>
- <target>1.7</target>
- <encoding>UTF-8</encoding>
- </configuration>
- </plugin>
- <!-- 打包jar文件时,配置manifest文件,加入lib包的jar依赖 -->
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-jar-plugin</artifactId>
- <configuration>
- <classesDirectory>target/classes/</classesDirectory>
- <archive>
- <manifest>
- <mainClass>dubbo.Provider</mainClass>
- <!-- 打包时 MANIFEST.MF文件不记录的时间戳版本 -->
- <useUniqueVersions>false</useUniqueVersions>
- <addClasspath>true</addClasspath>
- <classpathPrefix>lib/</classpathPrefix>
- </manifest>
- <manifestEntries>
- <Class-Path>.</Class-Path>
- </manifestEntries>
- </archive>
- </configuration>
- </plugin>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-dependency-plugin</artifactId>
- <executions>
- <execution>
- <id>copy-dependencies</id>
- <phase>package</phase>
- <goals>
- <goal>copy-dependencies</goal>
- </goals>
- <configuration>
- <type>jar</type>
- <includeTypes>jar</includeTypes>
- <useUniqueVersions>false</useUniqueVersions>
- <outputDirectory>
- ${project.build.directory}/lib
- </outputDirectory>
- </configuration>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
- </project>
maven-jar-plugin 可以设置打包的一些信息。设置jar包运行的主类是dubbo.Provider
maven-dependency-plugin是打包项目所依赖的jar包,上面配置是生成依赖的jar包到target的lib目录下。
运行maven install命令生成jar包和对应的lib目录:
cmd运行下面命令即可:
- E:\xiangmu\zookeper\target>set classpath=%classpath%;./lib;
- E:\xiangmu\zookeper\target>java -jar zookeper-0.0.-SNAPSHOT.jar
dubbo-admin查看项目:
2. 使用dubbo提供的类来发布
使用com.alibaba.dubbo.container.Main类来发布
修改pom.xml即可:
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>cn.qlq</groupId>
- <artifactId>zookeper</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.12</version>
- </dependency>
- <dependency>
- <groupId>org.apache.zookeeper</groupId>
- <artifactId>zookeeper</artifactId>
- <version>3.4.8</version>
- </dependency>
- <dependency>
- <groupId>org.apache.curator</groupId>
- <artifactId>curator-framework</artifactId>
- <version>2.12.0</version>
- </dependency>
- <dependency>
- <groupId>org.apache.curator</groupId>
- <artifactId>curator-recipes</artifactId>
- <version>2.12.0</version>
- </dependency>
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>dubbo</artifactId>
- <version>2.6.6</version>
- </dependency>
- <dependency>
- <groupId>org.javassist</groupId>
- <artifactId>javassist</artifactId>
- <version>3.20.0-GA</version>
- </dependency>
- <dependency>
- <groupId>io.netty</groupId>
- <artifactId>netty-all</artifactId>
- <version>4.1.34.Final</version>
- </dependency>
- <dependency>
- <groupId>com.101tec</groupId>
- <artifactId>zkclient</artifactId>
- <version>0.2</version>
- </dependency>
- <dependency>
- <groupId>org.apache.curator</groupId>
- <artifactId>curator-framework</artifactId>
- <version>2.12.0</version>
- </dependency>
- <dependency>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- <version>1.2.16</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-api</artifactId>
- <version>1.7.25</version>
- </dependency>
- <dependency>
- <groupId>commons-lang</groupId>
- <artifactId>commons-lang</artifactId>
- <version>2.6</version>
- </dependency>
- <dependency>
- <groupId>ch.qos.logback</groupId>
- <artifactId>logback-core</artifactId>
- <version>1.1.11</version>
- </dependency>
- </dependencies>
- <build>
- <!--jar包名称 -->
- <finalName>dubboService</finalName>
- <resources>
- <!--recources文件夹下的所有文件都打进jar包 -->
- <resource>
- <targetPath>${project.build.directory}/classes</targetPath>
- <directory>src/main/resources</directory>
- <filtering>true</filtering>
- <includes>
- <include>**/*.xml</include>
- <include>**/*.properties</include>
- </includes>
- </resource>
- <!-- 上文有说过,由于我的provider.xml文件不是在META-INF下,这一段代码的作用就是将 provider.xml文件拷贝到META-INF目录下 -->
- <resource>
- <targetPath>${project.build.directory}/classes/META-INF/spring</targetPath>
- <directory>src/main/resources/</directory>
- <filtering>true</filtering>
- <includes>
- <include>provider.xml</include>
- </includes>
- </resource>
- </resources>
- <pluginManagement>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>3.5.1</version>
- <configuration>
- <source>1.7</source>
- <target>1.7</target>
- <encoding>UTF-8</encoding>
- </configuration>
- </plugin>
- <!-- 解决Maven插件在Eclipse内执行了一系列的生命周期引起冲突 -->
- <plugin>
- <groupId>org.eclipse.m2e</groupId>
- <artifactId>lifecycle-mapping</artifactId>
- <version>1.0.0</version>
- <configuration>
- <lifecycleMappingMetadata>
- <pluginExecutions>
- <pluginExecution>
- <pluginExecutionFilter>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-dependency-plugin</artifactId>
- <versionRange>[2.0,)</versionRange>
- <goals>
- <goal>copy-dependencies</goal>
- </goals>
- </pluginExecutionFilter>
- <action>
- <ignore />
- </action>
- </pluginExecution>
- </pluginExecutions>
- </lifecycleMappingMetadata>
- </configuration>
- </plugin>
- </plugins>
- </pluginManagement>
- <plugins>
- <!-- 打包jar文件时,配置manifest文件,加入lib包的jar依赖 -->
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-jar-plugin</artifactId>
- <configuration>
- <classesDirectory>target/classes/</classesDirectory>
- <archive>
- <manifest>
- <mainClass>com.alibaba.dubbo.container.Main</mainClass>
- <!-- 打包时 MANIFEST.MF文件不记录的时间戳版本 -->
- <useUniqueVersions>false</useUniqueVersions>
- <addClasspath>true</addClasspath>
- <classpathPrefix>lib/</classpathPrefix>
- </manifest>
- <manifestEntries>
- <Class-Path>.</Class-Path>
- </manifestEntries>
- </archive>
- </configuration>
- </plugin>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-dependency-plugin</artifactId>
- <executions>
- <execution>
- <id>copy-dependencies</id>
- <phase>package</phase>
- <goals>
- <goal>copy-dependencies</goal>
- </goals>
- <configuration>
- <type>jar</type>
- <includeTypes>jar</includeTypes>
- <useUniqueVersions>false</useUniqueVersions>
- <outputDirectory>
- ${project.build.directory}/lib
- </outputDirectory>
- </configuration>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
- </project>
${project.build.directory} 构建目录,缺省为target。
运行maven install 或者package打包即可,然后到target目录下运行该jar包:
- PS E:\xiangmu\zookeper\target> java -jar .\dubboService.jar
- [-- ::] Dubbo service server started!
dubbo-admin查看项目:
简单的方式还是自己编写类发布服务。
dubbo服务运行的三种方式的更多相关文章
- 【dubbo】服务提供者运行的三种方式
[dubbo]服务提供者运行的三种方式 学习了:https://blog.csdn.net/yxwb1253587469/article/details/78712451 1,使用容器: 2,使用自建 ...
- Linux中设置服务自启动的三种方式
有时候我们需要Linux系统在开机的时候自动加载某些脚本或系统服务 主要用三种方式进行这一操作: ln -s 在/etc/rc.d/rc*.d目录中建立/e ...
- [转]Linux中设置服务自启动的三种方式
from:http://www.cnblogs.com/nerxious/archive/2013/01/18/2866548.html 有时候我们需要Linux系统在开机的时候自动加载某些脚本或系统 ...
- (转)Linux中设置服务自启动的三种方式
有时候我们需要Linux系统在开机的时候自动加载某些脚本或系统服务 主要用三种方式进行这一操作: ln -s 在/etc/rc.d/rc*.d目录中建立/e ...
- 【转发】Linux中设置服务自启动的三种方式
有时候我们需要Linux系统在开机的时候自动加载某些脚本或系统服务 主要用三种方式进行这一操作: ln -s 在/etc/rc.d/rc*.d目录中建立/e ...
- Linux中设置服务自启动的三种方式,ln -s 建立启动软连接
有时候我们需要Linux系统在开机的时候自动加载某些脚本或系统服务(http://www.0830120.com) 主要用三种方式进行这一操作: ln -s 在/etc/rc.d/rc*.d目录中建立 ...
- springboot与dubbo整合入门(三种方式)
Springboot与Dubbo整合三种方式详解 整合环境: jdk:8.0 dubbo:2.6.2 springboot:2.1.5 项目结构: 1.搭建项目环境: (1)创建父项目与三个子项目,创 ...
- Linux上后台保持Terminal交互运行的三种方式:nohub、screen和tmux
镜像下载.域名解析.时间同步请点击 阿里云开源镜像站 后台运行 Linux上,如果一个进程需要保持后台运行,尤其是在Linux服务器上,后台运行程序.避免因为SSH连接断开而导致进程停止运行时,该怎么 ...
- gradle springboot 项目运行的三种方式
一.java -jar 二.eclipse中 Java Application 三.命令行 gradle bootRun
随机推荐
- 001_JavaScript数组常用方法总结及使用案例
一. https://msdn.microsoft.com/zh-cn/library/ff679976(v=vs.94).aspx 二.ip地址转换成对应的二进制. const ip='172.16 ...
- 苹果手机连接Wifi认证机制
Wifi状态保持方法和nas设备 https://patents.google.com/patent/CN106793171A/zh 基于ios终端的离线wifi热点认证方法和认证系统 https:/ ...
- MyBatis 学习总结 01 快速入门
本文测试源码下载地址: http://onl5wa4sd.bkt.clouddn.com/MyBatis0918.rar 一.Mybatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级 ...
- 通过Docker发布RestAPI遇到的种种问题
目标:发布一个分词API 问题1:Docker外无法访问API 原因: Docker映射的地址是0.0.0.0:8888端口,而flask启动的时候默认地址是127.0.0.1:5000,需要手动配置 ...
- 一、TensorFlow初探
目录 计算模型 数据模型 运行模型 TensorFlow及神经网络 import tensorflow as tf a = tf.constant([1.0, 2.0], name='a', dtyp ...
- DependencyInjection源码解读之ServiceProvider
var services = new ServiceCollection(); var _serviceProvider = services.BuildServiceProvider(); serv ...
- CodeForces Round #545 Div.2
A. Sushi for Two 代码: #include <bits/stdc++.h> using namespace std; ; ; int a[maxn], vis[maxn]; ...
- 用Python开发小学二年级口算自动出题程序
版权声明:本文为博主原创文章,欢迎转载,并请注明出处.联系方式:460356155@qq.com 武汉光谷一小二年级要求家长每天要给小孩出口算题目,让孩子练习. 根据老师出题要求编写了Python程序 ...
- Python进阶5---StringIO和BytesIO、路径操作、OS模块、shutil模块
StringIO StringIO操作 BytesIO BytesIO操作 file-like对象 路径操作 路径操作模块 3.4版本之前:os.path模块 3.4版本开始 建议使用pathlib模 ...
- EntityFramework优化:查询性能
1. 禁用延迟加载 延迟加载是常见的方式,Entity Framework在需要时可以自动为一个实体的实例获取关联的数据. Entity Framework自动延迟加载需要同时满足以下3个条件: (1 ...