构建第一个微服务项目
1.实体类服务(只提供实体类)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package com.saxon.springcloud.pojo;
import lombok.Data; import lombok.NoArgsConstructor; import lombok.experimental.Accessors;
import java.io.Serializable; @Data @NoArgsConstructor @Accessors(chain = true) public class Dept implements Serializable { private long deptno; private String deptname; private String db_source;
public void setDeptname (String deptname) { this.deptname = deptname; } }
|
2.数据库处理阶段(dao+service)
1.Dao
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package com.saxon.springcloud.dao;
import com.saxon.springcloud.pojo.Dept; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper @Repository public interface DeptDao { int insertDept (Dept dept);
Dept queryDeptById (long id);
List<Dept> queryAllDept (); }
|
2.service
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package com.saxon.springcloud.service;
import com.saxon.springcloud.pojo.Dept; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Service;
import java.util.List; @Service public interface DeptService { int insertDept(Dept dept); @Bean Dept queryDeptById(long id);
List<Dept> queryAllDept(); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| package com.saxon.springcloud.service;
import com.saxon.springcloud.dao.DeptDao; import com.saxon.springcloud.pojo.Dept; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;
import java.util.List;
@Service public class DeptServiceimpl implements DeptService {
DeptDao deptDao;
@Autowired public void setDeptDao (DeptDao deptDao) { this.deptDao = deptDao; }
@Override public int insertDept (Dept dept) { return deptDao.insertDept (dept); }
@Override public Dept queryDeptById (long id) { return deptDao.queryDeptById (id); }
@Override public List<Dept> queryAllDept () { return deptDao.queryAllDept (); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| package com.saxon.springcloud.controller;
import com.saxon.springcloud.pojo.Dept; import com.saxon.springcloud.service.DeptService; import com.saxon.springcloud.service.DeptServiceimpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController public class DeptCotroller { @Autowired DeptService deptService;
@PostMapping ("/insert") @ResponseBody public int insertDept (Dept dept) { return deptService.insertDept (dept); }
@GetMapping ("/get/{id}") @ResponseBody public Dept queryDeptById (@PathVariable ("id") long id) { return deptService.queryDeptById (id); }
@GetMapping ("/getAll") @ResponseBody public List<Dept> queryAllDept () { return deptService.queryAllDept (); } }
|
3.controller
接收指定端口传过来的数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package com.saxon.springcloud.controller;
import com.saxon.springcloud.pojo.Dept; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate;
@RestController public class ConsumerDept { @Autowired RestTemplate restTemplate;
public static final String REQUEST_URL="http://localhost:8001/";
@GetMapping("/consumer/dept/get/{id}") public Dept queryDeptById(@PathVariable("id") long id){ return restTemplate.getForObject (REQUEST_URL+"get/ "+id,Dept.class); }
}
|
使用的核心对象就是RestTemplate
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package com.saxon.springcloud.configuration;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate;
@Configuration public class config { @Bean public RestTemplate getRestTemplate () { return new RestTemplate (); } }
|
使用的就是前面提到的@Bean注解;
RestTemplate的结构

对应的就是一些请求方式:分别是 get,post ,delete ,put,head,option。。。对应的方法就是 xxxForObject获得xxxForEntity,获得对象和获得实体的区别
关于方法里面的3个参数,有的是两个
- URL:地址
- object:返回值类型
- 最后一个属性,参数类型