Skip to content

1.快速入门

gzxishan edited this page Mar 11, 2019 · 1 revision

快速入门

一、LocalMain方式

1、创建maven项目OftenQuickStart1,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>cn.chenyg.demo.often</groupId>
    <artifactId>OftenQuickStart1</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.slf4j.version>1.7.25</project.slf4j.version>
        <project.often.version>1.1.1.65</project.often.version>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/com.xishankeji/Porter-Core -->
        <dependency>
            <groupId>com.xishankeji</groupId>
            <artifactId>Porter-Core</artifactId>
            <version>${project.often.version}</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${project.slf4j.version}</version>
        </dependency>

    </dependencies>

</project>

2、创建Porter类:HelloPorter.java

package cn.chenyg.demo.often.demo01;

import cn.xishan.oftenporter.porter.core.annotation.PortIn;
import cn.xishan.oftenporter.porter.core.base.OftenObject;

/**
 * @author Created by https://github.com/CLovinr on 2019/3/6.
 */
@PortIn
public class HelloPorter
{
    @PortIn
    public String say(OftenObject oftenObject)
    {
        return "Hello Word!";
    }
}

3、创建主类:Main.java

package cn.chenyg.demo.often.demo01;

import cn.xishan.oftenporter.porter.core.base.PortMethod;
import cn.xishan.oftenporter.porter.core.bridge.*;
import cn.xishan.oftenporter.porter.core.init.PorterConf;
import cn.xishan.oftenporter.porter.local.LocalMain;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Created by https://github.com/CLovinr on 2019/3/6.
 */
public class Main
{
    private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);

    public static void main(String[] args){
        LocalMain localMain = new LocalMain(true,new BridgeName("Default"),"utf-8");

        PorterConf porterConf = localMain.newPorterConf();
        porterConf.setOftenContextName("often");
        porterConf.getSeekPackages().addClassPorter(HelloPorter.class);
        localMain.startOne(porterConf);

        IBridge bridge = localMain.getBridgeLinker().currentBridge();

        bridge.request(new BridgeRequest(PortMethod.GET, "/often/Hello/say"), new BridgeCallback()
        {
            public void onResponse(BridgeResponse lResponse)
            {
                LOGGER.debug("*****************:{}",lResponse.getResponse());
            }
        });


        localMain.destroyAll();
    }
}

4、运行main函数,主要打印信息:

[DEBUG][main]03-06 22:12:55 at cn.xishan.oftenporter.porter.core.init.PorterMain._startOne(PorterMain.java:485)::Default/often beforeStart...
[DEBUG][main]03-06 22:12:55 at cn.xishan.oftenporter.porter.core.init.PorterMain._startOne(PorterMain.java:501)::Default/often afterStart...
[DEBUG][main]03-06 22:12:55 at cn.xishan.oftenporter.porter.core.init.PorterMain._startOne(PorterMain.java:505)::Default/often porterOne started!
[DEBUG][main]03-06 22:12:55 at cn.xishan.oftenporter.porter.core.init.PorterMain._startOne(PorterMain.java:508):*********************************
[DEBUG][main]03-06 22:12:55 at cn.xishan.oftenporter.porter.core.init.PorterMain._startOne(PorterMain.java:509)::Default/often before @PortInit...
[DEBUG][main]03-06 22:12:55 at cn.xishan.oftenporter.porter.core.init.PorterMain._startOne(PorterMain.java:514)::Default/often done @PortInit.
[DEBUG][main]03-06 22:12:55 at cn.chenyg.demo.often.demo01.Main.onResponse(Main.java:31):*****************:Hello Word!
[DEBUG][main]03-06 22:12:55 at cn.xishan.oftenporter.porter.core.init.PorterMain.destroyAll(PorterMain.java:533):[Default] destroyAll...
[DEBUG][main]03-06 22:12:55 at cn.xishan.oftenporter.porter.core.init.PorterMain.destroyOne(PorterMain.java:554):Context [often] beforeDestroy...
[DEBUG][main]03-06 22:12:55 at cn.xishan.oftenporter.porter.core.init.PorterMain.destroyOne(PorterMain.java:557):Context [often] destroyed!
[DEBUG][main]03-06 22:12:55 at cn.xishan.oftenporter.porter.core.init.PorterMain.destroyAll(PorterMain.java:542):[Default] destroyAll end!

二、Servlet方式

1、创建maven项目OftenQuickStart2,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>cn.chenyg.demo.often</groupId>
    <artifactId>OftenQuickStart2</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <project.slf4j.version>1.7.25</project.slf4j.version>
        <project.often.version>1.1.1.65</project.often.version>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/com.xishankeji/Porter-Core -->
        <dependency>
            <groupId>com.xishankeji</groupId>
            <artifactId>Porter-Core</artifactId>
            <version>${project.often.version}</version>
        </dependency>

        <dependency>
            <groupId>com.xishankeji</groupId>
            <artifactId>Porter-Bridge-Servlet</artifactId>
            <version>${project.often.version}</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${project.slf4j.version}</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.4.12.v20180830</version>
                <configuration>
                    <webApp>
                        <contextPath>/${artifactId}</contextPath>
                    </webApp>
                    <httpConnector>
                        <port>8080</port>
                    </httpConnector>
                    <reload>automatic</reload>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

2、创建Porter类:HelloPorter.java

package cn.chenyg.demo.often.demo01;

import cn.xishan.oftenporter.porter.core.annotation.PortIn;
import cn.xishan.oftenporter.porter.core.base.OftenObject;

/**
 * @author Created by https://github.com/CLovinr on 2019/3/6.
 */
@PortIn
public class HelloPorter
{
    @PortIn
    public String say(OftenObject oftenObject)
    {
        return "Hello Word!";
    }
}

3、创建启动类:StartupInitializer.java

package cn.chenyg.demo.often.demo02;

import cn.xishan.oftenporter.porter.core.init.PorterConf;
import cn.xishan.oftenporter.servlet.OftenInitializer;

import javax.servlet.ServletContext;

/**
 * @author Created by https://github.com/CLovinr on 2019/3/6.
 */
public class StartupInitializer implements OftenInitializer
{
    public void beforeStart(ServletContext servletContext, BuilderBefore builderBefore) throws Exception
    {
        builderBefore.setBridgeName("Default");
    }

    public void onStart(ServletContext servletContext, Builder builder) throws Exception
    {
        PorterConf porterConf = builder.newPorterConf();
        porterConf.setOftenContextName("Often");
        porterConf.getSeekPackages().addClassPorter(HelloPorter.class);
        builder.startOne(porterConf);
    }
}

4、执行"mvn jetty:run",或者在idea的maven窗口对应项目的"Plugins-->jetty-->jetty:run"右键执行"Debug..."