Dubbo官方推荐使用Zookeeper作为注册中心
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<version>2.0.0.RELEASEversion>
dependency>
<dependency>
<groupId>com.alibaba.bootgroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
<version>0.2.0version>
dependency>
<dependency>
<groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
<version>3.4.8version>
dependency>
<dependency>
<groupId>com.101tecgroupId>
<artifactId>zkclientartifactId>
<version>0.10version>
dependency>
dependencies>
public interface HelloService {
public String sayHello(String message);
}
@Service(interfaceClass = HelloService.class)
public class HelloServiceImpl implements HelloService {
public String sayHello(String message) {
return "hello:"+message;
}
}
这个地方的@Service是Dubbo框架中的注解,然后需要指明接口
配置文件的配置如下:
server:
port: 9001
dubbo:
application:
# 服务名称,保持唯一
name: server-provider
# zookeeper地址,用于向其注册服务
registry:
protocal: zookeeper
address: zookeeper://127.0.0.1:2181
#address: zookeeper://127.0.0.1:2181?backup=127.0.0.1:2182,127.0.0.1:2183
protocol:
name: dubbo
port: 20880
scan: com.pyh.provider.service
启动类:
@EnableDubbo
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
System.out.println("=============start============");
SpringApplication.run(MainApplication.class,args);
System.out.println("=============end============");
}
}
@RestController
public class HelloController {
@com.alibaba.dubbo.config.annotation.Reference
private HelloService helloService;
@RequestMapping("sayHello")
public String sayHello(String message){
return helloService.sayHello(message);
}
}
这个地方需要注意下,一定要使用dubbo的@reference注解,不然消费者就不能注册到dubbo中,我在这个地方折腾了好久,人都快崩溃了,切记,切记,切记!
配置文件:
server:
port: 9002
dubbo:
application:
name: server-consumer
registry:
protocol: zookeeper
address: zookeeper://127.0.0.1:2181
#address: zookeeper://127.0.0.1:2181?backup=127.0.0.1:2182,127.0.0.1:2183
scan:
base-packages: com.pyh.consumer
启动类:
@SpringBootApplication
@EnableDubboConfig
public class MainApplication {
public static void main(String[] args) {
System.out.println("=============start===============");
SpringApplication.run(MainApplication.class,args);
System.out.println("=============end===============");
}
}
启动完成后就会在dubbo的页面看到服务的提供者和消费者,到此就搭建完成了
出现一个很小的问题,但是耗费了好长时间才解决,在server-consumer中使用过的@reference不是dubbo中的,导致注册失败!
Dubbo 中有个 注册中心 这个东西,官方推荐的虽然是 Zookeeper,但由于 Zookeeper 东西也比较多,我们本篇博客暂时使用 Multicast 注册中心 带大家入门。
Multicast 只适用于学习和测试,非常不推荐用于实际生产,这也是官方说的。
Multicast 注册中心 不需要启动任何中心节点,只要广播地址一样,就可以互相发现。下面是官方提供的结构图:
- 提供方启动时广播自己的地址;
- 消费方启动时广播订阅请求;
- 提供方收到订阅请求时,单播自己的地址给订阅者,如果设置了 unicast=false,则广播给订阅者;
- 消费方收到提供方地址时,连接该地址进行 RPC 调用。
由组播受网络结构限制,只适合小规模应用或开发阶段使用。组播地址段: 224.0.0.0 - 239.255.255.255。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<packaging>pompackaging>
<modules>
<module>dubbo-providermodule>
<module>dubbo-consumermodule>
<module>dubbo-apimodule>
modules>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.5.4version>
<relativePath/>
parent>
<groupId>com.demogroupId>
<artifactId>dubbo-demoartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>dubbo-demoname>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
<dubbo.version>2.7.3dubbo.version>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
<version>${dubbo.version}version>
dependency>
dependencies>
dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>com.demogroupId>
<artifactId>dubbo-demoartifactId>
<version>0.0.1-SNAPSHOTversion>
parent>
<groupId>com.demogroupId>
<artifactId>dubbo-apiartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>dubbo-apiname>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
public class User implements Serializable {
private Integer id;
private String name;
// set..() & get..()
// toString()
}
public interface UserService {
User selectUserById(Integer id);
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.demo</groupId>
<artifactId>dubbo-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.demo</groupId>
<artifactId>dubbo-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dubbo-provider</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- dubbo-api依赖 -->
<dependency>
<groupId>com.demo</groupId>
<artifactId>dubbo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
#防止端口冲突
server:
port: 8001
dubbo:
application:
# 应用名称
name: provider
registry:
# 注册中心地址
address: multicast://224.5.6.7:1234
# 超时时间,单位毫秒
timeout: 6000
protocol:
#协议名称
name: dubbo
#协议端口
port: 20880
scan:
#扫描包的位置
base-packages: com.demo.service
import org.apache.dubbo.config.annotation.Service;
@Service(version = "1.0")
public class UserServiceImpl implements UserService {
@Override
public User selectUserById(Integer id) {
User user = new User();
user.setId(id);
user.setName("张三");
return user;
}
}
@SpringBootApplication
public class DubboProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DubboProviderApplication.class, args);
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>com.demogroupId>
<artifactId>dubbo-demoartifactId>
<version>0.0.1-SNAPSHOTversion>
parent>
<groupId>com.demogroupId>
<artifactId>dubbo-consumerartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>dubbo-consumername>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>com.demogroupId>
<artifactId>dubbo-apiartifactId>
<version>0.0.1-SNAPSHOTversion>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
#防止端口冲突
server:
port: 8002
dubbo:
application:
# 应用名称
name: consumer
registry:
# 注册中心地址
address: multicast://224.5.6.7:1234
@Component
public class UserInit implements CommandLineRunner {
@Reference(version = "1.0",parameters = {"unicast","false"})
private UserService userService;
@Override
public void run(String... args) throws Exception {
System.out.println(userService.selectUserById(2));
}
}
@SpringBootApplication
public class DubboConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(DubboConsumerApplication.class, args);
}
}
先执行 DubboProviderApplication ( 服务提供者程序),再执行 DubboConsumerApplication ( 服务消费者程序)。
有了前面的Multicast的经验,用Redis作为注册中心就很容易了。
环境搭建都大致相同。
<dependency>
<groupId>com.alibaba.spring.bootgroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
<version>1.0.0version>
dependency>
application.properties
server.port=80
spring.dubbo.appname=spring-boot-starter-dubbo-provider-test
spring.dubbo.registry=redis://127.0.0.1:6379
spring.dubbo.protocol=dubbo
服务提供者启动类
@SpringBootApplication
@DubboComponentScan(basePackages = "com.rookie.*.impl")
public class ProviderApp {
public static void main(String[] args) {
SpringApplication.run(ProviderApp.class, args);
}
}
服务提供者接口实现类
@Service(interfaceClass = DemoService.class)
public class DemoServiceImpl implements DemoService {
@Override
public User getUser() {
return new User("1", "2");
}
}
application.properties
server.port=8080
spring.dubbo.appname=spring-boot-starter-dubbo-consumer-test
spring.dubbo.registry=redis://127.0.0.1:6379
spring.dubbo.protocol=dubbo
服务消费者启动类
@SpringBootApplication
@EnableDubboConfiguration
public class ConsumerApp {
public static void main(String[] args) {
SpringApplication.run(ConsumerApp.class, args);
}
}
服务消费者controller
@RestController
public class Demo {
@Reference(interfaceClass = DemoService.class)
private DemoService demoService;
@RequestMapping("/")
public void demo() {
System.out.println(demoService.getUser());
}
}
和multicast一样,不再赘述。