一、创建springboot-dubbo-provider maven springboot工程 引入一下pom依赖
org.springframework.boot
spring-boot-starter-parent
1.5.8.RELEASE
UTF-8
UTF-8
1.8
com.alibaba.boot
dubbo-spring-boot-starter
0.1.0
com.101tec
zkclient
0.2
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
二、编写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上显示了
十、代码链接
点击打开链接