其他依赖见Spring Cloud-CSDN博客
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
org.springframework.cloud
spring-cloud-starter-loadbalancer
使用Feign(开启负载均衡)
org.springframework.cloud
spring-cloud-starter-openfeign
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
@RequestMapping("/{account}")
public String getByPath(@PathVariable("account") String account) {
log.info("account:{}",account);
return "";
}
@RequestMapping("/param")
public String getByParam(@RequestParam("account") String account,@RequestParam("password") String password) {
log.info("account:{},password:{}",account,password);
return "";
}
@RequestMapping("/pojo")
public String getByPojo(@RequestBody UserDto dto) {
//将dto中与User相关的属性拿出来给到User对象
User user=new User();
BeanUtils.copyProperties(dto,user);
log.info("user:{}",user);
return "";
}
}
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ConsumerApplication {
}
@FeignClient("provider")
public interface FeignUserService {
@RequestMapping("/user/{account}")
String getByPath(@PathVariable("account") String account);
@RequestMapping("/user/param")
String getByParam(@RequestParam("account") String account, @RequestParam("password") String password);
@RequestMapping("/user/pojo")
String getByPojo(@RequestBody UserDto dto);
}
消费者远程调用生产者: 需要网络传输,使用DTO同一封装对象
注意点
ma.glasnost.orika
orika-core
1.4.6
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private Long id;
private String account;
private String password;
}