2个服务(provider,consumer),一个提供出去的接口(user)
1,provider
@RestController
public class UserController implements UserAPI {
@Value("${server.port}")
private int port;
@Override
public String getHi() {
return "provider" + port;
}
@Override
public String getById() {
return "provider id" + port;
}
@PostMapping("/getPerson")
public Person getPerson(@RequestBody Person person) {
System.out.println("provider-person:" + person.toString());
person.setAge(122);
return person;
}
}
2,user接口
@RequestMapping("/User")
public interface UserAPI {
@GetMapping("/getHi")
public String getHi();
@GetMapping("/getById")
public String getById();
}
3,consumer
@FeignClient("provider")
public interface UserController extends UserAPI{
@PostMapping("/getPerson")
public Person getPerson(@RequestBody Person person);
}
4,consumer的control通过RPC调用provider的方法:
@RestController
public class UserClient {
@Autowired
private UserController userController;
@GetMapping("/getUserHi")
private String getHi() {
return userController.getHi();
}
@GetMapping("/getUser")
private String getUser(String name) {
System.out.println("name:" + name);
Person person = new Person(1, name, 22);
return userController.getPerson(person).toString();
}
}