k8s部署微服务springcloud从0-1(第一个微服务模块)
k8s部署微服务springcloud从0-1(第一个微服务模块)
一.文章微服务具体实现
1.编写启动类ArticleApplication
package cn.sm1234.article; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * 文章微服务 */ @SpringBootApplication public class ArticleApplication { public static void main(String[] args) { SpringApplication.run(ArticleApplication.class,args); } }
2.编写pojo实体类Article,Result
package cn.sm1234.article.pojo; import javax.persistence.*; import java.io.Serializable; import java.util.Date; /** *文章实体 */ @Entity @Table(name = "tb_article") public class Article implements Serializable{ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) //自增长 private Integer id; private String title; private String content; private String author; private Date addtime; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public Date getAddtime() { return addtime; } public void setAddtime(Date addtime) { this.addtime = addtime; } }
package cn.sm1234.article.pojo; import java.io.Serializable; /** * 统一返回数据实体类 */ public class Result implements Serializable{ private Boolean flag; //是否成功 private String message;//消息 private Object data;//返回数据 public Result() { } public Result(Boolean flag, String message) { this.flag = flag; this.message = message; } public Result(Boolean flag, String message, Object data) { this.flag = flag; this.message = message; this.data = data; } public Boolean getFlag() { return flag; } public void setFlag(Boolean flag) { this.flag = flag; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public Object getData() { return data; } public void setData(Object data) { this.data = data; } }
3.编写DAO
package cn.sm1234.article.dao;
import cn.sm1234.article.pojo.Article;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* 文章 dao
*/
public interface ArticleDao extends JpaRepository
4.Service实现ArticleService,ArticleServiceImpl
package cn.sm1234.article.service;
import cn.sm1234.article.pojo.Article;
import java.util.List;
/**
* 文章 service 接口
*/
public interface ArticleService {
public List
package cn.sm1234.article.service;
import cn.sm1234.article.dao.ArticleDao;
import cn.sm1234.article.pojo.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* 文章service实现
*/
@Service
public class ArticleServiceImpl implements ArticleService {
@Autowired
private ArticleDao articleDao;
@Override
public List
5.Controller实现
package cn.sm1234.article.controller; import cn.sm1234.article.pojo.Article; import cn.sm1234.article.pojo.Result; import cn.sm1234.article.service.ArticleService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; /** * 文章 Controller */ @RestController @RequestMapping("/article") public class ArticleController { @Autowired private ArticleService articleService; /** * 查询所有 */ @RequestMapping(method = RequestMethod.GET) public Result findAll(){ return new Result(true," 查询成功",articleService.findAll()); } /** * 查询一个 */ @RequestMapping(value = "/{id}",method = RequestMethod.GET) public Result findById(@PathVariable Integer id){ return new Result(true," 查询成功",articleService.findById(id)); } /** * 添加 */ @RequestMapping(method = RequestMethod.POST) public Result add(@RequestBody Article article){ articleService.add(article); return new Result(true," 添加成功"); } /** * 修改 */ @RequestMapping(value = "/{id}",method = RequestMethod.PUT) public Result update(@RequestBody Article article,@PathVariable Integer id){ article.setId(id); articleService.update(article); return new Result(true," 修改成功"); } /** * 删除 */ @RequestMapping(value = "/{id}",method = RequestMethod.DELETE) public Result deleteById(@PathVariable Integer id){ articleService.deleteById(id); return new Result(true," 删除成功"); } }
6.整体项目结构
7.启动并使用apipost工具对接口进行测试
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~