Spring boot 2.1.9 + Dubbo 2.7.3 + Nacos 1.1.4 构建微服务系统 , mydemo
下载最新版本的 Nacos Server , 具体参见下面 注册中心部分
#启动 Nacos 服务
sh startup.sh -m standalone
parent pom
<modelVersion>4.0.0modelVersion>
<packaging>pompackaging>
<modules>
<module>dubbo-apimodule>
<module>dubbo-providermodule>
<module>dubbo-consumermodule>
modules>
<groupId>com.examplegroupId>
<artifactId>dubbo-demoartifactId>
<version>1.0-SNAPSHOTversion>
<properties>
<spring-boot.version>2.1.9.RELEASEspring-boot.version>
<dubbo.version>2.7.3dubbo.version>
<nacos-config-spring-boot.version>0.2.1nacos-config-spring-boot.version>
<java.version>1.8java.version>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>${spring-boot.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-dependencies-bomartifactId>
<version>${dubbo.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
<version>${dubbo.version}version>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubboartifactId>
<version>${dubbo.version}version>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-registry-nacosartifactId>
<version>${dubbo.version}version>
dependency>
<dependency>
<groupId>com.alibaba.bootgroupId>
<artifactId>nacos-config-spring-boot-starterartifactId>
<version>${nacos-config-spring-boot.version}version>
dependency>
<dependency>
<groupId>com.alibaba.nacosgroupId>
<artifactId>nacos-clientartifactId>
<version>1.1.4version>
dependency>
<dependency>
<groupId>com.examplegroupId>
<artifactId>dubbo-apiartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
dependencies>
dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<version>${spring-boot.version}version>
<executions>
<execution>
<goals>
<goal>repackagegoal>
goals>
execution>
executions>
plugin>
plugins>
pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<version>3.8.0version>
<configuration>
<source>${maven.compiler.source}source>
<target>${maven.compiler.target}target>
configuration>
plugin>
plugins>
build>
扩展:在 parent pom 文件中为什么需要定义
标签?
原因是:parent pom 文件中没有继承 spring-boot-starter-parent pom 文件,即没有下面的配置
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.1.9.RELEASEversion>
parent>
如果没有自定义上面的
标签,则通过 mvn 打包的程序无法运行,提升错误:jar中没有主清单属性,根本原因是jar包中缺少main-class。查看mvn仓库关于spring-boot-starter-parent pom 文件的内容,发现已经定于了
标签,把相应的定义copy 过来,就解决问题了。
为什么不能继承 spring-boot-starter-parent pom 文件呢? 原因是:interface / util 项目不需要继承,只有真正需要运行的程序(无论是web 还是 console 程序,即有main函数的项目),才需要继承 spring-boot-starter-parent pom 文件
dubbo common , provider 和 consumer 共享模块
<parent>
<artifactId>dubbo-demoartifactId>
<groupId>com.examplegroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<groupId>com.jianzh5groupId>
<artifactId>dubbo-commonartifactId>
<packaging>jarpackaging>
// 定义接口
public interface HelloService {
String sayHello();
}
dubbo provider : 注意,不需要 spring-boot-starter-web 依赖,它不是 web api项目,不对外提供服务
<parent>
<artifactId>dubbo-demoartifactId>
<groupId>com.examplegroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<groupId>com.examplegroupId>
<artifactId>dubbo-providerartifactId>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubboartifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-registry-nacosartifactId>
dependency>
<dependency>
<groupId>com.alibaba.nacosgroupId>
<artifactId>nacos-clientartifactId>
dependency>
<dependency>
<groupId>com.examplegroupId>
<artifactId>dubbo-apiartifactId>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
#配置文件
dubbo.scan.base-packages=com.example.demo.user.service
dubbo.application.name = dubbo-provider
dubbo.registry.address = nacos://192.168.136.129:8848
dubbo.protocol.port=20881
dubbo.protocol.name=dubbo
// 接口实现
@Service
public class HelloServiceImpl implements HelloService {
public String sayHello() {
Return "welcome to WeChat public address: JAVA RI Zhi Lu".
}
}
@EnableDubbo 注解的作用 :通过 @EnableDubbo
可以在指定的包名下(通过 scanBasePackages
),或者指定的类中(通过 scanBasePackageClasses
)扫描 Dubbo 的服务提供者(以 @Service
标注)以及 Dubbo 的服务消费者(以 Reference
标注),它是 @EnableDubboConfig
和 @DubboComponentScan
两者组合的便捷表达方式。
@SpringBootApplication
@EnableDubbo
public class PrivoderApplication {
public static void main(String[] args) {
SpringApplication.run(PrivoderBootstrap.class, args);
}
}
dubbo consumer : 注意,需要 spring-boot-starter-web 依赖,因为它是web api 项目,让外部访问
<parent>
<artifactId>dubbo-demoartifactId>
<groupId>com.examplegroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<groupId>com.examplegroupId>
<artifactId>dubbo-consumerartifactId>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubboartifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-registry-nacosartifactId>
dependency>
<dependency>
<groupId>com.alibaba.nacosgroupId>
<artifactId>nacos-clientartifactId>
dependency>
<dependency>
<groupId>com.examplegroupId>
<artifactId>dubbo-apiartifactId>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
#配置文件
dubbo.scan.base-packages=com.example.demo.consumer
spring.application.name=dubbo-consumer
dubbo.registry.address = nacos://192.168.136.129:8848
server.port=9090
@RestController
public class HelloController {
@Reference
private HelloService helloService;
@GetMapping("/sayHello")
public String sayHello(){
return helloService.sayHello();
}
}
// 测试结果:如果没有 @EnableDubbo 注解,也是可以 run 的,但是如果provider没有此注解,就无法run,不知道为什么?
// 为了和官方文档一致,还是加上吧
@EnableDubbo
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerBootstrap.class, args);
}
}