motan 搭建

安装java

方法一:手动解压JDK的压缩包,然后设置环境变量

1.在/usr/目录下创建java目录

[root@localhost ~]# mkdir/usr/java

[root@localhost ~]# cd /usr/java

2.下载jdk,然后解压

[root@localhost java]# curl -O http://download.Oracle.com/otn-pub/java/jdk/7u79-b15/jdk-7u79-linux-x64.tar.gz 

[root@localhost java]# tar -zxvf jdk-7u79-linux-x64.tar.gz

3.设置环境变量

[root@localhost java]# vi /etc/profile

在profile中添加如下内容:

#set java environment

JAVA_HOME=/usr/java/jdk1.7.0_79

JRE_HOME=/usr/java/jdk1.7.0_79/jre

CLASS_PATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib

PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin

export JAVA_HOME JRE_HOME CLASS_PATH PATH

让修改生效:

[root@localhost java]# source /etc/profile

4.验证JDK有效性

[root@localhost java]# java -version

java version "1.7.0_79"

Java(TM) SE Runtime Environment (build 1.7.0_79-b15)

Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)

 

方法二:用yum安装JDK

1.查看yum库中都有哪些jdk版本(暂时只发现了openjdk)

[root@localhost ~]# yum search java|grep jdk

ldapjdk-javadoc.x86_64 : Javadoc for ldapjdk

java-1.6.0-openjdk.x86_64 : OpenJDK Runtime Environment

java-1.6.0-openjdk-demo.x86_64 : OpenJDK Demos

java-1.6.0-openjdk-devel.x86_64 : OpenJDK Development Environment

java-1.6.0-openjdk-javadoc.x86_64 : OpenJDK API Documentation

java-1.6.0-openjdk-src.x86_64 : OpenJDK Source Bundle

java-1.7.0-openjdk.x86_64 : OpenJDK Runtime Environment

java-1.7.0-openjdk-demo.x86_64 : OpenJDK Demos

java-1.7.0-openjdk-devel.x86_64 : OpenJDK Development Environment

java-1.7.0-openjdk-javadoc.noarch : OpenJDK API Documentation

java-1.7.0-openjdk-src.x86_64 : OpenJDK Source Bundle

java-1.8.0-openjdk.x86_64 : OpenJDK Runtime Environment

java-1.8.0-openjdk-demo.x86_64 : OpenJDK Demos

java-1.8.0-openjdk-devel.x86_64 : OpenJDK Development Environment

java-1.8.0-openjdk-headless.x86_64 : OpenJDK Runtime Environment

java-1.8.0-openjdk-javadoc.noarch : OpenJDK API Documentation

java-1.8.0-openjdk-src.x86_64 : OpenJDK Source Bundle

ldapjdk.x86_64 : The Mozilla LDAP Java SDK

2.选择版本,进行安装

//选择1.7版本进行安装

[root@localhost ~]# yum install java-1.7.0-openjdk

//安装完之后,默认的安装目录是在: /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.75.x86_64

3.设置环境变量

[root@localhost ~]# vi /etc/profile

在profile文件中添加如下内容

#set java environment

JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.75.x86_64

JRE_HOME=$JAVA_HOME/jre

CLASS_PATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib

PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin

export JAVA_HOME JRE_HOME CLASS_PATH PATH

让修改生效

[root@localhost java]# source /etc/profile

4.验证(同上一方法)

方法三:用rpm安装JDK

1.下载rpm安装文件

[root@localhost ~]$ curl -O http://download.oracle.com/otn-pub/java/jdk/7u79-b15/jdk-7u79-linux-x64.rpm

2.使用rpm命令安装

[root@localhost  ~]# rpm -ivh jdk-7u79-linux-x64.rpm

3.设置环境变量

[root@localhost java]# vi /etc/profile

在打开的profile文件中添加如下内容

#set java environment

JAVA_HOME=/usr/java/jdk1.7.0_79

JRE_HOME=/usr/java/jdk1.7.0_79/jre

CLASS_PATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib

PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin

export JAVA_HOME JRE_HOME CLASS_PATH PATH

让修改生效

[root@localhost java]# source /etc/profile

4.验证(同上一方法)

