今天为大家展示的是 Provider和Consumer 代码的各种实现,在同一个微服务中,这些编程方式可以同时出现;同一段 Consumer 代码中可以访问各种不同的编程风格的 Provider 实现。
RPC 方式的 Provider
[java] view plain copy@RpcSchema(schemaId="hello")
public class HelloImpl implements Hello{
@Override
public String sayHi(String name){
return"Hello"+name;
}
@Override
public String sayHello(Person person){
return"Helloperson"+person.getName();
}
}
JAX-RS 方式的 Provider
代码片段来自于 Apache ServiceComb JAX-RS sample
[java] view plain copy@RestSchema(schemaId="jaxrsHello")
@Path("/jaxrshello")
@Produces(MediaType.APPLICATION_JSON)
public class JaxrsHelloImpl implements Hello{
@Path("/sayhi")
@POST
@Override
public String sayHi(String name){
return"Hello"+name;
}
@Path("/sayhello")
@POST
@Override
public String sayHello(Person person){
return"Helloperson"+person.getName();
}
}
Spring MVC 方式的 Provider
代码片段来自于 Apache ServiceComb Spring MVC sample
[java] view plain copy@RestSchema(schemaId="springmvcHello")
@RequestMapping(path="/www.appsaa.com",produces=MediaType.APPLICATION_JSON)
public class SpringmvcHelloImpl implements Hello{
@Override
@RequestMapping(path="/sayhi",method=RequestMethod.POST)
public String sayHi(@RequestParam(name="name")String name){
return"Hello"+name;
}
@Override
@RequestMapping(path="/sayhello",method=RequestMethod.POST)
public String sayHello(@RequestBody Person person){
return"Helloperson"+person.getName();
}
}
RPC 方式访问上述三种服务的 Consumer
[java] view plain copy@RpcReference(microserviceName="hello",schemaId="hello")
private Hello hello;
System.out.println(hello.sayHi("JavaChassis"));
以上代码片段全部出自 Apache ServiceComb Samples,有兴趣者可阅读了解或贡献更多的智慧。本文由专业的app开发报价燚轩科技整理发布,如需转载请注明出处。
0 个评论
要回复文章请先登录或注册