Dubbo(八)之API 配置
生活随笔
收集整理的這篇文章主要介紹了
Dubbo(八)之API 配置
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
轉載自??DubboAPI 配置
以API 配置的方式來配置你的 Dubbo 應用
API 屬性與配置項一對一,各屬性含義,請參見:配置參考手冊,比如:ApplicationConfig.setName("xxx")?對應?<dubbo:application name="xxx" />?1
服務提供者
import org.apache.dubbo.rpc.config.ApplicationConfig; import org.apache.dubbo.rpc.config.RegistryConfig; import org.apache.dubbo.rpc.config.ProviderConfig; import org.apache.dubbo.rpc.config.ServiceConfig; import com.xxx.XxxService; import com.xxx.XxxServiceImpl;// 服務實現(xiàn) XxxService xxxService = new XxxServiceImpl();// 當前應用配置 ApplicationConfig application = new ApplicationConfig(); application.setName("xxx");// 連接注冊中心配置 RegistryConfig registry = new RegistryConfig(); registry.setAddress("10.20.130.230:9090"); registry.setUsername("aaa"); registry.setPassword("bbb");// 服務提供者協(xié)議配置 ProtocolConfig protocol = new ProtocolConfig(); protocol.setName("dubbo"); protocol.setPort(12345); protocol.setThreads(200);// 注意:ServiceConfig為重對象,內部封裝了與注冊中心的連接,以及開啟服務端口// 服務提供者暴露服務配置 ServiceConfig<XxxService> service = new ServiceConfig<XxxService>(); // 此實例很重,封裝了與注冊中心的連接,請自行緩存,否則可能造成內存和連接泄漏 service.setApplication(application); service.setRegistry(registry); // 多個注冊中心可以用setRegistries() service.setProtocol(protocol); // 多個協(xié)議可以用setProtocols() service.setInterface(XxxService.class); service.setRef(xxxService); service.setVersion("1.0.0");// 暴露及注冊服務 service.export();服務消費者
import org.apache.dubbo.rpc.config.ApplicationConfig; import org.apache.dubbo.rpc.config.RegistryConfig; import org.apache.dubbo.rpc.config.ConsumerConfig; import org.apache.dubbo.rpc.config.ReferenceConfig; import com.xxx.XxxService;// 當前應用配置 ApplicationConfig application = new ApplicationConfig(); application.setName("yyy");// 連接注冊中心配置 RegistryConfig registry = new RegistryConfig(); registry.setAddress("10.20.130.230:9090"); registry.setUsername("aaa"); registry.setPassword("bbb");// 注意:ReferenceConfig為重對象,內部封裝了與注冊中心的連接,以及與服務提供方的連接// 引用遠程服務 ReferenceConfig<XxxService> reference = new ReferenceConfig<XxxService>(); // 此實例很重,封裝了與注冊中心的連接以及與提供者的連接,請自行緩存,否則可能造成內存和連接泄漏 reference.setApplication(application); reference.setRegistry(registry); // 多個注冊中心可以用setRegistries() reference.setInterface(XxxService.class); reference.setVersion("1.0.0");// 和本地bean一樣使用xxxService XxxService xxxService = reference.get(); // 注意:此代理對象內部封裝了所有通訊細節(jié),對象較重,請緩存復用特殊場景
下面只列出不同的地方,其它參見上面的寫法
方法級設置
...// 方法級配置 List<MethodConfig> methods = new ArrayList<MethodConfig>(); MethodConfig method = new MethodConfig(); method.setName("createXxx"); method.setTimeout(10000); method.setRetries(0); methods.add(method);// 引用遠程服務 ReferenceConfig<XxxService> reference = new ReferenceConfig<XxxService>(); // 此實例很重,封裝了與注冊中心的連接以及與提供者的連接,請自行緩存,否則可能造成內存和連接泄漏 ... reference.setMethods(methods); // 設置方法級配置...點對點直連
...ReferenceConfig<XxxService> reference = new ReferenceConfig<XxxService>(); // 此實例很重,封裝了與注冊中心的連接以及與提供者的連接,請自行緩存,否則可能造成內存和連接泄漏 // 如果點對點直連,可以用reference.setUrl()指定目標地址,設置url后將繞過注冊中心, // 其中,協(xié)議對應provider.setProtocol()的值,端口對應provider.setPort()的值, // 路徑對應service.setPath()的值,如果未設置path,缺省path為接口名 reference.setUrl("dubbo://10.20.130.230:20880/com.xxx.XxxService"); ...API使用范圍說明:API 僅用于 OpenAPI, ESB, Test, Mock 等系統(tǒng)集成,普通服務提供方或消費方,請采用XML 配置方式使用 Dubbo???
總結
以上是生活随笔為你收集整理的Dubbo(八)之API 配置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Dubbo(七)之自动加载环境变量
- 下一篇: 多个路由器怎么设置多个路由器如何设置