Skip to content

快速开始

lexinfintech edited this page Jul 18, 2018 · 1 revision

应用目录结构

provider应用目录结构

provider-app-name/     
                 config/(业务相关配置,比如日志配置文件)   
                 provider/(provider发布服务配置)   
                 server/(业务逻辑实现目录)   
                 Bootstarp.php(provider的启动文件,用于标识provider应用的位置,可以在里面加载除应用目录下其他目录的文件)   

consumer应用目录结构

consumer-app-name/      
                 config/(业务相关配置,比如日志配置文件)   
                 consumer/(consumer发布服务配置)  
                 server/(业务逻辑实现目录)  

接下来用一个简单的例子来说明怎么使用dubbo-php-framework,有三种调用方式:

  • php调用php
  • php调用java(dubbo)
  • java(dubbo)调用php

全局配置:dubbo-php-framework/config/global/conf/fsof.ini

 [fsof_container_setting]  
;php path  
php = '/usr/bin/php' 
;app's user 
user = root  
[fsof_setting]  
;machine enviroment:[dev|pre|gray|pro]  
environment = dev  
;in p2p mode, provider not register to registry  
p2p_mode = false  
;zookeeper log switch: 0.close; 1.error; 2.warn; 3.info; 4.debug  
zklog_level = 0  
;zookeepr log path  
zklog_path = '/var/fsof/provider/zookeeper.log'  
;zookeeper url list  
zk_url_list = http://127.0.0.1:2181  
;provider overload mode switch  
overload_mode = true  
;if request wait more than waiting_time before processed, we will lost this quest, unit is micro-second  
waiting_time = 2000  
;if overload_number requests trigger overload rule continuous, we will open loss request mode  
overload_number = 5  
;how many quest is lost before lost mode is close  
loss_number = 20  

1.php调用php

Implement service provider

demo-provider/server/DemoServiceImpl.php

<?php
require_once('log4php/Logger.php');
Logger::configure(dirname(dirname(__FILE__)).'/config/log4php.xml');
date_default_timezone_set('PRC');

class DemoServiceImpl
{
	public function sayHello($name)
	{
        \Logger::getLogger(__CLASS__)->info("Hello $name");
        return "Hello $name";
	}
}

demo-provider/Bootstrap.php

<?php  

Configure service provider

demo-provider/provider/demo-provider.provider

[service_properties]  
owner = author  
version = 1.0.0  
group = default  
[service_providers]  
com.fenqile.example.DemoService[service] = DemoServiceImpl  
com.fenqile.example.DemoService[version] = 1.0.0  
com.fenqile.example.DemoService[group] = default  

dubbo-php-framework/config/app/conf/dev/provider/demo-provider.deploy

[server]  
; port  
listen[] = 10000 
;app log config file path  
log_cfg_file_path = '/root/fsof_php_core/dubbo-php-framework/demo/demo-provider/config/log4php.xml'  
;swoole log path  
swoole_log_path = '/var/fsof/provider/example_swoole.log'  
; point to app root path's Bootstrap.php  
root = '/root/fsof_php_core/dubbo-php-framework/demo/demo-provider/BootStrap.php'  
[setting] 
; worker num  
worker_num = 2  
; daemonize  
daemonize = true  
;max_request  
max_request = 10000   

demo-provider/config/log4php.xml

<configuration xmlns="http://logging.apache.org/log4php/">   
    <appender name="myAppender" class="LoggerAppenderFile">  
        <param name="file" value="/tmp/provider.log" />   
        <layout class="LoggerLayoutPattern">   
            <param name="conversionPattern" value="[%date{Y-m-d H:i:s,u}][%level][%logger][%line] %message%newline" /> 
        </layout> 
    </appender>
    <root>  
        <level value="INFO" />  
        <appender_ref ref="myAppender" /> 
    </root> 
</configuration> 

Start service provider

$php dubbo-php-framework/bin/app_admin.php demo-provider start  

$php dubbo-php-framework/bin/app_admin.php demo-provider restart

