Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
q549130180 committed Jul 8, 2016
1 parent b60a5e9 commit 74ca4ef
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 46 deletions.
1 change: 1 addition & 0 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ pages_list:
Blog: ''
Categories: 'categories'
Archives: 'posts'
# SiteMap: 'sitemap'
# Contact: 'contact'
About: 'about'

Expand Down
9 changes: 9 additions & 0 deletions _data/sitemap.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# sitemap navigation links

- title: 百度
icon: ddd.jpg
url: www.baidu.com
url2: https://www.baidu.com/s?word=%E7%99%BE%E5%BA%A6%E8%B4%B4%E5%90%A7&

- title: External Link
url: http://mademistakes.com
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
layout: post
title: Apache 与 Tomcat 服务器集成和集群配置
description: ""
modified: 2016-07-06 15:20:20
title: Apache与Tomcat服务器集成和集群配置
description: "Apache与Tomcat服务器集成和集群配置,通过mod_jk的方式进行Tomcat和Apache的集成"
modified: 2016-07-08 15:20:20
tags: [Apache,Apache Server,Tomcat]
post_type: developer
series:
Expand All @@ -14,9 +14,6 @@ image:
---


## Apache 与 Tomcat 服务器集成和集群配置


## 一、集成原因

Tomcat的功能分为以下两个主要部分:
Expand All @@ -29,34 +26,55 @@ Tomcat的功能分为以下两个主要部分:
还有在高并发的情况下,单个的Tomcat无法提供大量的http请求,需要使用Apache分发到多个Tomcat组成的集群,这样达到更大并发性的效果。


## 一、环境
## 二、集成原理

1、Tomcat服务器是通过Connector连接器组件与客户端程序建立连接,它负责接收客户端的请求,并把Tomcat服务器的响应结果发送给客户端。在server.xml中默认存在以下2个连接器组件:

{% highlight xml %}
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />

<!-- Define an AJP 1.3 Connector on port 8009 -->
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
{% endhighlight %}

第一个连接器是HTTP连接器,监听8080端口,负责建立HTTP链接。在使用Tomcat作为HTTP服务器时就是用的该连接器。

OS: Cent OS 7
JDK: 1.7
Tomcat: apache-tomcat-7.0.65
Apache Server: httpd-2.4.20
第二个连接器是AJP连接器,监听8009端口,负责和其它的HTTP服务器建立连接。与其它服务器集成时就是用到这个连接器。


## 二、安装
2、JK插件

Tomcat提供了专门的JK插件来负责与其它HTTP服务器的通信,该插件需要安置在对应的HTTP服务器上,它根据预先配置好的URL映射信息,决定是否把客户请求转发给Tomcat服务器。


## 三、环境

- OS: Cent OS 7
- JDK: 1.7
- Tomcat: apache-tomcat-7.0.65
- Apache Server: httpd-2.4.20


## 四、安装

### 1.安装依赖

```
{% highlight bash %}
yum install apr apr-util
yum install pcre-devel openssl-devel
```
{% endhighlight %}


**apr安装**

官网:http://apr.apache.org/

```
{% highlight bash %}
tar -zxvf apr-1.5.2.tar.gz
cd apr-1.5.2
./configure --prefix=/staples/apr
make && make install
```
{% endhighlight %}



Expand All @@ -65,21 +83,21 @@ make && make install

官网:http://apr.apache.org/

```
{% highlight bash %}
tar -zxvf apr-util-1.5.4.tar.gz
cd apr-util-1.5.4
./configure --prefix=/staples/apr-util --with-apr=/staples/apr/
make && make install
```
### 2.httpd安装
{% endhighlight %}

