研究了两天终于搭建成功,记录一下,也分享一下。
1、项目结构
建了三个项目,结构仅供参考
api:公共接口
consumer:消费者
provider:提供者
2、配置文件:
pom文件,这里就只贴一下依赖
parent的pom
<dependencies>
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.11</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
</dependencies>
provide的pom
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.dong</groupId>
<artifactId>api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
consumer的pom
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.dong</groupId>
<artifactId>api</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
application.properties
provider
## Dubbo 服务提供者配置
# Base packages to scan Dubbo Components (e.g., @Service, @Reference)
spring.dubbo.scan.basePackages = com.dong.serviceImpl
# Dubbo Config properties
## ApplicationConfig Bean
spring.dubbo.application.id = provider
spring.dubbo.application.name = provider
## ProtocolConfig Bean
spring.dubbo.protocol.id = dubbo
spring.dubbo.protocol.name = dubbo
spring.dubbo.protocol.port = 12345
## RegistryConfig Bean
spring.dubbo.registry.id = registry
spring.dubbo.registry.address = zookeeper://127.0.0.1:2181
server.port=8090
consumer
## Dubbo 服务提供者配置
# ApplicationConfig Bean
spring.dubbo.application.id = consumer
spring.dubbo.application.name = consumer
## ProtocolConfig Bean
spring.dubbo.protocol.id = dubbo
spring.dubbo.protocol.name = dubbo
spring.dubbo.protocol.port = 12345
spring.dubbo.registry.address = zookeeper://127.0.0.1:2181
server.port=8091
3、业务代码
api
定义公共接口
package com.dong.service;
public interface HelloService {
public String sayHello(String name);
}
provider
定义接口实现,并暴露服务,两个service注解都不可少,第一个spring用,第二个dubbo用
package com.dong.serviceImpl;
import com.dong.service.HelloService;
import org.springframework.stereotype.Service;
@Service
@com.alibaba.dubbo.config.annotation.Service(interfaceClass = HelloService.class)
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name) {
return "hello " + name;
}
}
consumer
(1)开启dubbo配置,(不开启注解@Reference不生效)
package com.dong;
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubboConfiguration
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
(2)引用接口调用服务
package com.dong.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.dong.service.HelloService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Reference
HelloService helloService;
@RequestMapping(value = "sayHello")
public String sayHello(String name) {
return helloService.sayHello(name);
}
}