當(dāng)前位置:
首頁(yè) >
前端技术
> javascript
>内容正文
javascript
03_SpringCloud整合Ribbon实现负载均衡
生活随笔
收集整理的這篇文章主要介紹了
03_SpringCloud整合Ribbon实现负载均衡
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
采用隨機(jī)負(fù)載均衡策略,四線服務(wù)之間的調(diào)用
2個(gè)用戶中心,1個(gè)內(nèi)容中心,內(nèi)容中心調(diào)用用戶中心服務(wù)
使用Ribbon實(shí)現(xiàn)負(fù)載均衡
Ribbon是什么?
引入Ribbon后的架構(gòu)嚴(yán)謹(jǐn)
整合Ribbon實(shí)現(xiàn)負(fù)載均衡
添加依賴
此依賴中已包括
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency>啟動(dòng)類加注解
@Bean@LoadBalancedpublic RestTemplate restTemplate(){return new RestTemplate();}寫配置(無)
Ribbon重構(gòu)后,負(fù)載均衡效果
package com.itmuch.contentcenter.service.impl;import com.itmuch.contentcenter.dao.content.ShareMapper; import com.itmuch.contentcenter.domain.entity.content.Share; import com.itmuch.contentcenter.dto.ShareDTO; import com.itmuch.contentcenter.dto.UserDTO; import com.itmuch.contentcenter.service.IShareService; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate;/*** @author gblfy* @ClassNme ShareServiceImpl* @Description TODO* @Date 2019/7/6 17:58* @version1.0*/ @Slf4j @Service @RequiredArgsConstructor(onConstructor = @__(@Autowired)) public class ShareServiceImpl implements IShareService {private final ShareMapper shareMapper;private final RestTemplate restTemplate;@Overridepublic ShareDTO findById(Integer id) {//獲取分享詳情Share share = this.shareMapper.selectByPrimaryKey(id);//發(fā)布人idInteger userId = share.getUserId();//怎么調(diào)用用戶微服務(wù)的/users/{userId}呢?//Ribbo重構(gòu)后UserDTO userDTO = this.restTemplate.getForObject("http://user-center/users/{userId}", UserDTO.class, userId);//消息的裝配ShareDTO shareDTO = new ShareDTO();BeanUtils.copyProperties(share, shareDTO);shareDTO.setWxNickname(userDTO.getWxNickname());return shareDTO;} }總結(jié)
以上是生活随笔為你收集整理的03_SpringCloud整合Ribbon实现负载均衡的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Axis2搭建WebService服务
- 下一篇: 计算机硬件基础-存取方式