注:和yum安装类似,不用设置环境变量就可以运行java命令。rpm安装方式默认会把jdk安装到/usr/java/jdk1.7.0_79,然后通过三层链接,链接到/usr/bin,具体链接如下:

[root@localhost ~]# cd /bin

[root@localhost bin]# ll|grep java

lrwxrwxrwx. 1 root root    25 Mar 28 11:24 jar ->/usr/java/default/bin/jar

lrwxrwxrwx. 1 root root    26 Mar 28 11:24 java -> /usr/java/default/bin/java

lrwxrwxrwx. 1 root root    27 Mar 28 11:24 javac ->/usr/java/default/bin/javac

lrwxrwxrwx. 1 root root    29 Mar 28 11:24 javadoc ->/usr/java/default/bin/javadoc

lrwxrwxrwx. 1 root root    28 Mar 28 11:24 javaws ->/usr/java/default/bin/javaws

lrwxrwxrwx. 1 root root    30 Mar 28 11:24 jcontrol ->/usr/java/default/bin/jcontrol

[root@localhost bin]# cd /usr/java/

[root@localhost java]# ll

total 4

lrwxrwxrwx. 1 root root  16 Mar 28 11:24 default-> /usr/java/latest

drwxr-xr-x. 8 root root 4096 Mar 28 11:24 jdk1.7.0_79

lrwxrwxrwx. 1 root root  21 Mar 28 11:24 latest -> /usr/java/jdk1.7.0_79

方法四:Ubuntu 上使用apt-get安装JDK

1.查看apt库都有哪些jdk版本

root@linuxidc:~# apt-cache search java|grep jdk

default-jdk - Standard Java or Java compatible Development Kit

default-jdk-doc - Standard Java or Java compatible Development Kit (documentation)

gcj-4.6-jdk - gcj and classpath development tools for Java(TM)

gcj-jdk - gcj and classpath development tools for Java(TM)

openjdk-6-dbg - Java runtime based on OpenJDK (debugging symbols)

openjdk-6-demo - Java runtime based on OpenJDK (demos and examples)

openjdk-6-doc - OpenJDK Development Kit (JDK) documentation

openjdk-6-jdk - OpenJDK Development Kit (JDK)

openjdk-6-jre-lib - OpenJDK Java runtime (architecture independent libraries)

openjdk-6-source - OpenJDK Development Kit (JDK) source files

openjdk-7-dbg - Java runtime based on OpenJDK (debugging symbols)

openjdk-7-demo - Java runtime based on OpenJDK (demos and examples)

openjdk-7-doc - OpenJDK Development Kit (JDK) documentation

openjdk-7-jdk - OpenJDK Development Kit (JDK)

openjdk-7-source - OpenJDK Development Kit (JDK) source files

uwsgi-plugin-jvm-openjdk-6 - Java plugin for uWSGI (OpenJDK 6)

uwsgi-plugin-jwsgi-openjdk-6 - JWSGI plugin for uWSGI (OpenJDK 6)

openjdk-6-jre - OpenJDK Java runtime, using Hotspot JIT

openjdk-6-jre-headless - OpenJDK Java runtime, using Hotspot JIT (headless)

openjdk-7-jre - OpenJDK Java runtime, using Hotspot JIT

openjdk-7-jre-headless - OpenJDK Java runtime, using Hotspot JIT (headless)

openjdk-7-jre-lib - OpenJDK Java runtime (architecture independent libraries)

2.选择版本进行安装

root@linuxidc:~# apt-get install openjdk-7-jdk

3.设置环境变量

root@linuxidc:~# vi /etc/profile

在打开的profile文件中添加如下内容

#set java environment

JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-amd64

JRE_HOME=$JAVA_HOME/jre

CLASS_PATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib

PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin

export JAVA_HOME JRE_HOME CLASS_PATH PATH

让修改生效

root@linuxidc:~# source /etc/profile

 

安装zookeeper

 

 1、创建 /usr/local/services/zookeeper 文件夹:

    mkdir -p /usr/local/services/zookeeper

 

2、进入到 /usr/local/services/zookeeper 目录中:

    cd /usr/local/services/zookeeper

 

