在使用Ribbon調用廣告投放系統API以前,咱們須要先建立2個VO對象,AdPlanVO
,AdPlanGetRequestVO
.java
//數據請求對象 @Data @NoArgsConstructor @AllArgsConstructor public class AdPlanGetRequestVO { private Long userId; private List<Long> planIds; } ---------------------------------- //API請求響應結果反序列化對象 @Data @AllArgsConstructor @NoArgsConstructor public class AdPlanVO { private Long planId; private Long userId; private String planName; private Integer planStatus; private Date startDate; private Date endDate; private Date createTime; private Date updateTime; }
在AdSearchApplication
啓動類中,添加RestTemplate
客戶端。app
public class AdSearchApplication { ... /** * 註冊{@link RestTemplate}Bean * @return */ @Bean @LoadBalanced //讓RestTemplate在調用服務的時候,能夠實現負載均衡 RestTemplate restTemplate(){ return new RestTemplate(); } }
建立一個controller,來測試調用廣告提供系統的API負載均衡
/** * SearchController for search information controller * * @author <a href="mailto:magicianisaac@gmail.com">Isaac.Zhang | 若初</a> */ @RestController @Slf4j @RequestMapping(path = "/search") public class SearchController { //注入RestTemplate private final RestTemplate restTemplate; @Autowired public SearchController(RestTemplate restTemplate) { this.restTemplate = restTemplate; } @GetMapping(path = "/plan/get-ribbon") public CommonResponse<List<AdPlanVO>> getAdPlansUseRibbon(@RequestBody AdPlanGetRequestVO requestVO) { log.info("ad-search::getAdPlansUseRibbon -> {}", JSON.toJSONString(requestVO)); return restTemplate.postForEntity( "http://mscx-ad-sponsor/ad-sponsor/plan/get", requestVO, CommonResponse.class ).getBody(); } @GetMapping(path = "/user/get") public CommonResponse getUsers(@Param(value = "username") String username) { log.info("ad-search::getUsers -> {}", JSON.toJSONString(username)); CommonResponse commonResponse = restTemplate.getForObject( "http://mscx-ad-sponsor/ad-sponsor/user/get?username={username}", CommonResponse.class, username ); return commonResponse; } }