接着上期内容延申:SpringCloud之基本使用与nacos
一、(消费者、生产者)远程调用接口定义
1、在父项目内pom依赖中的dependencies内导入
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
2、在生产者项目中建一个实体类
package com.provider.code.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
@AllArgsConstructor
@NoArgsConstructor
@Data
@Accessors(chain = true)
public class User {
private String account;
private String password;
}
3、提供接口操作User,controller层
package com.provider.code;
import com.provider.code.pojo.User;
import com.sun.xml.internal.bind.v2.runtime.output.SAXOutput;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
@RestController
@RequestMapping("/user")
public class UserController {
// 路径传参
@RequestMapping("/{account}")
public String getByPath(@PathVariable String account){
System.out.println("account:"+account);
return "provider say : yes";
}
// 接收两个参数,请求直接携带
@RequestMapping("/param")
public String getByParam(@RequestParam("account") String account,@RequestParam("password") String password){
System.out.println("account:"+account+"\tpassword:"+password);
return "provider say : yes";
}
// 接受json数据
@RequestMapping("/pojo")
public String getByPojo(@RequestBody User user){
System.out.println("pojo:"+user);
return "provider say : yes";
}
// 接收任意类型的方法
@RequestMapping("/more")
public String getByMore(@RequestBody Map<String,Object> map){
System.out.println("more:"+map);
return "provider say : yes";
}
}
每个方法呈现的结果:
1、
2、
3、在body内传入两个参数
4、
4、在父项目内pom依赖中的dependencies内导入:远程通信
<!-- 远程通信 Feign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
5、消费者调用生产者的
①、在消费者中定义出生产者的接口
将生产者内建的User实体类复制到消费者内,
建service接口:这个service和生产者的接口保持一致
package com.consumer.code.service;
import com.consumer.code.pojo.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
//远程通信,nacos-provider为生产者的名称
@FeignClient("nacos-provider")
@SuppressWarnings("all")
public interface FeignUserService {
// 路径传参
// 127.0.0.1/{account}
@RequestMapping("/user/{account}")
public String getByPath(@PathVariable(value = "account") String account);
// 接收两个参数,请求直接携带
@RequestMapping("/user/param")
public String getByParam(@RequestParam("account") String account,@RequestParam("password") String password);
// 接受json数据
@RequestMapping("/user/pojo")
public String getByPojo(@RequestBody User user);
// 接收任意类型的方法
@RequestMapping("/user/more")
public String getByMore(@RequestBody Map<String,Object> map);
}
②、在启动类加入注解:开启远程通信
@EnableFeignClients
二、Feign远程调用
1、远程调用三种传参方式:
①.@PathVariable 路径传参
②.@RequestParam 请求参数传参
③.@RequestBody json传参
2、在消费者内建UserController,调用User接口
package com.consumer.code;
import com.consumer.code.pojo.User;
import com.consumer.code.service.FeignUserService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
@RequestMapping("/user")
public class UserController {
private FeignUserService service;
public UserController(FeignUserService service) {
this.service = service;
}
@RequestMapping("/test01")
public String test01(String account){
service.getByPath(account);
return "yes";
}
@RequestMapping("/{account}")
public String test02(@PathVariable String account){
service.getByPath(account);
return "yes";
}
@RequestMapping("/test03")
public String test03(String account,String password){
service.getByParam(account,password);
return "yes";
}
@RequestMapping("/test04")
public String test04(String account,String password){
service.getByPojo(new User().setAccount(account).setPassword(password));
return "yes";
}
@RequestMapping("/test05")
public String test05(String account,String password){
Map<String,Object> map=new HashMap<>();
map.put("account",account);
map.put("password",password);
service.getByMore(map);
return "yes";
}
}
呈现结果:
三、DTO层的构建
解决:生产者的实体类每次都要复制到消费者内,代码冗余
1、DTO封装:
VO(View Object):视图对象,用于展示层,它的作用是把某个指定页面(或组件)的所有数据封装起来。
DTO(Data Transfer Object):数据传输对象,这个概念来源于J2EE的设计模式,原来的目的是为了EJB的分布式应用提供粗粒度的数据实体,以减少分布式调用的次数,从而提高分布式调用的性能和降低网络负载,但在这里,我泛指用于展示层与服务层之间的数据传输对象。
DO(Domain Object):领域对象,就是从现实世界中抽象出来的有形或无形的业务实体。
PO(Persistent Object):持久化对象,它跟持久层(通常是关系型数据库)的数据结构形成一一对应的映射关系,如果持久层是关系型数据库,那么,数据表中的每个字段(或若干个)就对应PO的一个(或若干个)属性。
消费者 远程调用 生产者 : 需要网络传输,使用DTO同一封装对象
原理与SpringBoot启动类相同
①、将DTO对象封装到公共DTO模块
②、为需要的项目引入公共DTO模块
2、、做一个类似启动器的类
①、新建一个模块
②、在此项目导入依赖,无需继承父类,若继承则会依赖冲突,将test类删掉
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
③、将实体类放入此项目
④、将此项目导成jar包
3、在父模块内引用刚导出的jar包
<dependency>
<groupId>com.cloud02</groupId>
<artifactId>code</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
单方面认亲:
<module>commons</module>
4、消费类就可以删掉实体类了,导入UserDto
生产者中的实体类不可删除,因为它需要到数据库查数据
同样,将生产者内的方法调用UserDto,而不是它的实体类:原因是dto中的属性可能比实体类多
// 接受json数据
@RequestMapping("/pojo")
public String getByPojo(@RequestBody UserDto dto){
// dto中的属性可能比实体类多
// 需要拿到的是实体类,根据实体类的属性去数据库查询数据
User u=new User();
u.setAccount(dto.getAccount()).setPassword(dto.getPassword());
System.out.println("pojo:"+dto);
return "provider say : yes";
}
四、Orika框架
1、含义:
Orika是java Bean映射框架,可以实现从一个对象递归拷贝数据至另一个对象。
在开发多层应用程序中非常有用。在这些层之间交换数据时,通常为了适应不同API需要转换一个实例至
另一个实例。
2、在生产者内导入依赖
<dependencies>
<dependency>
<groupId>ma.glasnost.orika</groupId>
<artifactId>orika-core</artifactId>
<version>1.4.6</version>
</dependency>
</dependencies>
3、生产者启动类
@Scope("prototype")表示原型链模式(为了使MapperFactory互不干涉
@Bean
@Scope("prototype")
public MapperFactory mapperFactory(){
return new DefaultMapperFactory.Builder().build();
}
4、在生产者UserController内注入
@Autowired
private MapperFactory factory;
// 接受json数据
@RequestMapping("/pojo")
public String getByPojo(@RequestBody UserDto dto){
// dto中的属性可能比实体类多
// 需要拿到的是实体类,根据实体类的属性去数据库查询数据
// User u=new User();
// u.setAccount(dto.getAccount()).setPassword(dto.getPassword());
// 在学习struts2的时候,BeanUtils(将我的数据与实体类数据匹配,然后将符合的数据放到实体类中)
// dto与实体类一致时
// User u=factory.getMapperFacade().map(dto,User.class);
// dto与实体类不一致时
factory.classMap(UserDto.class, User.class)
.field("name", "account")
.byDefault().register();
User u = factory.getMapperFacade().map(dto, User.class);
System.out.println("pojo:"+dto);
return "provider say : yes";
}
本期内容结束~~~~