Dubbo入门到精通学习笔记(二):Dubbo管理控制台、使用Maven构建Dubbo服务的可执行jar包

版权声明:本文为作者原创,转载请注明出处,联系qq:32248827 https://blog.csdn.net/dataiyangu/article/details/88543539

Dubbo管理控制台

1、Dubbo管理控制台的主要作用:

服务治理

2、管理控制台主要包含:

路由规则
动态配置
服务降级
访问控制
权重调整 负载均衡等管理功能

3、管理控制台版本:

当前稳定版:dubbo-admin-2.5.3.war (可到Q群367211134 共享文件下载)
最新2.5.4-SNAPSHOT版下载地址: https://github.com/alibaba/dubbo

安装 Dubbo 管理控制台

Dubbo 管控台可以对注册到 zookeeper 注册中心的服务或服务消费者进行管理,但
管控台是否正常对 Dubbo 服务没有影响,管控台也不需要高可用,因此可以单节点部署。
IP: 192.168.3.71
部署容器:apache-tomcat-7.0.57
端口:8080

1、 下载最新版的 Tomcat7:

$wget
http://mirrors.hust.edu.cn/apache/tomcat/tomcat-7/v7.0.57/bin/apache-tomcat-7.0.57 .tar.gz

2、 解压:

$ tar -zxvf apache-tomcat-7.0.57.tar.gz
$ mv apache-tomcat-7.0.57 dubbo-admin-tomcat

3、 移除/home/wusc/dubbo-admin-tomcat/webapps 目录下的所有文件:

 $ rm -rf *

4、 上传 Dubbo 管理控制台程序 dubbo-admin-2.5.3.war 到/home/wusc/dubbo-admin-tomcat/webapps
5、 解压并把目录命名为 ROOT:

$ unzip dubbo-admin-2.5.3.war -d ROOT

把 dubbo-admin-2.5.3.war 移到/home/wusc/tools 目录备份

 $ mv dubbo-admin-2.5.3.war /home/wusc/tools

6、 配置 dubbo.properties(在ROOT/WEB-INF/):

$ vi ROOT/WEB-INF/dubbo.properties
dubbo.registry.address=zookeeper://192.168.3.71:2181 
dubbo.admin.root.password=wusc.123//root用户的密码是wusc.123
dubbo.admin.guest.password=wusc.123//guest用户的密码是wusc.123

(以上密码在正式上生产前要修改)
7、 防火墙开启 8080 端口,用 root 用户修改/etc/sysconfig/iptables

 # vi /etc/sysconfig/iptables

增加:

## dubbo-admin-tomcat:8080
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT

重启防火墙:

# service iptables restart

8、 启动 Tomat7

$ /home/wusc/dubbo-admin-tomcat/bin/startup.sh 

9、 浏览 http://192.168.3.71:8080/
会要求输入用户名密码
在这里插入图片描述
10、 配置部署了 Dubbo 管控台的 Tomcat 开机启动: 在虚拟主机中编辑/etc/rc.local 文件,加入:

su - wusc -c '/home/wusc/dubbo-admin-tomcat/bin/startup.sh'

验证:
启动provider和consumer,Dubbo管理控制台==》服务治理==》应用

使用Maven构建Dubbo服务的可执行jar包

Dubbo服务的运行方式:

1、使用Servlet容器运行(Tomcat、Jetty等)----不可取

将provider做成web工程,在web.xml中配置

<web-app>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring/spring-context.xml</param-value>
	</context-param>
</web-app>

然后通过tomcat启动即可

缺点:增加复杂性(端口、管理)、浪费资源(内存)

服务器容器是一个standalone的启动程序,因为后台服务不需要tomcat或jboss等web容器的功能,如果硬要用wen容器去加载服务提供方,增加复杂性(需要多个端口,还需要配置,内存调优),也浪费资源(tomcat也占用内存)。

2、自建Main方法类来运行(Spring容器) ----不建议(本地调试可用)

自己写一个测试类来加载spring文件

package dubbo.test;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 
 * @描述: 启动Dubbo服务用的MainClass.
 * @作者: WuShuicheng .
 * @创建时间: 2013-11-5,下午9:47:55 .
 * @版本: 1.0 .
 */
public class DubboProvider {
	
	private static final Log log = LogFactory.getLog(DubboProvider.class);

