采用随机负载均衡策略,四线服务之间的调用
2个用户中心,1个内容中心,内容中心调用用户中心服务
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);//发布人idInteger userId = share.getUserId();//怎么调用用户微服务的/users/{userId}呢?List<ServiceInstance> instances = discoveryClient.getInstances("user-center");List<String> targetURLList = instances.stream()//数据变换.map(instance -> instance.getUri().toString() + "/users/{id}").collect(Collectors.toList());int i = ThreadLocalRandom.current().nextInt(targetURLList.size());log.info("请求的目标地址:{}", 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实现负载均衡
Ribbon是什么?
引入Ribbon后的架构严谨
整合Ribbon实现负载均衡
添加依赖
此依赖中已包括
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency>
启动类加注解
@Bean@LoadBalancedpublic RestTemplate restTemplate(){return new RestTemplate();}
写配置(无)
Ribbon重构后,负载均衡效果
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);//发布人idInteger userId = share.getUserId();//怎么调用用户微服务的/users/{userId}呢?//Ribbo重构后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;}
}