本项目是基于nacos作为注册中心实现的dubbo实现RPC远程调用,没有接触到nacos的小伙伴可以先对nacos进行学习。
项目目录结构
父项目的pom文件
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.0.RELEASE</spring-boot.version>
</properties>
<modules>
<module>springcloud-dubbo-sample-api</module>
<module>springcloud-dubbo-sample-provider</module>
<module>springcloud-dubbo-sample-consumer</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
统一访问接口API
public interface ISayHelloService {
String sayHello();
}
provide服务提供者
pom文件
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.apache.httpcomponents</groupId>-->
<!-- <artifactId>httpclient</artifactId>-->
<!-- <version>4.5.8</version>-->
<!-- </dependency>-->
<dependency>
<groupId>com.jgh</groupId>
<artifactId>springcloud-dubbo-sample-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
</dependencies>
暴露接口
import com.jgh.dubbo.service.ISayHelloService;
import org.apache.dubbo.config.annotation.Service;
/**
* @program: dubbo-springcloud-example
* @description:
* @author: 是小浩呀~
* @create: 2023-06-10 09:10
**/
@Service
public class sayHelloServiceImpl implements ISayHelloService {
public String sayHello() {
return "hello~!!!!";
}
}
注意:此处的@Service注解不是spring官方加载spring容器的bean配置的Service,而是dubbo用来暴露服务的Service注解
application.properties配置文件(nacos配置成自己的nacos地址)
spring.application.name=spring-cloud-dubbo-sample-provider
dubbo.scan.base-packages=com.jgh.dubbo.service.impl
dubbo.protocol.port=20882
dubbo.protocol.name=dubbo
spring.cloud.nacos.discovery.server-addr=192.168.56.1:8848
springboot启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @program: dubbo-springcloud-example
* @description:
* @author: 是小浩呀~
* @create: 2023-06-10 09:26
**/
@EnableDiscoveryClient
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class,args);
}
}
consumer服务消费者
pom文件
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.jgh</groupId>
<artifactId>springcloud-dubbo-sample-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
application.properties文件
spring.application.name=spring-cloud-dubbo-sample-consumer
spring.cloud.nacos.discovery.server-addr=192.168.56.1:8848
springboot启动类
为了方便,直接在启动类上添加测试接口@RestController,请各位自行优化
import com.jgh.dubbo.service.ISayHelloService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @program: dubbo-springcloud-example
* @description:
* @author: 是小浩呀~
* @create: 2023-06-10 09:45
**/
@RestController
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class,args);
}
@Reference
ISayHelloService sayHelloService;
@GetMapping("/say")
public String say(){
String s = sayHelloService.sayHello();
return s;
}
}
其中,@Reference是获取服务提供者注册在nacos的服务,该过程使用动态代理的方式实现。
最后最后,先启动provide,在启动consumer。然后就能成功的调用啦。。。