亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

使用Spring Cloud Feign遠(yuǎn)程調(diào)用的方法示例

 更新時(shí)間:2018年09月03日 14:22:59   作者:pomay  
這篇文章主要介紹了使用Spring Cloud Feign遠(yuǎn)程調(diào)用的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

在Spring Cloud Netflix棧中,各個(gè)微服務(wù)都是以HTTP接口的形式暴露自身服務(wù)的,因此在調(diào)用遠(yuǎn)程服務(wù)時(shí)就必須使用HTTP客戶端。我們可以使用JDK原生的URLConnection、Apache的Http Client、Netty的異步HTTP Client, Spring的RestTemplate。但是,用起來(lái)最方便、最優(yōu)雅的還是要屬Feign了。

Feign簡(jiǎn)介

Feign是一個(gè)聲明式的Web服務(wù)客戶端,使用Feign可使得Web服務(wù)客戶端的寫入更加方便。
它具有可插拔注釋支持,包括Feign注解和JAX-RS注解、Feign還支持可插拔編碼器和解碼器、Spring Cloud增加了對(duì)Spring MVC注釋的支持,并HttpMessageConverters在Spring Web中使用了默認(rèn)使用的相同方式。Spring Cloud集成了Ribbon和Eureka,在使用Feign時(shí)提供負(fù)載平衡的http客戶端。

Spring Cloud Feign簡(jiǎn)介參考:http://chabaoo.cn/article/133773.htm

根據(jù)專家學(xué)者提供的賬號(hào)密碼,要在用戶表注冊(cè)一個(gè)專家學(xué)者賬號(hào)(用戶和專家學(xué)者不同的數(shù)據(jù)庫(kù))

在userContorller.java寫一個(gè)方法:注冊(cè)專家學(xué)者賬號(hào)

/**
 * 專家學(xué)者注冊(cè)
 * 
 * @param username
 * @param password
 * @return
 */
 @ApiOperation(value = "專家學(xué)者注冊(cè)")
 @RequestMapping(value = "/registExpert", method = RequestMethod.POST)
 public long registExpert(@RequestParam("username") String username, @RequestParam("password") String password) {
 User user = new User();
 user.setUsername(username);
 user.setPassword(password);
 userService.insertSelective(user);
 long userId = user.getUserId();
 return userId;
 }

UserClient.java(這里的接口和要遠(yuǎn)程調(diào)用的controller方法聲明一樣(此處是UserController.java),可直接復(fù)制過(guò)來(lái),如下所示)

package com.lgsc.cjbd.expert.remote.client;
 
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
 
import com.lgsc.cjbd.vo.Response;
 
@FeignClient(name = "cjbd-user", fallback = UserClientFallback.class)
public interface UserClient {
 
 /**
 * 注冊(cè)專家學(xué)者
 */
 @RequestMapping(value = "/user/user/registExpert", method = RequestMethod.POST)
 long registExpert(@RequestParam("username") String username, @RequestParam("password") String password);
}

以及失敗回調(diào)用UserClientFallBack.java

package com.lgsc.cjbd.expert.remote.client;
 
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;
 
import com.lgsc.cjbd.vo.Response;
 
/**
 * 失敗回調(diào)
 * 
 * @author yeqj
 */
@Component
public class UserClientFallback implements UserClient {
 private static Logger log = LogManager.getLogger(UserClientFallback.class);
 
 @Override
 public long registExpert(String username, String password, String realName) {
 log.error("遠(yuǎn)程調(diào)用失敗,注冊(cè)專家學(xué)者失敗,參數(shù):[username=" + username + ",password=" + password + "]");
 return 0;
 }
}

之后再專家學(xué)者Service層傳遞專家學(xué)者用戶名和密碼過(guò)去,在用戶表新增專家學(xué)者注冊(cè)記錄

userClient.registExpert(username, password);

即可完成遠(yuǎn)程調(diào)用

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論