springcloud中使用ribbon的负载均衡会遇到的坑

加入依赖

<!--        负载均衡依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
        </dependency>

入口函数中

package com.xiaoyu;

import com.alibaba.cloud.nacos.loadbalancer.ConditionalOnLoadBalancerNacos;
import org.mybatis.spring.annotation.MapperScan;
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;

@SpringBootApplication
@MapperScan("com.xiaoyu.mapper")
@EnableDiscoveryClient
public class OrderApplication {
    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class, args);
    }
    @Bean//添加nacos客户端Bean
    @LoadBalanced//负载均衡注解
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

Controller中

package com.xiaoyu.controller;

import com.alibaba.cloud.nacos.loadbalancer.ConditionalOnLoadBalancerNacos;
import com.xiaoyu.entity.Order;
import com.xiaoyu.entity.Product;
import com.xiaoyu.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.List;
import java.util.Random;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private DiscoveryClient discoveryClient;
    @GetMapping("/prod/{pid}")
    public Order order(@PathVariable("pid") String pid) {
        //从服务中心获取请求的地址
//        List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
        //这里写死不恰当,所以定义随机数,相当于自定义负载均衡
//        int index = new Random().nextInt(instances.size());
//        System.out.println("index = " + index);
//        ServiceInstance instance = instances.get(index);
//        String host = instance.getHost();
//        System.out.println("host = " + host);
//        int port = instance.getPort();
//        System.out.println("port = " + port);
        String url = "http://service-product/product/" + pid;
        System.out.println("url = " + url);
        Product product = restTemplate.getForObject(url, Product.class);
//        Product product = restTemplate.getForObject("http://localhost:8072/product/" + pid, Product.class);
        log.info("product = " + product);
        Order order = Order.builder().uid(1L).username("张三").pid(product.getId()).pname(product.getName()).price(product.getPrice()).number(1).build();
//        orderService.save(order);
        return order;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值