<?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">
<parent>
<artifactId>dubbotest</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo_provider</artifactId>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
<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>
public interface ProviderService {
String sayHello(String name);
}
public class ProviderServiceImpl implements ProviderService {
public String sayHello(String name) {
return "Hello " + name;
}
}
<?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="laosun">
<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="N/A" />
<!--当前服务发布所依赖的协议;webserovice、Thrift、Hessain、http-->
<dubbo:protocol name="dubbo" port="20880"/>
<!--服务发布的配置,需要暴露的服务接口-->
<dubbo:service
interface="com.laosun.service.ProviderService"
ref="providerService"/>
<!--Bean bean定义-->
<bean id="providerService" class="com.laosun.service.impl.ProviderServiceImpl"/>
</beans>
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
public class Application {
public static void main( String[] args ) throws IOException, IOException {
//加载xml配置文件启动
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
context.start();
System.in.read(); // 按任意键退出
}
}
注意添加log4j.xml。
运行后,
注意图中dubbo的url,后续要用。
<?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">
<parent>
<artifactId>dubbotest</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo_consumer</artifactId>
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>dubbo_provider</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
<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>
<?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="sihai"/>
<!--dubbo这个服务所要暴露的服务地址所对应的注册中心-->
<!--点对点的方式-->
<dubbo:registry address="N/A" />
<!--<dubbo:registry address="zookeeper://localhost:2181" check="false"/>-->
<!--生成一个远程服务的调用代理-->
<!--点对点方式-->
<dubbo:reference id="providerService"
interface="com.laosun.service.ProviderService"
url="dubbo://10.4.16.50:20880/com.laosun.service.ProviderService"/>
<!--<dubbo:reference id="providerService"
interface="com.sihai.dubbo.provider.service.ProviderService"/>-->
</beans>
这里用到了服务端产生的dubbo的url。
package com.laosun.consumer;
import com.laosun.service.ProviderService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
public class Application {
public static void main( String[] args ) throws IOException {
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("consumer.xml");
context.start();
ProviderService providerService = (ProviderService) context.getBean("providerService");
String str = providerService.sayHello("laosun");
System.out.println(str);
System.in.read();
}
}
(1)官网下载链接,本文使用的是3.4.14版本,下载后解压。
(2)复制zookeeper-3.4.14\conf目录下的zoo_sample.cfg并改名为zoo.cfg,对其zoo.cfg配置进行修改:
dataDir=C:/ZHSUN/zookeeper-3.4.14/data
(3)cmd命令进入bin目录,输入命令:
zkServer.cmd
打开服务端
只需修改一句,将
<dubbo:registry address="N/A" />
修改为:
<dubbo:registry address="zookeeper://localhost:2181" check="false"/>
如果使用zookeeper,则改为:
<dubbo:registry protocol="zookeeper" address="192.168.11.129:2181,192.168.11.137:2181,192.168.11.138:2181"/>
(1)同provider.xml一样:
<dubbo:registry address="zookeeper://localhost:2181" check="false"/>
(2)使用zookeeper时,reference就不需要dubbo的url,改为:
<dubbo:reference id="providerService"
interface="com.laosun.service.ProviderService"/>
此方式不需要任何配置文件,只需要开启zookeeper,运行下面两个类即可。
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.ServiceConfig;
import com.laosun.service.ProviderService;
import com.laosun.service.impl.ProviderServiceImpl;
import java.io.IOException;
public class AppApi {
public static void main(String[] args) throws IOException {
// 生成服务端对象
ProviderService providerService = new ProviderServiceImpl();
// 当前应用配置
ApplicationConfig application = new ApplicationConfig();
application.setName("provider");
application.setOwner("laosun");
// 连接注册中心
RegistryConfig registry = new RegistryConfig();
registry.setAddress("zookeeper://127.0.0.1:2181");
// 服务提供者协议配置
ProtocolConfig protocol = new ProtocolConfig();
protocol.setName("dubbo");
protocol.setPort(20880);
// ServiceConfig内部封装了与注册中心的连接,以及开启服务端口,实例很重,正式应用需要进行缓存
// 服务者暴露服务配置
ServiceConfig<ProviderService> service = new ServiceConfig<ProviderService>();
service.setApplication(application);
service.setRegistry(registry);
service.setProtocol(protocol);
service.setInterface(ProviderService.class);
service.setRef(providerService);
service.setVersion("1.0.0");
// 暴露服务及注册
service.export();
System.in.read();
}
}
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.laosun.service.ProviderService;
import com.laosun.service.impl.ProviderServiceImpl;
public class AppApi {
public static void main(String[] args) {
ApplicationConfig application = new ApplicationConfig();
application.setName("consumer");
application.setOwner("laosun");
// 配置连接中心
RegistryConfig registry = new RegistryConfig();
registry.setAddress("zookeeper://127.0.0.1:2181");
// 引用远程服务
ReferenceConfig<ProviderService> reference = new ReferenceConfig<ProviderService>(); //实例很重,需要进行缓存
reference.setApplication(application);
reference.setRegistry(registry);
reference.setInterface(ProviderService.class);
// 使用服务端
ProviderService providerService = new ProviderServiceImpl();
String str = providerService.sayHello("laosun");
System.out.println(str);
}
}
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.ProviderConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableDubbo(scanBasePackages = "com.laosun.service")
public class DubboProviderConfig {
@Bean
// 服务端信息配置
public ProviderConfig providerConfig() {
ProviderConfig config = new ProviderConfig();
config.setTimeout(1000);
return config;
}
@Bean
// 分布式应用信息配置
public ApplicationConfig applicationConfig() {
ApplicationConfig config = new ApplicationConfig();
config.setName("provider");
return config;
}
@Bean
// 注册中心信息配置
public RegistryConfig registryConfig() {
RegistryConfig config = new RegistryConfig();
config.setProtocol("zookeeper");
config.setAddress("127.0.0.1");
config.setPort(2181);
return config;
}
@Bean
// 使用dubbo协议配置
public ProtocolConfig protocolConfig() {
ProtocolConfig config = new ProtocolConfig();
config.setName("dubbo");
config.setPort(20881);
return config;
}
}
import com.laosun.config.DubboProviderConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import java.io.IOException;
public class AppAnnotation {
public static void main(String[] args) throws IOException {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DubboProviderConfig.class);
context.start();
System.in.read();
}
}
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Component;
@Component
public class ConsumerAnnotationService {
@Reference
private ProviderService providerService;
public String sayHello(String name) {
return providerService.sayHello(name);
}
}
这里注意Reference包不要引用错了,不然会报NPE异常。
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ConsumerConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;
@Configuration
@EnableDubbo(scanBasePackages = "com.laosun.service")
@ComponentScan(value = {"com.laosun.service"})
public class DubboConsumerConfig {
@Bean
// 应用配置
public ApplicationConfig applicationConfig() {
ApplicationConfig config = new ApplicationConfig();
config.setName("consumer");
Map<String, String> map = new HashMap<String, String>();
map.put("qos.enable", "true");
map.put("qos.accept.foreign.ip", "false");
map.put("qos.port", "33333");
config.setParameters(map);
return config;
}
@Bean
// 消费者配置
public ConsumerConfig consumerConfig() {
ConsumerConfig config = new ConsumerConfig();
config.setTimeout(5000);
return config;
}
@Bean
// 配置注册中心
public RegistryConfig registryConfig() {
RegistryConfig config = new RegistryConfig();
config.setProtocol("zookeeper");
config.setAddress("127.0.0.1");
config.setPort(2181);
return config;
}
}
import com.laosun.config.DubboConsumerConfig;
import com.laosun.service.ConsumerAnnotationService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import java.io.IOException;
public class AppAnnotation {
public static void main(String[] args) throws IOException {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DubboConsumerConfig.class);
context.start();
ConsumerAnnotationService service = context.getBean(ConsumerAnnotationService.class);
String str = service.sayHello("laosun");
System.out.println(str);
System.in.read();
}
}