前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Dubbo的两种常规启动方式

Dubbo的两种常规启动方式

作者头像
田维常
发布2019-12-25 18:38:08
1.3K0
发布2019-12-25 18:38:08
举报
文章被收录于专栏:Java后端技术栈cwnait

本案例是dubbo版本:2.7.4.1 org.apache.dubbo

建一个工程为dubbo-demo,再创建三个module

代码语言:javascript
复制
dubbo-user-api
dubbo-user-provider
dubbo-user-consumer

dubbo-user-api

DemoService.java接口

代码语言:javascript
复制
package com.tian.dubbo.service;

public interface DemoService {
    String test(String string);
}

然后打成jar给dubbo-user-providerdubbo-user-consumer使用

代码语言:javascript
复制
mvn clean install

dubbo-user-api.jar

启动zookeeper

本地启动一个zookeepeer,作为dubbo的注册中心,(dubbo推荐zookeeper为注册中心)

dubbo-user-provider

pom.xml中添加

代码语言:javascript
复制
 <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <properties>
        <dubbo.version>2.5.3</dubbo.version>
        <spring.version>4.3.6.RELEASE</spring.version>
        <java.version>1.8</java.version>
        <log4j.version>1.2.17</log4j.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.7.4.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper</artifactId>
            <version>2.7.4.1</version>
        </dependency>
        <dependency>
            <artifactId>dubbo-user-api</artifactId>
            <groupId>com.tian.dubbo</groupId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>

创建一个DemoServiceImpl.java

代码语言:javascript
复制
public class DemoServiceImpl implements DemoService {
    @Override
    public String test(String msg) {
        System.out.println("接收到参数:" + msg);
        return "provider:" + msg;
    }
}

创建启动类ProviderApplication.java

代码语言:javascript
复制
public class ProviderApplication {
    private static String zookeeperHost = System.getProperty("zookeeper.address", "127.0.0.1");

    public static void main(String[] args) throws Exception {
        ServiceConfig<DemoService> service = new ServiceConfig<>();
        service.setApplication(new ApplicationConfig("first-dubbo-provider"));
        service.setRegistry(new RegistryConfig("zookeeper://" + zookeeperHost + ":2181"));
        service.setInterface(DemoService.class);
        service.setRef(new DemoServiceImpl());
        service.export();
        System.out.println("Dubbo server started");
        new CountDownLatch(1).await();
    }
}

启动

输出:Dubbo server started

启动成功,等着消费方调用。下面来说说消费方

dubbo-user-consumer

pom.xml添加

代码语言:javascript
复制
 <properties>
        <dubbo.version>2.5.3</dubbo.version>
        <spring.version>4.3.6.RELEASE</spring.version>
        <java.version>1.8</java.version>
        <log4j.version>1.2.17</log4j.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.7.4.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper</artifactId>
            <version>2.7.4.1</version>
        </dependency>
        <dependency>
            <artifactId>dubbo-user-api</artifactId>
            <groupId>com.tian.dubbo</groupId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
    </dependencies>

启动类ConsumerApplication.java

启动并调用

输出:provider:dubbo

dubbo-user-provider中输出:接收到参数:dubbo

xml方式

provider

provider.xml

代码语言:javascript
复制
<?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"/>
    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://localhost:2181" protocol="zookeeper" check="false"/>
    <!-- 用dubbo协议在随机端口暴露服务 -->
    <dubbo:protocol id="dubbo" name="dubbo" port="-1" threads="200" />
    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.tian.dubbo.service.DemoService" ref="demoService"/>
    <!-- 和本地bean一样实现服务 -->
    <bean id="demoService" class="com.tian.dubbo.service.impl.DemoServiceImpl"/>
</beans>

启动类ProviderApplication.java

代码语言:javascript
复制
public class ProviderApplication {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new          
                   ClassPathXmlApplicationContext("provider.xml");
        System.err.println(context.getDisplayName() + ": here");
        context.start();
        System.err.println("服务已经启动...");
        System.in.read();
    }
}

log4j.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
    <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="[%d{dd/MM/yy hh:mm:ss:sss z}] %t %5p %c{2}: %m%n" />
        </layout>
    </appender>
    <root>
        <level value="INFO" />
        <appender-ref ref="CONSOLE" />
    </root>
