- 在nacos已经存在的情况下,springboot配置dubbo集成,pom引入依赖包
<dubbo-spring-boot-starter.version>0.2.1.RELEASE</dubbo-spring-boot-starter.version>
<nacos-discovery-spring-boot-starter.version>0.2.1</nacos-discovery-spring-boot-starter.version>
<dubbo.version>2.6.5</dubbo.version>
<dubbo-registry-nacos.version>0.0.1</dubbo-registry-nacos.version>
<dubbo-spring-context-support.version>1.0.2</dubbo-spring-context-support.version>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
<version>${dubbo-registry-nacos.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.spring</groupId>
<artifactId>spring-context-support</artifactId>
<version>${dubbo-spring-context-support.version}</version>
</dependency>
<!-- dubbo + nacos end -->
<!-- dubbo + spring-boot + nacos start -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo-spring-boot-starter.version}</version>
<exclusions>
<exclusion>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-discovery-spring-boot-starter</artifactId>
<version>${nacos-discovery-spring-boot-starter.version}</version>
</dependency>
工程application.yml配置dubbo,nacos相关信息
dubbo:
application:
name: 应用名
version: 版本说明
registry:
address: nacos://ip:8848
scan:
base-packages: xxxx.service
protocol:
name: dubbo
port: 20880
定义注册的接口
@Service
public interface IFileService {
public Map<String, String> storageFile(String businessType, String fileName,
String corpCode, byte[] fileData, String systemType)
throws Exception;
}
调用接口的类注入上述接口
import com.alibaba.dubbo.config.annotation.Reference;
@Reference
private IFileService fileService;
- springboot使用hessian实现rpc,无须服务注册和服务发现,使用http方式发布接口和调用接口
引入依赖
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.51</version>
</dependency>
服务提供方
@Bean(name = "/rpc/appRedisService")
public HessianServiceExporter appRedisService() {
HessianServiceExporter exporter = new HessianServiceExporter();
exporter.setService(appRedisServiceImpl);
exporter.setServiceInterface(IRedisAppService.class);
return exporter;
}
服务调用方
@Bean
public HessianProxyFactoryBean redisRpcClient() {
HessianProxyFactoryBean hessianProxyFactory = new HessianProxyFactoryBean();
hessianProxyFactory.setServiceUrl("http://IP:port/rpc/redisService");
hessianProxyFactory.setServiceInterface(IRedisService.class);
return hessianProxyFactory;
}