当前位置: 移动技术网 > IT编程>开发语言>Java > spring cloud系列学习(二、SpringCloud之服务注册之Ribbon负载均衡)

spring cloud系列学习(二、SpringCloud之服务注册之Ribbon负载均衡)

2020年07月22日  | 移动技术网IT编程  | 我要评论

 上个章节我们已经启动了服务生产者实例,这一章我们会启动四个不同端口的实例,然后如何通过负载平衡访问呢?这时就需要Ribbon,为了使用Ribbon,我们需要使用@LoadBalanced元注解,那么这个注解放在哪里呢?一般有两个DiscoveryClient 和 RestTemplate

然后选择spring web 、spring cloud discovery和spring cloud routing支持

下一步,生成目录

pom.xml文件内容自动生成

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.springcloud</groupId>
    <artifactId>ribbon-consumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>ribbon-consumer</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR6</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

修改整合ribbon 启动类RibbonConsumerApplication 加入注解

@EnableDiscoveryClient 注册到eureka注册中心
@Bean  加入ioc容器
@LoadBalanced  让restemplate具有负载均衡能力
package com.springcloud.ribbonconsumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@EnableDiscoveryClient
@SpringBootApplication
public class RibbonConsumerApplication {
    @Bean
    @LoadBalanced
    RestTemplate restTemplate () {
        return new RestTemplate();
    }
    public static void main(String[] args) {
        SpringApplication.run(RibbonConsumerApplication.class, args);
    }

}

配置eureka参数

spring.application.name=eureka-consumer
server.port=8083
## 心跳时间,即服务续约间隔时间(缺省为30s)
#eureka.instance.lease-renewal-interval-in-seconds= 5
## 发呆时间,即服务续约到期时间(缺省为90s)
#eureka.instance.lease-expiration-duration-in-seconds= 15
## 开启健康检查(依赖spring-boot-starter-actuator)
#eureka.client.healthcheck.enabled=true
eureka.client.serviceUrl.defaultZone=http://www.lhsxpumps.com/_localhost:3456/eureka/

新增controller文件夹,加入ConsumerController接口

package com.springcloud.ribbonconsumer.Controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class ConsumerController {

    @Autowired
    RestTemplate restTemplate;

    @RequestMapping(value ="/ribbon-consumer", method = RequestMethod.GET)
    public String helloConsumer() {
       return restTemplate.getForEntity("http://eureka-client/hello", 
                String.class).getBody();
    }
 
}

其中http://www.lhsxpumps.com/_eureka-client/hello",中的eureka-client为注册在eureka注册中心的实例名称

hello为服务实例中提供的接口名,上章文章中服务实例没有这个接口,我们去添加一下

package com.springcloud.eurekaclient.Controller;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.logging.Logger;

@RestController
public class HelloController {
    @Value("${server.port}")
    String port;


    @RequestMapping(value = "hello", method = RequestMethod.GET)
    public String index() {
        StringBuffer uriList = new StringBuffer("Hello World " + port + " 端口为您服务!<br>");
        return uriList.toString();
    }
}

然后新增两个资源文件--为了同一个moudle可以启动多次(根据不同的端口启动)

spring.application.name=eureka-client
server.port=8081
# 心跳时间,即服务续约间隔时间(缺省为30s)
eureka.instance.lease-renewal-interval-in-seconds= 5
# 发呆时间,即服务续约到期时间(缺省为90s)
eureka.instance.lease-expiration-duration-in-seconds= 15
# 开启健康检查(依赖spring-boot-starter-actuator)
eureka.client.healthcheck.enabled=true
eureka.client.serviceUrl.defaultZone=http://localhost:3456/eureka/

spring.application.name=eureka-client
server.port=8082
# 心跳时间,即服务续约间隔时间(缺省为30s)
eureka.instance.lease-renewal-interval-in-seconds= 5
# 发呆时间,即服务续约到期时间(缺省为90s)
eureka.instance.lease-expiration-duration-in-seconds= 15
# 开启健康检查(依赖spring-boot-starter-actuator)
eureka.client.healthcheck.enabled=true
eureka.client.serviceUrl.defaultZone=http://www.lhsxpumps.com/_localhost:3456/eureka/

选择填写三处内容

新增EurekaclientApplication1\EurekaclientApplication2两个后最终为

此时可以分别启动EurekaServerApplication(服务注册中心)、EurekaclientApplication(实例1)、EurekaclientApplication1(实例2)、EurekaclientApplication2(实例3)、RibbonConsumerApplication(Ribbon请求入口)

再次访问http://localhost:3456/服务注册中心,就会发现三个client实例,一个EUREKA-CONSUMER

然后访问http://localhost:8083/ribbon-consumer

这是会根据注册的三个实例轮询请求

本文是通过RestTemplate进行访问,我们还可以通过DiscoveryClient 进行访问

1、获取实例集合,我们可以轮询或者根据自己要求去灵活访问

List<ServiceInstance> instances=discoveryClient.getInstances("eureka-client");
ServiceInstance serviceInstance=instances.get(0);

本文地址:https://blog.csdn.net/fsdad/article/details/107493295

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网