1. 架构
细化结构:
1. 1.hessian-study的pom文件
4.0.0
com.fracong
hessian-study
0.0.1-SNAPSHOT
pom
1.8
1.2.3
1.2.29
org.springframework.boot
spring-boot-dependencies
2.1.2.RELEASE
pom
import
ch.qos.logback
logback-classic
${ch.qos.logback.version}
ch.qos.logback
logback-core
${ch.qos.logback.version}
com.alibaba
fastjson
${fastjson-version}
org.apache.commons
commons-lang3
3.0
com.alibaba
fastjson
javax.servlet
javax.servlet-api
provided
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
hessian-server
hessian-client
hessian-interface
1.2.hessian-interface的pom文件
4.0.0
com.fracong
hessian-study
0.0.1-SNAPSHOT
hessian-interface
1.3.hessian-service的pom文件
4.0.0
com.fracong
hessian-study
0.0.1-SNAPSHOT
hessian-server
org.springframework.boot
spring-boot-starter-web
com.alibaba
fastjson
com.caucho
hessian
4.0.38
${project.parent.groupId}
hessian-interface
${project.parent.version}
jar
1.4.hessian-client的pom文件
4.0.0
com.fracong
hessian-study
0.0.1-SNAPSHOT
hessian-client
org.springframework.boot
spring-boot-starter-web
com.alibaba
fastjson
com.caucho
hessian
4.0.38
${project.parent.groupId}
hessian-interface
${project.parent.version}
jar
2.1.hessian-service的配置文件
server.servlet.context-path=/hessian-server
server.port=8280
2.2.hessian-client的配置文件
server.servlet.context-path=/hessian-client
server.port=8180
3.代码实现
3.1.hessian-interface的统一接口
package com.fracong.hessian.test.myinterface;
public interface HessianTestServiceInterface {
public String sayTest(String str);
}
3.2.hessian-service的服务注册
3.2.1.注入service,实现hessian-interface中的接口
package com.fracong.hessian.test.service;
import org.springframework.stereotype.Service;
import com.fracong.hessian.test.myinterface.HessianTestServiceInterface;
@Service
public class HessianTestService implements HessianTestServiceInterface{
@Override
public String sayTest(String str) {
return "aaa-"+str;
}
}
3.2.2.注入service的服务发现
书写TestServiceComponent
package com.fracong.hessian.component;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.remoting.caucho.HessianServiceExporter;
import org.springframework.stereotype.Component;
import com.fracong.hessian.test.myinterface.HessianTestServiceInterface;
import com.fracong.hessian.test.service.HessianTestService;
@Component
public class TestServiceComponent {
@Autowired
private HessianTestService hessianTestService;
@Bean(name = "/HessianTestService")
public HessianServiceExporter accountService() {
HessianServiceExporter exporter = new HessianServiceExporter();
exporter.setService(hessianTestService);
exporter.setServiceInterface(HessianTestServiceInterface.class);
return exporter;
}
}
3.3.hessian-client的客户端发现服务
3.3.1.发现服务
package com.fracong.hessian.test.component;
import org.springframework.context.annotation.Bean;
import org.springframework.remoting.caucho.HessianProxyFactoryBean;
import org.springframework.stereotype.Component;
import com.fracong.hessian.test.myinterface.HessianTestServiceInterface;
@Component
public class HessianClientComponent {
@Bean
public HessianProxyFactoryBean helloClient() {
HessianProxyFactoryBean factory = new HessianProxyFactoryBean();
//该接口的路径,需要和3.2.2中注册的bean名称一致
factory.setServiceUrl("http://localhost:8280/hessian-server/HessianTestService");
factory.setServiceInterface(HessianTestServiceInterface.class);
return factory;
}
}
3.3.2.服务调用测试
package com.fracong.hessian.test.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fracong.hessian.test.myinterface.HessianTestServiceInterface;
@RestController
public class TestController {
@Autowired
private HessianTestServiceInterface hessianService;
@RequestMapping(value="/test/{id}")
public String test(@PathVariable("id")String id) {
String sayTest = hessianService.sayTest("aa"+id);
return sayTest;
}
}
4.服务启动
4.1.服务端启动
package com.fracong.hessian;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan("com.fracong")
public class HessianServiceApplication {
//启动类入口
public static void main(String[] args) {
SpringApplication.run(HessianServiceApplication.class,args);
}
}
4.2.客户端启动
package com.fracong.hessian;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan("com.fracong")
public class HessianClientApplication {
public static void main(String[] args) {
SpringApplication.run(HessianClientApplication.class, args);
}
}
5.测试
http://localhost:8180/hessian-client/test/222
结果:aaa-aa222