SpringCloud:使用spring cloud LoadBalancer作为负载均衡器并使用随机策略

SpringCloud:使用spring cloud LoadBalancer作为负载均衡器并使用随机策略,第1张

SpringCloud:使用spring cloud LoadBalancer作为负载均衡器并使用随机策略

1.在配置文件里进行配置:

spring.cloud.loadbalancer.ribbon.enabled=false

完整的application.properties如下:

server.port=8088
spring.application.name=springboot
spring.datasource.url=jdbc:mysql://139.198.xx.xx/xxx
spring.datasource.username=root
spring.datasource.password=MyPassword
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
eureka.client.serviceUrl.defaultZone=http://139.198.xx.xx:8761/eureka/
eureka.instance.preferIpAddress=true
spring.cloud.loadbalancer.ribbon.enabled=false
spring.cloud.loadbalancer.enabled=true



2.在pom.xml中增加load balancer依赖

        
            org.springframework.cloud
            spring-cloud-starter-loadbalancer
        

完整pom.xml如下:



    4.0.0

    cn.edu.tju
    springcloudloadbalancer
    1.0-SNAPSHOT

    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.7.RELEASE
         
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Hoxton.SR12
                pom
                import
            
        
    

    
        
            org.springframework.boot
            spring-boot-starter-web
            
                
                    org.slf4j
                    slf4j-log4j12
                
            
        





        
            com.google.code.gson
            gson
            2.8.6
        

        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.2
        

        
            org.springframework.cloud
            spring-cloud-starter-loadbalancer
        

        
            mysql
            mysql-connector-java
            5.1.30
        





        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
            2.1.0.RELEASE


        




    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



3.配置RestTemplate

package cn.edu.tju.config;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RestConfig {
    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
        return new RestTemplate();

    }
}

4.创建随机策略配置类:

package cn.edu.tju.config;

import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.loadbalancer.core.RandomLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ReactorLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;


public class LoadBalancerConfig {

    @Bean
    ReactorLoadBalancer randomLoadBalancer(Environment environment,
                                                            LoadBalancerClientFactory loadBalancerClientFactory) {
        String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
        return new RandomLoadBalancer(loadBalancerClientFactory
                .getLazyProvider(name, ServiceInstanceListSupplier.class),
                name);
    }
}

5.在启动类加注解:

@LoadBalancerClient(name = "demoservice", configuration = LoadBalancerConfig.class)

其中name属性为调用的微服务的名称,configuration属性为上一步定义的随机负载均衡策略类
6.创建控制器类,通过RestTemplate来调用微服务:

package cn.edu.tju.controller;

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

@RestController
public class TestController {
    @Autowired
    private RestTemplate restTemplate;
    @RequestMapping("/hi")
    public String test(){
        String result="";
        for(int i=0;i<100;i++){
            result=restTemplate.getForObject("http://demoservice/test",String.class);
            System.out.println(result);
        }
        return result;
    }
}

其中demoservice是注册到eureka中的一个微服务
7.访问http://localhost:8088/hi,截图如下,可以看到是随机访问了两个微服务实例

8.如果想换成轮询(RoundRobin)策略,只需创建配置类,并在启动类上修改配置类名:

package cn.edu.tju.config;

import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.loadbalancer.core.RandomLoadBalancer;
import org.springframework.cloud.loadbalancer.core.RoundRobinLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ReactorLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;


public class LoadBalancerConfig2 {

    @Bean
    ReactorLoadBalancer roundLoadBalancer(Environment environment,
                                                            LoadBalancerClientFactory loadBalancerClientFactory) {
        String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
        return new RoundRobinLoadBalancer(loadBalancerClientFactory
                .getLazyProvider(name, ServiceInstanceListSupplier.class),
                name);
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5719855.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-18
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存