</log4j:configuration>

启动输出:

consumer

ConsumerApplication.java

代码语言:javascript
复制
public class ConsumerApplication {
  public static void main(String[] args) {
      //测试常规服务
      ClassPathXmlApplicationContext context =
              new ClassPathXmlApplicationContext("consumer.xml");
      context.start();
      System.err.println("consumer start");
      DemoService demoService = context.getBean(DemoService.class);
      System.out.println(demoService.test("dubbo xml"));
  }
}

consumer.xml

代码语言:javascript
复制
<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="demotest-consumer" owner="programmer" organization="dubbox"/>
    <!--向 zookeeper 订阅 provider 的地址,由 zookeeper 定时推送-->
    <dubbo:registry address="zookeeper://localhost:2181"/>
    <!--使用 dubbo 协议调用定义好的 api.PermissionService 接口-->
    <dubbo:reference id="demoService" interface="com.tian.dubbo.service.DemoService"/>
</beans>

log4j.xml

代码语言:javascript
复制
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
    <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="[%d{dd/MM/yy hh:mm:ss:sss z}] %t %5p %c{2}: %m%n" />
        </layout>
    </appender>
    <root>
        <level value="INFO" />
        <appender-ref ref="CONSOLE" />
    </root>
</log4j:configuration>

启动输出:

证明调用成功,再看provider的输出

自此,一个xml版本的dubbo demo搞定。

注解方式

provider

DemoServiceImpl.java中的@Service使用的是org.apache.dubbo下面的

代码语言:javascript
复制
import com.tian.dubbo.service.DemoService;
import org.apache.dubbo.config.annotation.Service;
@Service
public class DemoServiceImpl implements DemoService {
    @Override
    public String test(String msg) {
        System.out.println("接收到参数:" + msg);
        return "provider:" + msg;
    }
}

启动类

代码语言:javascript
复制
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;

@Configuration
@EnableDubbo(scanBasePackages = "com.tian.dubbo.service.impl")//扫描添加了@Service的注解
@PropertySource("classpath:dubbo-provider.properties")//配置文件的扫描
public class ProviderApplication {
    
    public static void main(String[] args) throws IOException {
        AnnotationConfigApplicationContext context = new 
             AnnotationConfigApplicationContext(ProviderApplication.class);
        context.start();
        System.err.println("服务已经启动...");
        System.in.read(); //需要等待
    }
}

dubbo-provider.properties

代码语言:javascript
复制
dubbo.application.name=annotation-provider
dubbo.registry.address=zookeeper://localhost:2181
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880

启动

consumer

dubbo-consumer.properties

代码语言:javascript
复制
dubbo.application.name=annotation-consumer
dubbo.registry.address=zookeeper://localhost:2181
dubbo.consumer.timeout=3000
代码语言:javascript
复制
import com.tian.dubbo.service.DemoService;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@EnableDubbo(scanBasePackages = "com.tian.dubbo")//启用Dubbo扫描并注入对象
@PropertySource("classpath:dubbo-consumer.properties")//扫描配置文件
public class ConsumerApplication {
    
    //这里需要使用Dubbo来进行注入
    @Reference
    DemoService demoService;

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new 
            AnnotationConfigApplicationContext(ConsumerApplication.class);
        ConsumerApplication customer = context.getBean(ConsumerApplication.class);
        String say = customer.demoService.test("dubbo annotation");
        System.out.println(say);
    }
}

启动

调用成功。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-12-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Java后端技术栈 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • dubbo-user-api
  • 启动zookeeper
  • dubbo-user-provider
  • dubbo-user-consumer
  • xml方式
    • provider
      • consumer
      • 注解方式
        • provider
          • consumer
          相关产品与服务
          微服务引擎 TSE
          微服务引擎(Tencent Cloud Service Engine)提供开箱即用的云上全场景微服务解决方案。支持开源增强的云原生注册配置中心(Zookeeper、Nacos 和 Apollo),北极星网格(腾讯自研并开源的 PolarisMesh)、云原生 API 网关(Kong)以及微服务应用托管的弹性微服务平台。微服务引擎完全兼容开源版本的使用方式,在功能、可用性和可运维性等多个方面进行增强。
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档