	public static void main(String[] args) {
		try {
			ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/spring-context.xml");
			context.start();
		} catch (Exception e) {
			log.error("== DubboProvider context start error:",e);
		}
		synchronized (DubboProvider.class) {
			while (true) {
				try {
					DubboProvider.class.wait();
				} catch (InterruptedException e) {
					log.error("== synchronized error:",e);
				}
			}
		}
	}
    
}

缺点: Dobbo本身提供的高级特性没用上、自已编写启动类可能会有缺陷

3、使用Dubbo框架提供的Main方法类来运行(Spring容器)----建议使用

优点:框架本身提供(com.alibaba.dubbo.container.Main)、可实现优雅关机(ShutdownHook)
优雅关机:
dubbo是通过jdk的ShutdownH噢OK未完成优雅停机的,所以如果用户使用kill -9 PID等强制关闭指令,是不会执行优雅停机的,只有通过kill PId,才会执行。
原理

  • 服务提供方:
    停止时,先标记为不接受新请求,心情求过来时直接报错,让客户端重试其他机器。
    然后,检测线程池中的线程是否正常运行,如果有,等待所有线程执行完成,除非超时,则强制关闭。
  • 服务消费方:
    停止时,不在发起新的调用请求,所有新的调用在客户端即报错。
    然后,检测有没有请求的响应还没有返回,等待响应返回,除非超时,则强制关闭。

注意点:
spring-context.xml

<import resource="classpath:spring/spring-xxx.xml" />

//为什么有这一步?
//dubbo官方文档中说:如果用到com.alibaba.dubbo.container.Main这个类启动的话,
//会自动加载META-INF/spring目录下的所有的spring配置,如何配置?
//配置在java命令-D参数或者dubbo.properties中
// dubbo.spring.config-classpath*:META-INF/spring/*.xml====》配置spring配置加载位置

maven配置文件

provider==>pom.xml

build节点

//项目构建项目的时候用的
<build>
		//构建的项目名
		<finalName>edu-service-user</finalName>
		<resources>
		//将resource下的所有配置文件构建进来
			<resource>
				<targetPath>${project.build.directory}/classes</targetPath>
				<directory>src/main/resources</directory>
				<filtering>true</filtering>
				<includes>
					<include>**/*.xml</include>
					<include>**/*.properties</include>
				</includes>
			</resource>
			<!-- 结合com.alibaba.dubbo.container.Main -->
			//为什么有这一步?
			//dubbo官方文档中说:如果用到com.alibaba.dubbo.container.Main这个类启动的话,
			//会自动加载META-INF/spring目录下的所有的spring配置,如何配置?
			//配置在java命令-D参数或者dubbo.properties中
			// dubbo.spring.config-classpath*:META-INF/spring/*.xml====》配置spring配置加载位置
			
			//因为我们项目中spring的配置文件是在src/main/resources/spring下的
			//所以这里的作用就是把这些配置文件在加载的时候放在
			//${project.build.directory}/classes/META-INF/spring目录下
			<resource>
				<targetPath>${project.build.directory}/classes/META-INF/spring</targetPath>
				<directory>src/main/resources/spring</directory>
				<filtering>true</filtering>
				<includes>
					<include>spring-context.xml</include>
				</includes>
			</resource>
		</resources>
		//插件管理
		<pluginManagement>
			<plugins>
				<!-- 解决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>
					//把target/classes/下所有的文件拷贝到jar包里面去
					<classesDirectory>target/classes/</classesDirectory>
					<archive>
					//可执行jar包的main类
						<manifest>
							<mainClass>com.alibaba.dubbo.container.Main</mainClass>
							<!-- 打包时 MANIFEST.MF文件不记录的时间戳版本 -->
							//这里是因为maven每打一个版本会有一个时间戳,
							//一个奇怪的问题是,下载下来的版本不是一个带时间戳的版本,
							//就会出现有些程序和类包引用不到
							<useUniqueVersions>false</useUniqueVersions>
							//依赖的路径
							<addClasspath>true</addClasspath>
							//把项目需要的jar包方法lib目录中去
							//项目依赖的路径是lib
							<classpathPrefix>lib/</classpathPrefix>
						</manifest>
						<manifestEntries>
							<Class-Path>.</Class-Path>
						</manifestEntries>
					</archive>
				</configuration>
			</plugin>
			//把服务的jar包指向${project.build.directory}/lib
			<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>

dependence节点

