docker微服務部署之:2、搭建文章微服務項目

docker微服務部署之:一,搭建Eureka微服務項目html

1、新增demo_article模塊,並編寫代碼

右鍵demo_parent->new->Module->Maven,選擇Module SK爲jdk8->ArtifactId:demo_articlejava

1.修改pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>demo_parent</artifactId>
        <groupId>com.demo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>demo_article</artifactId>

    <dependencies>
     <!-- jpa依賴 -->
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>
     <!-- mysql依賴-->
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
     <!-- eureka客戶端依賴 -->
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> </project>

2.在resources目錄下新增application.yml文件

server:
  port: 9001 #article啓動端口,可自定義
spring:
  application:
    name: demo-article #article項目名稱,可自定義
  datasource:
    url: jdbc:mysql://192.168.31.181:3306/docker?characterEncoding=UTF8 #192.168.31.181爲虛擬機宿主機的ip地址,mysql是安裝在docker容器裏。詳見:Centos7下安裝DockerDocker安裝Mysql
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: 123456
  jpa:
    database: mysql
    show-sql: true
    generate-ddl: false
eureka:
  client:
    fetch-registry: true
    register-with-eureka: true
    service-url:
      defaultZone: http://192.168.31.181:7000/eureka #在IDEA中運行時使用127.0.0.1,部署發佈時,請修改成虛擬機宿主機的ip地址
  instance:
    prefer-ip-address: true

3.新建com.demo.article包,並在該包下新建啓動類ArticleApplication

package com.demo.article;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

/**
* 文章微服務
*/
// 標註爲一個spring boot項目 @SpringBootApplication
// 標註爲一個eureka客戶端 @EnableEurekaClient
public class ArticleApplication { public static void main(String[] args) { SpringApplication.run(ArticleApplication.class, args); } }

4.編寫pojo類

package com.demo.article.pojo;

import lombok.Data;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

@Entity
@Table(name = "tb_article")
@Data //使用該註解,至關於自動生成了getter和setter方法
public class Article implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY) #id自增
    private Integer id;
    private String title;
    private String content;
    private String author;
    private Date addTime;
}

5.編寫dao類

package com.demo.article.dao;

import com.demo.article.pojo.Article;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ArticleDao extends JpaRepository<Article,Integer> {

}

6.編寫service類

package com.demo.article.service;

import com.demo.article.pojo.Article;

import java.util.List;

public interface ArticleService {
    List<Article> findAll();

    Article findById(Integer id);

    void add(Article article);

    void updae(Article article);

    void deleteById(Integer id);
}
package com.demo.article.service.impl;

import com.demo.article.dao.ArticleDao;
import com.demo.article.pojo.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    ArticleDao articleDao;

    @Override
    public List<Article> findAll() {
        return articleDao.findAll();
    }

    @Override
    public Article findById(Integer id) {
        return articleDao.findById(id).get();
    }

    @Override
    public void add(Article article) {
        articleDao.save(article);
    }

    @Override
    public void updae(Article article) {
        articleDao.save(article);
    }

    @Override
    public void deleteById(Integer id) {
        articleDao.deleteById(id);
    }
}

7.編寫vo類

package com.demo.article.vo;

import lombok.Data;

import java.io.Serializable;

/**
 * 統一返回數據實體類
 */
@Data
public class Result implements Serializable {
    private Boolean flag;//是否成功
    private String message;//消息
    private Object data;//返回數據

    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;
    }
}

8.編寫controller類

package com.demo.article.controller;

import com.demo.article.pojo.Article;
import com.demo.article.service.ArticleService;
import com.demo.article.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    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("id") Integer id) {
        article.setId(id);
        articleService.updae(article);
        return new Result(true,"修改爲功");
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Result deleteById(@PathVariable("id") Integer id) {
        articleService.deleteById(id);
        return new Result(true, "刪除成功");
    }
}

9.數據庫sql腳本

先建立數據庫docker,字符集utf8,排序規則utf8-general_cimysql

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for tb_article
-- ----------------------------
DROP TABLE IF EXISTS `tb_article`;
CREATE TABLE `tb_article`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `content` mediumtext CHARACTER SET utf8 COLLATE utf8_general_ci NULL,
  `author` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `add_time` datetime(0) NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = MyISAM AUTO_INCREMENT = 3 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

10.運行ArticleApplication啓動類

刷新eureka界面,能夠看到有一個名爲DEMO-ARTICLE的服務已經註冊上來了web

11.使用Postman進行CRUD測試

11.1 測試add方法

11.2 測試findAll方法

11.3 測試udpate方法

11.4 測試findById方法

11.5 測試delete方法

 docker微服務部署之:三,搭建Zuul微服務項目spring

相關文章
相關標籤/搜索