3、下载 zookeeper-3.4.9.tar.gz:

    wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.4.9/zookeeper-3.4.9.tar.gz

 

4、解压缩 zookeeper-3.4.9.tar.gz:

    tar -zxvf zookeeper-3.4.9.tar.gz

 

5、进入到 /usr/local/services/zookeeper/zookeeper-3.4.9/conf 目录中:

    cd zookeeper-3.4.9/conf/

 

6、复制 zoo_sample.cfg 文件的并命名为为 zoo.cfg:

    cp zoo_sample.cfg zoo.cfg

 

7、用 vim 打开 zoo.cfg 文件并修改其内容为如下:

    # The number of milliseconds of each tick

 

    # zookeeper 定义的基准时间间隔,单位:毫秒

    tickTime=2000

 

    # The number of ticks that the initial 

    # synchronization phase can take

    initLimit=10

    # The number of ticks that can pass between 

    # sending a request and getting an acknowledgement

    syncLimit=5

    # the directory where the snapshot is stored.

    # do not use /tmp for storage, /tmp here is just 

    # example sakes.

    # dataDir=/tmp/zookeeper

 

    # 数据文件夹

    dataDir=/usr/local/services/zookeeper/zookeeper-3.4.9/data

 

    # 日志文件夹

    dataLogDir=/usr/local/services/zookeeper/zookeeper-3.4.9/logs

 

    # the port at which the clients will connect

    # 客户端访问 zookeeper 的端口号

    clientPort=2181

 

    # the maximum number of client connections.

    # increase this if you need to handle more clients

    #maxClientCnxns=60

    #

    # Be sure to read the maintenance section of the 

    # administrator guide before turning on autopurge.

    #

    # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance

    #

    # The number of snapshots to retain in dataDir

    #autopurge.snapRetainCount=3

    # Purge task interval in hours

    # Set to "0" to disable auto purge feature

    #autopurge.purgeInterval=1

 

8、保存并关闭 zoo.cfg 文件:

    

9、进入到 /usr/local/services/zookeeper/zookeeper-3.4.9/bin 目录中:

    cd ../bin/

 

10、用 vim 打开 /etc/ 目录下的配置文件 profile:

    vim /etc/profile

    并在其尾部追加如下内容:

 

    # idea - zookeeper-3.4.9 config start - 2016-09-08

 

    export ZOOKEEPER_HOME=/usr/local/services/zookeeper/zookeeper-3.4.9/

    export PATH=$ZOOKEEPER_HOME/bin:$PATH

    export PATH

 

    # idea - zookeeper-3.4.9 config start - 2016-09-08

 

11、使 /etc/ 目录下的 profile 文件即可生效:

    source /etc/profile

 

12、启动 zookeeper 服务:

    zkServer.sh start

    如打印如下信息则表明启动成功:

    ZooKeeper JMX enabled by default

    Using config: /usr/local/services/zookeeper/zookeeper-3.4.9/bin/../conf/zoo.cfg

    Starting zookeeper ... STARTED

 

13、查询 zookeeper 状态:

    zkServer.sh status

 

14、关闭 zookeeper 服务:

    zkServer.sh stop

    如打印如下信息则表明成功关闭:

    ZooKeeper JMX enabled by default

    Using config: /usr/local/services/zookeeper/zookeeper-3.4.9/bin/../conf/zoo.cfg

    Stopping zookeeper ... STOPPED

 

15、重启 zookeeper 服务:

    zkServer.sh restart

    如打印如下信息则表明重启成功:

    ZooKeeper JMX enabled by default

    Using config: /usr/local/services/zookeeper/zookeeper-3.4.9/bin/../conf/zoo.cfg

    ZooKeeper JMX enabled by default

    Using config: /usr/local/services/zookeeper/zookeeper-3.4.9/bin/../conf/zoo.cfg

    Stopping zookeeper ... STOPPED

    ZooKeeper JMX enabled by default

    Using config: /usr/local/services/zookeeper/zookeeper-3.4.9/bin/../conf/zoo.cfg

    Starting zookeeper ... STARTED

 

zookeeper集群

 

#修改配置文件zoo.cfg,如下:

egrep -v "^#|^$" zoo.cfg

tickTime=2000 initLimit=10 syncLimit=5 dataLogDir=/opt/zookeeper/logs dataDir=/opt/zookeeper/data clientPort=2181 autopurge.snapRetainCount=500 autopurge.purgeInterval=24 server.1= 192.168.1.148:2888:3888 server.2= 192.168.1.149:2888:3888 server.3= 192.168.1.150:2888:3888

#创建相关目录,三台节点都需要

mkdir -p /tmp/zookeeper/{logs,data}

#其余zookeeper节点安装完成之后,同步配置文件zoo.cfg。

 

cd /tmp/zookeeper/

 

创建ServerID标识

除了修改zoo.cfg配置文件外,zookeeper集群模式下还要配置一个myid文件,这个文件需要放在dataDir目录下。

这个文件里面有一个数据就是A的值(该A就是zoo.cfg文件中server.A=B:C:D中的A),在zoo.cfg文件中配置的dataDir路径中创建myid文件。

#在192.168.1.148服务器上面创建myid文件,并设置值为1,同时与zoo.cfg文件里面的server.1保持一致,如下

echo "1" > /tmp/zookeeper//myid

#在192.168.1.149服务器上面创建myid文件,并设置值为1,同时与zoo.cfg文件里面的server.2保持一致,如下

echo "2" > /tmp/zookeeper//myid

#在192.168.1.150服务器上面创建myid文件,并设置值为1,同时与zoo.cfg文件里面的server.3保持一致,如下

echo "3" > /tmp/zookeeper//myid

到此,相关配置已完成

 

启动zk,如果报错,检查zoo.cfg配置文件和iptbale防火墙

 

或者

mkdir -p /usr/local/services/zookeeper/zookeeper-3.4.9/data

 

cd /usr/local/services/zookeeper/zookeeper-3.4.9/data

 

echo "2" > /usr/local/services/zookeeper/zookeeper-3.4.9/data/myid

 

重启zk

注:官网上是maven项目,这里把它都变成了web项,官网上用maven只创建了一个maven的接口,然后生产者,消费者去依赖这个接口,这里是在消费者和生产者中都创建了一个相同的接口

motan 生产者配置

已mian方法启动 和 web 方式启动

创建接口

package com.business.user.service;

 

import java.util.List;

 

import com.business.user.domain.User;

/**

* @ClassName: UserService

* @Description: User服务接口

*

*/

public interface UserServices {

/**

* 批量删除

* @param ids 要删除的UserID集合

* @return 删除成功的记录生活

*/

public String hello(String name);

}

 

实现类UserServiceImpls实现接口,如下,其中,MotanService是motan的服务注册注解

 

package com.business.user.service.impl;

import com.business.user.service.UserServices;

import com.weibo.api.motan.config.springsupport.annotation.MotanService;

 

 

/**

* @ClassName: UserServiceImpl

* @Description: User服务实现类

*

*/

 

@MotanService

public class UserServiceImpls implements UserServices {

@Override

public String hello(String name) {

return "hello,"+name;

}

}

如果没有controller,不需要启动tomcat服务,可以直接使用main函数启动,类如下:

public class Main { public static void main(String[] args) { @SuppressWarnings("unused") ApplicationContext applicationContext = new ClassPathXmlApplicationContext( new String[] {"classpath*:spring.xml"}); MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true); System.out.println("server start..."); } }

 

 

 

配置web.xml文件 (已main方法启动不需要配置web)

<?xml version="1.0" encoding="utf-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

id="WebApp_ID" version="3.0">

<!-- Spring -->

<!-- 配置Spring配置文件路径 -->

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:resources/spring.xml</param-value>

</context-param>

<!-- 配置Spring上下文监听器 -->

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<!-- Spring -->

<!-- 配置Spring字符编码过滤器 -->

<filter>

<filter-name>encodingFilter</filter-name>

<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

<init-param>

<param-name>encoding</param-name>

<param-value>UTF-8</param-value>

</init-param>

<init-param>

<param-name>forceEncoding</param-name>

<param-value>true</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>encodingFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

<!-- Spring MVC 核心控制器 DispatcherServlet 配置 -->

<servlet>

<servlet-name>spring_mvc_dispatcher</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath*:spring-mvc.xml</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>spring_mvc_dispatcher</servlet-name>

<!-- 拦截所有/* 的请求,交给DispatcherServlet处理,性能最好 -->

<url-pattern>/</url-pattern>

</servlet-mapping>

 

<!-- motan优雅起停功能 -->

<listener>

<listener-class>motan.demo.server.server.listener.MotanServletContextListener</listener-class>

</listener>

</web-app>

 

在src/main/resources中添加spring.xml

 

 

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:motan="http://api.weibo.com/schema/motan"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://api.weibo.com/schema/motan http://api.weibo.com/schema/motan.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-2.5.xsd">

<context:component-scan base-package="com.business.user.service.UserServices"/>

<motan:annotation package="com.business.user.Server"/>

<bean id="serviceImpl" class="com.business.user.service.impl.UserServiceImpls" />

<!-- spring管理的服务具体实现 /z01/src/test01/FooServiceImpl.java

-->

<motan:registry regProtocol="zookeeper" name="registry" address="192.168.218.138:1234,192.168.218.138:1237,192.168.218.138:1235" connectTimeout="4000"/>

<!-- 协议配置。为防止多个业务配置冲突,推荐使用id表示具体协议。-->

<motan:protocol id="demoMotan" default="true" name="motan"

maxServerConnection="80000" maxContentLength="1048576"

maxWorkerThread="800" minWorkerThread="20"/>

<!-- 服务的默认配置 group:标识服务的分组 module:标识模块信息 registry:标识service使用的注册中心,与motan:registry中的name对应 -->

<motan:basicService export="demoMotan:8009"

group="lyncc-motan-demo-rpc" accessLog="true" shareChannel="true" module="motan-demo-rpc"

application="myMotanDemo" registry="registry" id="serviceBasicConfig"/>

<!-- 要暴露的服务,唯一标识是integerface

-->

<motan:service interface="com.business.user.service.UserServices" ref="serviceImpl" export="demoMotan:5561" basicService="serviceBasicConfig" />

</beans>

 

 

为了实现优雅的起启服务,需要定义一个ServletContextListener来启动或关闭服务MotanSwitcherUtil,经测试,如果不配置的话,通过servlet发布的服务将无法使用

 

package motan.demo.server.server.listener;

 

import javax.servlet.ServletContextEvent;

import javax.servlet.ServletContextListener;

import com.weibo.api.motan.common.MotanConstants;

import com.weibo.api.motan.util.MotanSwitcherUtil;

 

public class MotanServletContextListener implements ServletContextListener {

 

public void contextDestroyed(ServletContextEvent arg0) {

}

@Override

public void contextInitialized(ServletContextEvent event) {

System.out.println("8888888");

MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true);

}

 

}

 

 

在src/main/resources目录中添加spring-mvc.xml配置文件(如果项目是 spingmvc 需要访问controller 则配置,否则可以不配)

 

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:mvc="http://www.springframework.org/schema/mvc"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:util="http://www.springframework.org/schema/util"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc.xsd

http://www.springframework.org/schema/util

http://www.springframework.org/schema/util/spring-util.xsd ">

 

<!-- 指定自己定义的validator -->

<mvc:annotation-driven validator="validator"/>

<context:component-scan base-package="com.business.user" use-default-filters="false">

<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>

<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>

</context:component-scan>

<mvc:default-servlet-handler/>

 

</beans>

 

 

motan 消费者配置

已mian方法启动 和 web 方式启动

 

创建接口(要和生产者提供的接口相同,包名也需要相同)

package com.business.user.service;

 

import java.util.List;

 

import com.business.user.domain.User;

/**

* @ClassName: UserService

* @Description: User服务接口

*

*/

public interface UserServices {

/**

* 批量删除

* @param ids 要删除的UserID集合

* @return 删除成功的记录生活

*/

public String hello(String name);

}

 

 

 定义一个Service,引用motan服务

 

package com.business.user.service.impl;

import org.springframework.stereotype.Service;

import com.business.user.service.UserServices;

import com.weibo.api.motan.config.springsupport.annotation.MotanReferer;

/**

* @ClassName: UserServiceImpl

* @Description: User服务实现类

*

*/

 

@Service

public class UserServiceImpls {

@MotanReferer

private UserServices userServices;

public String say(String name){

System.out.println("motan"+"999999999999999"+"ooooooo");

System.out.println(name);

String result=userServices.hello("woaini");

System.out.println(result);

return result;

}

 

}

 

定义controller,引用Service

@Autowired

private UserServiceImpls userServiceImpls;

/**

* 批量删除记录

*/

public void delete() {

String msg = "";

if (ids != null && ids.length > 0){

System.out.println(userServiceImpls.say("binibin"));

int s = userService.deleteByIds(ids);

msg = "删除成功" + s + "条记录";

} else {

msg = "请选择要删除的记录!";

}

JSONOuter().writeSuccessMsg(getOut(), msg);

}

 

 

如果这个项目也没有controller,则也可以直接使用main函数启动, 进行调用测试,内容如下

package com;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

import com.business.user.service.UserServices;

 

public class ClientStart {

public static void main(String[] args) {

ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:spring/spring-server.xml");

UserServices service = (UserServices) ctx.getBean("remoteService");

for(int i = 0;i< 50;i++){

System.out.println(service.hello("motan"));

}

}

}

 

实现优雅的起启服务

public class MotanServletContextListener implements ServletContextListener {

 

public void contextDestroyed(ServletContextEvent arg0) {

}

@Override

public void contextInitialized(ServletContextEvent event) {

System.out.println("8888888");

MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true);

}

 

 web.xml配置文件与motan-server项目相同

 

集成马场 web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app id="WebApp_ID" version="2.4" metadata-complete="true"

xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath*:spring/spring-*.xml</param-value>

</context-param>

<listener>

<listener-class>

org.springframework.web.context.ContextLoaderListener

</listener-class>

</listener>

<context-param>

<param-name>log4jExposeWebAppRoot</param-name>

<param-value>false</param-value>

</context-param>

<context-param>

<param-name>log4jConfigLocation</param-name>

<param-value>classpath:log4j.properties</param-value>

</context-param>

<context-param>

<param-name>webAppRootKey</param-name>

<param-value>horseFarm.root</param-value>

</context-param>

 

 

<session-config>

<session-timeout>300</session-timeout>

</session-config>

 

<listener>

<listener-class>

org.springframework.web.util.Log4jConfigListener

</listener-class>

</listener>

<servlet>

<servlet-name>UploadServlet</servlet-name>

<servlet-class>com.webuploader.UploadServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>UploadServlet</servlet-name>

<url-pattern>/UploadServlet.do</url-pattern>

</servlet-mapping>

<servlet>

<servlet-name>WxMsgPushHandleNew</servlet-name>

<servlet-class>com.wx.push.WxMsgPushHandleNew</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>WxMsgPushHandleNew</servlet-name>

<url-pattern>/WxMsgPushHandleNew.do</url-pattern>

</servlet-mapping>

<servlet>

<servlet-name>WxNativePay</servlet-name>

<servlet-class>com.wx.pay.twocode.WxNativePay</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>WxNativePay</servlet-name>

<url-pattern>/twoCodePay.do</url-pattern>

</servlet-mapping>

<filter>

<filter-name>requestEncodefilter</filter-name>

<filter-class>

com.uniontrust.framework.web.filter.RequestEncodeFilter

</filter-class>

<init-param>

<param-name>encoding</param-name>

<param-value>UTF-8</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>requestEncodefilter</filter-name>

<url-pattern>*.action</url-pattern>

</filter-mapping>

<filter-mapping>

<filter-name>requestEncodefilter</filter-name>

<url-pattern>*.jsp</url-pattern>

</filter-mapping>

<!--

<filter>

<filter-name>getRequestFilter</filter-name>

<filter-class>

com.system.filter.GetRequestFilter

</filter-class>

<init-param>

<param-name>encoding</param-name>

<param-value>UTF-8</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>getRequestFilter</filter-name>

<url-pattern>*.action</url-pattern>

</filter-mapping>

-->

<error-page>

<error-code>404</error-code>

<location>/page/pc/404.html</location>

</error-page>

<filter>

<filter-name>responseEncodefilter</filter-name>

<filter-class>

com.uniontrust.framework.web.filter.ResponseEncodeFilter

</filter-class>

<init-param>

<param-name>encoding</param-name>

<param-value>UTF-8</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>responseEncodefilter</filter-name>

<url-pattern>*.action</url-pattern>

</filter-mapping>

<filter-mapping>

<filter-name>responseEncodefilter</filter-name>

<url-pattern>*.jsp</url-pattern>

</filter-mapping>

<filter-mapping>

<filter-name>responseEncodefilter</filter-name>

<url-pattern>*.html</url-pattern>

</filter-mapping>

 

<filter>

<filter-name>struts2</filter-name>

<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

<init-param>

<param-name>configProviders</param-name>

<param-value>

com.uniontrust.framework.web.InitConfigurationProvider

</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>struts2</filter-name>

<url-pattern>*.action</url-pattern>

</filter-mapping>

 

<mime-mapping>

<extension>html</extension>

<mime-type>text/html;charset=UTF-8</mime-type>

</mime-mapping>

<mime-mapping>

<extension>htm</extension>

<mime-type>text/html;charset=UTF-8</mime-type>

</mime-mapping>

<servlet>

<servlet-name>ReceiveResp1</servlet-name>

<servlet-class>com.bill99.plfk.servlet.ReceiveResp1</servlet-class>

</servlet>

<servlet>

<servlet-name>ReceiveResp2</servlet-name>

<servlet-class>com.bill99.plfk.servlet.ReceiveResp2</servlet-class>

</servlet>

<servlet>

<servlet-name>SendBatch</servlet-name>

<servlet-class>com.bill99.plfk.test.send.SendBatch</servlet-class>

</servlet>

 

 

 

<servlet-mapping>

<servlet-name>ReceiveResp1</servlet-name>

<url-pattern>/servlet/receive1</url-pattern>

</servlet-mapping>

<servlet-mapping>

<servlet-name>ReceiveResp2</servlet-name>

<url-pattern>/servlet/receive2</url-pattern>

</servlet-mapping>

<servlet-mapping>

<servlet-name>SendBatch</servlet-name>

<url-pattern>/servlet/senddata</url-pattern>

</servlet-mapping>

<!-- Spring MVC 核心控制器 DispatcherServlet 配置

<servlet>

<servlet-name>spring_mvc_dispatcher</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath*:spring/springmvc.xml</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>spring_mvc_dispatcher</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>

-->

<listener>

<listener-class>motan.demo.server.server.listener.MotanServletContextListener</listener-class>

</listener>

</web-app>

 

 web.xml配置文件与motan-server项目相同

 

 spring.xml配置文件调整如下:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:motan="http://api.weibo.com/schema/motan"

xmlns:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://api.weibo.com/schema/motan

http://api.weibo.com/schema/motan.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<!-- 自动扫描包 ,将带有注解的类 纳入spring容器管理

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="prefix" value="/WEB-INF/views/"></property>

<property name="suffix" value=".jsp"></property>

</bean>

<mvc:annotation-driven></mvc:annotation-driven>

-->

<context:annotation-config/>

<context:component-scan base-package="com.business.user.service">

<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>

<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>

</context:component-scan>

<context:component-scan base-package="com.business.user.service"/>

<motan:annotation package="com.business.user.service"/>

<motan:registry regProtocol="zookeeper" name="registry" address="192.168.218.138:1234,192.168.218.138:1237,192.168.218.138:1235"/>

<motan:protocol default="true" name="motan" haStrategy="failover"

loadbalance="roundrobin" maxClientConnection="10" minClientConnection="2"/>

<motan:basicReferer requestTimeout="4000" accessLog="true"

retries="2" group="lyncc-motan-demo-rpc" module="motan-demo-rpc"

application="myMotanDemo" protocol="motan" registry="registry"

id="motantestClientBasicConfig" throwException="false" check="true"/>

<motan:referer id="remoteService" interface="com.business.user.service.UserServices" connectTimeout="2000" requestTimeout="10000" basicReferer="motantestClientBasicConfig"/>

</beans>

 

spring-mvc.xml配置文件与server相同(如果没有controller,也可以不用配置)如果项目不是springmvc的,配置会报错,jar包冲突,jar版本过低等。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值