SpringCloud整合Zookeeper

isWulongbo 2021-01-21 11:31:59
java SpringCloud zookeeper SegmentFault 整合


创建项目

使用 Spring Initializr 新建一个项目命名为: springcloud-zookeeper,删除多余的文件,并新建两个子模块分别为:springcloud-zookeeper-memberspringcloud-zookeeper-order
image.png

父工程 pom.xml配置文件spring-boot 版本这里选用 2.0.1.RELEASE

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<modules> <module>springcloud-zookeeper-member</module>
<module>springcloud-zookeeper-order</module>
</modules> <parent> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.baba.wlb</groupId>
<artifactId>springcloud-zookeeper</artifactId>
<version>1.0-SNAPSHOT</version>
<name>springcloud-zookeeper</name>
<description>Demo project for Spring Boot</description>
<properties> <java.version>1.8</java.version>
</properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency> <dependency> <groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency> <dependency> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> </dependencies>
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude> </excludes> </configuration> </plugin> </plugins> </build>
</project>

子模块

springcloud-zookeeper-member模块:

image.png

pom.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent> <artifactId>springcloud-zookeeper</artifactId>
<groupId>com.baba.wlb</groupId>
<version>1.0-SNAPSHOT</version>
</parent> <modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-zookeeper-member</artifactId>
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.M7</version>
<type>pom</type>
<scope>import</scope>
</dependency> </dependencies> </dependencyManagement>
<dependencies>
<!--springboot 整合web组件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--springcloud 整合zookeeper客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency> </dependencies>
<!--注意:这里必须添加,否则各种依赖有问题-->
<repositories>
<repository> <id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots> <enabled>false</enabled>
</snapshots> </repository> </repositories></project>

application.yml配置文件:

##服务器端口号
server:
port: 7001
##dubbo 注册到注册中心的名称
spring:
application:
name: zk-member
cloud:
zookeeper:
connect-string: 39.102.56.91:2181

MemberApiController 控制页面:

package com.baba.wlb.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author wulongbo
* @Date 2021/1/9 15:20
* @Version 1.0
*/@RestController
public class MemberApiController {
@Value("${server.port}")
private String serverPort;
@RequestMapping("/getMember")
public String getMember() {
return "我是会员服务!端口号:"+serverPort;
}
}

AppMember 启动类:

package com.baba.wlb.controller;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @Author wulongbo
* @Date 2021/1/9 15:22
* @Version 1.0
*/@EnableDiscoveryClient
@SpringBootApplication
public class AppMember {
// @EnableDiscoveryClient作用是 如果服务使用zookeeper或者connsul,使用@EnableDiscoveryClient向注册中心注册服务
public static void main(String[] args) {
SpringApplication.run(AppMember.class,args);
}
}

springcloud-zookeeper-order模块:

image.png

pom.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent> <artifactId>springcloud-zookeeper</artifactId>
<groupId>com.baba.wlb</groupId>
<version>1.0-SNAPSHOT</version>
</parent> <modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-zookeeper-order</artifactId>
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.M7</version>
<type>pom</type>
<scope>import</scope>
</dependency> </dependencies> </dependencyManagement>
<dependencies>
<!--springboot 整合web组件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--springcloud 整合zookeeper客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency> </dependencies>
<!--注意:这里必须添加,否则各种依赖有问题-->
<repositories>
<repository> <id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots> <enabled>false</enabled>
</snapshots> </repository> </repositories></project>

application.yml配置文件:

##服务器端口号
server:
port: 7002
##dubbo 注册到注册中心的名称
spring:
application:
name: zk-order
cloud:
zookeeper:
connect-string: 39.102.56.91:2181

OrderApiController 控制页面:

package com.baba.wlb.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* @Author wulongbo
* @Date 2021/1/9 15:32
* @Version 1.0
*/@RestController
public class OrderApiController {
@Autowired
private RestTemplate restTemplate;
/**
* springCloud中,两种方式调用(rest/feign)
*
* @return
*/
// 订单服务调用会员服务
@RequestMapping("/getOrder")
public String getOrder() {
// 有两种调用方式,一种是采用服务别名方式调用,另一种是使用别名去注册中心上获取对应服务调用地址
// 第一种方式
String url = "http://dy-202006281547:8000/getMember";
// 第二种方式
url = "http://zk-member/getMember";
String result = restTemplate.getForObject(url, String.class);
return "订单服务调用会员服务:" + result;
}
}

AppOrder.java 启动类:

package com.baba.wlb.controller;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/**
* @Author wulongbo
* @Date 2021/1/9 15:39
* @Version 1.0
*/@SpringBootApplication
@EnableDiscoveryClient
public class AppOrder {
public static void main(String[] args) {
SpringApplication.run(AppOrder.class,args);
// 第二种方式 如果使用rest方式以别名方式进行调用依赖ribbon负载均衡器
// 第二种方式 @LoadBalanced能让restTemplate 模板在请求时拥有客户端负载均衡的能力
}
// 解决RestTemplate 找不到原因, 把RestTemplate注册到Springboot容器中 @Bean
// 第一种方式
// @Bean
// RestTemplate restTemplate(){
// return new RestTemplate();
// }
// 第二种方式 @LoadBalanced能让restTemplate 模板在请求时拥有客户端负载均衡的能力
@Bean
@LoadBalanced RestTemplate restTemplate(){
return new RestTemplate();
}
}

启动项目

启动 AppMember.javaAppOrder.java

image.png

image.png

查看zookeeper上新注册了两个临时节点
image.png

浏览器访问 http://localhost:7002/getOrderorder服务可以调用到member服务。
image.png
读者也可以给member服务做集群,启动多个member服务,实现负载均衡。

版权声明
本文为[isWulongbo]所创,转载请带上原文链接,感谢
https://segmentfault.com/a/1190000039046859

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云