Hello,World 百态

简介:

最简陋HelloWorld

首先,编写一个页面文件叫:hello.page,输入下面的信息,然后运行之!

?
1
Hello:$!name

浏览器URL:http://localhost:/hello.page

运行结果如下:


浏览器URL:http://localhost:/hello.page?name=abc

运行结果如下:
?
1
Hello:abc
很明显,上面的HelloWorld仅仅是在展现层完成的,不管怎么样,他不是用JS直接在页面输出的。


增加HelloWorld处理类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
@ServiceComponent ()
public class HelloWorldService {
     @ServiceMethod (serviceId = "helloWorldService" )
     @ServiceResult (name = "sayHelloResult" )
     @ServiceViewMapping (type= "page" ,path= "/helloworld.page" )
     public String helloWorld(String name) {
         if (name == null ) {
             return "您好,guest!" ;
         } else {
             return "您好," + name;
         }
     }
}

编写helloworld.page文件,内容如下:

?
1
$!sayHelloResult

浏览器URL:http://localhost:8080/helloWorldService.servicepage

运行结果如下:
?
1
您好,guest!

浏览器URL:http://localhost:8080/helloWorldService.servicepage?name=abc

运行结果如下:
?
1
您好,abc
好的,现在已经写了Java类,并且写了展现页面,并且已经正确的执行了结果。


换一种访问方式,试试看?

浏览器URL:http://localhost:8080/helloWorldService.servicexml?name=abc

?
1
<string>您好,abc</string>

换一种访问方式,试试看?

浏览器URL:http://localhost:8080/helloWorldService.servicejson?name=abc

?
1
<string>您好,abc</string>

运行结果如下:

?
1
"您好,abc"
限于时间关系,怎么通过webservice访问就不再展示了,实际上,通过WebService访问也是没有问题的。


小结:只要定义一个Service,就可以用N种方式来访问它。

可以渲染为一个html页面,也可以渲染为了个JSon,也可以是一段Xml,还可以是一个Excel表格,等等。

这个时候,我们在4台机器上运行,其中一台配置为AR,两台配置为AS,一台配置为SC。

AR为Web接入服务器,AS为应用服务器,SC为服务中心,这个时候用JMeter来对AR进行并发访问,你会发现两台AS都在提供服务。这证明了你的应用服务已经可以进行水平扩展了,而且是基于SOA模式的。


同样的去访问WebService,你会发现,也会被负载给两台AS。

也就是说,你只写一次,就可以以各种方式向外提供服务。

当然,你看到了,在你的服务处理类上定义了我们的注解,这会对你的代码形成侵入性。

如果一点也不想依赖我们的类和接口,那也没有问题。

类的写法如下:

?
1
2
3
4
5
6
7
8
9
public class HelloWorldService {
     public String helloWorld(String name) {
         if (name == null ) {
             return "您好,guest!" ;
         } else {
             return "您好," + name;
         }
     }
}

另外添加如下配置:

hello.service.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
< service-components >
     < service-component type = "org.tinygroup.helloproject.HelloWorldService" >
         < service-method service-id = "helloWorldService" method-name = "helloWorld" >
             < service-parameters >
                 < service-parameter name = "name" type = "java.lang.String"
                     required = "true" is-array = "false" />
             </ service-parameters >
             < service-result name = "sayHelloResult" required = "true"
                 is-array = "false" type = "java.lang.String" />
         </ service-method >
     </ service-component >
</ service-components >

hello.serviceMapping.xml


?
1
2
3
< service-mappings >
     < service-mapping service = "helloWorldService" type = "page" path="/helloworld/helloworld.page<span></ span >"></ service-mapping >
</ service-mappings >


这种方式与用注解方式达到的结果是完全一样的。

通过Hello的了解,您可能知道了Tiny框架的一些特点,这里小结一下:

  • 可以没有任何侵入性,但是需要写一些xml文件
  • 如果可以接受一些注解,那么开发将更加简单
  • 开发了服务,就代表着可以做许多扩展的功能,而这些扩展的功能,不会要你做额外的工作
  • Tiny框架中的Service与Spring中的Service的函义不同,它等价于WebService中的Service,就是说:你不用管它在哪里,实际上你也不知道它是在哪台物理机器上运行的,总之它被执行了。
  • Tiny框架天生支持前后台服务器的水平扩展,而你不需要做任何针对性的开发-当然需要遵守其规约--所有的要发布成服务的参数及返回值必须是可序列化的,其它没有任何附加条件。

如果这个HelloWorld示例学会了,表示你学会了:

  • Tiny的界面开发
  • Tiny的服务开发

而普通的程序员不需要学习Tiny的其它内容,当然,架构师要学的东西还是要多些的。

相关文章
01:Hello, World!
01:Hello, World!
103 0
|
Web App开发 PHP 索引
简单hello world
第一步配置路由:   打开app/http/route.php文件,输入:Route::get('/home', 'HomeController@index'); 第二步配置控制器:   控制文件可以手动添加,也可以通过命令进行添加,文件放在目录app/http/controllers/目录下,如HomeController.
828 0
|
Python
hello world!
基本上所有的计算机语言的学习第一件的事情就是问候一下世界,之前我们都只是在编码器中问候,这次让我们在浏览器中问候一下这个残酷但却美好的世界吧。 from flask import Flask #导入库 app = Flask(__name__) #实例化 @app.
862 0
Hello,World
引用自http://www.cnblogs.com/jbelial/archive/2013/05/08/3067471.html#2676127 题目: 1 public class text { 2 public static void main(String[] args) { 3 if ( 你的代码 ) 4 System.
770 0

热门文章

最新文章