1 Zookeeper简介
- zookeeper的官网。
- zookeeper由雅虎研究院开发,是Google Chubby的开源实现,后来托管到Apache,于2010年11月正式成为Apache的顶级项目。
- 大数据生态系统里的很多组件的命名都是某种动物或者昆虫,比如Hadoop就是
,Hive就是
。zookeeper即动物园管理者,顾名思义就是管理大数据生态系统各组件的管理员,如下图所示。
2 服务提供者注册到Zookeeper
2.1 导入相关jar包的Maven坐标
<!-- SpringCloud整合zookeeper --><dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId> <!--排除zk3.5.3--> <exclusions> <exclusion> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> </exclusion> </exclusions></dependency><dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.10</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> </exclusions></dependency>
<?xml version="1.0" encoding="UTF-8"?><project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>spring_cloud_demo</artifactId> <groupId>org.sunxiaping</groupId> <version>1.0</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>product_service_zookeeper9001</artifactId> <dependencies> <!-- SpringCloud整合zookeeper --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId> <!--排除zk3.5.3--> <exclusions> <exclusion> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.10</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> </exclusions> </dependency> <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.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies></project>
2.2 application.yml
spring:# SpringCloud cloud: zookeeper: connect-string: 192.168.237.100:2181
server: port: 9001 # 微服务的端口号spring: application: name: service-product # 微服务的名称 datasource: url: jdbc:mysql://192.168.237.100:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true driver-class-name: com.mysql.cj.jdbc.Driver username: root password: 123456 jpa: generate-ddl: true show-sql: true open-in-view: true database: mysql # SpringCloud cloud: zookeeper: connect-string: 192.168.237.100:2181# 微服务info内容详细信息info: app.name: xxx company.name: xxx build.artifactId: $project.artifactId$ build.version: $project.version$
2.3 在启动类上标注@EnableDiscoveryClient注解
package com.sunxiaping.product;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@SpringBootApplication@EnableDiscoveryClient //该注解用于向使用Consul或者zookeeper作为注册中心时注册服务public class Product9001Application { public static void main(String[] args) { SpringApplication.run(Product9001Application.class, args); }}
2.4 ProductController
package com.sunxiaping.product.controller;import com.sunxiaping.product.domain.Product;import com.sunxiaping.product.service.ProductService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.*;@RestController@RequestMapping(value = "/product")public class ProductController { @Value("${server.port}") private String port; @Value("${spring.cloud.client.ip-address}") private String ip; @Autowired private ProductService productService; @PostMapping(value = "/save") public String save(@RequestBody Product product) { productService.save(product); return "新增成功"; } @GetMapping(value = "/findById/{id}") public Product findById(@PathVariable(value = "id") Long id) { Product product = productService.findById(id); product.setProductName("访问的地址是:" + ip + ":" + port); return product; }}
3 服务消费者注册到Zookeeper
3.1 导入相关jar包的Maven坐标
<!-- SpringCloud整合zookeeper --><dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId> <!--排除zk3.5.3--> <exclusions> <exclusion> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> </exclusion> </exclusions></dependency><dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.10</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> </exclusions></dependency>
<?xml version="1.0" encoding="UTF-8"?><project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>spring_cloud_demo</artifactId> <groupId>org.sunxiaping</groupId> <version>1.0</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>order_service_zookeeper8001</artifactId> <dependencies> <!-- SpringCloud整合zookeeper --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId> <!--排除zk3.5.3--> <exclusions> <exclusion> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.10</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> </exclusions> </dependency> <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.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies></project>
3.2 application.yml
spring: # SpringCloud cloud: zookeeper: connect-string: 192.168.237.100:2181
server: port: 8001 # 微服务的端口号spring: application: name: service-order # 微服务的名称 # SpringCloud cloud: zookeeper: connect-string: 192.168.237.100:2181# 微服务info内容详细信息info: app.name: xxx company.name: xxx build.artifactId: $project.artifactId$ build.version: $project.version$# 开启日志debuglogging: level: root: info
3.3 在Spring容器中注入RestTemplate
package com.sunxiaping.order.config;import org.springframework.cloud.client.loadbalancer.LoadBalanced;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.client.RestTemplate;@Configurationpublic class SpringConfig { @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); }}
3.4 在启动类上标注@EnableDiscoveryClient注解
package com.sunxiaping.order;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@SpringBootApplication@EnableDiscoveryClientpublic class Order8001Application { public static void main(String[] args) { SpringApplication.run(Order8001Application.class, args); }}
3.5 OrderController
package com.sunxiaping.order.controller;import com.sunxiaping.order.domain.Product;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate;@RestController@RequestMapping(value = "/order")public class OrderController { @Autowired private RestTemplate restTemplate; /** * 使用OrderCommand调用远程远程服务 * * @param id * @return */ @GetMapping(value = "/buy/{id}") public Product buy(@PathVariable(value = "id") Long id) { return restTemplate.getForObject("http://service-product/product/findById/" + id, Product.class); }}