关于Grails的Remoting插件的问题?

搞了有一会了,搞不出来,在Web应用程序中我写的

StudentService.java

interface StudentService{
    List getStudents()
}

StudentServiceImpl .java

class StudentServiceImpl implements StudentService{
    static expose = ['hessian', 'rmi']
    static remote = [ protocol: 'hessian',
                      iface: StudentService,
                      host: 'localhost',
                      port: '8080',
                      webcontext: 'Student']
    @Override
    public List getStudents() {
        ArrayList students=Student.list()
        println students
    return students;
    }
}

Student这个类我序列化了。 我已经安装了插件remoting,其实我是按照http://www.grails.org/plugin/remoting一步一步操作的。

在客户端的应用程序写的代码:并且加入了hessian-3.2.0.jar

StudentService.java

interface StudentService{
    List getStudents()
}

RemoteClient .java

public class RemoteClient {
    public static main(args) {
        def url = "http://localhost:8080/GrailsTest/hessian/StudentServiceImpl"
        def factory = new HessianProxyFactory()
        def basic = factory.create(StudentService.class,url)
        println basic.getStudents()
    }
}

在web控制台的错误:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'hessian.StudentServiceImpl' is defined

在客户端应用程序的错误:

Exception in thread "main" com.caucho.hessian.client.HessianConnectionException: 500: java.io.IOException: Server returned HTTP response code: 500 for URL: http://localhost:8080/GrailsTest/hessian/StudentServiceImpl

我的分析:我知道web这里没有配置好,唉,菜鸟嘛,能力有限。。求帮助了。谢谢哈

你的客户端的代码是正确,问题出在服务实现部分。你的用法有问题:expose和remote属性是互斥的。前者是针对远程服务而言,可简单的理解为服务提供者;后者针对Grails应用中的服务客户端。

正确的使用步骤如下:

  1. 创建一个接口A,该文件可以放在GrailsApp/src/java或者src/groovy目录下,或者编译后放在jar文件中;
  2. 在GrailsApp/grails-app/services目录下创建一个服务实现这个接口,并在其中使用expose:
  3. class MyService implements A {
       static expose = ['hessian', 'rmi']
       def methodA() { … }
    }
    

    此时,运行Grails程序之后,该服务就被暴露成一个远程服务。然后在客户端里使用:http://localhost:8080/appname/hessian/MyService,便可访问服务MyService。

  4. 如果要在Grails中使用远程服务,可以在GrailsApp/grails-app/services目录下创建一个服务YourService,在其中使用remote属性:
  5. class YourService {
        static remote = [
            protocol: 'hessian', //访问远程服务使用的协议
            iface: A,//远程服务实现的接口名 packageName.interfaceName
            host: 'localhost',//远程服务所在的主机名
            port: '8080', //远程服务使用的端口
            webcontext: 'temp',//远程服务所在应用的上下文
        ]
    }
    

    之后,在Controller或者其他服务中注入YourService,对其方法的调用都会路由到远程服务。

By 匿名用户 - Posted on 19 五月 2011

新问题产生。访问url有问题吗?

com.caucho.hessian.client.HessianConnectionException: HessianProxy cannot connect to 'http://localhost:8080/GrailsTest/hessian/StudentServiceImpl

这次还是有问题,web应用端没有出现问题。客户端出现问题: 客户端代码:

public class RemoteClient { 
    public static main(args) { 
        def url = "http://localhost:8080/GrailsTest/hessian/StudentServiceImpl" 
        def factory = new HessianProxyFactory() 
        def studentService = (StudentService)factory.create(StudentService.class,url) 
        println studentService.getStudents() 
    } 
}

我尝试了http://localhost:8080/GrailsTest.hessian/StudentServiceImpl还是不行,不知道问题根源,网上查了无果,我想知道问题出现在什么地方。谢谢了

很明显还是链接的问题。

我这里客户端正常,可以找到服务。检查以下几项:

  1. 请确保StudentServiceImpl是在grails-app\services目录下;
  2. 看看客户端里面的那段代码在grails下是否能运行;
  3. 将localhost或者127.0.0.1换成IPv4的地址,形如:192.168.6.1

搞定问题

请确保StudentServiceImpl是在grails-app\services目录下;仅仅不够。

还有一个问题是hessian-3.1-3.jar没问题,我引入的hessian-3.2.0.jar会存在兼容性的问题。最后谢谢解答人。

 

图片没有显示出来,就是Service类必须以Service结尾,我犯了一个基本错误。

The above example will create a service at the location grails-app/services/SimpleService.groovy. A service's name ends with the convention Service, other than that a service is a plain Groovy class: