先分享下源码:
https://download.csdn.net/download/weixin_42209881/11926219
我的总目录结构:
总pom.xml内容:
<?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>
<packaging>pom</packaging>
<modules>
<module>dubbo-api</module>
<module>dubbo-provider</module>
<module>dubbo-consumer</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringDubboDemo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<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>
<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>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/com.101tec/zkclient -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
注意一点:dubbo,springboot,zookeeper的版本基本上要保持配套,如果版本差异过大,会出现各种异常,并很难发现
公共接口: dubbo-api
不需要配置文件,application.properties文件为空
服务提供者:dubbo-provider
里面配置文件和Service实现类如下:
server.port=8081
spring.application.name=dubbo-provider
spring.dubbo.application.name=service-provider
spring.dubbo.base-package=com.dubbo.provider.service
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.registry.port=2181
spring.dubbo.registry.protocol=zookeeper
spring.dubbo.registry.register = true
spring.dubbo.protocol.host=127.0.0.1
spring.dubbo.protocol.name = dubbo
spring.dubbo.protocol.port = 20880
spring.dubbo.protocol.accesslog = true
spring.dubbo.provider.retries = 1
spring.dubbo.provider.delay = -1
接口的实现类:
package com.dubbo.provider.service;
import com.alibaba.dubbo.config.annotation.Service;
import com.dubbo.demo.ProviderService;
import org.springframework.stereotype.Component;
@Service(version = "1.0.0",interfaceClass = ProviderService.class)
@Component
public class ProviderServiceImpl implements ProviderService {
@Override
public String sayHello(String name) {
System.out.println("执行这里---------------->" + name);
return "Hello " + name + ",德玛西亚万岁";
}
}
启动类配置:
package com.dubbo.provider;
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubboConfiguration
public class SpringDubboDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringDubboDemoApplication.class, args);
}
}
服务消费者: dubbo-consumer
这里注意:不要信网上其他人说的什么包名必须一致的原则,毛线。我起初就不相信一个这么优秀的框架怎么会有这么无脑的规定,结果我自己测了下,和预期完全一样,实践出真理。
看调用的接口:
package com.dubbo.consumer.rest;
import com.dubbo.demo.ProviderService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.dubbo.config.annotation.*;
@RestController
public class ConsumerController{
//使用兼容注入,可以使用dubbo原生注解@Reference注入
@Reference(version = "1.0.0")
public ProviderService service;
@GetMapping(value = "/hello/{name}")
public String hello(@PathVariable String name){
System.out.println("这里执行--------------》" + name);
return service.sayHello(name);
}
}
配置文件:
#避免和服务端口冲突
server.port=8082
spring.application.name=dubbo-consumer
spring.dubbo.application.name=service-consumer
spring.dubbo.base-package=com.dubbo.consumer.rest
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.registry.port=2181
spring.dubbo.registry.protocol = zookeeper
spring.dubbo.registry.subscribe=true
spring.dubbo.protocol.name = dubbo
spring.dubbo.protocol.host = 127.0.0.1
spring.dubbo.consumer.timeout = 1000
spring.dubbo.consumer.check = false
spring.dubbo.consumer.retries = 2
启动类:
package com.dubbo.consumer;
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubboConfiguration
public class SpringDubboConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringDubboConsumerApplication.class, args);
}
}
这代码部分就完成了,然后按照顺序,先启动zookeeper,然后服务端提供者,然后服务消费者,启动完毕。
测试一把:
这就大功告成了