码迷,mamicode.com
首页 > 其他好文 > 详细

Dubbo——入门案例

时间:2020-03-11 19:47:40      阅读:48      评论:0      收藏:0      [点我收藏+]

标签:pre   pat   指定   end   project   system   生成   framework   static   

1. pmo:

<?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.jcx.dubbo.demo</groupId>
    <artifactId>dubbo-demo</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>dubbo-api</module>
        <module>dubbo-consumer</module>
        <module>dubbo-provider</module>
    </modules>

    <dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.12.0</version>
        </dependency>
    </dependencies>

</project>

2. dubbo-api

service:

package com.jcx.dubbo.demo.service;

public interface IDemoService {

    String sayHello(String userName);

}

3. dubbo-provider:

service.impl

package com.jcx.dubbo.demo.service.impl;

import com.jcx.dubbo.demo.service.IDemoService;

public class DemoServiceImpl implements IDemoService {

    public String sayHello(String userName) {
        return "Hello," + userName;
    }
    
}

ProviderApplication:

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

public class ProviderApplication {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("provider.xml");
        ioc.start();
        System.out.println("provider start...");
        System.in.read();
    }
}

provider.xml:

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       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="dubbo-demo-provider"/>

    <!--指定注册中心-->
    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>

    <!--指定通信规则(通信协议,通信端口)-->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!--暴露服务 ref指向真正实现-->
    <dubbo:service interface="com.jcx.dubbo.demo.service.IDemoService" ref="demoServiceImpl"/>

    <!--服务实现-->
    <bean id="demoServiceImpl" class="com.jcx.dubbo.demo.service.impl.DemoServiceImpl"/>

</beans>

4. dubbo-consumer:

ConsumerApplication:

import com.jcx.dubbo.demo.service.IDemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

public class ConsumerApplication {

    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("consumer.xml");
        applicationContext.start();
        IDemoService demoService = (IDemoService) applicationContext.getBean("demoService");
        String hello = demoService.sayHello("jcx");
        System.out.println(hello);
    }
}

consumer.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       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="dubbo-demo-consumer"></dubbo:application>

    <!--指定注册中心-->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry>

    <!--生成远程服务代理接口  check:启动时是否检查-->
    <dubbo:reference id="demoService" interface="com.jcx.dubbo.demo.service.IDemoService" check="false"/>
</beans>

注:先启动提供者,后启动消费者

Dubbo——入门案例

标签:pre   pat   指定   end   project   system   生成   framework   static   

原文地址:https://www.cnblogs.com/s-star/p/12464753.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!