Apache Dubbo 是一款高性能、轻量级的开源Java RPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。
下载地址:http://zookeeper.apache.org/releases.html
下载完毕后解压缩,在zookeeper的conf目录下 把 zoo_sample.cfg复制一份重命名为 zoo.cfg ,然后只想 bin 目录下的 zkServer.cmd
tickTime=2000
initLimit=10
syncLimit=5
#这个参数可以改为自己的想要的位置
dataDir=../data
#zookeeper的启动端口
clientPort=2181
源码地址:https://github.com/apache/dubbo-admin
通过git clone 来回来源码自己编译,mvn clean package ,
编译之前需要改一下配置 dubbo-admin\dubbo-admin-server\src\main\resources\application.properties
#如果要换启动端口可以在这设置
server.port=8081
# centers in dubbo2.7
admin.registry.address=zookeeper://127.0.0.1:2181
admin.config-center=zookeeper://127.0.0.1:2181
admin.metadata-report.address=zookeeper://127.0.0.1:2181
#group
admin.registry.group=dubbo
admin.config-center.group=dubbo
admin.metadata-report.group=dubbo
admin.apollo.token=e16e5cd903fd0c97a116c873b448544b9d086de9
admin.apollo.appId=test
admin.apollo.env=dev
admin.apollo.cluster=default
admin.apollo.namespace=dubbo
编译完执行 jave -jar dubbo-admin\dubbo-admin-distribution\target\dubbo-admin-0.1.jar
访问本地 dubbo-admin
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
<version>2.7.3version>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-dependencies-zookeeperartifactId>
<version>2.7.3version>
<type>pomtype>
<exclusions>
<exclusion>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
exclusion>
exclusions>
dependency>
server:
port: 9080
spring:
application:
name: dubbo-p
dubbo:
scan:
base-packages: me.zingon.test.dubbop
protocol:
name: dubbo
port: -1
registry:
address: zookeeper://127.0.0.1:${embedded.zookeeper.port}
file: ${user.home}/dubbo-cache/${spring.application.name}/dubbo.cache
metadata-report:
address: zookeeper://127.0.0.1:2181
embedded:
zookeeper:
port: 2181
public interface TestService{
Object getTest(String id);
}
import me.zingon.test.interfac.TestService;
import org.apache.dubbo.config.annotation.Service;
import java.util.HashMap;
import java.util.Map;
//注意这个注解,可以通过此注解设置相关参数,如超时,重试次数。。。
@Service
public class TestServiceImpl implements TestService {
@Override
public Object getTest(String id) {
Map<String,Object> result=new HashMap<>();
result.put("id",id);
result.put("test","this is dubbop");
return result;
}
}
启动
此dubbo服务提供者已完成,只需要启动此sproogboot项目,在dubbo-admin中即可看到此服务
spring:
application:
name: dubbo-c
embedded:
zookeeper:
port: 2181
dubbo:
registry:
address: zookeeper://127.0.0.1:${embedded.zookeeper.port}
file: ${user.home}/dubbo-cache/${spring.application.name}/dubbo.cache
import me.zingon.test.interfac.TestService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.util.Map;
@Service
public class CallService {
//通过此注解注入 dubbo 服务提供者的接口
@Reference
private TestService testService;
@PostConstruct
public void test(){
Map<String,Object> result= (Map<String, Object>) testService.getTest("asdasd");
System.out.println(result.size());
result.forEach((k,v)->{
System.out.println(k+":"+v);
});
}
}
/*
最后执行结果:
2
test:this is dubbop
id:asdasd
*/
通过starter+注解的方式使用 dubbo 非常方便简洁