以zookeeper为注册中心搭建spring cloud环境
在spring cloud体系中,有多种手段实现注册中心,本例中采用zookeeper作为注册中心的角色。服务提供者向zookeeper注册,服务消费者从zookeeper中发现服务提供者的相关信息,从而远程调用服务提供方。
spring cloud与zookeeper的集成主要依赖spring-cloud-starter-zookeeper-discovery模块,下面是实例:
1、服务提供者zktest
引入相关依赖pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dxz.zktestclient</groupId>
<artifactId>zktestclient</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version> <!--配合spring cloud版本 -->
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<!--设置字符编码及java版本 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>
<!--用于测试的,本例可省略 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <!--依赖管理,用于管理spring-cloud的依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Brixton.SR3</version> <!--官网为Angel.SR4版本,但是我使用的时候总是报错 -->
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<!--使用该插件打包 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
启动类
package com.dxz.zktest; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient
@SpringBootApplication
public class ZktestApplication { public static void main(String[] args) {
SpringApplication.run(ZktestApplication.class, args);
}
}
配置信息
1、application.properties
server.port=8822
spring.application.name=zktest
2、bootstrap.properties
spring.cloud.zookeeper.connectString=192.168.5.14:2181
spring.cloud.zookeeper.discovery.instanceHost=192.168.5.3
spring.cloud.zookeeper.discovery.instancePort=${server.port}
2、服务消费者zktestClient
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dxz.zktestclient</groupId>
<artifactId>zktestclient</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version> <!--配合spring cloud版本 -->
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<!--设置字符编码及java版本 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>
<!--用于测试的,本例可省略 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <!--依赖管理,用于管理spring-cloud的依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Brixton.SR3</version> <!--官网为Angel.SR4版本,但是我使用的时候总是报错 -->
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<!--使用该插件打包 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
启动类
package com.dxz.zktestclient; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @SpringBootApplication
@EnableDiscoveryClient
@RestController
public class ZktestClientApplication { @Autowired
private LoadBalancerClient loadBalancer; @Autowired
private DiscoveryClient discovery; @RequestMapping("/discovery")
public Object discovery() {
System.out.println(loadBalancer.choose("zktest"));
return "discovery";
} @RequestMapping("/all")
public Object all() {
System.out.println(discovery.getServices());
return "all";
} public static void main(String[] args) {
SpringApplication.run(ZktestClientApplication.class, args);
}
}
配置信息
1、application.properties
server.port=8844
spring.application.name=zktestclient
2、bootstrap.properties
spring.cloud.zookeeper.connectString=192.168.5.14:2181
spring.cloud.zookeeper.discovery.register=false
3、zookeeper环境搭建
3.1、zookeeper安装
http://mirrors.shuosc.org/apache/zookeeper/zookeeper-3.4.11/
见《zk 03之 linux上的zookeeper单机与伪集群安装》
4、测试
启动zookeeper后, 先启动zktest项目,日志如下

查看zookeeper的节点情况,
在zookeeper所在bin目录下, 执行如下命令
[root@localhost bin]# ./zkCli.sh -server localhost,登录成功会展示如下:

查看zookeeper上的"/services/zktest"目录
[zk: localhost(CONNECTED) 1] ls /services/zktest
[22aa5d6a-f8a9-4053-bdc4-1b926ddc10b4]
[zk: localhost(CONNECTED) 2]
启动zktestClient项目
以zookeeper为注册中心搭建spring cloud环境的更多相关文章
- Spring Boot + Spring Cloud 实现权限管理系统 后端篇(十八):注册中心(Spring Cloud Consul)
什么是 Consul Consul 是 HashiCorp 公司推出的开源工具,用于实现分布式系统的服务发现与配置.与其它分布式服务注册与发现的方案,Consul 的方案更“一站式”,内置了服务注册与 ...
- 阶段5 3.微服务项目【学成在线】_day09 课程预览 Eureka Feign_02-Eureka注册中心-搭建Eureka单机环境
我们先搭建单机环境 govern是治理的意思, 这样就把工程创建好了 创建包 创建SpringBoot的启动类. 在父工程里面已经确定了Spring Cloud的版本了.相当于锁定了版本 接下里只需要 ...
- SpringCloud之使用Zookeeper作为注册中心
SpringCloud之使用Zookeeper作为注册中心 linux安装zookeeper 安装zookeeper 关闭linux防火墙 启动zookeeper 1 创建项目导入依赖和配置文件 &l ...
- 用ZooKeeper做为注册中心搭建基于Spring Cloud实现服务注册与发现
前提: 先安装好ZooKeeper的环境,搭建参考:http://www.cnblogs.com/EasonJim/p/7482961.html 说明: 可以再简单的理解为有两方协作,一个是服务提供这 ...
- Spring Cloud之服务注册中心搭建Eureka Server服务注册中⼼
Spring Cloud并不与Spring MVC类似是一个开源框架,而是一组解决问题的规范(个人理解).解决哪些问题呢?如下: 1)服务管理:⾃动注册与发现.状态监管 2)服务负载均衡 3)熔断 4 ...
- spring cloud 专题一 (spring cloud 入门搭建 之 Eureka注册中心搭建)
一.前言 本文为spring cloud 微服务框架专题的第一篇,主要讲解如何快速搭建spring cloud微服务及Eureka 注册中心 以及常用开发方式等. 本文理论不多,主要是傻瓜式的环境搭建 ...
- Zookeeper注册中心搭建-单机版(三)
Zookeeper是一个分布式协调组件,本质是一个软件. Zookeeper常用的功能有: 发布订阅功能,把 zookeeper 当作注册中心的原因. 分布式/集群管理功能 Zookeeper是Jav ...
- Spring-cloud (一):Eureka注册中心搭建
前提 系统安装jdk1.8及以上,配置好maven的ide(这里用idea进行演示,maven版本3.5,配置阿里云源) 项目搭建 新建一个maven项目,创建最简单的那种就好,项目名这里为Eurek ...
- maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目
项目的简单介绍: 项目采用maven聚合工程 用spring boot 搭建 spring cloud的微服务 模块式开发 项目的截图: 搭建开始: 能上图 我少打字 1.首先搭建maven的聚合工程 ...
随机推荐
- 斐波那契 - pythoon实现
def fn_1(n): if n == 0 : return n elif n == 1 : return n else: a = [0,1] for i in range(2,n): a.appe ...
- Hive错误:User root is not allowed to impersonate anonymous
修改hadoop 配置文件 etc/hadoop/core-site.xml,加入如下配置项: <property> <name>hadoop.proxyuser.root.h ...
- 【HDOJ1051】【排序+LIS】【贪心】
http://acm.hdu.edu.cn/showproblem.php?pid=1051 Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) ...
- hdu2509 Be the Winner 博弈
Let's consider m apples divided into n groups. Each group contains no more than 100 apples, arranged ...
- MYSQL 常用函数大全
1. 数学函数 greatest(x1,x2,...,xn)返回集合中最大的值 least(x1,x2,...,xn) 返回集合中最小的值 rand()返回0到1内的随机值,可以通过提供一个参数(种子 ...
- 用Python开发Zeroc Ice应用
Zeroc Ice简介 Zeroc ICE(Internet Communications Engine ,互联网通信引擎)是目前功能比较强大和完善的RPC框架,支持跨平台.跨语言调用.它非常灵活 ...
- getpwnam,getgrnam,getpwent,crypt等函数
[root@bogon code]# cat a.c #include<stdio.h> #include<pwd.h> int main() { struct passwd ...
- centos配置apache的https服务
因为公司要开发微信小程序,由于小程序比较特殊,需要https服务,所以就研究了下apache的https服务了,大致过程如下: 1.向证书机构申请https证书,会得到证书和私钥 2.安装apache ...
- 移除元素(remove,remove_if...unique...)
remove 因为本算法作用的是iterator,所以并不会改变Container大小,会返回一个新的iterator new_last,是的first到new_last中的元素都不等于value,左 ...
- 我发起了一个 .Net 平台上的 直播平台 开源项目 BalaBala
直播平台, 需要解决的 技术点 是 2 个: 1 直播数据 的 传输 和 在 客户端 的 播放 2 大并发 关于 网络通信, 数据传输, 可以参考 <利用 MessageRPC 和 Shar ...