Implement service consumer

demo-consumer/server/Consumer.php

<?php  
$APP_SRC_PATH = __DIR__;  
//fsofRootPath为dubbo-php-framework所在路径 
$fsofRootPath= dirname(dirname(dirname($APP_SRC_PATH)))
$fsofApiPath = $fsofRootPath.DIRECTORY_SEPARATOR.'api'.DIRECTORY_SEPARATOR.'FSOFApi.php';   
require_once($fsofApiPath);  
FSOFApi::configure('demo-consumer', $APP_SRC_PATH);  

require_once('log4php/Logger.php');  
Logger::configure(dirname(dirname(__FILE__)).'/config/log4php.xml');  
date_default_timezone_set('PRC');  

//php框架调用php提供的服务
$service = 'com.fenqile.example.DemoService';  
$proxy = FSOFApi::newProxy($service, 3);  
$ret = $proxy->invoke("sayHello","zhangsan"); 
echo "ret:$ret".PHP_EOL;  

Configure service consumer

demo-consumer/consumer/demo-consumer.consumer

[consumer_config]  
p2p_mode = false  
[consumer_services]  
com.fenqile.example.DemoService[group] = *  
com.fenqile.example.DemoService[version] = 1.0.0   

demo-consumer/config/log4php.xml

<configuration xmlns="http://logging.apache.org/log4php/">   
   <appender name="myAppender" class="LoggerAppenderFile">   
        <param name="file" value="/tmp/consumer.log" />  
        <layout class="LoggerLayoutPattern">   
            <param name="conversionPattern" value="[%date{Y-m-d H:i:s,u}][%level][%logger][%line] %message%newline" />  
        </layout>  
    </appender>  
    <root>  
        <level value="INFO" />  
        <appender_ref ref="myAppender" />  
    </root> 
</configuration>  

Run agent

$sh dubbo-php-framework/agent/src/c/release/start_agent_server.sh   

Run service consumer

$php demo-consumer/server/Consumer.php 

2.php调用java(dubbo)

Implement service provider

Define service interface

package com.alibaba.dubbo.demo;  

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

Implement service provider

package com.alibaba.dubbo.demo.provider;  
import com.alibaba.dubbo.demo.DemoService;  
public class DemoServiceImpl implements DemoService {  
    public String sayHello(String name) { 
        return "Hello " + name;  
    }  
}  

Configure service provider

<?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="demo-provider"/>  
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/> 
    <dubbo:protocol name="dubbo" port="20880" serialization="fastjson"/>  
    <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService"/> 
    <bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl"/>  
</beans>  

Start service provider

import org.springframework.context.support.ClassPathXmlApplicationContext;  
public class Provider {  
    public static void main(String[] args) throws Exception {  
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(  
                new String[] {"META-INF/spring/dubbo-demo-provider.xml"});  
        context.start();  
        // press any key to exit 
        System.in.read();  
    }  
}  

Implement service consumer

demo-consumer/server/Consumer.php

<?php  
$APP_SRC_PATH = __DIR__;  
//fsofRootPath为dubbo-php-framework所在路径  
$fsofRootPath= dirname(dirname(dirname($APP_SRC_PATH)))  
$fsofApiPath = $fsofRootPath.DIRECTORY_SEPARATOR.'api'.DIRECTORY_SEPARATOR.'FSOFApi.php';   
require_once($fsofApiPath); 
FSOFApi::configure('demo-consumer', $APP_SRC_PATH);  

require_once('log4php/Logger.php');  
Logger::configure(dirname(dirname(__FILE__)).'/config/log4php.xml');  
date_default_timezone_set('PRC');  

//php框架调用java(dubbo)提供的服务 
$service = 'com.alibaba.dubbo.demo.DemoService';  
$proxy = FSOFApi::newProxy($service, 3);  
$ret = $proxy->invoke("sayHello","zhangsan");  
echo "ret:$ret".PHP_EOL;  

Configure service consumer

