企业级应用,如何实现服务化三(dubbo入门案例) 企业级应用,如何实现服务化二(dubbo架构)

  今天是六一儿童节,从千里之外的广州,回到了贵州老家,真好!好山好水好心情,好了接着写点东西。这是企业级应用,如何实现服务化系列的第三篇。在上一篇:企业级应用,如何实现服务化二(dubbo架构)中,认识了dubbo的整体架构,和dubbo四大特点:连通性、健壮性、伸缩性、升级性。下面先来实现一个入门级的demo,直观感受一下。

1.案例说明

通过一个简单的案例,演示dubbo入门使用。案例中只有服务提供者,服务消费者。

2.案例实现

  2.1.创建项目

  2.2.配置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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.anan</groupId>
    <artifactId>dubbo-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>jar</packaging>

    <properties>
        <!--spring 版本-->
        <spring.version>5.0.2.RELEASE</spring.version>
        <!--dubbo版本-->
        <dubbo.version>2.7.0</dubbo.version>
    </properties>

    <dependencies>
        <!--spring依赖包-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--dubbo依赖-->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>${dubbo.version}</version>
        </dependency>

    </dependencies>
</project>

  2.3.服务端开发

    2.3.1.service接口

package com.anan.dubbo.service;

/**
 * 服务接口
 */
public interface HelloService {

    /**
     * 问好
     */
    String sayHello(String name);
}

    2.3.2.service接口实现

package com.anan.dubbo.service.impl;

import com.anan.dubbo.service.HelloService;

/**
 * 服务接口实现
 */
public class HelloServiceImpl implements HelloService{

    /**
     * 问好
     *
     * @param name
     */
    public String sayHello(String name) {
        return "hello,"+name+"!";
    }
}

  2.4.配置服务提供者

<?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.xsd
       http://dubbo.apache.org/schema/dubbo
       http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!--提供方应用信息,用于计算依赖关系-->
    <dubbo:application name="provider-hello-world-app"  />

    <!--使用multicast广播注册中心暴露服务地址-->
    <dubbo:registry address="multicast://224.1.1.1:6666"/>

    <!--用dubbo协议在20880端口暴露服务-->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!--声明需要暴露的服务接口-->
    <dubbo:service interface="com.anan.dubbo.service.HelloService" ref="helloService"/>

    <!--和本地bean一样实现服务-->
    <bean id="helloService" class="com.anan.dubbo.service.impl.HelloServiceImpl"/>

</beans>

  2.5.配置服务消费者

<?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.xsd
       http://dubbo.apache.org/schema/dubbo
       http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!--消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样-->
    <dubbo:application name="consumer-hello-world-app"  />

    <!--使用multicast广播注册中心暴露发现服务地址-->
    <dubbo:registry address="multicast://224.1.1.1:6666"/>

    <!--生成远程服务代理,可以和本地bean一样使用helloService-->
    <dubbo:reference id="helloService" interface="com.anan.dubbo.service.HelloService" />

</beans>

  2.6.测试

    2.6.1.启动服务提供者Provider

package com.anan.dubbo.provider;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 服务提供者
 */
public class Provider {

    public static void main(String[] args) throws Exception{
        // 加载spring配置文件,创建spring容器
        ApplicationContext context =
                new ClassPathXmlApplicationContext("classpath:provider.xml");

        // 阻塞:等待输入,可按任意键退出
        System.in.read();
    }
}

    2.6.2.启动服务消费者Consumer

package com.anan.dubbo.consumer;

import com.anan.dubbo.service.HelloService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 服务消费者
 */
public class Consumer {

    public static void main(String[] args) {
        // 加载spring配置文件,创建spring容器
        ApplicationContext context =
                new ClassPathXmlApplicationContext("classpath:consumer.xml");

        // 获取远程服务代理
        HelloService helloService = (HelloService)context.getBean("helloService");
        //System.out.println(helloService.getClass());

        // 执行远程方法
        String result = helloService.sayHello("anan");
        System.out.println(result);


    }
}

猜你喜欢

转载自www.cnblogs.com/itall/p/10958365.html