基于SpringBoot的Dubbo泛化调用的实现代码
短信预约 -IT技能 免费直播动态提醒
Dubbo的泛化调用不需要引入调用方的接口,只需要指定接口的全类名,就可以调用服务,一般用于框架集成。接下来就基于SpringBoot实现了Dubbo的泛化调用。
1、服务端定义
1.1 服务定义及实现
package com.smart.springboot.api;
public interface HelloServiceFacade {
void sayHello();
}
package com.smart.springboot.service;
import com.alibaba.dubbo.config.annotation.Service;
import com.smart.springboot.api.HelloServiceFacade;
@Service(version = "1.0.0",timeout = 20000)
public class HelloService implements HelloServiceFacade {
@Override
public void sayHello() {
System.out.println("123");
}
}
1.2 服务提供者配置
package com.smart.springboot.config;
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.MonitorConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class DubboServiceConfig {
@Bean
public ApplicationConfig applicationConfig(){
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setId("springboot-dubbo-provider");
applicationConfig.setName("springboot-dubbo-provider");
return applicationConfig;
}
@Bean
public ProtocolConfig protocolConfig() {
ProtocolConfig protocolConfig = new ProtocolConfig();
protocolConfig.setPort(20880);
protocolConfig.setName("dubbo");
return protocolConfig;
}
@Bean
public RegistryConfig registryConfig() {
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setAddress("zookeeper://ip1:2181?backup=ip2:2181");
registryConfig.setCheck(false);
return registryConfig;
}
@Bean
public MonitorConfig monitorConfig() {
MonitorConfig monitorConfig = new MonitorConfig();
monitorConfig.setProtocol("registry");
return monitorConfig;
}
}
1.3 启动类
package com.smart.springboot;
import com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@DubboComponentScan("com.smart.springboot")
public class SpringBootDubboProvider {
public static void main(String[] args) {
SpringApplication.run(SpringBootDubboProvider.class,args);
}
}
1.4 pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.smart.springboot</groupId>
<artifactId>springboot-dubbo-provider</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--引入dubbo环境-->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>com.smart.springboot</groupId>
<artifactId>springboot-dubbo-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、消费端定义
2.1 Dubbo配置类
package com.smart.springboot.config;
import com.alibaba.dubbo.config.*;
import com.alibaba.dubbo.rpc.service.GenericService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class DubboConsumerConfig {
@Bean
public ApplicationConfig applicationConfig(){
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setId("springboot-dubbo-consumer");
applicationConfig.setName("springboot-dubbo-consumer");
return applicationConfig;
}
@Bean
public ProtocolConfig protocolConfig() {
ProtocolConfig protocolConfig = new ProtocolConfig();
protocolConfig.setPort(20880);
protocolConfig.setName("dubbo");
return protocolConfig;
}
@Bean
public RegistryConfig registryConfig() {
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setAddress("zookeeper://10.0.20.121:2181?backup=10.0.20.131:2181,10.0.20.132:2181,10.0.20.133:2181");
registryConfig.setCheck(false);
return registryConfig;
}
@Bean
public MonitorConfig monitorConfig() {
MonitorConfig monitorConfig = new MonitorConfig();
monitorConfig.setProtocol("registry");
return monitorConfig;
}
@Bean
public ReferenceConfig<GenericService> referenceConfig(){
ReferenceConfig<GenericService> referenceConfig = new ReferenceConfig<>();
referenceConfig.setInterface("com.smart.springboot.api.HelloServiceFacade");
referenceConfig.setApplication(applicationConfig());
referenceConfig.setRegistry(registryConfig());
referenceConfig.setMonitor(monitorConfig());
referenceConfig.setVersion("1.0.0");
referenceConfig.setTimeout(20000);
referenceConfig.setId("helloService");
referenceConfig.setGeneric(Boolean.TRUE);
return referenceConfig;
}
}
2.2 启动类
package com.smart.springboot;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.rpc.service.GenericService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import java.util.Arrays;
@SpringBootApplication
public class SpringBootDubboConsumer {
public static void main(String[] args) {
ConfigurableApplicationContext configurableApplicationContext = SpringApplication.run(SpringBootDubboConsumer.class,args);
String[] beanNames = configurableApplicationContext.getBeanDefinitionNames();
Arrays.asList(beanNames).forEach(System.out::println);
ReferenceConfig<GenericService> referenceConfig = configurableApplicationContext.getBean(ReferenceConfig.class);
GenericService genericService = referenceConfig.get();
genericService.$invoke("sayHello",null,null);
}
}
2.3 pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.smart.springboot</groupId>
<artifactId>springboot-dubbo-consumer</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--引入dubbo环境-->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3、 运行结果
4 、结论
从泛化调用实现的过程来看,我们可以对自己提供所有服务进行测试,不需要引入调用的接口,减少代码的侵入,如有问题,欢迎指正。
5、改进
关于服务暴露的改进
5.1 关于服务的实现
public class HelloService implements HelloServiceFacade {
@Override
public void sayHello() {
System.out.println("123");
}
}
5.2 在服务端配置中增加代码
@Bean
public ServiceConfig<HelloServiceFacade> serviceConfig(){
ServiceConfig<HelloServiceFacade> serviceConfig = new ServiceConfig<>();
serviceConfig.setInterface("com.smart.springboot.api.HelloServiceFacade");
serviceConfig.setRef(helloService());
serviceConfig.setApplication(applicationConfig());
serviceConfig.setRegistry(registryConfig());
serviceConfig.setMonitor(monitorConfig());
serviceConfig.setVersion("1.0.0");
serviceConfig.setTimeout(20000);
serviceConfig.setId("helloService");
serviceConfig.export();
return serviceConfig;
}
@Bean
public HelloService helloService(){
return new HelloService();
}
到此这篇关于基于SpringBoot的Dubbo泛化调用的实现代码的文章就介绍到这了,更多相关SpringBoot 泛化调用内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341