# blog-backend
**Repository Path**: kangbinqiang/blog-backend
## Basic Information
- **Project Name**: blog-backend
- **Description**: 用Dubbo+Zookeeper+SpringBoot+MongoDB等搭建一套多模块分布式的博客后台
- **Primary Language**: Unknown
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2020-07-05
- **Last Updated**: 2020-12-19
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
前端地址:https://github.com/kangbinqiang/blog-font.git
后端地址:https://github.com/kangbinqiang/blog-backend.git
# 项目搭建
## 搭建父工程
### 选择maven项目

### 填写GroupId和ArtifactId

### 此时项目结构如下

### 修改pom.xml文件,如下
```xml
4.0.0
com.blog
blog
1.0-SNAPSHOT
pom
```
## 搭建子模块
### 在blog项目的基础上,搭建接口模块api


### 依次建立consumer、provider,此时项目结构如下

## 修改每个module的pom文件
### 根项目的pom.xml
```xml
4.0.0
com.blog
blog
1.0-SNAPSHOT
api
consumer
provider
pom
org.springframework.boot
spring-boot-starter-parent
2.2.5.RELEASE
UTF-8
1.8
2.5.5
0.10
1.16.18
1.5.7.RELEASE
org.springframework.boot
spring-boot-starter
${spring-boot.version}
org.springframework.boot
spring-boot-starter-web
${spring-boot.version}
org.projectlombok
lombok
${lombok.version}
provided
com.alibaba
dubbo
${dubbo.version}
com.101tec
zkclient
${zkclient.version}
```
### api的pom.xml
```xml
blog
com.blog
1.0-SNAPSHOT
4.0.0
api
org.projectlombok
lombok
provided
```
### provider的pom.xml
```xml
blog
com.blog
1.0-SNAPSHOT
4.0.0
provider
com.blog
blog
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter
2.2.5.RELEASE
com.alibaba
dubbo
com.101tec
zkclient
com.blog
api
1.0-SNAPSHOT
org.springframework.boot
spring-boot-maven-plugin
```
### consumer的pom.xml
```xml
blog
com.blog
1.0-SNAPSHOT
4.0.0
consumer
com.blog
blog
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter
2.2.5.RELEASE
com.alibaba
dubbo
com.101tec
zkclient
com.blog
api
1.0-SNAPSHOT
org.springframework.boot
spring-boot-maven-plugin
```
# 项目配置
## 测试接口
### 在api中创建以下两个文件

#### Article:
```java
package com.boot.model;
import lombok.Data;
import java.util.Date;
@Data
public class Article implements Serializable{
private String name;
private String type;
private String author;
private Date publishDate;
private Integer score;
}
```
#### TestService:
```java
package com.boot.service;
import com.boot.model.Article;
public interface TestService {
String descArticle();
Article find();
}
```
### 在provider中创建文件结构及文件

#### TestServiceImpl:
```java
package com.boot.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.boot.model.Article;
import com.boot.service.TestService;
import java.util.Date;
@Service
public class TestServiceImpl implements TestService {
@Override
public String descArticle() {
return "this is kbq's Article";
}
@Override
public Article find() {
Article article = new Article();
article.setAuthor("kbq");
article.setName("Dubbo");
article.setPublishDate(new Date());
article.setScore(100);
article.setType("program");
return article;
}
}
```
#### ProviderApplication:
```java
package com.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
import java.io.IOException;
@SpringBootApplication
@ImportResource({"classpath:config/spring-dubbo.xml"})
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
#### spring-dubbo.xml:
```xml
```
#### application.yml:
```yml
# 在这里编写springboot的配置信息
server:
port: 8091
```
### 在consumer中创建文件结构以及文件

#### TestController:
```java
package com.boot.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.boot.model.Article;
import com.boot.service.TestService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/")
public class TestController {
@Reference(version = "1.0.0")
private TestService testService;
@GetMapping("hello")
public String hello() {
return testService.descArticle();
}
@GetMapping("user")
public Article user() {
return testService.find();
}
}
```
#### ConsumerApplication:
```java
package com.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
@ImportResource({"classpath:config/spring-dubbo.xml"})
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
```
#### spring-dubbo.xml:
```xml
```
#### application.yml:
```yml
# 在这里编写springboot的配置信息
server:
port: 8090
```
以上就是项目的初始搭建过程,后面配置模块只会以各自的文件出现,只需要在上述的基础上增加即可
# 整合MongoDB
准备MongoDB的环境,建立名为blog的库
1、打开命令窗口,切换到mongodb安装目录下的“bin”目录中
2、启动服务。输入命令:”mongod --dbpath E:\mongodb\data
推荐使用Robot工具连接MongoDB,建好如下图所示:

## 添加依赖
```xml
org.springframework.boot
spring-boot-starter-data-mongodb
```
## 添加配置
```yml
spring:
data:
mongodb:
uri: mongodb://localhost:27017/blog
```
## Entity:
```java
package com.boot.service.entity;
import lombok.Data;
import org.springframework.data.mongodb.core.mapping.Document;
import java.util.Date;
@Document(collection = "article")
@Data
public class ArticleDO {
private String title;
private String author;
private Date createdTime;
}
```
## Mapper:
```java
package com.boot.service.mapper;
import com.boot.service.entity.ArticleDO;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface ArticleMapper extends MongoRepository {
}
```
## ServiceImpl:
```java
package com.boot.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.boot.model.Article;
import com.boot.service.TestService;
import com.boot.service.entity.ArticleDO;
import com.boot.service.mapper.ArticleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Date;
import java.util.List;
@Service(version = "1.0.0")
public class TestServiceImpl implements TestService {
@Autowired
private ArticleMapper articleMapper;
@Override
public String descArticle() {
List all = articleMapper.findAll();
return JSON.toJSONString(all);
}
@Override
public Article find() {
Article article = new Article();
article.setAuthor("kbq");
article.setName("Dubbo");
article.setPublishDate(new Date());
article.setScore(100);
article.setType("program");
return article;
}
@Override
public void add() {
ArticleDO articleDO = new ArticleDO();
articleDO.setAuthor("kangbinqiang");
articleDO.setTitle("Dubbo");
articleDO.setCreatedTime(new Date());
articleMapper.insert(articleDO);
}
}
```
## Controller:
```java
package com.boot.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.boot.model.Article;
import com.boot.service.TestService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/")
public class TestController {
@Reference(version = "1.0.0")
private TestService testService;
@GetMapping("hello")
public String hello() {
return testService.descArticle();
}
@GetMapping("user")
public Article user() {
return testService.find();
}
@GetMapping("add")
public void add() {
testService.add();
}
}
```
结果如图:
### add()

### hello()
