1 Zookeeper简介

  • zookeeper的官网
  • zookeeper由雅虎研究院开发,是Google Chubby的开源实现,后来托管到Apache,于2010年11月正式成为Apache的顶级项目。
  • 大数据生态系统里的很多组件的命名都是某种动物或者昆虫,比如Hadoop就是Eureka替换方案Zookeeper(不推荐) - 图1,Hive就是Eureka替换方案Zookeeper(不推荐) - 图2。zookeeper即动物园管理者,顾名思义就是管理大数据生态系统各组件的管理员,如下图所示。

2 服务提供者注册到Zookeeper

2.1 导入相关jar包的Maven坐标

  • 修改部分:
  1. <!-- SpringCloud整合zookeeper -->
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
  5. <!--排除zk3.5.3-->
  6. <exclusions>
  7. <exclusion>
  8. <groupId>org.apache.zookeeper</groupId>
  9. <artifactId>zookeeper</artifactId>
  10. </exclusion>
  11. </exclusions>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.apache.zookeeper</groupId>
  15. <artifactId>zookeeper</artifactId>
  16. <version>3.4.10</version>
  17. <exclusions>
  18. <exclusion>
  19. <groupId>org.slf4j</groupId>
  20. <artifactId>slf4j-log4j12</artifactId>
  21. </exclusion>
  22. </exclusions>
  23. </dependency>
  • 完整部分:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://maven.apache.org/POM/4.0.0"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>spring_cloud_demo</artifactId>
  7. <groupId>org.sunxiaping</groupId>
  8. <version>1.0</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>product_service_zookeeper9001</artifactId>
  12. <dependencies>
  13. <!-- SpringCloud整合zookeeper -->
  14. <dependency>
  15. <groupId>org.springframework.cloud</groupId>
  16. <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
  17. <!--排除zk3.5.3-->
  18. <exclusions>
  19. <exclusion>
  20. <groupId>org.apache.zookeeper</groupId>
  21. <artifactId>zookeeper</artifactId>
  22. </exclusion>
  23. </exclusions>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.apache.zookeeper</groupId>
  27. <artifactId>zookeeper</artifactId>
  28. <version>3.4.10</version>
  29. <exclusions>
  30. <exclusion>
  31. <groupId>org.slf4j</groupId>
  32. <artifactId>slf4j-log4j12</artifactId>
  33. </exclusion>
  34. </exclusions>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.springframework.boot</groupId>
  38. <artifactId>spring-boot-starter-web</artifactId>
  39. </dependency>
  40. <dependency>
  41. <groupId>org.springframework.boot</groupId>
  42. <artifactId>spring-boot-devtools</artifactId>
  43. <scope>runtime</scope>
  44. <optional>true</optional>
  45. </dependency>
  46. <dependency>
  47. <groupId>org.springframework.boot</groupId>
  48. <artifactId>spring-boot-starter-test</artifactId>
  49. <scope>test</scope>
  50. </dependency>
  51. <dependency>
  52. <groupId>org.springframework.boot</groupId>
  53. <artifactId>spring-boot-starter-data-jpa</artifactId>
  54. </dependency>
  55. <dependency>
  56. <groupId>mysql</groupId>
  57. <artifactId>mysql-connector-java</artifactId>
  58. </dependency>
  59. <dependency>
  60. <groupId>org.springframework.boot</groupId>
  61. <artifactId>spring-boot-starter-actuator</artifactId>
  62. </dependency>
  63. </dependencies>
  64. </project>

2.2 application.yml

  • 修改部分:
  1. spring:
  2. # SpringCloud
  3. cloud:
  4. zookeeper:
  5. connect-string: 192.168.237.100:2181
  • 完整部分:
  1. server:
  2. port: 9001 # 微服务的端口号
  3. spring:
  4. application:
  5. name: service-product # 微服务的名称
  6. datasource:
  7. url: jdbc:mysql://192.168.237.100:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
  8. driver-class-name: com.mysql.cj.jdbc.Driver
  9. username: root
  10. password: 123456
  11. jpa:
  12. generate-ddl: true
  13. show-sql: true
  14. open-in-view: true
  15. database: mysql
  16. # SpringCloud
  17. cloud:
  18. zookeeper:
  19. connect-string: 192.168.237.100:2181
  20. # 微服务info内容详细信息
  21. info:
  22. app.name: xxx
  23. company.name: xxx
  24. build.artifactId: $project.artifactId$
  25. build.version: $project.version$

2.3 在启动类上标注@EnableDiscoveryClient注解

  1. package com.sunxiaping.product;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  5. @SpringBootApplication
  6. @EnableDiscoveryClient //该注解用于向使用Consul或者zookeeper作为注册中心时注册服务
  7. public class Product9001Application {
  8. public static void main(String[] args) {
  9. SpringApplication.run(Product9001Application.class, args);
  10. }
  11. }

