springboot使用dubbo,dubbo的序列化方式
目录
1、服务端定义1.1 服务定义及实现1.2 服务提供者配置1.3 启动类1.4 pom文件2、消费端定义2.1杜博配置类2.2 启动类2.3 pom文件3、 运行结果4 、结论5、改进5.1 关于服务的实现5.2 在服务端配置中增加代码杜博的泛化调用不需要引入调用方的接口,只需要指定接口的全类名,就可以调用服务,一般用于框架集成。接下来就基于跳羚实现了杜博的泛化调用。
1、服务端定义
1.1 服务定义及实现
包com。聪明。弹簧靴。API公共接口HelloServiceFacade { void say hello();}打包com。聪明。弹簧靴。服务;导入com。阿里巴巴。杜博。配置。注释。服务;导入com。聪明。弹簧靴。API。helloservicefacade/* * * * @作者: cuan tianhou 2020/1/14 */@ Service(version= 1。0 .0 ,超时=20000)公共类示例服务实现hello Service facade { @ Override public void say hello(){ system。出去。println( 123 );}}
1.2 服务提供者配置
包com。聪明。弹簧靴。配置;导入com。阿里巴巴。杜博。配置。应用程序配置;导入com。阿里巴巴。杜博。配置。监视器配置;导入com。阿里巴巴。杜博。配置。协议配置;导入com。阿里巴巴。杜博。配置。注册表配置;导入org。spring框架。语境。注释。豆;导入org。spring框架。语境。注释。配置;/* * * * @ author : cuantianhou 2020/4/1 */@ configuration公共类DubboServiceConfig { @ Bean公共应用配置application config(){ application config application config=new application config();应用程序配置。setid( spring boot-dubbo-provider );应用程序配置。设置名称(“spring boot-dubbo-provider”);返回应用程序配置;} @ Bean公共协议配置协议配置(){协议配置协议配置=新协议配置();协议配置。setport(20880);协议配置。setname(“dubbo”);返回protocolConfig} @ Bean公共注册表配置registry config(){ registry config registry config=new registry config();注册表配置。设置地址( zookeeper ://IP 1:2181?backup=IP 2:2181’);注册表配置。设置检查(假);返回registryConfig} @ Bean公共monitor config monitor config(){ monitor config monitor config=new monitor config();monitorConfig.setProtocol("注册表");返回监控配置
}}
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;/** * 启动类 * * @author : cuantianhou 2020/1/14 */@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;/** * @author : cuantianhou 2020/4/1 */@Configurationpublic 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;/** * @author : cuantianhou 2020/1/14 */@SpringBootApplicationpublic 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 关于服务的实现
/** * @author : cuantianhou 2020/1/14 */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泛化调用内容请搜索盛行IT以前的文章或继续浏览下面的相关文章希望大家以后多多支持盛行IT!
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。