废话不多说直接上代码
项目目录
├─.idea
├─apache-dubbo-consumer
│ ├─src
│ │ ├─main
│ │ │ ├─java
│ │ │ │ └─com
│ │ │ │ └─mrduan
│ │ │ └─resources
├─apache-dubbo-interface
│ ├─src
│ │ ├─main
│ │ │ ├─java
│ │ │ │ └─com
│ │ │ │ └─mrduan
│ │ │ │ └─service
│ │ │ └─resources
├─apache-dubbo-provider
│ ├─src
│ │ ├─main
│ │ │ ├─java
│ │ │ │ └─com
│ │ │ │ └─mrduan
│ │ │ │ └─service
│ │ │ │ └─impl
│ │ │ │ └─com
│ │ │ │ └─mrduan
│ │ │ │ └─service
│ │ │ └─resources
└─src
├─main
│ ├─java
│ └─resources
└─test
└─java
maven坐标
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>apache-dubbo</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>apache-dubbo-provider</module>
<module>apache-dubbo-consumer</module>
<module>apache-dubbo-interface</module>
</modules>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.6</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.10</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.5</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.32.Final</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.8.0</version>
</dependency>
</dependencies>
</project>
服务端
服务端只需要做4件事情 1.编写业务代码实现业务接口在apache-dubbo-interface中定义接口 然后在服务端和消费端都能引用的到
package com.mrduan.service;
public interface IProviderService {
String sayHello(String name);
}
2.在服务端编写接口实现 apache-dubbo-provider
package com.mrduan.service.impl;
import com.mrduan.service.IProviderService;
public class ProviderServiceImpl implements IProviderService {
public String sayHello(String name) {
return "hello," + name;
}
}
3.编写配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!--当前项目在整个分布式架构里面的唯一名称,计算依赖关系的标签-->
<dubbo:application name="provider" owner="mrduan">
<dubbo:parameter key="qos.enable" value="true"/>
<dubbo:parameter key="qos.accept.foreign.ip" value="false"/>
<dubbo:parameter key="qos.port" value="55555"/>
</dubbo:application>
<dubbo:monitor protocol="registry"/>
<!--dubbo这个服务所要暴露的服务地址所对应的注册中心-->
<!--<dubbo:registry address="N/A"/>-->
<dubbo:registry address="zookeeper://localhost:2181" check="false"/>
<!--当前服务发布所依赖的协议;webserovice、Thrift、Hessain、http-->
<dubbo:protocol name="dubbo" port="20880"/>
<!--服务发布的配置,需要暴露的服务接口-->
<dubbo:service interface="com.mrduan.service.IProviderService" ref="providerService"/>
<!--Bean bean定义-->
<bean id="providerService" class="com.mrduan.service.impl.ProviderServiceImpl"/>
</beans>
4.启动zk
5.编写启动类
package com.mrduan.service.impl.com.mrduan.service;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
public class ProviderStarter {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("provider.xml");
applicationContext.start();
System.in.read();
}
}
到次我们就可以启动项目了,执行main方法即可
消费端
1.编写配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!--当前项目在整个分布式架构里面的唯一名称,计算依赖关系的标签-->
<dubbo:application name="consumer" owner="mrduan"/>
<!--dubbo这个服务所要暴露的服务地址所对应的注册中心-->
<!--点对点的方式-->
<!--<dubbo:registry address="N/A" />-->
<dubbo:registry address="zookeeper://localhost:2181" check="false"/>
<!--生成一个远程服务的调用代理-->
<!--点对点方式-->
<!--<dubbo:reference id="providerService" interface="com.mrduan.service.IProviderService" url="dubbo://192.168.10.160:20880/com.mrduan.service.IProviderService"/>-->
<dubbo:reference id="providerService" interface="com.mrduan.service.IProviderService"/>
</beans>
2.编写启动类
package com.mrduan;
import com.mrduan.service.IProviderService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
public class ComsumerStarter {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("consumer.xml");
applicationContext.start();
IProviderService bean = applicationContext.getBean(IProviderService.class);
String bruce = bean.sayHello("Bruce");
System.out.println(bruce);
System.in.read();
}
}
如需自己编写微服务框架,可以参考 Netty手动实现Dubbo(含视频教程)