1.新建实体类user
package com.sjy.code1.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
@AllArgsConstructor
@NoArgsConstructor
@Data
@Accessors
public class User {
private String account;
private String pwd;
}
2.在生产者中创建controller层
package com.sjy.code.controller;
import com.sjy.code.pojo.User;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
@RestController
@RequestMapping("/user")
public class UserController {
@RequestMapping("/{account}")
public String getByPath(@PathVariable("account") String account){
System.out.println("account--"+account);
return "getByPath";
}
@RequestMapping("/param")
public String getByParam(@RequestParam("account") String account,@RequestParam("pwd") String pwd){
System.out.println("account--"+account+"--pwd--"+pwd);
return "getByParam";
}
@RequestMapping("/pojo")
public String getByPojo(@RequestBody User user){
System.out.println("user--"+user);
return "getByJosn";
}
@RequestMapping("/map")
public String getByMap(@RequestBody Map map){
System.out.println("map--"+map);
return "getByMap";
}
}
3.在消费者模块中添加接口
注解@FeignClient("provider")表示远程通信,括号内
package com.sjy.code1.service;
import com.sjy.code1.pojo.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
@FeignClient("provider")
public interface UserService {
@RequestMapping("/user/{account}")
public String getByPath(@PathVariable("account") String account);
@RequestMapping("/user/param")
public String getByParam(@RequestParam("account") String account,@RequestParam("pwd") String pwd);
@RequestMapping("/user/pojo")
public String getByPojo(@RequestBody User user);
@RequestMapping("/user/map")
public String getByMap(@RequestBody Map map);
}
4.在启动类中添加注解@EnableFeignClients
表示开启远程通信功能
5.远程调用三种传参方式:
①.@PathVariable 路径传参
②.@RequestParam 请求参数传参
③.@RequestBody json传参
二、远程调用1.在消费者创建controller层进行测试,需要将pojo包复制过来
package com.sjy.code1.controller;
import com.sjy.code1.pojo.User;
import com.sjy.code1.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/user")
public class userController {
private UserService service;
@Autowired
public userController(UserService service) {
this.service = service;
}
@RequestMapping("/{account}")
public String test01(@PathVariable("account") String account){
service.getByPath(account);
return "yes";
}
@RequestMapping("/test2")
public String test02(String account,String pwd){
service.getByParam(account,pwd);
return "yes";
}
@RequestMapping("/test3")
public String test03(String account,String pwd){
User user = new User().setAccount(account).setPwd(pwd);
service.getByPojo(user);
return "yes";
}
@RequestMapping("/test4")
public String test04(String account,String pwd){
Map map=new HashMap<>();
map.put("account",account);
map.put("pwd",pwd);
service.getByMap(map);
return "yes";
}
}
2.结果
三、DOT层的构建由于消费者调用接口时,需要使用到user实体类,这就使得代码重复,为了使代码简介,我们采取dot层的构建
下面介绍以下对象命名分类
1.VO(View Object):视图对象,用于展示层,前端将数据传到后台
2.DTO(Data Transfer Object):数据传输对象,用于后端服务互相传数据
3.DO(Domain Object):领域对象,就是从现实世界中抽象出来的有形或无形的业务实体。用于服务调用service层
4.PO(Persistent Object):持久化对象,用于service调用数据库
关系图
dot层的构建:目的:作为启动器,将实体类移到里面作为公共部分为其他模块所用
1.创建spring项目,版本号为2.4.1
2.将实体类移进来
3.在pom文件中导入lombok依赖
4.0.0 com.sjy demo40.0.1-SNAPSHOT demo4 Demo project for Spring Boot 1.8 UTF-8 UTF-8 2.4.1 org.springframework.boot spring-boot-starterorg.springframework.boot spring-boot-starter-testtest org.projectlombok lombokorg.springframework.boot spring-boot-dependencies${spring-boot.version} pom import org.apache.maven.plugins maven-compiler-plugin3.8.1 1.8 1.8 UTF-8 org.springframework.boot spring-boot-maven-plugin2.4.1 com.sjy.demo4.Demo4Application repackage repackage
4.将dot模块变成jar包的形式,使得其他模块可导入pom文件添加依赖
5.父模块pom文件中添加dot依赖并进行单方面认亲
注意:dot模块不能继承父模块
然后消费者中的user类就可以删除,并在使用过user类的地方用userdto代替
在这里会产生一个问题:dto类的属性比user中的多,或者字段名不一样
有以下两种解决方式:
①拿到user类,然后去修改属性值
②orika 对象复制
生产者pom文件中添加依赖
ma.glasnost.orika orika-core1.4.6
在生产者的启动类中对MapperFactory属性进行spring注入
@Scope("prototype")表示原型链模式(为了使MapperFactory互不干涉)
@Bean
@Scope("prototype")
public MapperFactory mapperFactory(){
return new DefaultMapperFactory.Builder().build();
}
生产者controller



