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 springcloudloadbalancer1.0-SNAPSHOT org.springframework.boot spring-boot-starter-parent2.3.7.RELEASE org.springframework.cloud spring-cloud-dependenciesHoxton.SR12 pom import org.springframework.boot spring-boot-starter-weborg.slf4j slf4j-log4j12com.google.code.gson gson2.8.6 org.mybatis.spring.boot mybatis-spring-boot-starter1.3.2 org.springframework.cloud spring-cloud-starter-loadbalancermysql mysql-connector-java5.1.30 org.springframework.cloud spring-cloud-starter-netflix-eureka-client2.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 ReactorLoadBalancerrandomLoadBalancer(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 ReactorLoadBalancerroundLoadBalancer(Environment environment, LoadBalancerClientFactory loadBalancerClientFactory) { String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME); return new RoundRobinLoadBalancer(loadBalancerClientFactory .getLazyProvider(name, ServiceInstanceListSupplier.class), name); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)