<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">
<modelVersion>4.0.0modelVersion>
<groupId>com.bloggroupId>
<artifactId>blogartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>pompackaging>
project>
<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">
<modelVersion>4.0.0modelVersion>
<groupId>com.bloggroupId>
<artifactId>blogartifactId>
<version>1.0-SNAPSHOTversion>
<modules>
<module>apimodule>
<module>consumermodule>
<module>providermodule>
modules>
<packaging>pompackaging>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.2.5.RELEASEversion>
parent>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<java.version>1.8java.version>
<dubbo.version>2.5.5dubbo.version>
<zkclient.version>0.10zkclient.version>
<lombok.version>1.16.18lombok.version>
<spring-boot.version>1.5.7.RELEASEspring-boot.version>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
<version>${spring-boot.version}version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<version>${spring-boot.version}version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>${lombok.version}version>
<scope>providedscope>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>dubboartifactId>
<version>${dubbo.version}version>
dependency>
<dependency>
<groupId>com.101tecgroupId>
<artifactId>zkclientartifactId>
<version>${zkclient.version}version>
dependency>
dependencies>
dependencyManagement>
project>
<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>blogartifactId>
<groupId>com.bloggroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>apiartifactId>
<dependencies>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<scope>providedscope>
dependency>
dependencies>
project>
<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>blogartifactId>
<groupId>com.bloggroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>providerartifactId>
<dependencies>
<dependency>
<groupId>com.bloggroupId>
<artifactId>blogartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
<version>2.2.5.RELEASEversion>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>dubboartifactId>
dependency>
<dependency>
<groupId>com.101tecgroupId>
<artifactId>zkclientartifactId>
dependency>
<dependency>
<groupId>com.bloggroupId>
<artifactId>apiartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
<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>blogartifactId>
<groupId>com.bloggroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>consumerartifactId>
<dependencies>
<dependency>
<groupId>com.bloggroupId>
<artifactId>blogartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
<version>2.2.5.RELEASEversion>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>dubboartifactId>
dependency>
<dependency>
<groupId>com.101tecgroupId>
<artifactId>zkclientartifactId>
dependency>
<dependency>
<groupId>com.bloggroupId>
<artifactId>apiartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
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;
}
package com.boot.service;
import com.boot.model.Article;
public interface TestService {
String descArticle();
Article find();
}
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;
}
}
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();
}
}
}
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="provider"/>
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<dubbo:annotation package="com.boot.service.impl"/>
beans>
# 在这里编写springboot的配置信息
server:
port: 8091
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();
}
}
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);
}
}
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="consumer"/>
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<dubbo:annotation package="com.boot.controller"/>
beans>
# 在这里编写springboot的配置信息
server:
port: 8090
以上就是项目的初始搭建过程,后面配置模块只会以各自的文件出现,只需要在上述的基础上增加即可
准备MongoDB的环境,建立名为blog的库
1、打开命令窗口,切换到mongodb安装目录下的“bin”目录中
2、启动服务。输入命令:”mongod --dbpath E:\mongodb\data
推荐使用Robot工具连接MongoDB,建好如下图所示:
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-mongodbartifactId>
dependency>
spring:
data:
mongodb:
uri: mongodb://localhost:27017/blog
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;
}
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<ArticleDO, String> {
}
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<ArticleDO> 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);
}
}
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();
}
}