提交 40276648 编写于 作者: R Renyuan Sun

Merge pull request #188 from ainilife/biz

Refactor the maven plugin and update README.md
CAT
CAT
===
<hr>
Central Application Tracking [![Build Status](https://travis-ci.org/dianping/cat.png?branch=biz)](https://travis-ci.org/dianping/cat)
#####1、安装Mysql服务器
#####2、配置Hadoop集群服务器【可选】
#####3、启动命令行,并将当前工作目录移动到cat目录
#####4、导入mysql的脚本文件,文件在'script/Cat.sql'
#####5、在本地磁盘中创建两个目录/data/appdatas/cat,以及/data/applogs/cat
mkdir -p /data/appdatas/cat
mkdir -p /data/applogs/cat
#####6、确保运行程序对刚刚创建的两个目录拥有读写权限,比如用cat用户启动程序
chown cat:cat /data/appdatas/cat -R
chown cat:cat /data/applogs/cat -R
#####7、确保系统的临时目录程序拥有读写权限,Linux为/tmp/目录
#####8、拷贝script文件夹下的'client.xml datasources.xml server.xml'到/data/appdatas/cat目录下
cp script/*.xml /data/appdatas/cat
#####9、修改/data/appdatas/cat/datasource.xml中cat数据源的基本信息,使用您自己的数据库链接信息
#####10、如果你安装了hadoop集群,请配置/data/appdatas/cat/server.xml中对应hadoop信息,如果没有hadoop集群,server.xml中localmode必须为true,CAT只能在开发环境工作。【可选】
#####11、用maven构建项目
Quick Started
---------------------
#####1、安装Java 6+,Maven以及MySQL
#####2、cd到CAT目录下,用maven构建项目
mvn eclipse:eclipse
#####12、导入项目到eclipse中,并运行testcase启动项目 ‘com.dianping.cat.TestServer’
\ No newline at end of file
#####3、安装CAT的maven plugin,使用它配置CAT的环境
cd cat-maven-plugin;mvn install;mvn cat:install
确保系统的临时目录程序拥有读写权限,Linux为/tmp/目录
#####4、运行CAT
cd cat-home;mvn jetty:run
然后浏览http://localhost:2281
#####5、如果你安装了hadoop集群,请配置/data/appdatas/cat/server.xml中对应hadoop信息,如果没有hadoop集群,server.xml中localmode必须为true,CAT只能在开发环境工作。【可选】
#####6、导入项目到eclipse中,在开发时,可以运行testcase启动项目 ‘com.dianping.cat.TestServer’来启动CAT
Copyright and license
---------------------
Copyright 2013 DianPing, Inc.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in compliance with the License. You may obtain a copy of the License in the LICENSE file, or at:
<http://www.apache.org/licenses/LICENSE-2.0>
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
......@@ -26,6 +26,11 @@
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.unidal.maven.plugins</groupId>
<artifactId>common</artifactId>
<version>2.0.3</version>
</dependency>
<dependency>
<groupId>org.unidal.framework</groupId>
<artifactId>foundation-service</artifactId>
......
package com.dianping.cat.plugin;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.regex.Pattern;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.unidal.helper.Files;
import org.unidal.helper.Files.AutoClose;
import org.unidal.maven.plugin.common.PropertyProviders;
import org.unidal.maven.plugin.common.PropertyProviders.IValidator;
/**
* @goal install
* @aggregator true
*/
public class CatInstallMojo extends AbstractMojo {
private final String m_datasourceUrl = "JDBC-URL";
private final String m_datasourceUser = "JDBC-USER";
private final String m_datasourcePassword = "JDBC-PASSWORD";
public class InstallMojo extends AbstractMojo {
private String m_path = "/data/appdatas/cat";
......@@ -51,6 +46,11 @@ public class CatInstallMojo extends AbstractMojo {
*/
private String m_password;
/**
* @parameter expression="${verbose}"
*/
private boolean m_verbose = false;
private void createDatabase(Statement stmt) throws SQLException {
try {
stmt.executeUpdate("create database cat");
......@@ -60,7 +60,7 @@ public class CatInstallMojo extends AbstractMojo {
getLog().info("Database 'cat' already exists, drop it first...");
stmt.executeUpdate("drop database cat");
getLog().info("Database 'cat' has dropped.");
getLog().info("Database 'cat' has dropped");
stmt.executeUpdate("create database cat");
} else {
throw e;
......@@ -83,13 +83,15 @@ public class CatInstallMojo extends AbstractMojo {
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
getLog().info("Delopying the Cat environment...");
getLog().info("Preparing Cat environment...");
validate();
setupDatabase();
setupConfigurationFiles();
if (setupDatabase() && setupConfigurationFiles()) {
getLog().info("Preparing Cat environment ... DONE");
getLog().info("Use following command line to start local Cat server:");
getLog().info(" cd cat-home; mvn jetty:run");
}
}
private Connection getConnection(String jdbcUrl) throws ClassNotFoundException, SQLException {
......@@ -99,7 +101,7 @@ public class CatInstallMojo extends AbstractMojo {
return conn;
}
private void setupConfigurationFiles() {
private boolean setupConfigurationFiles() {
File path = new File(m_path);
if (!path.exists()) {
......@@ -107,58 +109,77 @@ public class CatInstallMojo extends AbstractMojo {
}
if (!path.canRead() || !path.canWrite()) {
getLog()
.warn(m_path
+ " doesn't have enough privilege to read or write this pathname, please add read and write privileges to the current user.");
getLog().error("Don't have privilege to read/write " + m_path);
return false;
}
getLog().info("Generating the configuration files to " + m_path + "...");
getLog().info("Generating the configuration files to " + m_path + " ...");
boolean isSuccess = false;
try {
debug("Generating client.xml ...");
Files.forIO().copy(getClass().getResourceAsStream("client.xml"), new FileOutputStream(m_clientPath),
AutoClose.INPUT_OUTPUT);
getLog().info("generate client.xml .");
debug("Generating server.xml ...");
Files.forIO().copy(getClass().getResourceAsStream("server.xml"), new FileOutputStream(m_serverPath),
AutoClose.INPUT_OUTPUT);
getLog().info("generate server.xml .");
debug("Generating datasources.xml .");
String datasources = Files.forIO().readFrom(getClass().getResourceAsStream("datasources.xml"), "utf-8");
datasources = datasources.replaceAll(m_datasourceUrl, m_jdbcUrl + "/cat");
datasources = datasources.replaceAll(m_datasourceUser, m_user);
datasources = datasources.replaceAll(m_datasourcePassword, m_password);
datasources = datasources.replaceAll(Pattern.quote("${jdbc.url}"), m_jdbcUrl + "/cat");
datasources = datasources.replaceAll(Pattern.quote("${jdbc.user}"), m_user);
datasources = datasources.replaceAll(Pattern.quote("${jdbc.password}"), m_password);
Files.forIO().writeTo(new File(m_datasourcePath), datasources);
getLog().info("generate datasources.xml .");
getLog().info("Configuration files are generated successfully");
isSuccess = true;
} catch (Exception e) {
getLog().error(e);
}
return isSuccess;
}
private void debug(String info) {
if (m_verbose) {
getLog().debug(info);
}
}
private void setupDatabase() {
private boolean setupDatabase() {
Connection conn = null;
Statement stmt = null;
boolean isSuccess = false;
try {
getLog().info("Connect the mysql database : " + m_jdbcUrl);
getLog().info("Connecting to database(" + m_jdbcUrl + ") ...");
conn = getConnection(m_jdbcUrl);
stmt = conn.createStatement();
getLog().info("Connected to database(" + m_jdbcUrl + ")");
getLog().info("Creating database...");
getLog().info("Creating database(cat) ...");
stmt = conn.createStatement();
createDatabase(stmt);
getLog().info("Database 'cat' created successfully.");
getLog().info("Database(cat) is created successfully");
getLog().info("Create tables...");
getLog().info("Creating tables ...");
createTables(stmt);
getLog().info("Create tables successfully.");
} catch (Exception e) {
e.printStackTrace();
getLog().info("Tables are created successfully");
isSuccess = true;
} catch (Throwable e) {
getLog().error(e);
} finally {
try {
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
......@@ -166,43 +187,24 @@ public class CatInstallMojo extends AbstractMojo {
// ignore it
}
}
return isSuccess;
}
private void validate() {
Reader inputStream = null;
BufferedReader reader = null;
try {
inputStream = new InputStreamReader(System.in);
reader = new BufferedReader(inputStream);
if (m_jdbcUrl == null || m_jdbcUrl.length() == 0) {
System.out.print("Please input the mysql jdbc url(jdbc:mysql://192.168.1.1:3306):");
m_jdbcUrl = reader.readLine();
}
if (m_user == null || m_user.length() == 0) {
System.out.print("Please input the mysql user:");
m_user = reader.readLine();
}
if (m_password == null || m_password.length() == 0) {
System.out.print("Please input the mysql password:");
m_password = reader.readLine();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
if (reader != null) {
reader.close();
}
} catch (IOException e) {
// ignore it
}
}
m_jdbcUrl = PropertyProviders.fromConsole().forString("jdbc.url", "Please input jdbc url:", null,
"jdbc:mysql://127.0.0.1:3306", new IValidator<String>() {
@Override
public boolean validate(String url) {
if (url.startsWith("jdbc:mysql://")) {
return true;
} else {
return false;
}
}
});
m_user = PropertyProviders.fromConsole().forString("jdbc.user", "Please input username:", null, null, null);
m_password = PropertyProviders.fromConsole().forString("jdbc.password", "Please input password:", null, null,
null);
}
}
\ No newline at end of file
<!--客户端需要配置Cat服务器的地址。客户端使用多个地址进行热备,从第一个地址开发发送消息,如果成功发送,则停止;如果发送失败,则从第二个地址开发重新发送。 -->
<config mode="client" xmlns:xsi="http://www.w3.org/2001/XMLSchema"
xsi:noNamespaceSchemaLocation="config.xsd">
<?xml version="1.0" encoding="utf-8"?>
<config mode="client" xmlns:xsi="http://www.w3.org/2001/XMLSchema" xsi:noNamespaceSchemaLocation="config.xsd">
<servers>
<!-- 开发模式的地址。真实环境下删除这行。 -->
<!-- Local mode for development -->
<server ip="127.0.0.1" port="2280" http-port="2281" />
<!-- <server ip="192.168.7.70" port="2280" /> -->
<!-- If under production environment, put actual server address as list. -->
<!--
<server ip="192.168.7.71" port="2280" />
<server ip="192.168.7.72" port="2280" />
-->
</servers>
</config>
<?xml version="1.0" encoding="utf-8"?>
<!-- 配置Cat服务器数据源。id为cat的是生产环境数据源,cat.qa是测试环境数据源,user是用户信息表。默认情况下使用cat库,也可以自行修改指向相应的表。 -->
<data-sources>
<data-source id="cat">
<maximum-pool-size>3</maximum-pool-size>
......@@ -8,22 +8,9 @@
<statement-cache-size>1000</statement-cache-size>
<properties>
<driver>com.mysql.jdbc.Driver</driver>
<url><![CDATA[JDBC-URL]]></url>
<user>JDBC-USER</user>
<password>JDBC-PASSWORD</password>
<connectionProperties><![CDATA[useUnicode=true&autoReconnect=true]]></connectionProperties>
</properties>
</data-source>
<data-source id="cat.qa">
<maximum-pool-size>3</maximum-pool-size>
<connection-timeout>1s</connection-timeout>
<idle-timeout>10m</idle-timeout>
<statement-cache-size>1000</statement-cache-size>
<properties>
<driver>com.mysql.jdbc.Driver</driver>
<url><![CDATA[JDBC-URL]]></url>
<user>JDBC-USER</user>
<password>JDBC-PASSWORD</password>
<url><![CDATA[${jdbc.url}]]></url>
<user>${jdbc.user}</user>
<password>${jdbc.password}</password>
<connectionProperties><![CDATA[useUnicode=true&autoReconnect=true]]></connectionProperties>
</properties>
</data-source>
......@@ -34,9 +21,9 @@
<statement-cache-size>1000</statement-cache-size>
<properties>
<driver>com.mysql.jdbc.Driver</driver>
<url><![CDATA[JDBC-URL]]></url>
<user>JDBC-USER</user>
<password>JDBC-PASSWORD</password>
<url><![CDATA[${jdbc.url}]]></url>
<user>${jdbc.user}</user>
<password>${jdbc.password}</password>
<connectionProperties><![CDATA[useUnicode=true&autoReconnect=true]]></connectionProperties>
</properties>
</data-source>
......
<!-- Cat服务器的配置文件。主要配置hadoop存储地址、消费机和控制台 -->
<?xml version="1.0" encoding="utf-8"?>
<!-- Configuration for development environment-->
<config local-mode="true" job-machine="false">
<!-- 以下配置是Hadoop集群配置,请根据自身情况修改之 -->
<consumer>
<long-config default-url-threshold="1000" default-sql-threshold="100">
<domain name="Cat" url-threshold="500" sql-threshold="40"/>
</long-config>
</consumer>
<console default-domain="Cat" show-cat-domain="true">
<remote-servers>127.0.0.1:2281</remote-servers>
</console>
</config>
<!-- Configuration for production environment -->
<!-- Note: -->
<!-- 1. Set local-mode false to activate remote mode. -->
<!-- 2. If machine is job-machine, set job-machine true. -->
<!-- 3. Please set hadoop environment accordingly. -->
<!--
<config local-mode="false" job-machine="false">
<storage local-base-dir="/data/appdatas/cat/bucket/">
<hdfs id="logview" max-size="128M" server-uri="hdfs://10.1.77.86/user/cat" base-dir="logview"/>
<hdfs id="dump" max-size="128M" server-uri="hdfs://10.1.77.86/user/cat" base-dir="dump"/>
<hdfs id="remote" max-size="128M" server-uri="hdfs://10.1.77.86/user/cat" base-dir="remote"/>
<properties>
<property name="hadoop.security.authentication" value="kerberos"/>
<property name="dfs.namenode.kerberos.principal" value="hadoop/test86.hadoop@DIANPING.COM"/>
<property name="dfs.cat.kerberos.principal" value="cat@DIANPING.COM"/>
<property name="dfs.cat.keytab.file" value="/data/appdatas/cat/cat.keytab"/>
<property name="java.security.krb5.realm" value="DIANPING.COM"/>
<property name="java.security.krb5.kdc" value="192.168.7.80"/>
</properties>
</storage>
<!-- 以下配置是消费机集群配置,请根据自身情况修改之 -->
<consumer>
<long-config default-url-threshold="1000" default-sql-threshold="100">
<domain name="Cat" url-threshold="500" sql-threshold="40"/>
</long-config>
</consumer>
<!-- 以下是配置控制台配置,请根据自身情况修改之 -->
<console default-domain="Cat" show-cat-domain="true">
<!-- 添加多台consumer的地址,用逗号分隔 -->
<remote-servers>127.0.0.1:2281</remote-servers>
<remote-servers>192.168.0.1:2281,192.168.0.2:2281</remote-servers>
</console>
</config>
-->
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册