demo-consumer/consumer/demo-consumer.consumer

[consumer_config]  
p2p_mode = false  
[consumer_services]  
com.alibaba.dubbo.demo.DemoService[group] = *  
com.alibaba.dubbo.demo.DemoService[version] = 1.0.0  

demo-consumer/config/log4php.xml

<configuration xmlns="http://logging.apache.org/log4php/">  
    <appender name="myAppender" class="LoggerAppenderFile">   
        <param name="file" value="/tmp/consumer.log" />    
        <layout class="LoggerLayoutPattern">  
            <param name="conversionPattern" value="[%date{Y-m-d H:i:s,u}][%level][%logger][%line] %message%newline" /> 
        </layout>  
    </appender> 
    <root>  
        <level value="INFO" /> 
        <appender_ref ref="myAppender" />  
    </root>  
</configuration>  

Run agent

$sh dubbo-php-framework/agent/src/c/release/start_agent_server.sh

Run service consumer

$php demo-consumer/server/Consumer.php 

3.java(dubbo)调用php

Implement service provider

demo-provider/server/DemoServiceImpl.php

<?php 
require_once('log4php/Logger.php'); 
Logger::configure(dirname(dirname(__FILE__)).'/config/log4php.xml');  
date_default_timezone_set('PRC');  

class DemoServiceImpl  
{ 
	public function sayHello($name) 
	{  
        \Logger::getLogger(__CLASS__)->info("Hello $name");  
        return "Hello $name";  
	}  
}

demo-provider/Bootstrap.php

<?php 

Configure service provider

demo-provider/provider/demo-provider.provider

[service_properties]  
owner = author 
version = 1.0.0  
group = default  

[service_providers]  
com.fenqile.example.DemoService[service] = DemoServiceImpl 
com.fenqile.example.DemoService[version] = 1.0.0 
com.fenqile.example.DemoService[group] = default 

dubbo-php-framework/config/app/conf/dev/provider/demo-provider.deploy

[server]  
; port  
listen[] = 10000  
;app log config file path  
log_cfg_file_path = '/root/fsof_php_core/dubbo-php-framework/demo/demo-provider/config/log4php.xml'  
;swoole log path 
swoole_log_path = '/var/fsof/provider/example_swoole.log'  
; point to app root path's bootstrap.php  
root = '/root/fsof_php_core/dubbo-php-framework/demo/demo-provider/BootStrap.php'  
[setting]  
; worker num  
worker_num = 2  
; daemonize 
daemonize = true 
;max_request  
max_request = 10000  

demo-provider/config/log4php.xml

<configuration xmlns="http://logging.apache.org/log4php/">  
    <appender name="myAppender" class="LoggerAppenderFile">   
        <param name="file" value="/tmp/provider.log" />   
        <layout class="LoggerLayoutPattern">   
            <param name="conversionPattern" value="[%date{Y-m-d H:i:s,u}][%level][%logger][%line] %message%newline" /> 
        </layout> 
    </appender>
    <root> 
        <level value="INFO" />  
        <appender_ref ref="myAppender" />  
    </root>  
</configuration> 

Start service provider

$php dubbo-php-framework/bin/app_admin.php demo-provider start  

$php dubbo-php-framework/bin/app_admin.php demo-provider restart 

Configure service consumer

<?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="demo-consumer"/> 
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>  
    <dubbo:reference id="demoService" interface="com.fenqile.example.DemoService" group="*" version="1.0.0" generic="true"/> 
</beans>  

Run service consumer

import org.springframework.context.support.ClassPathXmlApplicationContext;  

public class Consumer {` 
    public static void main(String[] args) throws Exception {  
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( 
                new String[]{"META-INF/spring/dubbo-demo-consumer.xml"});  
        context.start();  
        GenericService demoService = (GenericService) context.getBean("demoService");  
        Object hello = demoService .$invoke("sayHello", new String[] {
                "java.lang.String" }, new Object[] { "zhangsan" });  
        System.out.println(hello);  
    }
}