docker微服務部署之:一,搭建Eureka微服務項目html
右鍵demo_parent->new->Module->Maven,選擇Module SK爲jdk8->ArtifactId:demo_articlejava
<?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>
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下安裝Docker和Docker安裝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
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); } }
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; }
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> { }
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); } }
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; } }
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, "刪除成功"); } }
先建立數據庫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;
刷新eureka界面,能夠看到有一個名爲DEMO-ARTICLE的服務已經註冊上來了web