注册中心可以用Eureka等技术来实现,但是为了更好的扩展下知识层面,所以这次就使用Zookeeper作为注册中心,搭建一下以Zookeeper作为注册中心的微服务项目。
Zookeeper是一个高性能,分布式的,开源分布式应用协调服务。它提供了简单原始的功能,分布式应用可以基于它实现更高级的服务,比如同步,配置管理,集群管理,名空间。它被设计为易于编程,使用文件系统目录树作为数据模型。
首先在Zookeeper官网下载,地址如下图:
这次我们下载3.5.5的版本,因为我是以springboot2.0以上版本整合的,必须整好版本不然会报错,下载成功解压。
进入conf文件夹中如图:
复制一份conf文件夹下的zoo_sample. cfg文件并重命名为zoo.cfg修改dataDir=(你的zookeeper安装目录)
# The number of milliseconds of each tick
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.
# 你的zookeeper安装目录路径
dataDir=F:\java\apache-zookeeper-3.5.5-bin\data
# the port at which the clients will connect
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
进入bin目录下,运行zkServer.cmd启动zookeeper服务。
启动成功如下图:
在Eclipse中创建两个项目
pom.xml中的内容:
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.4.RELEASE
com.itmuch.cloud
springcloud-zookeeper
0.0.1-SNAPSHOT
pom
1.8
springcloud-zookeeper-order
springcloud-zookeeper-user
org.springframework.cloud
spring-cloud-starter-feign
1.4.1.RELEASE
org.springframework.boot
spring-boot-dependencies
2.1.4.RELEASE
pom
import
org.springframework.cloud
spring-cloud-dependencies
Greenwich.RELEASE
pom
import
org.springframework.boot
spring-boot-maven-plugin
springcloud-zookeeper-user项目中的pom.xml内容:
4.0.0
com.itmuch.cloud
springcloud-zookeeper
0.0.1-SNAPSHOT
springcloud-zookeeper-user
jar
UTF-8
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-zookeeper-discovery
springcloud-zookeeper-user项目中的application.yml内容:
server:
port: 9001
spring:
application:
name: zookeeper-user
cloud:
zookeeper:
connect-string: localhost:2181
discovery:
enabled: true
register: true
springcloud-zookeeper-user项目中的启动类内容:
@SpringBootApplication
@EnableDiscoveryClient
public class ZookeeperApplication {
public static void main( String[] args ){
SpringApplication.run(ZookeeperApplication.class, args);
}
}
springcloud-zookeeper-order项目中的pom.xml内容:
4.0.0
com.itmuch.cloud
springcloud-zookeeper
0.0.1-SNAPSHOT
springcloud-zookeeper-order
jar
UTF-8
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-zookeeper-discovery
springcloud-zookeeper-order项目中的application.yml内容:
server:
port: 9002
spring:
application:
name: zookeeper-order
cloud:
zookeeper:
connect-string: localhost:2181
discovery:
enabled: true
register: true
springcloud-zookeeper-order项目中的启动类内容:
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ZookeeperApplication {
public static void main( String[] args ){
SpringApplication.run(ZookeeperApplication.class, args);
}
}
springcloud-zookeeper-user项目中的Controller:
@RestController
public class UserController {
@GetMapping("/getUserList")
public Map getUserList() {
Map map = new HashMap();
List userlist = new ArrayList();
userlist.add("孙林");
userlist.add("王伟");
userlist.add("关天棋");
userlist.add("于晓野");
userlist.add("王大龙");
map.put("userlist", userlist);
return map;
}
}
springcloud-zookeeper-order项目中的Controller:
@RestController
public class OrderController {
@Autowired
private FeginInterfase feginInterfase;
@GetMapping("/getUserList")
public Map getUserList() {
Map map = feginInterfase.getUserList();
return map;
}
}
springcloud-zookeeper-order项目中的Fegin接口:
@FeignClient(name="zookeeper-user")
public interface FeginInterfase {
@RequestMapping(value="/getUserList",method=RequestMethod.GET)
public Map getUserList();
}
成功启动2个项目后,打开zookeeper管理工具可以查看到2个节点
在浏览器中输入请求地址可看到输出结果,至此SpringCloud整合Zookeeper注册中心以成功搭建完成。