一、介绍:
只适合局域网开发,与cloud同级都是分布式,比cloud简单,适合中小型企业开发,整体上是把nacos注册中心作为大型的ioc,将service方法、controller注册上去,这样通过nacos实现跨项目通信(controller(调用者) 通过接口(interface) 调用service(提供者)中的方法),本质上仍然是uri的请求通信,只是外表看的是调用关系。
controller就类似于cloud的gateway。,是个中台的作用。
二、流程:
1、父项目导包:(父包)
spring-boot-dependencies
dubbo-spring-boot-starter
dubbo-registry-nacos
alibaba.nacos-client
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>${nacos.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
2、子项目导包
//把需要的放入到nacos上的子项目(service、controller)
spring-boot-。。。web
dubbo-spring-boot-starter
dubbo-registry-nacos
alibaba.nacos-client
3、yml配置
//以service为例
server:
port: 8081
spring:
application:
name: service
dubbo:
application:
name: service
protocol:
name: dubbo
port: -1
registry:
address: nacos://服务器主机号:8848
timeout: 60000
check: false
scan: //只有service需要扫描,同考虑老ioc的使用
base-packages: com.neuedu.service
4、新建interface项目用来存接口
5、service放入到大小ioc中
@Component //小ioc
@Service //dubbo nacos的注册中心
public class TestServiceImpl implements TestInterface {
@Override
public Student getStudent() {
return new Student(1,"wukai","12234");
}
}
6、controller 远端注入 @Reference
@RestController
public class TestController {
@Reference //注意是dubbo的包
TestInterface testInterface;
@GetMapping("test")
Student test(){
return testInterface.getStudent();
}
}