一、目录结构
此项目实例是使用多模块maven,并配合springboot进行构建
结构如下图所示
二、每个maven的配置
这里是父maven的配置
4.0.0
pom
order
product
org.springframework.boot
spring-boot-starter-parent
2.0.2.RELEASE
com.example
springboot_zookeeper
0.0.1-SNAPSHOT
springboot_zookeeper
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.apache.zookeeper
zookeeper
3.4.12
com.101tec
zkclient
0.10
org.apache.curator
curator-framework
4.0.0
org.apache.curator
curator-recipes
4.0.0
org.apache.maven.plugins
maven-compiler-plugin
1.8
1.8
UTF-8
order服务下的maven
springboot_zookeeper
com.example
0.0.1-SNAPSHOT
4.0.0
order
product服务下的maven
springboot_zookeeper
com.example
0.0.1-SNAPSHOT
4.0.0
product
三、product模块
此模块下的Application代码
package com.example.product;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.Properties;
@SpringBootApplication
@RestController
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
@RequestMapping(value = "/product/id",method = RequestMethod.GET)
public String get(HttpServletRequest request){
return "访问 product 服务端口:"+ request.getLocalPort();
}
@Bean
public Register register(){
Register register = new Register();
register.register11(getAddressAndPort());
return register;
}
private String getAddressAndPort(){
try {
Properties properties = new Properties();
InputStream stream = Application.class.getClassLoader().getResourceAsStream("application.properties");
properties.load(stream);
Object port = properties.get("server.port");
return "127.0.0.1:" + port;
}catch (Exception e){
throw new RuntimeException();
}
}
}
使用Register实现服务的注册
package com.example.product;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
public class Register {
private static final String SERVER_PATH = "/product";
private static final String ZK_ADDRESS = "94.*.*.51:4180";
private static final int ZK_TIMEOUT = 20000;
public void register11(String address){
try{
ZooKeeper zooKeeper = new ZooKeeper(ZK_ADDRESS,ZK_TIMEOUT, WatchEvent ->{
});
Stat stat = zooKeeper.exists(SERVER_PATH,false);
if(stat==null){
zooKeeper.create(SERVER_PATH,"".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
String path = address;
//创建短暂的可排序的子节点
zooKeeper.create(SERVER_PATH+"/instance",path.getBytes(),ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.EPHEMERAL_SEQUENTIAL);
}catch (Exception e){
e.printStackTrace();
}
}
}
此模块中对应的application.properties配置
server.port=8086
四、order模块
此模块下的Application代码
package com.example.order;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
@SpringBootApplication
@RestController
public class Application {
@Resource
private RestTemplate restTemplate;
@Resource
private ZookListener listener;
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
@Bean
public ZookListener zookListener(){
ZookListener listener = new ZookListener();
listener.init();
return listener;
}
@RequestMapping(value = "/order/id",method = RequestMethod.GET)
public String get(){
//从zookeeper中获取调用的ip
String path = listener.getPath();
if(path == null){
return "对不起,产品暂时停止服务!";
}
return restTemplate.getForObject("http://"+listener.getPath()+"/product/id",String.class);
}
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
此模块下使用ZookListener类实现获取zookeeper的节点列表,也可以监听zookeeper里面的节点变化
package com.example.order;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
public class ZookListener {
private static final String SERVER_PATH="/product";
private static final String ZK_ADDRESS="94.*.*.51:4180";
private static final int ZK_TIMEOUT = 20000;
private ZooKeeper zooKeeper;
private List paths = new LinkedList<>();
public void init(){
try{
zooKeeper = new ZooKeeper(ZK_ADDRESS,ZK_TIMEOUT,(WatchEvent)->{
getChilds();
});
}catch (Exception e){
e.printStackTrace();
}
}
private void getChilds(){
List ips = new LinkedList<>();
try{
//添加监听
List childs = this.zooKeeper.getChildren(SERVER_PATH,true);
for(String child:childs){
byte[] obj = zooKeeper.getData(SERVER_PATH+"/"+child,false,null);
String path = new String(obj,"utf-8");
ips.add(path);
}
this.paths = ips;
}catch (Exception e){
e.printStackTrace();
}
}
public String getPath(){
if(paths.isEmpty()){
return null;
}
int index = new Random().nextInt(paths.size());
return paths.get(index);
}
}
此模块中的application.properties
server.port = 8080
四、测试
分别启动两个模块的application,如下图
访问浏览器地址http://127.0.0.1:8080/order/id,如下显示
查看zookeeper中的节点值,如下图显示
停止服务
当我们停止了product模块的时候,访问浏览器,会提示服务停止,如下图
再看看zookeeper中的节点,过了几秒后,就会自动删除了