翻译-Your first Dubbo application

Your-First-Dubbo-Demo

Your First Dubbo Demo

Java RMI Introduction (RMI Profile)

RMI is a mechanism that allows access to or call the object or method on another JVM. RMI is a special RPC, using OOP support of java language. Users can not pass IDL (Interface Define Language) can build distributed applications, depending on the interface easy and natural way.

Java RMI Work Flow (RMI workflow)

  1. Service end to the service registration and bind RMI interfaces;
  2. The client and get to the RMI remote interface level service;
  3. The client calls the local stub object method, just like other objects as local calls;
  4. Local stub object compression request information, and sends it to the server through the network;
  5. The framework code server receives a network request, and the decompression.
  6. The server method call information request target object, and the results are compressed and returned to the client via the network.

image

Some of the concepts of RMI

java RMI is the cornerstone technology to create distributed applications java field. EJB technology and distributed framework of current services still inherits the basic concept of java RMI. In RMI's call, we have the following core concepts:

  1. Remote call depends on the interface
  2. The remote call camouflage costs call, the client called the Stub Object , called the service side Skeleton Object .
  3. Service is registered in the RMI registry and discovery.
    On the first point: The client-dependent interface should be implemented server.
    For the second point: before and J2SE 1.5, client and server need to be pre-compiled rmic Stub object and Skeleton Object . Later versions do not need to do that.

Code Example:

Server service registry
// 初始化服务实例
Hello obj = new HelloImpl(); 
// 创建存根对象
Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0); // #2
// 创建注册服务
Registry registry = LocateRegistry.createRegistry(1099); // #3
// 注册并绑定存根对象,客户端即可以通过Hello来找到服务
registry.rebind("Hello", stub); // #4
Client service look-up
// 获取注册中心实例
Registry registry = LocateRegistry.getRegistry(); 
// 寻找名字叫Hello的那个服务实例
Hello stub = (Hello) registry.lookup("Hello");
// 调用RMI远程调用
String response = stub.sayHello(); 

Understand the basic concepts of workflow and RMI is very helpful for the current distributed service framework.
Recommended reading RMI and other official documentation for more information.

Basic Concepts of Dubbo (Basic Concepts of dubbo)

At present the basic concepts of distributed services and RMI framework is similar. They use the java interface as the service contract, to register and query the registry by using proxy hide the details of remote communication. In particular, when running Dubbo There are four types of roles

  • Service provider, at a specified port initialization when exposed services, registered address and port services in the registry.
  • Consumer services, initialization of a subscription service that interest in the registry, obtain a list of addresses service providers
  • Registration center, registration and search services. Address storage service provider, and sends it to the consumer.
  • Monitoring center to collect and monitor service providers and consumers of running, the number of calls, call delay.
  • Run container load initialization and service providers, management and operation cycle.

image

Deployment phase
  • Initialization of the specified service port exposed, registered address and port services in the registry.
  • Initialization of a subscription service they are interested in registering Center for ISP address list

    The operational phase
  • Registry address will be sent to the service consumer;
  • Upon receiving an address list, service consumers to choose one of them and call out.
  • During the call, service providers and consumers operational status is sent to the monitoring center.

Dubbo Applications Based on API

Dubbo are usually assembled by the spring. To quickly create a Dubbo applicable, the example here of abandoned complex configuration to Dubbo Api-oriented way to create a service provider and service consumer. Further, the present example does not require the installation or configuration registry and monitoring center.

在生产环境中,Dubbo服务通常需要和分布式服务注册中心配合起来,比如zookeeper。出于方便的考虑,dubbo提供了俩种方式来避免额外的关于建构注册中心的工作,分别是【namely direct connection】和【assembled podcast】。
本例中使用了后者来注册和查找服务。

Define Service Contract(定义服务契约)
public interface GreetingsService {
    String sayHi(String name); // #1
}

