# :-: cat-client配置
**1 被监控的三方应用所在的服务器上配置如下内容:**
~~~
mkdir /data/applogs/cat
mkdir /data/appdatas/cat
~~~
**2在此目录创建 client.xml**
~~~
// /data/appdatas/cat/client.xml 配置内容
<?xml version="1.0" encoding="utf-8"?>
<config mode="client">
<servers>
<server ip="127.0.0.1" port="2280" http-port="8080" />
</servers>
</config>
~~~
// ip 为监控服务端cat-home 地址
// http-port 为监控服务端cat-home 端口
// port2280 为固定值 ,jetty端口
**3 引入 `cat-client.xxx.jar` 包 【坑!!】**
若从maven repository上下载的 `cat-client.jar ` 包,
很有可能会报` 未找到 ModeContext异常 `,
所以一定要注意使用github下载的源码编译cat工程:
```
git clone https://github.com/dianping/cat.git
mvn clean install -DskipTests
```
到 ` \cat\cat-client\target
`目录 或者 `D:\workspace\java\cat\lib\java\jar`
把 `cat-client-3.0.0.jar` 安装到本地maven仓库或工程
```
mvn install:install-file -Dfile=cat-client-3.0.0.jar -DgroupId=com.dianping.cat -DartifactId=cat-
client -Dversion=3.0.0 -Dpackaging=jar
```
**官方文档通知 :**
1. 根目录下 cat-client 模块以后不再维护,下个大版本更新计划移除。新版Java客户端参考:lib/java
2. 管理端、服务端、告警服务均使用 cat-home 模块部署即可
### 4cat-client部署步骤
假设工程启动时模块名application-context设置为:nic,即/nic/***
在工程的`resources`下创建`META-INF
`
~~~
|--resources
--META-INF
--cat
--client.xml
--app.properties
~~~
其中,`client.xml` 内容如下:
~~~
<?xml version="1.0" encoding="UTF-8"?>
<config mode="client">
<domain id="nic"/>
</config>
// mode=client 客户端
// <domain id="nic"/> 要监控的应用的domain 为 nic,CAT后台 会自动添加一个名为
// nic的 transaction
~~~
`app.properties` 配置如下:
~~~
app.name=nic
// 此需要监控的应用的名字为nic,需要与上文 <domain id="nic"/> 中的id一致
~~~
### 5配置埋点
拦截器模式配置:
~~~
public class CatInterceptor implements HandlerInterceptor {
Logger logger = LoggerFactory.getLogger(CatInterceptor.class);
private ThreadLocal<Transaction> tranLocal = new ThreadLocal<Transaction>();
private ThreadLocal<Transaction> pageLocal = new ThreadLocal<Transaction>();
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
String uri = httpServletRequest.getRequestURI();
Transaction transaction = Cat.newTransaction("URL", uri);
Cat.logEvent("URL.Method", httpServletRequest.getMethod(), Message.SUCCESS, httpServletRequest.getRequestURL().toString());
Cat.logEvent("URL.Host", httpServletRequest.getRemoteHost(), Message.SUCCESS, httpServletRequest.getRemoteHost());
tranLocal.set(transaction);
return true;
}
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
String viewName = modelAndView != null ? modelAndView.getViewName() : "NONE";
Transaction transaction = Cat.newTransaction("view", viewName);
pageLocal.set(transaction);
}
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
Transaction pt = pageLocal.get();
pt.setStatus(Transaction.SUCCESS);
pt.complete();
Transaction t = tranLocal.get();
t.setStatus(Transaction.SUCCESS);
t.complete();
}
}
~~~
### 6 spring-web.xml中添加拦截器
~~~
<!--添加拦截器-->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="org.seckill.interceptor.CatInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
~~~
## 7 cat-home 与 cat-client配置在同一服务器上的步骤
cat-home 与 cat-client可以共用 /data/appdatas/cat
cat-client只需要在 /data/appdatas/cat下配置client.xml即可,如果已配置不需要再重复配置