一、包引入
1、父模块pom.xml中加入依赖:
<dependency> <groupId>com.alibaba.bootgroupId> <artifactId>dubbo-spring-boot-starterartifactId> <version>0.2.0version> dependency>
我这里还引入了SpringBoot和一些常用组件
<parent> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-parentartifactId> <version>2.0.4.RELEASEversion> parent> <dependencies> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starterartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-testartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-devtoolsartifactId> <version>2.0.4.RELEASEversion> dependency> <dependency> <groupId>org.projectlombokgroupId> <artifactId>lombokartifactId> <version>1.16.22version> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-data-jpaartifactId> <version>2.0.4.RELEASEversion> dependency> <dependency> <groupId>com.alibaba.bootgroupId> <artifactId>dubbo-spring-boot-starterartifactId> <version>0.2.0version> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-webartifactId> dependency> <dependency> <groupId>com.alibabagroupId> <artifactId>fastjsonartifactId> <version>1.2.51version> 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.yungoal.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 TenantProvider implements ITenantProvider { @Resource private TenantBusiness tenantBusiness; @Override public FuncResultBocreateCcUser(CcUserVo user, ApiKeyVo api_info) { return tenantBusiness.createCcUser(user, api_info); } }
3、服务提供者SpringBoot启动类中加入:@EnableDubbo属性标记,例如我的:
package com.yungoal.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.yungoal"}) @EnableJpaRepositories(basePackages = {"com.yungoal.repository"}) @EnableAutoConfiguration @EntityScan("com.yungoal.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.yungoal.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.yungoal.domain.po") @ComponentScan("com.yungoal") @PropertySource({"classpath:schedule.properties"}) @EnableJpaRepositories(basePackages = "com.yungoal.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;