Skip to content

Commit

Permalink
ribbon 配置文件实现负载均衡,前提是使用到@LoadBalanced
Browse files Browse the repository at this point in the history
  • Loading branch information
junguangbai committed May 28, 2020
1 parent e8b8f98 commit 448619d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.atguigu.springcloud.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;
Expand All @@ -12,7 +13,7 @@
public class ApplicationContextConfig {

@Bean
// @LoadBalanced
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ public CommonResult<Payment> create(Payment payment){
@GetMapping("/consumer/payment/get/{id}")
public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){

return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,CommonResult.class);
CommonResult result = restTemplate.getForObject(PAYMENT_URL + "/payment/get/" + id, CommonResult.class);
System.out.println("服务请求回来******"+result.getMessage());
return result;
}

@GetMapping("/consumer/custom/payment/get/{id}")
Expand Down
9 changes: 7 additions & 2 deletions cloud-consumer-order80/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,10 @@ eureka:
instance-id: order80
prefer-ip-address: true



#服务提供者的项目名称不可以小写,小写配置不起作用,亲测过
#负载均衡机制是在@LoadBalanced注解基础之上的,如果使用类文件达到负载均衡
#需要重写IRule接口并在主启动类上开启@RibbonClient(name = "CLOUD-PAYMENT-SERVICE",configuration = MySelfRule.class)
#CLOUD-PAYMENT-SERVICE:
# ribbon:
# NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
# com.netflix.loadbalancer.WeightedResponseTimeRule
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ eureka:
#eureka客户端向服务器发送心跳的时间间隔(默认30秒)
lease-renewal-interval-in-seconds: 1
#eruka服务器收到最后一次心跳等待时间上线(默认90秒)
lease-expiration-duration-in-seconds: 2
lease-expiration-duration-in-seconds: 2


0 comments on commit 448619d

Please sign in to comment.