一、包引入
1、父模块pom.xml中加入依赖:
com.alibaba.boot dubbo-spring-boot-starter 0.2.0
我这里还引入了SpringBoot和一些常用组件
org.springframework.boot spring-boot-starter-parent 2.0.4.RELEASE org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test org.springframework.boot spring-boot-devtools 2.0.4.RELEASE org.projectlombok lombok 1.16.22 org.springframework.boot spring-boot-starter-data-jpa 2.0.4.RELEASE com.alibaba.boot dubbo-spring-boot-starter 0.2.0 org.springframework.boot spring-boot-starter-web com.alibaba fastjson 1.2.51
2、服务提供者子模块的application.properties中加入下列配置
server.port=8084spring.application.name=dubbo-provider-usersdubbo.application.name=dubbo-provider-usersdubbo.registry.protocol=zookeeperdubbo.registry.address=172.16.4.132:2181|172.16.4.131:2181|172.16.4.133:2181dubbo.protocol.name=dubbodubbo.protocol.port=20884dubbo.scan.base-packages=com.yungoal.usersservicedubbo.consumer.timeout=3600000dubbo.provider.timeout=3600000dubbo.registry.timeout=3600000dubbo.consumer.retries=5dubbo.provider.retries=5dubbo.consumer.callbacks=3600000dubbo.provider.callbacks=3600000dubbo.consumer.check=falsedubbo.registry.check=falsedubbo.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")@RestControllerpublic 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")@EnableDubbopublic class UsersProviderApplication { public static void main(String[] args) { SpringApplication.run(UsersProviderApplication.class, args); }}
4、消费者的application.properties中加入:
spring.application.name=dubbo-consumer-backendsyncdubbo.application.name=dubbo-consumer-backendsyncdubbo.registry.protocol=zookeeperdubbo.registry.address=172.16.4.131:2181|172.16.4.132:2181|172.16.4.133:2181dubbo.scan.base-packages=com.yungoal.syncbackenddubbo.protocol.name=dubbodubbo.protocol.port=20880dubbo.consumer.timeout=3600000dubbo.provider.timeout=3600000dubbo.registry.timeout=3600000dubbo.consumer.retries=5dubbo.provider.retries=5dubbo.consumer.callbacks=3600000dubbo.provider.callbacks=3600000dubbo.consumer.check=truedubbo.registry.check=falsedubbo.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@EnableDubbopublic 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;