基于feign传参MultipartFile问题解决
510
2022-08-04
基于SpringBoot的Dubbo泛化调用的实现代码(dubbo默认序列化协议)
目录1、服务端定义1.1 服务定义及实现1.2 服务提供者配置1.3 启动类1.4 pom文件2、消费端定义2.1 Dubbo配置类2.2 启动类2.3 pom文件3、 运行结果4 、结论5、改进5.1 关于服务的实现5.2 在服务端配置中增加代码
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;
/**
* @author : cuantianhou 2020/1/14
*/
@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;
importhttp:// org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author : cuantianhou 2020/4/1
*/
@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;
/**
* 启动类
*
* @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文件
xmlns:xsi="http://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">
xmlns:xsi="http://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">
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
*/
@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
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.autocoJdexQKPfQBnfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import java.util.Arrays;
/**
* @author : cuantianhou 2020/1/14
*/
@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 genericService = referenceConfig.get();
genericService.$invoke("sayHello",null,null);
}
}
2.3 pom文件
xmlns:xsi="http://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">
xmlns:xsi="http://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">
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
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();
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~