一、创建springboot-dubbo-provider maven springboot工程 引入一下pom依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.101tec/zkclient -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
二、编写application.properties/.yml文件
dubbo.application.name=springboot-dubbo-provider
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.scan.base-packages=com.xxx.service
server.port=8888
三、编写启动文件Application.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
四、编写provider service接口及其实现类
public interface ProvideService {
public String sayHello(String name);
}
import com.alibaba.dubbo.config.annotation.Service;
import com.camelot.service.ProvideService;
import org.springframework.stereotype.Component;
@Component
@Service//该注解为dubbo的@service注解
public class ProvideServiceImpl implements ProvideService {
public String sayHello(String name) {
return "-------hello" + name;
}
}
五、启动zookeeper和dubbo-admin-2.8.4,发现提供者可以运行了
六、创建dubbo的消费者maven springboot工程(依赖和dubbo提供者相同)
编写application.properties/.yml文件
dubbo.application.name=springboot-dubbo-consumer
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.scan.base-packages=com.xxx.service
server.port=8888
七、编写consumer service接口及其实现类
public interface ConsumerService {
public String sayHello(String name);
}
import com.alibaba.dubbo.config.annotation.Reference;
import com.camelot.service.ProvideService;
import org.springframework.stereotype.Service;
@Service//该注解为spring的@service注解
public class ConsumerServiceImpl {
@Reference//dubbo注解 private ProvideService provideService; public String sayHello(String name) { return provideService.sayHello(name); }
此外,还需要引入提供者的service(要求包名相同)
@Service
public interface ProvideService {
public String sayHello(String name);
}
八、编写consumer的测试类
import com.alibaba.dubbo.config.annotation.Reference;
import com.camelot.service.ProvideService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTest {
@Reference
private ProvideService provideService;
@Test
public void getMessage() {
String sayHello = provideService.sayHello(" John-------");
System.out.println(sayHello);
}
}
九、运行consumer工程,发现提供者消费者都在dubbo-admin上显示了
十、代码链接