[Apache Server官网](https://httpd.apache.org/),下载源码包。
### 2.httpd安装


mod_jk插件下载[地址](http://tomcat.apache.org/connectors-doc/),下载:tomcat-connectors-1.2.41-src.tar.gz
[Apache Server官网](https://httpd.apache.org/),下载源码包。

编译安装:

```
{% highlight bash %}
./configure \
--with-apr=/staples/apr \
--with-apr-util=/staples/apr-util \
Expand All @@ -95,15 +113,20 @@ mod_jk插件下载[地址](http://tomcat.apache.org/connectors-doc/),下载:to
--enable-mpms-shared=all


```
{% endhighlight %}

编译安装mod_jk模块
### 3.mod_jk 插件安装

```
tar -zxvf tomcat-connectors-1.2.41-src.tar.gz
mod_jk插件下载[地址](http://tomcat.apache.org/connectors-doc/),下载:tomcat-connectors-1.2.41-src.tar.gz

cd tomcat-connectors-1.2.41-src/
cd native/

编译安装mod_jk模块:

{% highlight bash %}
tar -zxvf tomcat-connectors-1.2.41-src.tar.gz

cd tomcat-connectors-1.2.41-src/
cd native/

./configure --with-apxs=/staples/apachehttpd/bin/apxs
#这里指定的是apache安装目录中apxs的位置,这个apxs方便我们动态加载模块
Expand All @@ -113,13 +136,13 @@ make
# 将mod_jk.so文件复制到apache的modules文件夹下
cp apache-2.0/mod_jk.so /staples/apachehttpd/modules/

```
{% endhighlight %}

### httpd配置
### 4.Apache Server配置文件

在/staples/apachehttpd/conf下面建立两个配置文件mod_jk.conf和workers.properties。

```
{% highlight bash %}
# vi mod_jk.conf

# 添加以下内容:
Expand Down Expand Up @@ -148,9 +171,10 @@ JkRequestLogFormat "%w%V %T"

# 集群模式下,将所有请求发送给负载平衡器
JkMount /* loadbalancer
```
{% endhighlight %}

```

{% highlight bash %}
# vi workers.properties
# 添加以下内容:

Expand All @@ -173,54 +197,72 @@ worker.worker2.lbfactor=100 # 负载平衡因数,指工作负荷

worker.loadbalancer.type=lb # 负载平衡器
worker.loadbalancer.balanced_workers=worker1,worker2
worker.loadbalancer.sticky_session=false # 粘性复制为false,即不会进行会话复制。即一个客户端访问了tomcat1,则始终由它来服务
worker.loadbalancer.sticky_session_force=false # 粘性复制为false,该参数影响不大。如果粘性复制为true,sticky_session_force为true,客户会收到500的错误。如果为false,负载平衡器会重新请求正常的服务器
worker.loadbalancer.sticky_session=true
# 此处指定集群是否需要会话复制,如果设为true,则表明为会话粘性,不进行会话复制,当某用户的请求第一次分发到哪台Tomcat后,后继的请求会一直分发到此Tomcat服务器上处理;如果设为false,则表明需求会话复制

worker.loadbalancer.sticky_session_force=true
# 如果上面的sticky_session设为true时,建议此处也设为true,此参数表明如果集群中某台Tomcat服务器在多次请求没有响应后,是否将当前的请求,转发到其它Tomcat服务器上处理;此参数在sticky_session=true时,影响比较大,会导致转发到其它Tomcat服务器上的请求,找不到原来的session,所以如果此时请求中有读取session中某些信息的话,就会导致应用的null异常

```
workers.properties配置文件tomcat官方文档http://tomcat.apache.org/connectors-doc/reference/workers.html

{% endhighlight %}
workers.properties配置文件[tomcat官方文档](http://tomcat.apache.org/connectors-doc/reference/workers.html)




修改http.conf,在文件末尾加入以下内容

```
{% highlight apache %}
LoadModule jk_module modules/mod_jk.so
Include /staples/apachehttpd/conf/mod_jk.conf
```

## 三、与tomcat集成
{% endhighlight %}

### 5.Tomcat配置

jvmRoute="worker1"

修改Tomcat的conf/server.xml文件中的AJP连接器的端口,确保它们和workers.properties文件中的配置对应。此外,在使用了loadbalancer后,要求worker的名字和Tomcat的server.xml文件中的<Engine>元素的jvmRoute属性一致

Tomcat修改如下:

```
{% highlight xml %}
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />

<!-- Define an AJP 1.3 Connector on port 8009 -->
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

<Engine name="Catalina" defaultHost="localhost" jvmRoute="worker1" >

```
{% endhighlight %}

```
{% highlight xml %}
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />

<!-- Define an AJP 1.3 Connector on port 8009 -->
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

<Engine name="Catalina" defaultHost="localhost" jvmRoute="worker2" >

```
{% endhighlight %}


测试页面

最后编辑Tomcat的配置文件server.xml,在HOST段中加入:

{% highlight xml %}

<Context path="" docBase="/var/wwwroot" debug="0" reloadable="true" crossContext="true"/>

{% endhighlight %}

在/var/wwwroot下建立一个index.jsp,启动Apache和Tomcat,用浏览器访问http://localhost/,应该可以看到正确的页面了。






<div class="elementHide" >
tomcat集群管理器

```
Expand Down Expand Up @@ -264,3 +306,4 @@ tomcat集群管理器
```
</div>
Binary file added images/icon/baiduzhongban-4.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/icon/defaultIcon1229.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
61 changes: 61 additions & 0 deletions note/JavaWeb/JavaWeb/log4j.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@

## log4j与commons-logging,slf4j的关系

### commons-logging

common-logging是apache提供的一个通用的日志接口。用户可以自由选择第三方的日志组件作为具体实现,像log4j,或者jdk自带的logging, common-logging会通过动态查找的机制,在程序运行时自动找出真正使用的日志库。当然,common-logging内部有一个Simple logger的简单实现,但是功能很弱。所以使用common-logging,通常都是配合着log4j来使用。使用它的好处就是,代码依赖是common-logging而非log4j, 避免了和具体的日志方案直接耦合,在有必要时,可以更改日志实现的第三方库。

JCL+Log4J组合使用模式(即commons-logging+log4j):

1. commons-logging-1.1.jar
2. log4j-1.2.15.jar
3. log4j.properties

JCL+log4j:

```java
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
private static Log log = LogFactory.getLog(xx.class);
```

### log4j

slf4j全称为Simple Logging Facade for JAVA,java简单日志门面。类似于Apache Common-Logging,是对不同日志框架提供的一个门面封装,可以在部署的时候不修改任何配置即可接入一种日志实现方案。但是,他在编译时静态绑定真正的Log库。使用SLF4J时,如果你需要使用某一种日志实现,那么你必须选择正确的SLF4J的jar包的集合(各种桥接包)。

slf4j+log4j组合使用模式:

1. slf4j-api-1.5.11.jar
2. slf4j-log4j12-1.5.11.jar
3. log4j-1.2.15.jar
4. log4j.properties(也可以是 log4j.xml)

slf4j+log4j:

```java
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Logger logger = LoggerFactory.getLogger(xx.class);
```



log4j:

```java
import org.apache.log4j.Logger;
Logger logger= Logger.getLogger(xx.class);
```

总结

总的来说,slf4j与commons-logging只是一个日志门面,实际还是要依赖真正的日志库log4j,虽然slf4j和commons-loggins自带了日志库,但是毕竟log4j才是最强大的。



## log4j.properties配置详解与实例

```
```
28 changes: 28 additions & 0 deletions sitemap/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
layout: post-index
title: site Map
description: ""
comments: true
modified: 2016-07-08
section-type: page
image:
feature: posts_header/abstract-1.jpg
---



<div>
<ul style="width: 868px;padding-left: 23px;padding-top: 3px;padding-bottom: 3px;position: relative;">
<li style="position: relative;width: 130px;
padding-top: 6px;padding-bottom: 4px;margin-right: 4px;
padding-left: 9px;color: #000;font-size: 14px;height: 20px;line-height: 20px;
vertical-align: top;display: inline-block;">
<a hidefocus="true" cls="qt,n" target="_blank" href="http://www.baidu.com/?tn=sitehao123_15" data-title="百度" style="background:url(https://gss0.bdstatic.com/5eR1dDebRNRTm2_p8IuM_a/res/img/defaultIcon1229.png) no-repeat 2px -198px;" class="js_text sitemainlink " alog-custom="ind:qt,sal:0">百度</a>
&nbsp; • &nbsp;
<a href="https://www.baidu.com/s?word=%E7%99%BE%E5%BA%A6%E8%B4%B4%E5%90%A7&amp;tn=sitehao123_16" target="_blank" cls="qt,n" class="sitesublink " style="" alog-custom="ind:qt,sal:0">贴吧</a>
</li>

</ul>
</div>

# 开发中。。。。

0 comments on commit 74ca4ef

Please sign in to comment.