定义了一个简单的服务契约,仅仅只有一个方法【sayHi】会被调用。
入参和返回值都是string类型的。
Provide Contract Implementation(提供契约实现)
public class GreetingsServiceImpl implements GreetingsService { // #1
    @Override
    public String sayHi(String name) {
        return "hi, " + name; // #2
    }
}
服务提供者需要实现服务契约的接口【GreetingsService】。
Implement Dubbo Service Provider(实现服务提供者)
public class Application {
    public static void main(String[] args) throws IOException {
        // 创建一个ServiceConfig 实例,类型是GreetingsService
        ServiceConfig<GreetingsService> service = new ServiceConfig<>(); 
        // 创建一个ApplicationConfig 实例,并将之组装到ServiceConfig实例中
        service.setApplication(new ApplicationConfig("first-dubbo-provider"));
        // 创建一个RegistryConfig 实例,并将之组装到ServiceConfig实例中。
        // 因为注册中心使用的是上文提到的第二种方案,所以参数应该是【multicast://224.5.6.7:1234】
        // 有效的assembled地址范围是224.0.0.0 - 239.255.255.255
        service.setRegistry(new RegistryConfig("multicast://224.5.6.7:1234")); 
        // 将服务契约GreetingsService 组装到ServiceConfig实例中
        service.setInterface(GreetingsService.class); 
        // 将服务提供者提供的GreetingsServicelmpl实例组装到ServiceConfig实例中
        service.setRef(new GreetingsServiceImpl()); 
        // 装配好足够的信息后,ServiceConfig实例开始将自己暴露在默认的端口20880。
        service.export();
        // 按任意键或ctrl-c退出以避免服务器停止。
        System.in.read(); 
    }
}
Implement Dubbo Service Consumer(实现dubbo的服务消费者)
public class Application {
    public static void main(String[] args) {
        // 创建一个指定类型为GreetingsService的ReferenceConfig 实例
        ReferenceConfig<GreetingsService> reference = new ReferenceConfig<>(); // #1
        // AplicatonConfig实例
        reference.setApplication(new ApplicationConfig("first-dubbo-client")); // #2
        // 创建RegistryConfig实例,地址必须和服务提供者一样
        reference.setRegistry(new RegistryConfig("multicast://224.5.6.7:1234")); // #3
        reference.setInterface(GreetingsService.class); // #4
        // 从ReferenceConfig中获得一个GreetingsService的代理
        GreetingsService greetingsService = reference.get(); // #5
        // 通过GreetingsService代理执行远程调用
        String message = greetingsService.sayHi("dubbo"); // #6
        System.out.println(message); // #7
    }
}

run (运行这个例子)

完整的例子可以从 https://github.com/dubbo/dubbo-samples/tree/master/dubbo-samples-api中获得。通过maven或者直接使用ide都可以很方便运行这个例子。一个值得注意的是:因为使用了委派的方式来查找服务,运行的时候需要加上【-Djava.net.preferIPv4Stack=true】。

Build Example(构造实例代码)
  1. 下载代码:git clone https://github.com/dubbo/dubbo-samples.git
  2. Build:mvn clean package
$ git clone https://github.com/dubbo/dubbo-samples.git
$ cd dubbo-samples/dubbo-samples-api/
$ mvn clean package
INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building dubbo-samples-api 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ dubbo-samples-api ---
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.182 s
[INFO] Finished at: 2018-05-28T14:56:08+08:00
[INFO] Final Memory: 20M/353M
[INFO] ------------------------------------------------------------------------
Run the server(运行服务)
$ mvn -Djava.net.preferIPv4Stack=true -Dexec.mainClass=org.apache.dubbo.samples.provider.Application exec:java
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building dubbo-samples-api 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- exec-maven-plugin:1.6.0:java (default-cli) @ dubbo-samples-api ---
log4j:WARN No appenders could be found for logger (org.apache.dubbo.common.logger.LoggerFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
first-dubbo-provider is running.
Run the server(运行客户端)
$ mvn -Djava.net.preferIPv4Stack=true -Dexec.mainClass=org.apache.dubbo.samples.consumer.Application exec:java
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building dubbo-samples-api 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- exec-maven-plugin:1.6.0:java (default-cli) @ dubbo-samples-api ---
log4j:WARN No appenders could be found for logger (org.apache.dubbo.common.logger.LoggerFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
hi, dubbo

Quick Creation of A Dubbo Application

http://start.dubbo.io下载(跟spring.io下载boot项目类似)

Summary

后续教程

Dubbo user manual

Dubbo developer guide

Dubbo admin manual

Guess you like

Origin www.cnblogs.com/po-shi/p/11236697.html