创建一个工程dubbo,其中一共分三个module:
- provider:服务提供者
- consumer:服务消费者
- api:是针对服务的接口和实体install成jar给provider和consumer使用
1:基于配置方式调用
api结构和代码如下:
import java.io.Serializable;
/**
* 用户信息
*
* @Author tianweichang
* @Date 2018-08-14 15:55
**/
public class User implements Serializable{
private String name;
private Integer id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", id=" + id +
'}';
}
}
provider结构和代码如下:
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>dubbo</artifactId>
<groupId>com.tian.dubbo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>provider</artifactId>
<name>provider</name>
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.9</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<artifactId>api</artifactId>
<groupId>com.tian.dubbo</groupId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
provider.xml
具体dubbo服务实现类内容:
启动类
启动输出:
consumer结构和代码如下:
pom.xml
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>dubbo</artifactId>
<groupId>com.tian.dubbo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>consumer</artifactId>
<name>consumer</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.9</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<artifactId>api</artifactId>
<groupId>com.tian.dubbo</groupId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</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-app"/>
<dubbo:registry address="zookeeper://localhost:2181"/>
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="userService" interface="com.tian.dubbo.service.UserService"/>
</beans>
Consumer类
import com.tian.dubbo.model.User;
import com.tian.dubbo.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
/**
* Hello world!
*/
public class Consumer {
public static void main(String[] args) throws Exception {
// FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("E:\\mystudy\\otherCode\\dubbo\\consumer\\src\\main\\resource\\consumer.xml");
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml");
context.start();
// 获取远程服务代理
UserService userService = (UserService) context.getBean("userService");
// 执行远程方法
User user = userService.getUserByUserId(1);
System.out.println(user);
}
}
启动输出:
成功调用到provider提供的dubbo服务
2:基于Dubbo API方式调用
服务端代码:
import com.alibaba.dubbo.config.*;
import com.alibaba.dubbo.registry.Registry;
import com.tian.dubbo.service.impl.UserServiceImpl;
/**
* Copyright © 2018 上海金互行金融服务有限公司. All rights reserved. *
* <p>
* TODO
*
* @Author tianweichang
* @Date 2018-08-15 9:21
**/
public class TestProviderApi {
public static void main(String[] args) throws InterruptedException {
//相当于<bean id="" class="">
UserService userService = new UserServiceImpl();
//相当于<dubbo:application name="">
ApplicationConfig application = new ApplicationConfig();
application.setName("dubbo-provider");
//注册中心
RegistryConfig registry = new RegistryConfig();
registry.setAddress("localhost:2181");
registry.setProtocol("zookeeper");
//协议和端口
ProtocolConfig protocol = new ProtocolConfig();
protocol.setName("dubbo");
protocol.setPort(20880);
//监控
MonitorConfig monitor = new MonitorConfig();
monitor.setProtocol("registry");
//服务端入口类
ServiceConfig<UserService> service = new ServiceConfig<>();
service.setApplication(application);
service.setMonitor(monitor);
service.setRegistry(registry);
service.setProtocol(protocol);
service.setInterface(UserService.class);
service.setRef(userService);
service.export();
Thread.currentThread().join();
}
}
启动输出:
消费端代码:
package com.tian.dubbo;
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.MonitorConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.tian.dubbo.service.UserService;
/**
* 消费端api形式调用
*
* @Author tianweichang
* @Date 2018-08-15 9:47
**/
public class TestConsumerApi {
public static void main(String[] args) throws InterruptedException {
ApplicationConfig application = new ApplicationConfig();
application.setName("dubbo-consumer");
RegistryConfig registry = new RegistryConfig();
registry.setAddress("localhost:2181");
registry.setProtocol("zookeeper");
MonitorConfig monitor = new MonitorConfig();
monitor.setProtocol("registry");
ReferenceConfig<UserService> reference = new ReferenceConfig<UserService>();
reference.setApplication(application);
reference.setRegistry(registry);
reference.setMonitor(monitor);
reference.setInterface(UserService.class);
reference.setTimeout(1000);
reference.setInjvm(false);
UserService userService = reference.get();
System.out.println(userService.getUserByUserId(22));
}
}
启动输出:
证明已经调用到了dubbo服务
标签:dubbo,系列,Dubbo,src,import,搭建,com,png,10 From: https://blog.51cto.com/u_11702014/6443683