sofa与springboot的入门案例 |
您所在的位置:网站首页 › citydo官网 › sofa与springboot的入门案例 |
一、什么是sofa: SOFAStack(Scalable Open Financial Architecture Stack)是用于快速构建金融级分布式架构的一套中间件,也是在金融场景里锤炼出来的最佳实践。 首先我们县城最简单的项目、SOFARPC 是蚂蚁金服开源的一款基于 Java 实现的 RPC 服务框架,为应用之间提供远程服务调用能力,具有高可伸缩性,高容错性,目前蚂蚁金服所有的业务的相互间的 RPC 调用都是采用 SOFARPC。SOFARPC 为用户提供了负载均衡,流量转发,链路追踪,链路数据透传,故障剔除等功能。SOFARPC 还支持不同的协议,目前包括 bolt,RESTful,dubbo,H2C 协议进行通信。其中 bolt 是蚂蚁金融服务集团开放的基于 Netty 开发的网络通信框架。 详细信息读API文档:https://www.sofastack.tech/sofa-rpc/docs/Home 二、多的不说上代码: 1、服务端: rpc-sofa.xml HelloSyncService.java、HelloSyncServiceImpl.java package com.citydo.sofaboot_server.service; /** * @author nick */ public interface HelloSyncService { String saySync(String string); } package com.citydo.sofaboot_server.service.impl; import com.citydo.sofaboot_server.service.HelloSyncService; public class HelloSyncServiceImpl implements HelloSyncService { @Override public String saySync(String sync) { return sync; } }启动类 package com.citydo.sofaboot_server; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.ImportResource; /** * 单向调用、同步调用、异步调用和回调 */ @ImportResource({"classpath:spring/rpc-sofa.xml"}) @SpringBootApplication public class SofabootServerApplication { public static void main(String[] args) { SpringApplication springApplication = new SpringApplication(SofabootServerApplication.class); ApplicationContext applicationContext = springApplication.run(args); } }2、客户端 rpc-sofa.xml HelloSyncService.java package com.citydo.sofaboot_client.service; public interface HelloSyncService { String saySync(String string); }启动类 package com.citydo.sofaboot_client; import com.citydo.sofaboot_client.service.HelloCallbackService; import com.citydo.sofaboot_client.service.HelloFutureService; import com.citydo.sofaboot_client.service.HelloSyncService; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.ImportResource; /** * @author nick */ @ImportResource({"classpath:spring/rpc-sofa.xml"}) @SpringBootApplication public class SofabootClientApplication { public static void main(String[] args) { System.setProperty("server.port", "8081"); SpringApplication springApplication = new SpringApplication(SofabootClientApplication.class); ApplicationContext applicationContext = springApplication.run(args); HelloSyncService boltHelloSyncService = (HelloSyncService) applicationContext.getBean("boltHelloSyncServiceReference"); HelloSyncService restHelloSyncService = (HelloSyncService) applicationContext.getBean("restHelloSyncServiceReference"); HelloSyncService dubboHelloSyncService = (HelloSyncService) applicationContext.getBean("dubboHelloSyncServiceReference"); System.out.println("Bolt result:" + boltHelloSyncService.saySync("bolt")); System.out.println("Rest result:" + restHelloSyncService.saySync("rest")); System.out.println("Dubbo result:" + dubboHelloSyncService.saySync("dubbo")); // HelloFutureService helloFutureServiceReference = (HelloFutureService) applicationContext .getBean("helloFutureServiceReference"); helloFutureServiceReference.sayFuture("future"); try { String result = (String)SofaResponseFuture.getResponse(1000, true); System.out.println("Future result: " + result); } catch (InterruptedException e) { e.printStackTrace(); } // HelloCallbackService helloCallbackServiceReference = (HelloCallbackService) applicationContext // .getBean("helloCallbackServiceReference"); // helloCallbackServiceReference.sayCallback("callback"); // try { // Thread.sleep(3000); // } catch (InterruptedException e) { // e.printStackTrace(); // } } }源码:https://github.com/863473007/sofa-springboot |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |