开门见山,直接写实现的过程,不介绍motan和consul。
这里使用的是eclipse,其他的工具也可以。
**建立server端
**建立client端
**配置consul 集群
**建立server端
在eclipse上创建一个maven项目。
1、新建服务接口HelloService.java,包名可自定义,需在后面配置文件中注意写成自己的包名。
package com.raventech.user.motan;
public interface HelloService {
String hello(String world);
}
2、新建实现服务接口类HelloServiceImpl.java
package com.raventech.user.motan;
public class HelloServiceImpl implements HelloService {
public String hello(String world) {
return "hello " + world;
}
}
3、在 在src/main/resources下新建motan-server.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: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">
<motan:registry regProtocol="consul" name="registry" address="192.168.233.129:8500"/>
<bean id="helloServiceImpl" class="com.raventech.user.motan.HelloServiceImpl"/>
<motan:service interface="com.raventech.user.motan.HelloService" ref="helloServiceImpl" export="8002"/>
</beans>
motan-server.xml中的<motan:registry>是配置consul注册服务的(192.168.233.129是虚拟机consul client配置地址)。
<motan:service>是指明服务接口的(注意包名别写错了)。端口默认8002.
4、在src/test/java下新建test.java用以启动服务程序
package motanServer;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.weibo.api.motan.common.MotanConstants;
import com.weibo.api.motan.util.MotanSwitcherUtil;
public class text {
public static void main(String[] args) {
//读配置文件
new ClassPathXmlApplicationContext("classpath:motan-server.xml");
//设置启动服务调用心跳开关
MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true);
System.out.println("Server start ...");
}
}
**建立client端
同样新开一个eclipse,并在eclipse上创建一个maven项目。
1、新建要调用的服务接口HelloService.java,包名要与server端同名。
package com.raventech.user.motan;
public interface HelloService {
String hello(String world);
}
2、在 在src/main/resources下新建client-server.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: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">
<motan:registry regProtocol="consul" name="registry" address="192.168.233.129:8500"/>
<motan:referer id="helloService" interface="com.raventech.user.motan.HelloService"/>
</beans>
client-server.xml中的registry是配置consul注册服务的(192.168.233.129是虚拟机consul client配置地址)。
referer是指明服务接口的(注意包名别写错了)。端口默认8002.
4、在src/test/java下新建test.java用以启动服务程序
package com.raventech.web;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.raventech.user.motan.HelloService;
public class test {
public static void main(String[] args) {
//加载配置xml
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:motan-client.xml");
//获取服务
HelloService fooService = (HelloService) applicationContext.getBean("helloService");
System.out.println(fooService.hello("world"));
}
}
**配置consul 集群
这里用的 VMware建立了两个虚拟机。
consul最常用到的命令是agent
输入consul agent -h 可以查看帮助。其中常见的参数解释如下:
-bootstrap:用来控制一个server是否在bootstrap模式,在一个datacenter中只能有一个server处于bootstrap模式,当一个server处于bootstrap模式时,可以自己选举为raft leader。
-bootstrap-expect:在一个datacenter中期望提供的server节点数目,当该值提供的时候,consul一直等到达到指定sever数目的时候才会引导整个集群,该标记不能和bootstrap公用-bind:该地址用来在集群内部的通讯,集群内的所有节点到地址都必须是可达的,默认是0.0.0.0
-client:consul绑定在哪个client地址上,这个地址提供HTTP、DNS、RPC等服务,默认是127.0.0.1
-config-file:明确的指定要加载哪个配置文件
-config-dir:配置文件目录,里面所有以.json结尾的文件都会被加载
-data-dir:提供一个目录用来存放agent的状态,所有的agent允许都需要该目录,该目录必须是稳定的,系统重启后都继续存在
-dc:该标记控制agent允许的datacenter的名称,默认是dc1
-encrypt:指定secret key,使consul在通讯时进行加密,key可以通过consul keygen生成,同一个集群中的节点必须使用相同的key
-join:加入一个已经启动的agent的ip地址,可以多次指定多个agent的地址。如果consul不能加入任何指定的地址中,则agent会启动失败,默认agent启动时不会加入任何节点。
-retry-join:和join类似,但是允许你在第一次失败后进行尝试。
-retry-interval:两次join之间的时间间隔,默认是30s
-retry-max:尝试重复join的次数,默认是0,也就是无限次尝试
-log-level:consul agent启动后显示的日志信息级别。默认是info,可选:trace、debug、info、warn、err。
-node:节点在集群中的名称,在一个集群中必须是唯一的,默认是该节点的主机名
-protocol:consul使用的协议版本
-rejoin:使consul忽略先前的离开,在再次启动后仍旧尝试加入集群中。
-server:定义agent运行在server模式,每个集群至少有一个server,建议每个集群的server不要超过5个
一:下载consul
consul 官网https://www.consul.io/
可以windows下下载,然后ssh到虚拟机。
最方便的事在linux下wegt 下载。
命令如:
wget https://releases.hashicorp.com/consul/0.7.5/consul_0.7.5_linux_amd64.zip
把上面下载的文件解压,并把consul拷贝到/usr/local/bin目录下。
输入consul 提示如下图,表示成功。
二 配置和运行
1:在192.168.233.129节点上面进行
cd /opt/consul
mkdir data
consul agent -server -bootstrap -bind=0.0.0.0 -client=192.168.233.129 -data-dir=/opt/consul/data -ui -node=server
这样,就启动了一个节点
2:在192.168.233.128节点上面进行
cd /opt/consul
mkdir data
consul agent -bind=0.0.0.0 -client=192.168.233.128 -data-dir=/opt/consul/data -node=client1 -join=192.168.233.129
访问http://192.168.233.129:8500/ 即可查看consul集群的管理页面
在确认启动好consul后,先启动eclipse-server端,在eclipse-client。
亲测试成功。
server端
client端
学习参考:
http://www.jianshu.com/p/fdc6a49526d3
http://blog.csdn.net/mn960mn/article/details/51753893