<dependencies>
//注意这里面的四个是项目依赖的其他项目,虽然都在eclipse里面,但是
//maven找的时候是去repository中去拿
//所以应该先去构建,否则会报错
//对其他项目执行  mvn install 命令
	
		<dependency>
			<groupId>wusc.edu.common</groupId>
			<artifactId>edu-common</artifactId>
			<version>${edu-common.version}</version>
		</dependency>
		
		<dependency>
			<groupId>wusc.edu.common</groupId>
			<artifactId>edu-common-config</artifactId>
			<version>${edu-common-config.version}</version>
		</dependency>
		
		<dependency>
			<groupId>wusc.edu.common</groupId>
			<artifactId>edu-common-core</artifactId>
			<version>${edu-common-core.version}</version>
		</dependency>

		<dependency>
			<groupId>wusc.edu.facade</groupId>
			<artifactId>edu-facade-user</artifactId>
			<version>${edu-facade-user.version}</version>
		</dependency>
		
				<!-- Common Dependency Begin -->
		<dependency>
			<groupId>antlr</groupId>
			<artifactId>antlr</artifactId>
		</dependency>
		<dependency>
			<groupId>aopalliance</groupId>
			<artifactId>aopalliance</artifactId>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
		</dependency>
		<dependency>
			<groupId>cglib</groupId>
			<artifactId>cglib</artifactId>
		</dependency>
		<dependency>
			<groupId>net.sf.json-lib</groupId>
			<artifactId>json-lib</artifactId>
			<classifier>jdk15</classifier>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>ognl</groupId>
			<artifactId>ognl</artifactId>
		</dependency>
		<dependency>
			<groupId>oro</groupId>
			<artifactId>oro</artifactId>
		</dependency>
		<dependency>
			<groupId>commons-beanutils</groupId>
			<artifactId>commons-beanutils</artifactId>
		</dependency>
		<dependency>
			<groupId>commons-codec</groupId>
			<artifactId>commons-codec</artifactId>
		</dependency>
		<dependency>
			<groupId>commons-collections</groupId>
			<artifactId>commons-collections</artifactId>
		</dependency>
		<dependency>
			<groupId>commons-digester</groupId>
			<artifactId>commons-digester</artifactId>
		</dependency>
		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
		</dependency>
		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
		</dependency>
		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
		</dependency>
		<dependency>
			<groupId>commons-validator</groupId>
			<artifactId>commons-validator</artifactId>
		</dependency>
		<dependency>
			<groupId>dom4j</groupId>
			<artifactId>dom4j</artifactId>
		</dependency>
		<dependency>
			<groupId>net.sf.ezmorph</groupId>
			<artifactId>ezmorph</artifactId>
		</dependency>
		<dependency>
			<groupId>javassist</groupId>
			<artifactId>javassist</artifactId>
		</dependency>
		<dependency>
			<groupId>jstl</groupId>
			<artifactId>jstl</artifactId>
		</dependency>
		<dependency>
			<groupId>javax.transaction</groupId>
			<artifactId>jta</artifactId>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
		</dependency>

		<!-- Common Dependency End -->

		<!-- Spring Dependency Begin -->
		//此处省略spring的依赖
		<!-- Spring Dependency End -->

		<!-- MyBatis Dependency Begin -->
		//此处省略MyBaties的依赖
		<!-- MyBatis Dependency End -->

		<!-- Struts2 Dependency Begin -->
		//此处省略Struts2的依赖
		<!-- Struts2 Dependency End -->

		<!-- Others Begin -->
		<dependency>
			<groupId>org.apache.tomcat</groupId>
			<artifactId>servlet-api</artifactId>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.tomcat</groupId>
			<artifactId>jsp-api</artifactId>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
		</dependency>
		<dependency>
			<groupId>org.jsoup</groupId>
			<artifactId>jsoup</artifactId>
		</dependency>
		<!-- Others End -->
		
		<!-- Mysql Driver Begin -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
		<!-- Mysql Driver End -->
		
		<!-- dubbo 需要的jar start -->
		<dependency>
			<groupId>org.jboss.netty</groupId>
			<artifactId>netty</artifactId>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<exclusions>
				<exclusion>
					<groupId>org.springframework</groupId>
					<artifactId>spring</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
		</dependency>

		<dependency>
			<groupId>com.101tec</groupId>
			<artifactId>zkclient</artifactId>
		</dependency>
		<!-- dubbo 需要的jar end -->

	</dependencies>

打包

mvn  clean package

最后java -jar启动即可。

猜你喜欢

转载自blog.csdn.net/dataiyangu/article/details/88543539