• springcloud整合Zookeeper


    注册中心Zookeeper

    zookeeper是一个分布式协调工具,可以实现注册中心功能 

    关闭Linux服务器防火墙后启动zookeeper服务器 

    zookeeper服务器取代Eureka服务器,zk作为服务注册中心 

    服务提供者

     新建cloud-provider-payment8004

    version已经交给父工程管理 

    1. <properties>
    2. <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    3. <maven.compiler.source>1.8maven.compiler.source>
    4. <maven.compiler.target>1.8maven.compiler.target>
    5. <junit.version>4.12junit.version>
    6. <log4j.version>1.2.17log4j.version>
    7. <lombok.version>1.16.18lombok.version>
    8. <mysql.version>8.0.16mysql.version>
    9. <druid.version>1.1.16druid.version>
    10. <mybatis.spring.boot.version>1.3.0mybatis.spring.boot.version>
    11. properties>

      pom文件

    1. <project xmlns="http://maven.apache.org/POM/4.0.0"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <parent>
    5. <artifactId>mscloud03artifactId>
    6. <groupId>com.atguigu.springcloudgroupId>
    7. <version>1.0-SNAPSHOTversion>
    8. parent>
    9. <modelVersion>4.0.0modelVersion>
    10. <artifactId>cloud-provider-payment8004artifactId>
    11. <dependencies>
    12. <dependency>
    13. <groupId>org.springframework.bootgroupId>
    14. <artifactId>spring-boot-starter-webartifactId>
    15. dependency>
    16. <dependency>
    17. <groupId>com.atguigu.springcloudgroupId>
    18. <artifactId>cloud-api-commonsartifactId>
    19. <version>${project.version}version>
    20. dependency>
    21. <dependency>
    22. <groupId>org.springframework.cloudgroupId>
    23. <artifactId>spring-cloud-starter-zookeeper-discoveryartifactId>
    24. <exclusions>
    25. <exclusion>
    26. <groupId>org.apache.zookeepergroupId>
    27. <artifactId>zookeeperartifactId>
    28. exclusion>
    29. exclusions>
    30. dependency>
    31. <dependency>
    32. <groupId>org.apache.zookeepergroupId>
    33. <artifactId>zookeeperartifactId>
    34. <version>3.4.9version>
    35. dependency>
    36. <dependency>
    37. <groupId>org.springframework.bootgroupId>
    38. <artifactId>spring-boot-devtoolsartifactId>
    39. <scope>runtimescope>
    40. <optional>trueoptional>
    41. dependency>
    42. <dependency>
    43. <groupId>org.projectlombokgroupId>
    44. <artifactId>lombokartifactId>
    45. <optional>trueoptional>
    46. dependency>
    47. <dependency>
    48. <groupId>org.springframework.bootgroupId>
    49. <artifactId>spring-boot-starter-testartifactId>
    50. <scope>testscope>
    51. dependency>
    52. dependencies>
    53. project>

     yml文件

    1. #8004表示注册到zookeeper服务器的支付服务提供者端口号
    2. server:
    3. port: 8004
    4. #服务别名----注册zookeeper到注册中心名称
    5. spring:
    6. application:
    7. name: cloud-provider-payment
    8. cloud:
    9. zookeeper:
    10. connect-string: 192.168.111.144:2181

     主启动类

    1. @SpringBootApplication
    2. @EnableDiscoveryClient //该注解用于向使用consul或者zookeeper作为注册中心时注册服务
    3. public class PaymentMain8004
    4. {
    5. public static void main(String[] args)
    6. {
    7. SpringApplication.run(PaymentMain8004.class,args);
    8. }
    9. }

    Controller 类

    1. @RestController
    2. public class PaymentController
    3. {
    4. @Value("${server.port}")
    5. private String serverPort;
    6. @RequestMapping(value = "/payment/zk")
    7. public String paymentzk()
    8. {
    9. return "springcloud with zookeeper: "+serverPort+"\t"+ UUID.randomUUID().toString();
    10. }
    11. }

    验证测试 

     http://localhost:8004/payment/zk

     

     

    服务消费者 

     新建cloud-consumerzk-order80

    pom文件 

    1. <project xmlns="http://maven.apache.org/POM/4.0.0"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <parent>
    5. <artifactId>mscloudartifactId>
    6. <groupId>com.atguigu.springcloudgroupId>
    7. <version>1.0-SNAPSHOTversion>
    8. parent>
    9. <modelVersion>4.0.0modelVersion>
    10. <artifactId>cloud-consumerzk-order81artifactId>
    11. <dependencies>
    12. <dependency>
    13. <groupId>org.springframework.bootgroupId>
    14. <artifactId>spring-boot-starter-webartifactId>
    15. dependency>
    16. <dependency>
    17. <groupId>org.springframework.cloudgroupId>
    18. <artifactId>spring-cloud-starter-zookeeper-discoveryartifactId>
    19. <exclusions>
    20. <exclusion>
    21. <groupId>org.apache.zookeepergroupId>
    22. <artifactId>zookeeperartifactId>
    23. exclusion>
    24. exclusions>
    25. dependency>
    26. <dependency>
    27. <groupId>org.apache.zookeepergroupId>
    28. <artifactId>zookeeperartifactId>
    29. <version>3.4.9version>
    30. dependency>
    31. <dependency>
    32. <groupId>org.springframework.bootgroupId>
    33. <artifactId>spring-boot-devtoolsartifactId>
    34. <scope>runtimescope>
    35. <optional>trueoptional>
    36. dependency>
    37. <dependency>
    38. <groupId>org.projectlombokgroupId>
    39. <artifactId>lombokartifactId>
    40. <optional>trueoptional>
    41. dependency>
    42. <dependency>
    43. <groupId>org.springframework.bootgroupId>
    44. <artifactId>spring-boot-starter-testartifactId>
    45. <scope>testscope>
    46. dependency>
    47. dependencies>
    48. project>

     yml文件

    1. server:
    2. port: 80
    3. spring:
    4. application:
    5. name: cloud-consumer-order
    6. cloud:
    7. #注册到zookeeper地址
    8. zookeeper:
    9. connect-string: 192.168.231.1362181

      主启动类

    1. @SpringBootApplication
    2. @EnableDiscoveryClient
    3. public class OrderZK80 {
    4. public static void main(String[] args) {
    5. SpringApplication.run(OrderZK80.class,args);
    6. }
    7. }

     配置Bean

    1. @Configuration
    2. public class ApplicationContextBean
    3. {
    4. @Bean
    5. @LoadBalanced//负载均衡,虽然在这里没有用到
    6. public RestTemplate getRestTemplate()
    7. {
    8. return new RestTemplate();
    9. }
    10. }

    Controler 类

    1. @RestController
    2. public class OrderZKController
    3. {
    4. public static final String INVOKE_URL = "http://cloud-provider-payment";
    5. @Autowired
    6. private RestTemplate restTemplate;
    7. @RequestMapping(value = "/consumer/payment/zk")
    8. public String paymentInfo()
    9. {
    10. String result = restTemplate.getForObject(INVOKE_URL+"/payment/zk", String.class);
    11. System.out.println("消费者调用支付服务(zookeeper)--->result:" + result);
    12. return result;
    13. }
    14. }

     验证测试

     

    http://localhost/consumer/payment/zk

  • 相关阅读:
    SpringSecurity知识点总结-DX的笔记
    1109. 航班预订统计
    计算机毕业设计JavaOA办公系统设计与实现(源码+系统+mysql数据库+lw文档)
    webpack自定义loader
    【Python3】【力扣题】169. 多数元素
    Cadence Allegro PCB设计88问解析(十三) 之 Allegro中artwork层的建立
    1024网络技术命令汇总(第54课)
    java计算机毕业设计ssm企业日常事务管理系统sl5xl(附源码、数据库)
    上位机与MES对接的常见方式
    mysql语句的执行流程以及集群的高可用
  • 原文地址:https://blog.csdn.net/m0_62436868/article/details/126264316