其他分享
首页 > 其他分享> > 第四章 AlibabaCloud集成Ribbon实现负载均衡

第四章 AlibabaCloud集成Ribbon实现负载均衡

作者:互联网

1.什么负载均衡和常见的解决方案

2.AlibabaCloud集成Ribbon实现负载均衡

@SpringBootApplication
@EnableDiscoveryClient
public class OrderApplication {
    
    public static void main(String [] args){
        SpringApplication.run(OrderApplication.class,args);
    }
    
    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}
@RestController
@RequestMapping("api/v1/video")
public class VideoController {

    @Autowired
    private VideoService videoService;

    @RequestMapping("find_by_id")
    public Object findVideoById(int videoId, HttpServletRequest request){
        Video video = videoService.findById(videoId);
        video.setServerInfo(request.getServerName()+":"+request.getServerPort());
        return video;
    }

}
@RestController
@RequestMapping("api/v1/video_order")
public class OrderController {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private DiscoveryClient discoveryClient;
    @RequestMapping("/save")
    public Object save(int videoId){
        //Video video = restTemplate.getForObject("http://localhost:9000/api/v1/video/find_by_id?videoId="+videoId, Video.class);
        //List<ServiceInstance> list =  discoveryClient.getInstances("xdclass-video-service");
        //ServiceInstance serviceInstance = list.get(0);
        Video video = restTemplate.getForObject("http://xdclass-video-service/api/v1/video/find_by_id?videoId="+videoId, Video.class);
        VideoOrder videoOrder = new VideoOrder();
        videoOrder.setVideoId(video.getId());
        videoOrder.setVideoTitle(video.getTitle());
        videoOrder.setCreateTime(new Date());
        videoOrder.setServerInfo(video.getServerInfo());
        return videoOrder;
    }
}

标签:负载,videoId,videoOrder,video,均衡,public,Ribbon,AlibabaCloud
来源: https://blog.csdn.net/qq_15906903/article/details/122756273