三、springcloud负载均衡

本文详细介绍了微服务架构中如何通过RestTemplate、Ribbon及OpenFeign实现负载均衡,包括自定义轮询规则、服务调用及超时处理等关键环节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

微服务间调用实现负载均衡

客户端实现:

RestTemplate实现

配置注入:

@Configuration
public class ApplicationContextConfig {
    @Bean
    @LoadBalanced  // 开启
    public RestTemplate getResultTemplate() {
        return new RestTemplate();
    }
}

用法:

    public static final String PAYMENT_URL = "http://CLOUD-PAYMENT";
    @Resource
    private RestTemplate restTemplate;
    @GetMapping("/create")
    public CommonRestlt<Payment> create(@RequestBody Payment payment){
        return restTemplate.postForObject(PAYMENT_URL+"/payment/create",payment,CommonRestlt.class);
    }
ribbon+RestTemplate实现可自定义轮询规则

添加依赖:

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>
@SpringBootApplication
@EnableEurekaClient
@RibbonClient(name = "CLOUD-PAYMENT",configuration = MySelRule.class)
public class OrderMain {
    public static void main(String[] args) {
        SpringApplication.run(OrderMain.class,args);
    }
}

开启RibbonClient定义自己的规则

@Configuration
public class MySelRule {
    @Bean 
    public IRule myRule(){
        return new RandomRule();//随机  默认是轮询
    }
}

 

openfeign实现底层也是ribbon+RestTemplate

依赖:

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>

开启:

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class OrderOpenFeignMain {
    public static void main(String[] args) {
        SpringApplication.run(OrderOpenFeignMain.class,args);
    }
}

用法:定义接口把提供服务的接口copy过来

@Component
@FeignClient(value="CLOUD-PAYMENT",path = "/payment",fallback = PaymentServiceFallBack.class)
public interface PaymentService {
    @GetMapping("/queryById/{id}")
    CommonRestlt queryById(@PathVariable("id") Integer id);
    @GetMapping("/feignTimeout")
    String feignTimeout();
}

controller里面调用此接口内的方法,就会转发到提供的服务

@RestController
@RequestMapping("order")
@Slf4j
public class OrderController {
    @Resource
    private PaymentService paymentService;

    @GetMapping("/get/{id}")
    public CommonRestlt<Payment> get(@PathVariable("id") Integer id){
        System.out.println("************");
        return paymentService.queryById(id);
    }
    @GetMapping("/feignTimeout")
    public String feignTimeout(){
        return paymentService.feignTimeout();
    }
}

默认1秒内提供服务方未应答报错显示超时。此处设置2秒。

ribbon:
#  设置feign读取超时(默认是1秒)
  ReadTimeout: 2000
#  设置feign连接超时
  ConnectTimeout: 2000

定义fallback方法来处理发生异常的信息

public class PaymentServiceFallBack implements PaymentService {
    @Override
    public CommonRestlt queryById(Integer id) {
        return null;
    }
    @Override
    public String feignTimeout() {
        return "feignTimeout请求超时";
    }
}

 

引出后面的服务降级+服务熔断

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值