本示例是基于spring boot构建得父子 工程,不会搭建的请点这里:idea创建springboot父子工程背景使用Idea 创建一个模块化的SpringBoot项目,但是发现Idea 创建父子项目的方式较Eclipse 较为不同,且Idea 创建的过程较Eclipse创建父子项目的过程复杂。Eclipse 创建SpringBoot父子项目传送门网上虽然有Idea创建SpringBoot父子项目,但是携带各种其他的功能,导致无法简单的搞懂如何创建。下面就一个例子进行说明一下。构建简单SpringBoot父子项目环境说明:jdk1.8Idea 版本:2019.3(1)打开Idea,如果是新安装的软件,https://blog.csdn.net/qq_43842093/article/details/121940496
一、创建父项目
1.导入pom
org.springframework.boot spring-boot-starter-parent2.7.0
org.springframework.cloud spring-cloud-dependencies2021.0.1 pom import com.alibaba.cloud spring-cloud-alibaba-dependencies2021.0.1.0 pom import
二、创建公共api模块
public interface HelloService {
void sayHello();
}
三、创建服务提供方模块
1.导入pom
com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discoverycom.alibaba.cloud spring-cloud-starter-dubbocom.example dubbo-api0.0.1-SNAPSHOT
2.配置application.properties
server.port=8081 spring.application.name=provider spring.cloud.nacos.discovery.server-addr=192.168.221.132:8848 dubbo.scan.base-packages=com.provider.service spring.main.allow-circular-references=true
3.服务业务代码
@DubboService
public class HelloServiceImpl implements HelloService {
@Override
public void sayHello() {
System.out.println("hello,world");
}
}
四、创建服务消费方模块
1.导入pom
org.springframework.boot spring-boot-starter-webcom.alibaba.cloud spring-cloud-starter-alibaba-nacos-discoverycom.alibaba.cloud spring-cloud-starter-dubbocom.example dubbo-api0.0.1-SNAPSHOT
2.配置application.properties
spring.application.name=consumer server.port=8082 #本机同时启动服务提供方和消费方时,避免端口冲突 dubbo.protocol.port=-1 dubbo.cloud.subscribed-services=provider spring.cloud.nacos.discovery.server-addr=192.168.221.132:8848 spring.main.allow-circular-references=true
3.消费业务代码
@RestController
public class HelloController {
@DubboReference
private HelloService helloService;
@RequestMapping("/hello")
public void sayHello() {
helloService.sayHello();
}
}
五、测试
http://localhost:8082/hello
免费demo下载地址
springcloudalibaba集成dubbo示例-Java文档类资源-CSDN下载springcloudalibaba集成dubbo示例更多下载资源、学习资料请访问CSDN下载频道.https://download.csdn.net/download/u013059257/85445382



