zoukankan      html  css  js  c++  java
  • docker微服务部署之:二、搭建文章微服务项目

    docker微服务部署之:一,搭建Eureka微服务项目

    一、新增demo_article模块,并编写代码

    右键demo_parent->new->Module->Maven,选择Module SK为jdk8->ArtifactId:demo_article

    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_ci

    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的服务已经注册上来了

    11.使用Postman进行CRUD测试

    11.1 测试add方法

    11.2 测试findAll方法

    11.3 测试udpate方法

    11.4 测试findById方法

    11.5 测试delete方法

     docker微服务部署之:三,搭建Zuul微服务项目

  • 相关阅读:
    git 删除远程文件、文件夹
    pod install太慢 可以使用代理的方式
    flutter Container()最小宽度 最小高度
    flutter common init 常用Widget初始化
    xcode 嵌入flutter_module后编译报错 This app could not be installed at this time.
    Spring AOP
    Spring @Value 配置项解析 vs Spring @ConfigurationProperties 配置项解析
    Spring Bean 的实例化过程
    SpringBoot 配置项解析
    Spring IOC 自动注入流程
  • 原文地址:https://www.cnblogs.com/ztone/p/10569239.html
Copyright © 2011-2022 走看看