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 ZooKeeperhttps://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

 <!-- 统一管理jar包版本 -->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <junit.version>4.12</junit.version>
        <log4j.version>1.2.17</log4j.version>
        <lombok.version>1.16.18</lombok.version>
        <mysql.version>5.1.47</mysql.version>
        <druid.version>1.1.16</druid.version>
        <mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version>
    </properties>

    <!-- 子模块继承之后,提供作用:锁定版本+子modlue不用写groupId和version  -->
    <dependencyManagement>
        <dependencies>
            <!--spring boot 2.2.2-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.2.2.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--spring cloud Hoxton.SR1-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--spring cloud alibaba 2.1.0.RELEASE-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.1.0.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql.version}</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>${druid.version}</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>${mybatis.spring.boot.version}</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
            </dependency>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok.version}</version>
                <optional>true</optional>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.2.2.RELEASE</version>
                <configuration>
                    <fork>true</fork>
                    <addResources>true</addResources>
                </configuration>
            </plugin>
        </plugins>
    </build>

生产者模块

pom.xml

 <dependencies>


        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-zookeeper-discovery -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>


    </dependencies>

application.yml

server:
  port: 8081

spring:
  application:
    name: cloud-provider-payment
  cloud:
    zookeeper:
      connect-string: 222.22.22.2:2181

启动类

业务类

按相同套路 再创建个8082

消费者模块

依赖

 <dependencies>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-zookeeper-discovery -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

启动类

业务类

配置类

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

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>

</dependency>

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)

以此类推

相关推荐
搬码后生仔43 分钟前
asp.net core webapi项目中 在生产环境中 进不去swagger
chrome·后端·asp.net
凡人的AI工具箱1 小时前
每天40分玩转Django:Django国际化
数据库·人工智能·后端·python·django·sqlite
Lx3522 小时前
Pandas数据重命名:列名与索引为标题
后端·python·pandas
小池先生2 小时前
springboot启动不了 因一个spring-boot-starter-web底下的tomcat-embed-core依赖丢失
java·spring boot·后端
小蜗牛慢慢爬行3 小时前
如何在 Spring Boot 微服务中设置和管理多个数据库
java·数据库·spring boot·后端·微服务·架构·hibernate
wm10434 小时前
java web springboot
java·spring boot·后端
龙少95435 小时前
【深入理解@EnableCaching】
java·后端·spring
溟洵7 小时前
Linux下学【MySQL】表中插入和查询的进阶操作(配实操图和SQL语句通俗易懂)
linux·运维·数据库·后端·sql·mysql
SomeB1oody10 小时前
【Rust自学】6.1. 定义枚举
开发语言·后端·rust
SomeB1oody10 小时前
【Rust自学】5.3. struct的方法(Method)
开发语言·后端·rust