2.4 ProductController

  1. package com.sunxiaping.product.controller;
  2. import com.sunxiaping.product.domain.Product;
  3. import com.sunxiaping.product.service.ProductService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.beans.factory.annotation.Value;
  6. import org.springframework.web.bind.annotation.*;
  7. @RestController
  8. @RequestMapping(value = "/product")
  9. public class ProductController {
  10. @Value("${server.port}")
  11. private String port;
  12. @Value("${spring.cloud.client.ip-address}")
  13. private String ip;
  14. @Autowired
  15. private ProductService productService;
  16. @PostMapping(value = "/save")
  17. public String save(@RequestBody Product product) {
  18. productService.save(product);
  19. return "新增成功";
  20. }
  21. @GetMapping(value = "/findById/{id}")
  22. public Product findById(@PathVariable(value = "id") Long id) {
  23. Product product = productService.findById(id);
  24. product.setProductName("访问的地址是:" + ip + ":" + port);
  25. return product;
  26. }
  27. }

3 服务消费者注册到Zookeeper

3.1 导入相关jar包的Maven坐标

  • 修改部分:
  1. <!-- SpringCloud整合zookeeper -->
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
  5. <!--排除zk3.5.3-->
  6. <exclusions>
  7. <exclusion>
  8. <groupId>org.apache.zookeeper</groupId>
  9. <artifactId>zookeeper</artifactId>
  10. </exclusion>
  11. </exclusions>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.apache.zookeeper</groupId>
  15. <artifactId>zookeeper</artifactId>
  16. <version>3.4.10</version>
  17. <exclusions>
  18. <exclusion>
  19. <groupId>org.slf4j</groupId>
  20. <artifactId>slf4j-log4j12</artifactId>
  21. </exclusion>
  22. </exclusions>
  23. </dependency>
  • 完整部分:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://maven.apache.org/POM/4.0.0"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>spring_cloud_demo</artifactId>
  7. <groupId>org.sunxiaping</groupId>
  8. <version>1.0</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>order_service_zookeeper8001</artifactId>
  12. <dependencies>
  13. <!-- SpringCloud整合zookeeper -->
  14. <dependency>
  15. <groupId>org.springframework.cloud</groupId>
  16. <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
  17. <!--排除zk3.5.3-->
  18. <exclusions>
  19. <exclusion>
  20. <groupId>org.apache.zookeeper</groupId>
  21. <artifactId>zookeeper</artifactId>
  22. </exclusion>
  23. </exclusions>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.apache.zookeeper</groupId>
  27. <artifactId>zookeeper</artifactId>
  28. <version>3.4.10</version>
  29. <exclusions>
  30. <exclusion>
  31. <groupId>org.slf4j</groupId>
  32. <artifactId>slf4j-log4j12</artifactId>
  33. </exclusion>
  34. </exclusions>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.springframework.boot</groupId>
  38. <artifactId>spring-boot-starter-web</artifactId>
  39. </dependency>
  40. <dependency>
  41. <groupId>org.springframework.boot</groupId>
  42. <artifactId>spring-boot-devtools</artifactId>
  43. <scope>runtime</scope>
  44. <optional>true</optional>
  45. </dependency>
  46. <dependency>
  47. <groupId>org.springframework.boot</groupId>
  48. <artifactId>spring-boot-starter-test</artifactId>
  49. <scope>test</scope>
  50. </dependency>
  51. <dependency>
  52. <groupId>org.springframework.boot</groupId>
  53. <artifactId>spring-boot-starter-actuator</artifactId>
  54. </dependency>
  55. </dependencies>
  56. </project>

3.2 application.yml

  • 修改部分:
  1. spring:
  2. # SpringCloud
  3. cloud:
  4. zookeeper:
  5. connect-string: 192.168.237.100:2181
  • 完整部分:
  1. server:
  2. port: 8001 # 微服务的端口号
  3. spring:
  4. application:
  5. name: service-order # 微服务的名称
  6. # SpringCloud
  7. cloud:
  8. zookeeper:
  9. connect-string: 192.168.237.100:2181
  10. # 微服务info内容详细信息
  11. info:
  12. app.name: xxx
  13. company.name: xxx
  14. build.artifactId: $project.artifactId$
  15. build.version: $project.version$
  16. # 开启日志debug
  17. logging:
  18. level:
  19. root: info

3.3 在Spring容器中注入RestTemplate

  • SpringConfig.java
  1. package com.sunxiaping.order.config;
  2. import org.springframework.cloud.client.loadbalancer.LoadBalanced;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.web.client.RestTemplate;
  6. @Configuration
  7. public class SpringConfig {
  8. @Bean
  9. @LoadBalanced
  10. public RestTemplate restTemplate() {
  11. return new RestTemplate();
  12. }
  13. }

3.4 在启动类上标注@EnableDiscoveryClient注解

  1. package com.sunxiaping.order;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  5. @SpringBootApplication
  6. @EnableDiscoveryClient
  7. public class Order8001Application {
  8. public static void main(String[] args) {
  9. SpringApplication.run(Order8001Application.class, args);
  10. }
  11. }

3.5 OrderController

  1. package com.sunxiaping.order.controller;
  2. import com.sunxiaping.order.domain.Product;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.PathVariable;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RestController;
  8. import org.springframework.web.client.RestTemplate;
  9. @RestController
  10. @RequestMapping(value = "/order")
  11. public class OrderController {
  12. @Autowired
  13. private RestTemplate restTemplate;
  14. /**
  15. * 使用OrderCommand调用远程远程服务
  16. *
  17. * @param id
  18. * @return
  19. */
  20. @GetMapping(value = "/buy/{id}")
  21. public Product buy(@PathVariable(value = "id") Long id) {
  22. return restTemplate.getForObject("http://service-product/product/findById/" + id, Product.class);
  23. }
  24. }