以后我就直接从每一个微服务模块针对功能进行讲解,本系统由于微服务有18个微服务模块,不知道的可以看一下微服务项目实战(一),下面主要以招聘模块为例讲解一下如何搭建微服务以及微服务如何应用。
项目共有7个版块:头条、问答、活动、交友、招聘、人才中心
这些模块会划分为后台微服务模块进行后台接口开发,前端会利用静态模板搭建前后端分离的微服务架构。
招聘版块包含3个功能区,一个推荐职位列表、最新列表以及热门企业功能。
招聘微服务依赖于我们的base微服务模块和公共微服务模块。
我们系统中每一个微服务都会有一个数据库,一个微服务对应一个数据库,数据库表如下:
tb_recruit:职位表
tb_enterprise:企业表
@RestController
@CrossOrigin
@RequestMapping("/recruit")
public class RecruitController {
@Autowired
private RecruitService recruitService;
/**
* 查询全部数据
* @return
*/
@RequestMapping(method= RequestMethod.GET)
public Result findAll(){
return new Result(true,StatusCode.OK,"查询成功",recruitService.findAll());
}
/**
* 根据ID查询
* @param id ID
* @return
*/
@RequestMapping(value="/{id}",method= RequestMethod.GET)
public Result findById(@PathVariable String id){
return new Result(true,StatusCode.OK,"查询成功",recruitService.findById(id));
}
/**
* 分页+多条件查询
* @param searchMap 查询条件封装
* @param page 页码
* @param size 页大小
* @return 分页结果
*/
@RequestMapping(value="/search/{page}/{size}",method=RequestMethod.POST)
public Result findSearch(@RequestBody Map searchMap , @PathVariable int page, @PathVariable int size){
Page<Recruit> pageList = recruitService.findSearch(searchMap, page, size);
return new Result(true,StatusCode.OK,"查询成功", new PageResult<Recruit>(pageList.getTotalElements(), pageList.getContent()) );
}
/**
* 根据条件查询
* @param searchMap
* @return
*/
@RequestMapping(value="/search",method = RequestMethod.POST)
public Result findSearch( @RequestBody Map searchMap){
return new Result(true,StatusCode.OK,"查询成功",recruitService.findSearch(searchMap));
}
/**
* 增加
* @param recruit
*/
@RequestMapping(method=RequestMethod.POST)
public Result add(@RequestBody Recruit recruit ){
recruitService.add(recruit);
return new Result(true,StatusCode.OK,"增加成功");
}
/**
* 修改
* @param recruit
*/
@RequestMapping(value="/{id}",method= RequestMethod.PUT)
public Result update(@RequestBody Recruit recruit, @PathVariable String id ){
recruit.setId(id);
recruitService.update(recruit);
return new Result(true,StatusCode.OK,"修改成功");
}
/**
* 删除
* @param id
*/
@RequestMapping(value="/{id}",method= RequestMethod.DELETE)
public Result delete(@PathVariable String id ){
recruitService.deleteById(id);
return new Result(true,StatusCode.OK,"删除成功");
}
/***
* 查询推荐职位列表
*/
@RequestMapping(value="/search/recommend",method= RequestMethod.GET)
public Result recommend(){
List<Recruit> list=recruitService.findrecommend("2");
return new Result(true,StatusCode.OK,"查询成功",list);
}
/***
* 查看最新职位
*/
@RequestMapping(value = "/search/newlist", method = RequestMethod.GET)
public Result newlist(){
List<Recruit> list=recruitService.findnewlist("0");
return new Result(true,StatusCode.OK,"查询成功",list);
}
}
@RestController
@CrossOrigin
@RequestMapping("/recruit")
public class RecruitController {
@Autowired
private RecruitService recruitService;
/**
* 查询全部数据
* @return
*/
@RequestMapping(method= RequestMethod.GET)
public Result findAll(){
return new Result(true,StatusCode.OK,"查询成功",recruitService.findAll());
}
/**
* 根据ID查询
* @param id ID
* @return
*/
@RequestMapping(value="/{id}",method= RequestMethod.GET)
public Result findById(@PathVariable String id){
return new Result(true,StatusCode.OK,"查询成功",recruitService.findById(id));
}
/**
* 分页+多条件查询
* @param searchMap 查询条件封装
* @param page 页码
* @param size 页大小
* @return 分页结果
*/
@RequestMapping(value="/search/{page}/{size}",method=RequestMethod.POST)
public Result findSearch(@RequestBody Map searchMap , @PathVariable int page, @PathVariable int size){
Page<Recruit> pageList = recruitService.findSearch(searchMap, page, size);
return new Result(true,StatusCode.OK,"查询成功", new PageResult<Recruit>(pageList.getTotalElements(), pageList.getContent()) );
}
/**
* 根据条件查询
* @param searchMap
* @return
*/
@RequestMapping(value="/search",method = RequestMethod.POST)
public Result findSearch( @RequestBody Map searchMap){
return new Result(true,StatusCode.OK,"查询成功",recruitService.findSearch(searchMap));
}
/**
* 增加
* @param recruit
*/
@RequestMapping(method=RequestMethod.POST)
public Result add(@RequestBody Recruit recruit ){
recruitService.add(recruit);
return new Result(true,StatusCode.OK,"增加成功");
}
/**
* 修改
* @param recruit
*/
@RequestMapping(value="/{id}",method= RequestMethod.PUT)
public Result update(@RequestBody Recruit recruit, @PathVariable String id ){
recruit.setId(id);
recruitService.update(recruit);
return new Result(true,StatusCode.OK,"修改成功");
}
/**
* 删除
* @param id
*/
@RequestMapping(value="/{id}",method= RequestMethod.DELETE)
public Result delete(@PathVariable String id ){
recruitService.deleteById(id);
return new Result(true,StatusCode.OK,"删除成功");
}
/***
* 查询推荐职位列表
*/
@RequestMapping(value="/search/recommend",method= RequestMethod.GET)
public Result recommend(){
List<Recruit> list=recruitService.findrecommend("2");
return new Result(true,StatusCode.OK,"查询成功",list);
}
/***
* 查看最新职位
*/
@RequestMapping(value = "/search/newlist", method = RequestMethod.GET)
public Result newlist(){
List<Recruit> list=recruitService.findnewlist("0");
return new Result(true,StatusCode.OK,"查询成功",list);
}
}
@RestController
@CrossOrigin
@RequestMapping("/enterprise")
public class EnterpriseController {
@Autowired
private EnterpriseService enterpriseService;
/**
* 查询全部数据
* @return
*/
@RequestMapping(method= RequestMethod.GET)
public Result findAll(){
return new Result(true,StatusCode.OK,"查询成功",enterpriseService.findAll());
}
/**
* 根据ID查询
* @param id ID
* @return
*/
@RequestMapping(value="/{id}",method= RequestMethod.GET)
public Result findById(@PathVariable String id){
return new Result(true,StatusCode.OK,"查询成功",enterpriseService.findById(id));
}
/**
* 分页+多条件查询
* @param searchMap 查询条件封装
* @param page 页码
* @param size 页大小
* @return 分页结果
*/
@RequestMapping(value="/search/{page}/{size}",method=RequestMethod.POST)
public Result findSearch(@RequestBody Map searchMap , @PathVariable int page, @PathVariable int size){
Page<Enterprise> pageList = enterpriseService.findSearch(searchMap, page, size);
return new Result(true,StatusCode.OK,"查询成功", new PageResult<Enterprise>(pageList.getTotalElements(), pageList.getContent()) );
}
/**
* 根据条件查询
* @param searchMap
* @return
*/
@RequestMapping(value="/search",method = RequestMethod.POST)
public Result findSearch( @RequestBody Map searchMap){
return new Result(true,StatusCode.OK,"查询成功",enterpriseService.findSearch(searchMap));
}
/**
* 增加
* @param enterprise
*/
@RequestMapping(method=RequestMethod.POST)
public Result add(@RequestBody Enterprise enterprise ){
enterpriseService.add(enterprise);
return new Result(true,StatusCode.OK,"增加成功");
}
/**
* 修改
* @param enterprise
*/
@RequestMapping(value="/{id}",method= RequestMethod.PUT)
public Result update(@RequestBody Enterprise enterprise, @PathVariable String id ){
enterprise.setId(id);
enterpriseService.update(enterprise);
return new Result(true,StatusCode.OK,"修改成功");
}
/**
* 删除
* @param id
*/
@RequestMapping(value="/{id}",method= RequestMethod.DELETE)
public Result delete(@PathVariable String id ){
enterpriseService.deleteById(id);
return new Result(true,StatusCode.OK,"删除成功");
}
/*** 查询热门企业 * @return */
@RequestMapping(value = "/search/hotlist", method = RequestMethod.GET)
public Result hotlist() {
System.out.println("你不好吗你很好(我不好!!)");
return new Result(true, StatusCode.OK, "查询成功", enterpriseService.hotlist());
}
}
/**
* lryab
* 20191118
* 招聘微服务版块
* 招聘JS
* */
$(function() {
//查询推荐职位
initrecommd();
//查询热门企业
inithotlist();
//查询最新职位列表
initnewlist();
})
function initrecommd() {
//微服务调用
MicroAjax.Microrequest({
url: 'http://127.0.0.1:9003/recruit/search/recommend', //访问路径
dataType: 'json', //访问类型 'json','html'等
params:{},
resultMsg: false, //是否需要提示信息
type: 'GET',//,'get','post'
isLoading:false,
successCallback: function (data) {
console.log(data);
//拼接html:根据条件查询商品列表,然后动态拼接分页 拼接数据渲染html
if (data.data!=null) {
renderrecommdhtmldata(data.data)
}
} //提交后处理
});
}
controler增加注解@CrossOrigin不好使,反正我测试的是不好使。
最终解决方案:
增加跨域配置:
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class CorsFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
response.setContentType("textml;charset=UTF-8");
response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "0");
response.setHeader("Access-Control-Allow-Headers","Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With,userId,token");
response.setHeader("Access-Control-Allow-Credentials", "true");
response.setHeader("XDomainRequestAllowed", "1");
chain.doFilter(req, res);
}
@Override
public void destroy() {
// TODO Auto-generated method stub
}
}
1 文章头条微服务搭建(一个新的微服务和一套数据库)
2redis缓存在头条微服务模块的应用
重要的不是学习项目实战,而是通过这一个项目去了解微服务架构的应用场景、微服务的思想、spring boot微服务搭建、消息中间件rabbitmq的应用、spring cloud应用技术、dockfile微服务部署。慢慢的都会通过这套项目实战展示。
经验是由一点一点积累的,思维也是由一天一天训练出来的。
如果你急需一个单体结构项目练手下面的项目可能适合你,可以学习一个完整的项目流程,项目业务以及前后端项目的常用技术:电商在线演示地址为:演示地址
持续更新分享此项目微服务架构项目实战(开发中…)
说好的12点之前睡觉呢?今天又晚了,睡觉了。