• Ribbon实现Cloud负载均衡


    安装Zookeeper要先安装JDK环境

    解压

    tar -zxvf /usr/local/develop/jdk-8u191-linux-x64.tar.gz -C /usr/local/develop

    配置JAVA_HOME

    vim /etc/profile

    export JAVA_HOME=/usr/local/develop/jdk1.8.0_191

    export PATH=$JAVA_HOME/bin:$PATH

    export CLASSPATH=.:$JAVA_HOME/lib

    让环境变量生效

    source /etc/profile

    java -version 查看jdk版本 至此JDK安装完成

    which java 查看调用的是安装在哪里的java

    做Zookeeper做服务治理(安装Zookeeper)

    Apache ZooKeepericon-default.png?t=N7T8https://zookeeper.apache.org/

    进入Zookeeper 包目录

    cd /usr/local/develop/

    解压

    tar -zxvf apache-zookeeper-3.9.1-bin.tar.gz -C /usr/local/develop

    进入配置文件  

    cd /usr/local/develop/apache-zookeeper-3.9.1-bin/conf

    复制文件

    cp zoo_sample.cfg zoo.cfg

    编辑文件 

    vim zoo.cfg

    现在可以进入bin目录直接启动

    cd /usr/local/develop/apache-zookeeper-3.9.1-bin/bin

    启动Zookeeper

    ./zkServer.sh start

    如果需要配置Zookeeper为系统服务

    vim /etc/systemd/system/zookeeper.service

    [Unit]
    Description=Apache ZooKeeper server
    After=network.target

    [Service]
    Type=forking
    ExecStart=/usr/local/develop/apache-zookeeper-3.9.1-bin/bin/zkServer.sh start
    ExecStop=/usr/local/develop/apache-zookeeper-3.9.1-bin/bin/zkServer.sh stop
    User=root
    Group=root
    Restart=on-failure
    Environment="JAVA_HOME=/usr/local/develop/jdk1.8.0_191"

    [Install]
    WantedBy=multi-user.target

    是配置生效

    systemctl daemon-reload

    开机自启  看自己实际需要

    systemctl enable zookeeper.service

    systemctl start zookeeper.service    启动
    systemctl stop zookeeper.service    停止
    systemctl restart zookeeper.service    重启
    systemctl status zookeeper.service   查看状态

    admin.serverPort=8888指定了ZooKeeper的管理服务器端口。这个管理服务器提供了一个简单的HTTP接口,用于获取ZooKeeper服务的状态和性能指标等信息。通过访问这个端口,你可以获取到ZooKeeper实例的各种管理信息,比如运行状态、连接数、节点数量等。

    默认情况下,ZooKeeper的管理界面并不提供一个全面的Web界面来浏览这些信息,而是提供了一个简单的HTTP服务,通过发送HTTP请求到这个端口,可以获取到JSON格式的状态信息。

    IP:8888/commands/stat

    搭建Cloud项目

    创建父项目,只做依赖版本管理

    pom.xml

    1. <!-- 统一管理jar包版本 -->
    2. <properties>
    3. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    4. <maven.compiler.source>1.8</maven.compiler.source>
    5. <maven.compiler.target>1.8</maven.compiler.target>
    6. <junit.version>4.12</junit.version>
    7. <log4j.version>1.2.17</log4j.version>
    8. <lombok.version>1.16.18</lombok.version>
    9. <mysql.version>5.1.47</mysql.version>
    10. <druid.version>1.1.16</druid.version>
    11. <mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version>
    12. </properties>
    13. <!-- 子模块继承之后,提供作用:锁定版本+子modlue不用写groupId和version -->
    14. <dependencyManagement>
    15. <dependencies>
    16. <!--spring boot 2.2.2-->
    17. <dependency>
    18. <groupId>org.springframework.boot</groupId>
    19. <artifactId>spring-boot-dependencies</artifactId>
    20. <version>2.2.2.RELEASE</version>
    21. <type>pom</type>
    22. <scope>import</scope>
    23. </dependency>
    24. <!--spring cloud Hoxton.SR1-->
    25. <dependency>
    26. <groupId>org.springframework.cloud</groupId>
    27. <artifactId>spring-cloud-dependencies</artifactId>
    28. <version>Hoxton.SR1</version>
    29. <type>pom</type>
    30. <scope>import</scope>
    31. </dependency>
    32. <!--spring cloud alibaba 2.1.0.RELEASE-->
    33. <dependency>
    34. <groupId>com.alibaba.cloud</groupId>
    35. <artifactId>spring-cloud-alibaba-dependencies</artifactId>
    36. <version>2.1.0.RELEASE</version>
    37. <type>pom</type>
    38. <scope>import</scope>
    39. </dependency>
    40. <dependency>
    41. <groupId>mysql</groupId>
    42. <artifactId>mysql-connector-java</artifactId>
    43. <version>${mysql.version}</version>
    44. </dependency>
    45. <dependency>
    46. <groupId>com.alibaba</groupId>
    47. <artifactId>druid</artifactId>
    48. <version>${druid.version}</version>
    49. </dependency>
    50. <dependency>
    51. <groupId>org.mybatis.spring.boot</groupId>
    52. <artifactId>mybatis-spring-boot-starter</artifactId>
    53. <version>${mybatis.spring.boot.version}</version>
    54. </dependency>
    55. <dependency>
    56. <groupId>junit</groupId>
    57. <artifactId>junit</artifactId>
    58. <version>${junit.version}</version>
    59. </dependency>
    60. <dependency>
    61. <groupId>log4j</groupId>
    62. <artifactId>log4j</artifactId>
    63. <version>${log4j.version}</version>
    64. </dependency>
    65. <dependency>
    66. <groupId>org.projectlombok</groupId>
    67. <artifactId>lombok</artifactId>
    68. <version>${lombok.version}</version>
    69. <optional>true</optional>
    70. </dependency>
    71. </dependencies>
    72. </dependencyManagement>
    73. <build>
    74. <plugins>
    75. <plugin>
    76. <groupId>org.springframework.boot</groupId>
    77. <artifactId>spring-boot-maven-plugin</artifactId>
    78. <version>2.2.2.RELEASE</version>
    79. <configuration>
    80. <fork>true</fork>
    81. <addResources>true</addResources>
    82. </configuration>
    83. </plugin>
    84. </plugins>
    85. </build>

    生产者模块

    pom.xml

    1. <dependencies>
    2. <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
    3. <dependency>
    4. <groupId>org.springframework.boot</groupId>
    5. <artifactId>spring-boot-starter-web</artifactId>
    6. </dependency>
    7. <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-zookeeper-discovery -->
    8. <dependency>
    9. <groupId>org.springframework.cloud</groupId>
    10. <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
    11. </dependency>
    12. <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
    13. <dependency>
    14. <groupId>org.springframework.boot</groupId>
    15. <artifactId>spring-boot-devtools</artifactId>
    16. <scope>runtime</scope>
    17. <optional>true</optional>
    18. </dependency>
    19. <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
    20. <dependency>
    21. <groupId>org.projectlombok</groupId>
    22. <artifactId>lombok</artifactId>
    23. <optional>true</optional>
    24. </dependency>
    25. <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
    26. <dependency>
    27. <groupId>org.springframework.boot</groupId>
    28. <artifactId>spring-boot-starter-test</artifactId>
    29. <scope>test</scope>
    30. </dependency>
    31. </dependencies>

    application.yml

    1. server:
    2. port: 8081
    3. spring:
    4. application:
    5. name: cloud-provider-payment
    6. cloud:
    7. zookeeper:
    8. connect-string: 222.22.22.2:2181

    启动类

    业务类

    按相同套路 再创建个8082

    消费者模块

    依赖

    1. <dependencies>
    2. <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
    3. <dependency>
    4. <groupId>org.springframework.boot</groupId>
    5. <artifactId>spring-boot-starter-web</artifactId>
    6. </dependency>
    7. <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-zookeeper-discovery -->
    8. <dependency>
    9. <groupId>org.springframework.cloud</groupId>
    10. <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
    11. </dependency>
    12. <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
    13. <dependency>
    14. <groupId>org.springframework.boot</groupId>
    15. <artifactId>spring-boot-devtools</artifactId>
    16. <scope>runtime</scope>
    17. <optional>true</optional>
    18. </dependency>
    19. <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
    20. <dependency>
    21. <groupId>org.projectlombok</groupId>
    22. <artifactId>lombok</artifactId>
    23. <optional>true</optional>
    24. </dependency>
    25. <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
    26. <dependency>
    27. <groupId>org.springframework.boot</groupId>
    28. <artifactId>spring-boot-starter-test</artifactId>
    29. <scope>test</scope>
    30. </dependency>
    31. </dependencies>

    启动类

    业务类

    配置类

    虽然没有直接引入Ribbon  Ribbon依赖通过以下依赖已经在了

     
                org.springframework.cloud
                spring-cloud-starter-zookeeper-discovery

    application.yml

    如何改变Ribbon负载均衡策略

    啥意思??

    就是要放在启动类扫描不到的地方

    重启8083消费者  可以自己多点点   现在是随机的

    如果需要恢复轮询可以将启动类上的@RibbonClient注释掉

    这样即使MySlefRule这个类存在,也没有效果   因为在启动类所在包以外

    Ribbon负载均衡原理

    负载均衡算法:rest接口第几次请求数%服务器集群总数量=实际调用服务器位置下标,每次服务重启后Rest接口计数从1开始

    上面 生产者总共2个实例

    List   2个实例

    index=1

    1%2=1    这个1  做为list.get(1%2)

    index变成2

    2%2=0   这个0  做为list.get(1%2)

    index变成3

    3%2=1   这个1  做为list.get(3%2)

    index变成4

    4%2=0   这个0  做为list.get(4%2)

    以此类推

  • 相关阅读:
    了解web框架
    linux-windows10使用wsl运行linux子系统
    SD模块上线切换-问题预判及对策清单
    MySQL (2)
    uniapp如何在页面中只展示一条随机数据
    八、kotlin的高阶函数
    MySQL优化、锁、总结常见问题
    Sumati GameFi生态纵览,神奇世界中的元素设计
    Linux常用命令
    SAP SMARTFORMS打印文本内容手动换行
  • 原文地址:https://blog.csdn.net/tiantiantbtb/article/details/136436212