# 使用Ribbon实现负载均衡

  • Ribbon是什么
  • 引入Ribbon后的架构演进
  • 整合Ribbon实现负载均衡

# 导航

回到spring cloud alibaba导航页

# Ribbon是什么?

  • Netflix开源的客户端侧负载均衡器
  • Ribbon为我们提供了非常丰富的负载均衡算法

# 架构的演进

服务消费者集成Ribbon之后,Ribbon会自动的从Nacos Server获取想要调用的服务列表,Ribbon会用负载均衡算法,计算出一个实例,然后交给RestTemplate去请求 引入Ribbon的架构演进

# 整合Ribbon

# 第一步:加依赖

因为spring-cloud-starter-alibaba-nacos-discovery已经包含了Ribbon,所以就不需要加依赖了

ribbon

# 第二步:写注解

注解需要写在:RestTemplate那里

package com.itmuch.contentcenter;

import lombok.Builder;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
import tk.mybatis.spring.annotation.MapperScan;

//MapperScan注解作用:扫描mybatis哪些包里面的接口
@MapperScan(basePackages = "com.itmuch")
@SpringBootApplication
public class ContentCenterApplication {

    public static void main(String[] args) {
        SpringApplication.run(ContentCenterApplication.class, args);
    }
    //@Bean 在spring容器中,创建一个对象,名称/ID是:方法名
    //<bean id="restTemplate" class="xxx.RestTemplate">
    //@LoadBalanced : 让RestTemplate整合了Ribbon
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

# 第三步:写配置

没有配置,不用写配置

# 使用Ribbon作为负载均衡请求的方法示例

package com.itmuch.contentcenter.service.content;

import com.itmuch.contentcenter.dao.share.ShareMapper;
import com.itmuch.contentcenter.domain.dto.content.ShareDTO;
import com.itmuch.contentcenter.domain.dto.user.UserDTO;
import com.itmuch.contentcenter.domain.entity.share.Share;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.naming.ldap.PagedResultsControl;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;

/**
 * @ClassName:ShareService
 * @Description:
 * @author: Torey
 */
@Slf4j
@Service
public class ShareService {
    @Autowired
    private ShareMapper shareMapper;
    @Autowired
    private RestTemplate restTemplate;
    public ShareDTO findById(Integer id){
        Share share = shareMapper.selectByPrimaryKey(id);
        Integer userId = share.getUserId();
        //当restTemplate去请求的时候,Ribbon会自动的把user-center这个名称转换成用户中心在Nacos上的地址,
        //并且根据负载均衡算法,返回一个实例给restTemplate去请求
        UserDTO userDTO= restTemplate.getForObject("http://user-center/users/{userId}",
                UserDTO.class,
                userId);
// 消息的装配
        ShareDTO shareDTO=new ShareDTO();
        BeanUtils.copyProperties(share,shareDTO);
        shareDTO.setWxNickname(userDTO.getWxNickname());
        return shareDTO;
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

# 导航,上一页,下一页

上一页
下一页

# 支持我-微信扫一扫-加入微信公众号

Aseven公众号

# 赞赏作者

赞赏作者