0、docker 启动 zk
docker-compose.yml
version: '3'
services:
zoo1:
image: zookeeper
restart: always
hostname: zoo1
ports:
- 2181:2181
docker-compose up -d
1、创建父 pom
4.0.0
com.csp
dubbo-exam
pom
1.0-SNAPSHOT
dubbo-api
dubbo-provider
dubbo-consumer
org.springframework.boot
spring-boot-starter-parent
2.1.7.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.apache.dubbo
dubbo-spring-boot-starter
2.7.7
org.slf4j
slf4j-log4j12
org.apache.dubbo
dubbo
2.7.7
org.apache.curator
curator-framework
4.0.1
org.apache.curator
curator-recipes
2.8.0
org.apache.zookeeper
zookeeper
3.4.13
pom
org.slf4j
slf4j-log4j12
com.101tec
zkclient
0.10
2、创建 api maven 工程
package com.csp.dubbo.demo;
public interface TestApiService {
String sayHello(String name);
}
3、创建服务提供者
Application
package com.csp.dubbo.demo.provider;
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);
}
}
TestApiServiceImpl
package com.csp.dubbo.demo.provider.impl;
import com.csp.dubbo.demo.TestApiService;
import org.apache.dubbo.config.annotation.DubboService;
@DubboService(version = "1.0.0", interfaceClass = TestApiService.class)
public class TestApiServiceImpl implements TestApiService {
@Override
public String sayHello(String name) {
System.out.println("我是 provider, 我被调用了");
return name + " say hello";
}
}
application.properties
#spring项目名
spring.application.name=dubbo_auto_configuration_provider_demo
#Dubbo provider configuration
dubbo.application.name=dubbo_provider
dubbo.registry.protocol=zookeeper
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
#扫描注解包通过该设置将服务注册到zookeeper
dubbo.scan.base-packages=com.csp.dubbo.demo.provider
4、创建消费者
Application
package com.csp.dubbo.demo.consumer;
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);
}
}
TestController
package com.csp.dubbo.demo.consumer.controller;
import com.csp.dubbo.demo.Test2Service;
import com.csp.dubbo.demo.TestApiService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@DubboReference(version = "1.0.0")
TestApiService testApiService;
@GetMapping("/sayHello")
public Object sayHello(String name) {
String hello = testApiService.sayHello(name);
return hello;
}
}
application.properties
#spring项目名
spring.application.name=dubbo_auto_configuration_consumer_demo
#Dubbo provider configuration
dubbo.application.name=dubbo_consumer
dubbo.registry.protocol=zookeeper
dubbo.registry.address=zookeeper://127.0.0.1:2181
server.port=8082
5、总结
- provider 和 consumer 必须实现同一个接口。
- consumer 设置的 version 也要和 provider 写的 version 一致。
- 实际上,provider 是不需要启动 web 容器的!