Dubbo的使用入门
一、包引入
1、父模块pom.xml中加入依赖:
<!-- dubbo --> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.0</version> </dependency>
我这里还引入了SpringBoot和一些常用组件
<!--Add Spring boot Parent--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> </parent> <dependencies> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools --> <!--SpringBoot核心模块,包括自动配置支持、日志和YAML--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!--测试模块,包括JUnit、Hamcrest、Mockito--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <!--热部署--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <version>2.0.4.RELEASE</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.22</version> </dependency> <!-- JPA操作数据库 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <version>2.0.4.RELEASE</version> </dependency> <!-- dubbo --> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.0</version> </dependency> <!--Web模块--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.51</version> </dependency> </dependencies>
2、服务提供者子模块的application.properties中加入下列配置
server.port=8084 spring.application.name=dubbo-provider-users dubbo.application.name=dubbo-provider-users dubbo.registry.protocol=zookeeper dubbo.registry.address=172.16.4.132:2181|172.16.4.131:2181|172.16.4.133:2181 dubbo.protocol.name=dubbo dubbo.protocol.port=20884 dubbo.scan.base-packages=com.test.usersservice dubbo.consumer.timeout=3600000 dubbo.provider.timeout=3600000 dubbo.registry.timeout=3600000 dubbo.consumer.retries=5 dubbo.provider.retries=5 dubbo.consumer.callbacks=3600000 dubbo.provider.callbacks=3600000 dubbo.consumer.check=false dubbo.registry.check=false dubbo.application.qos-enable=false
发部服务,可以这样:
import com.alibaba.dubbo.config.annotation.Service; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import javax.validation.constraints.NotNull; import java.util.List; @Service(version = "1.0.0") @RestController public class UserProvider implements IUserProvider { @Resource private TenantBusiness tenantBusiness; @Override public FuncResultBo<UserVo> createUser(UserVo user, ApiKeyVo api_info) { return tenantBusiness.createUser(user, api_info); } }
3、服务提供者SpringBoot启动类中加入:@EnableDubbo属性标记,例如我的:
package com.test.usersservice; import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; @Configuration @SpringBootApplication() @ComponentScan({"com.test"}) @EnableJpaRepositories(basePackages = {"com.test.repository"}) @EnableAutoConfiguration @EntityScan("com.test.domain.po") @EnableDubbo public class UsersProviderApplication { public static void main(String[] args) { SpringApplication.run(UsersProviderApplication.class, args); } }
4、消费者的application.properties中加入:
spring.application.name=dubbo-consumer-backendsync dubbo.application.name=dubbo-consumer-backendsync dubbo.registry.protocol=zookeeper dubbo.registry.address=172.16.4.131:2181|172.16.4.132:2181|172.16.4.133:2181 dubbo.scan.base-packages=com.test.syncbackend dubbo.protocol.name=dubbo dubbo.protocol.port=20880 dubbo.consumer.timeout=3600000 dubbo.provider.timeout=3600000 dubbo.registry.timeout=3600000 dubbo.consumer.retries=5 dubbo.provider.retries=5 dubbo.consumer.callbacks=3600000 dubbo.provider.callbacks=3600000 dubbo.consumer.check=true dubbo.registry.check=false dubbo.application.qos-enable=false
5、在消费者启动类中,可以加入EnableDubbo属性标记
@SpringBootApplication @EnableAutoConfiguration @EntityScan("com.test.domain.po") @ComponentScan("com.test") @PropertySource({"classpath:schedule.properties"}) @EnableJpaRepositories(basePackages = "com.test.repository") @EnableScheduling @EnableDubbo public class BackendApplicationServer { public static void main(String[] args) { SpringApplicationBuilder springApplicationBuilder = new SpringApplicationBuilder(BackendApplicationServer.class); springApplicationBuilder.web(WebApplicationType.NONE); springApplicationBuilder.run(args); } }
消费者中调用服务:
@Reference(version = "1.0.0", timeout = 360000) private ITenantProvider iTenantProvider = null;
Reference属性类完整路径位于:import com.alibaba.dubbo.config.annotation.Reference;
宋兴柱:转载内容,请标明出处,谢谢!源文来自 宝贝云知识分享:https://www.dearcloud.cn