dubbo-nacos|zookeeper quick start


dubbo快速开始

快速开始使用 Dubbo
Dubbo 采用全 Spring 配置方式,透明化接入应用,对应用没有任何 API 侵入,只需用 Spring 加载 Dubbo 的配置即可,Dubbo 基于 Spring 的 Schema 扩展 进行加载。

如果不想使用 Spring 配置,可以通过 API 的方式 进行调用。

  • 项目结构图
    1608C5DE-13C9-48c8-AD32-AB3A1DA93B36.png

服务提供者(Service provider)

定义服务接口(Defining service interfaces)

DemoService.java :

该接口需单独打包,在服务提供方和消费方共享

package hosystem;

public interface DemoService {
    String sayHello(String name);

}

The project structure should look like this:

.
├── dubbo-interface
│   ├── pom.xml
│   └── src
│       └── main
│           └── java
│               └── hosystem
│                   └── DemoService.java

在服务提供方实现接口(Implement interface in service provider)

DemoServiceImpl.java :

对服务消费方隐藏实现

package hosystem.Impl;
 
import hosystem.DemoService;
 
public class DemoServiceImpl implements DemoService {
    public String sayHello(String name) {
        return "Hello " + name;
    }
}

用 Spring 配置声明暴露服务(Exposing service with Spring configuration)

provider.xml :

<?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://dubbo.apache.org/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        http://dubbo.apache.org/schema/dubbo        http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
 
    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="hello-world-app"  />
 
    <!-- 使用zookeeper广播注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://192.168.188.128:2181" />
    <!-- 若想使用nacos作为注册中心 则只需要修改zookeeper为nacos地址即可 -->
    <!-- 注:要先注册成功 要先启动nacos客户端 -->
    <!-- 使用nacos广播注册中心暴露服务地址 -->
    <!--<dubbo:registry address="dubbo://192.168.188.128:8848" />-->
 
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />
 
    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="hosystem.DemoService" ref="demoServiceImpl"/>
 
    <!-- 和本地bean一样实现服务 -->
    <bean id="demoServiceImpl" class="hosystem.Impl.DemoServiceImpl"/>
</beans>

配置日志记录系统(Configure the logging system)

默认情况下,Dubbo使用log4j作为日志记录系统,它还支持slf4j,Apache Commons Logging和JUL日志记录。

以下是一个示例配置:

log4j.properties :

###set log levels###
log4j.rootLogger=info, stdout
###output to the console###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d{dd/MM/yy hh:mm:ss:sss z}] %t %5p %c{2}: %m%n

加载 Spring 配置(Bootstrap the service provider)

Provider.java :

import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class provider {
    public static void main(String[] args) throws Exception {
        // provider.xml文件的位置resources下的 若直接放在resources下则不需要前缀
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"provider.xml"});
        context.start();
        System.in.read(); // 按任意键退出
    }
}

the project structure should look like this:

├── dubbo-provider
│   ├── pom.xml
│   └── src
│       └── main
│           ├── java
│           │       └── hosystem
│           │           └── Impl
│           │               └── DemoServiceImpl.java
│           │       └── Provider.java
│           └── resources
│               └── provider.xml
│               └── log4j.properties

服务消费者(Service consumer)

通过 Spring 配置引用远程服务

consumer.xml :

<?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://dubbo.apache.org/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        http://dubbo.apache.org/schema/dubbo        http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
 
    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
    <dubbo:application name="consumer-of-helloworld-app"  />
 
    <!-- 使用zookeeper广播注册中心暴露发现服务地址 -->
    <dubbo:registry address="zookeeper://192.168.188.128:2181" />
    <!-- 若想使用nacos作为注册中心 则只需要修改zookeeper为nacos地址即可 -->
    <!-- 注:要先注册成功 要先启动nacos客户端 -->
    <!-- 使用nacos广播注册中心暴露服务地址 -->
    <!--<dubbo:registry address="dubbo://192.168.188.128:8848" />-->
 
    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <dubbo:reference id="demoService" interface="hosystem.DemoService" />
</beans>

加载Spring配置,并调用远程服务

Consumer.java :

也可以使用 IoC 注入

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.apache.dubbo.demo.DemoService;
 
public class Consumer {
    public static void main(String args[]) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("consumer.xml");
        DemoService demoService = (DemoService) ctx.getBean("demoService");
        System.out.println("DemoService...start ");
        System.out.println(demoService.sayHello("hosystem") + " successful run...");
        System.out.println("DemoService...end ");
    }
}

the project structure should look like this:

├── dubbo-demo-provider
│   ├── pom.xml
│   └── src
│       └── main
│           ├── java
│           │   └── Consumer.java
│           └── resources
│               └── consumer.xml
│               └── log4j.properties

测试:

先运行dubbo-pro,在运行dubbo-consumer

7C69E0D8-E110-4b54-9066-9EF9399E0A73.png

参考文档1: 点我传送-dubbo快速开始

参考文档2: 点我传送-zookeeper安装

posted @ 2021-03-03 09:55  HOsystem  阅读(215)  评论(0编辑  收藏  举报