其他依赖见Spring Cloud-CSDN博客
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
使用Feign(开启负载均衡)
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
@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同一封装对象?
?注意点
<dependency>??
<groupId>ma.glasnost.orika</groupId>
??<artifactId>orika-core</artifactId>
??<version>1.4.6</version>
</dependency>
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private Long id;
private String account;
private String password;
}