日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

03_SpringCloud整合Ribbon实现负载均衡

發(fā)布時(shí)間:2024/9/27 javascript 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 03_SpringCloud整合Ribbon实现负载均衡 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

采用隨機(jī)負(fù)載均衡策略,四線服務(wù)之間的調(diào)用

2個(gè)用戶中心,1個(gè)內(nèi)容中心,內(nèi)容中心調(diào)用用戶中心服務(wù)

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.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate;import java.util.List; import java.util.concurrent.ThreadLocalRandom; import java.util.stream.Collectors;/*** @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;private final DiscoveryClient discoveryClient;@Overridepublic ShareDTO findById(Integer id) {//獲取分享詳情Share share = this.shareMapper.selectByPrimaryKey(id);//發(fā)布人idInteger userId = share.getUserId();//怎么調(diào)用用戶微服務(wù)的/users/{userId}呢?List<ServiceInstance> instances = discoveryClient.getInstances("user-center");List<String> targetURLList = instances.stream()//數(shù)據(jù)變換.map(instance -> instance.getUri().toString() + "/users/{id}").collect(Collectors.toList());int i = ThreadLocalRandom.current().nextInt(targetURLList.size());log.info("請(qǐng)求的目標(biāo)地址:{}", targetURLList.get(i));UserDTO userDTO = this.restTemplate.getForObject(targetURLList.get(i), UserDTO.class, userId);//消息的裝配ShareDTO shareDTO = new ShareDTO();BeanUtils.copyProperties(share, shareDTO);shareDTO.setWxNickname(userDTO.getWxNickname());return shareDTO;} }

使用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();}

寫配置(無(wú))

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)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。