【dubbo】dubbo項目基本結構及provider構建


dubbo項目基本結構如下,分別部署於不同服務器:

1.provider(接口API 實現)

2.consumer(web)

3.zookeeper

4.DB

 

provider構建

1.api構建

intellij中新建"maven"項目,groupId:com.dubbo.api.demo,artifactId:demo,projectName:dubbo-api-demo();

java下添加interface:cmo.dubbo.api.demo.DemoService;

public interface DemoService {
    String sayHello(String name);
}

 maven-install

 

2.provider 實現構建

pom.xml中添加DUBBO-API,DUBBO,SPRING

<dependencies>
  <dependency>
            <groupId>com.dubbo.api.demo</groupId>
            <artifactId>demo</artifactId>
            <version>1.0-SNAPSHOT</version>
  </dependency>
  
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.4.10</version>
</dependency>
<dependency>
    <groupId>com.github.sgroschupf</groupId>
    <artifactId>zkclient</artifactId>
    <version>0.1</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>3.2.16.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>3.2.16.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>3.2.16.RELEASE</version>
</dependency>
</dependencies>

 

intellij中新建"maven"項目,groupId:com.dubbo.provider.demo,artifactId:demo,projectName:dubbo-provider-demo();

java下添加class:cmo.dubbo.provider.demo.DemoServiceImpl:

 

package com.dubbo.provider.demo;


import java.text.SimpleDateFormat;
import java.util.Date;

import com.alibaba.dubbo.rpc.RpcContext;
import com.dubbo.api.demo.*;
/**
 * Created by Administrator on 17-1-1.
 */
public class DemoServiceImpl implements DemoService {

    public String sayHello(String name) {
        System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
        return "Hello " + name + ", response form provider: " + RpcContext.getContext().getLocalAddress();
    }

}

 

resources下添加dubbo配置META-INFO.spring-dubbo-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://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <dubbo:application name="dubbo-provider-demo" ></dubbo:application>
    <dubbo:registry id="zk1" address="zookeeper://127.0.0.1:2181" protocol="zookeeper"></dubbo:registry>
    <dubbo:protocol id="mydubbo" name="dubbo" port="20886"></dubbo:protocol>
    <dubbo:provider register="zk1" protocol="mydubbo"></dubbo:provider>
    <dubbo:service interface="com.dubbo.api.demo.DemoService" ref="demoService"></dubbo:service>
    <bean id="demoService" class="com.dubbo.provider.demo.DemoServiceImpl" />
</beans>

 

啟動provider,在Edit Configuration 中添加application 的啟動方法,main class選擇com.alibaba.dubbo.container.Main。控制台顯示[2017-01-07 15:07:25] Dubbo service server started! 則表示成功啟動

 
        

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM