Spring Cloud Feign简介参考:http://blog.youkuaiyun.com/neosmith/article/details/52449921
根据专家学者提供的账号密码,要在用户表注册一个专家学者账号(用户和专家学者不同的数据库)
在userContorller.java写一个方法:注册专家学者账号
/**
* 专家学者注册
*
* @param username
* @param password
* @return
*/
@ApiOperation(value = "专家学者注册")
@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(这里的接口和要远程调用的controller方法声明一样(此处是UserController.java),可直接复制过来,如下所示)
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 {
/**
* 注册专家学者
*/
@RequestMapping(value = "/user/user/registExpert", method = RequestMethod.POST)
long registExpert(@RequestParam("username") String username, @RequestParam("password") String password);
}
以及失败回调用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;
/**
* 失败回调
*
* @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("远程调用失败,注册专家学者失败,参数:[username=" + username + ",password=" + password + "]");
return 0;
}
}
之后再专家学者Service层传递专家学者用户名和密码过去,在用户表新增专家学者注册记录
userClient.registExpert(username, password);
即可完成远程调用