溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

[Spring cloud 一步步實(shí)現(xiàn)廣告系統(tǒng)] 10. 使用Ribbon 實(shí)現(xiàn)微服務(wù)調(diào)用

發(fā)布時(shí)間:2020-08-19 08:07:55 來源:網(wǎng)絡(luò) 閱讀:321 作者:zhangpan0614 欄目:編程語(yǔ)言

在使用Ribbon調(diào)用廣告投放系統(tǒng)API之前,我們需要先創(chuàng)建2個(gè)VO對(duì)象,AdPlanVO,AdPlanGetRequestVO.

//數(shù)據(jù)請(qǐng)求對(duì)象
@Data
@NoArgsConstructor
@AllArgsConstructor
public class AdPlanGetRequestVO {
    private Long userId;
    private List<Long> planIds;
}

----------------------------------

//API請(qǐng)求響應(yīng)結(jié)果反序列化對(duì)象
@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啟動(dòng)類中,添加RestTemplate客戶端。

public class AdSearchApplication {
  ...
    /**
     * 注冊(cè){@link RestTemplate}Bean
     * @return
     */
    @Bean
    @LoadBalanced //讓RestTemplate在調(diào)用服務(wù)的時(shí)候,可以實(shí)現(xiàn)負(fù)載均衡
    RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

創(chuàng)建一個(gè)controller,來測(cè)試調(diào)用廣告提供系統(tǒng)的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;
    }
}
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI