使用springboot搭建dubbo服务,首先封装出springboot-dubbo的项目,引入在服务上加直接@Service直接使用,方便于拆封统一管理。
package hello.dubbo.configuration; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import com.alibaba.dubbo.config.ApplicationConfig; import com.alibaba.dubbo.config.ProtocolConfig; import com.alibaba.dubbo.config.ProviderConfig; import com.alibaba.dubbo.config.RegistryConfig; import com.alibaba.dubbo.config.spring.AnnotationBean; import com.alibaba.dubbo.rpc.Exporter; @Configuration @ConditionalOnClass(Exporter.class) @PropertySource(value = "classpath:/dubbo.properties") public class DubboConfiguration { @Value("${dubbo.application.name}") private String applicationName; @Value("${spring.redis.host}") private String logger; @Value("${dubbo.registr.protocol}") private String protocol; @Value("${dubbo.registry.address}") private String registryAddress; @Value("${dubbo.protocol.name}") private String protocolName; @Value("${dubbo.protocol.port}") private int protocolPort; @Value("${dubbo.provider.timeout}") private int timeout; @Value("${dubbo.provider.retries}") private int retries; @Value("${dubbo.provider.delay}") private int delay; /** * 设置dubbo扫描包 * @param packageName * @return */ @Bean public static AnnotationBean annotationBean(@Value("${dubbo.annotation.package}") String packageName) { AnnotationBean annotationBean = new AnnotationBean(); annotationBean.setPackage(packageName); return annotationBean; } /** * 注入dubbo上下文 * * @return */ @Bean public ApplicationConfig applicationConfig() { // 当前应用配置 ApplicationConfig applicationConfig = new ApplicationConfig(); applicationConfig.setName(this.applicationName); return applicationConfig; } /** * 注入dubbo注册中心配置,基于zookeeper * * @return */ @Bean public RegistryConfig registryConfig() { // 连接注册中心配置 RegistryConfig registry = new RegistryConfig(); registry.setProtocol(protocol); registry.setAddress(registryAddress); return registry; } /** * 默认基于dubbo协议提供服务 * * @return */ @Bean public ProtocolConfig protocolConfig() { // 服务提供者协议配置 ProtocolConfig protocolConfig = new ProtocolConfig(); protocolConfig.setName(protocolName); protocolConfig.setPort(protocolPort); protocolConfig.setThreads(200); System.out.println("默认protocolConfig:" + protocolConfig.hashCode()); return protocolConfig; } /** * dubbo服务提供 * * @param applicationConfig * @param registryConfig * @param protocolConfig * @return */ @Bean(name="defaultProvider") public ProviderConfig providerConfig(ApplicationConfig applicationConfig, RegistryConfig registryConfig, ProtocolConfig protocolConfig) { ProviderConfig providerConfig = new ProviderConfig(); providerConfig.setTimeout(timeout); providerConfig.setRetries(retries); providerConfig.setDelay(delay); providerConfig.setApplication(applicationConfig); providerConfig.setRegistry(registryConfig); providerConfig.setProtocol(protocolConfig); return providerConfig; } public String getApplicationName() { return applicationName; } public void setApplicationName(String applicationName) { this.applicationName = applicationName; } public String getLogger() { return logger; } public void setLogger(String logger) { this.logger = logger; } public String getProtocol() { return protocol; } public void setProtocol(String protocol) { this.protocol = protocol; } public String getRegistryAddress() { return registryAddress; } public void setRegistryAddress(String registryAddress) { this.registryAddress = registryAddress; } public String getProtocolName() { return protocolName; } public void setProtocolName(String protocolName) { this.protocolName = protocolName; } public int getProtocolPort() { return protocolPort; } public void setProtocolPort(int protocolPort) { this.protocolPort = protocolPort; } public int getTimeout() { return timeout; } public void setTimeout(int timeout) { this.timeout = timeout; } public int getRetries() { return retries; } public void setRetries(int retries) { this.retries = retries; } public int getDelay() { return delay; } public void setDelay(int delay) { this.delay = delay; } }
代码启动读取dubbo.properties资源文件,也可以把配置信息存在application.properties。
maven依赖配置文件pom.xml
<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.jon-spring</groupId> <artifactId>jon-spirng-boot-starter-dubbo</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.0.BUILD-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- dubbo依赖 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> <exclusions> <exclusion> <artifactId>spring</artifactId> <groupId>org.springframework</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> <compilerArguments> <extdirs>src/main/webapp/WEB-INF/lib</extdirs> </compilerArguments> </configuration> </plugin> </plugins> </build> </project>
基础dubbo服务项目配置成功!
dubbo服务项目依赖基础dubbo服务项目,即可使用dubbo服务
dubbo.properties文件
dubbo.application.name=application_ dubbo.application.logger=slf4j dubbo.annotation.package=hello.dubbo dubbo.protocol.name=dubbo dubbo.protocol.port=20880 dubbo.protocol.accessLog=true dubbo.provider.timeout=3000 dubbo.provider.retries=1 dubbo.provider.delay=-1 dubbo.registr.protocol=zookeeper dubbo.registry.address=127.0.0.1:2181 dubbo.registry.register=true dubbo.registry.subscribe=true
服务提供类
package hello.dubbo; import com.alibaba.dubbo.config.annotation.Service; import com.jon.show.service.IDubboDemoService; @Service(version="1.0.0") public class DubboDemoServiceImpl implements IDubboDemoService{ @Override public String sayHello(String name) { return "hello " + name; } @Override public String sayYourAge(int age) { return null; } }
application启动类
package hello; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.orm.jpa.vendor.HibernateJpaSessionFactoryBean; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } /** * 注入sessionfatory * @return */ @Bean public HibernateJpaSessionFactoryBean sessionFactory() { return new HibernateJpaSessionFactoryBean(); } }
dubbo服务配置成功!即可使用。
时间: 2024-11-08 20:44:20