目录
文件结构
代码
1、api
1.1实体类(Dept )
1.2数据库
2、provider
2.1 DeptController
2.2 DeptDao
2.3 DeptService
2.4 DeptServiceImpl
2.5 application.yml
3、consumer
3.1 ConfigBean
3.2 DeptConsumerController
测试
1.启动provider
访问provider能够得到结果
2.启动consummer
能够通过consummer获得结果
文件结构
代码
1、api
1.1实体类(Dept )
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true) //链式写法
public class Dept implements Serializable {
private Long deptno;
private String dname;
//一个服务对应一个数据库,同一信息可能存在不同数据库
private String db_source;
public Dept(String dname) {
this.dname = dname;
}
}
1.2数据库
2、provider
2.1 DeptController
//提供restful服务
@RestController
public class DeptController {
@Autowired
private DeptService deptService;
@PostMapping("/dept/add")
public boolean addDept(Dept dept){
return deptService.addDept(dept);
}
@GetMapping("/dept/get/{id}")
public Dept get(@PathVariable("id") Long id){
return deptService.queryById(id);
}
@GetMapping("/dept/list")
public List<Dept> queryAll(){
return deptService.queryAll();
}
}
2.2 DeptDao
@Mapper
@Repository
public interface DeptDao {
public boolean addDept(Dept dept);
public Dept queryById(Long id);
public List<Dept> queryAll();
}
2.3 DeptService
public interface DeptService {
public boolean addDept(Dept dept);
public Dept queryById(Long id);
public List<Dept> queryAll();
}
2.4 DeptServiceImpl
@Service
public class DeptServiceImpl implements DeptService{
@Autowired
private DeptDao deptDao;
@Override
public boolean addDept(Dept dept) {
return deptDao.addDept(dept);
}
@Override
public Dept queryById(Long id) {
return deptDao.queryById(id);
}
@Override
public List<Dept> queryAll() {
return deptDao.queryAll();
}
}
2.5 application.yml
server:
port: 8001
#mybatis配置
mybatis:
type-aliases-package: com.guo.springcloud.pojo
config-location:
mapper-locations: classpath:mybatis/mapper/*.xml
#spring配置
spring:
application:
name: springcloud-provider-dept
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/db01?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
username: root
password: gj001212
3、consumer
3.1 ConfigBean
@Configuration
public class ConfigBean {
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
3.2 DeptConsumerController
@RestController
public class DeptConsumerController {
//理解:消费者,不应该有service层
//RestTemplate ... 供我们直接调用就可以了!
//(url,实体:map,Class<T> responseType)
@Autowired
private RestTemplate restTemplate;//提供多种便捷访问远程http服务的方法,简单的Restful服务模板
private static final String REST_URL_PREFIX = "http://localhost:8001";
@RequestMapping("/consumer/dept/add")
public boolean add(Dept dept){
return restTemplate.postForObject(REST_URL_PREFIX+"/dept/add",dept,Boolean.class);
}
@RequestMapping("/consumer/dept/get/{id}")
public Dept get(@PathVariable("id") Long id){
return restTemplate.getForObject(REST_URL_PREFIX+"/dept/get/"+id,Dept.class);
}
@RequestMapping("/consumer/dept/list")
public List<Dept> queryAll(){
return restTemplate.getForObject(REST_URL_PREFIX+"/dept/list",List.class);
}
}
测试
1.启动provider
访问provider能够得到结果
2.启动consummer
能够通过consummer获得结果