当前位置: 首页 > news >正文

北方工业大学网站建设/百度热搜榜排名今日头条

北方工业大学网站建设,百度热搜榜排名今日头条,接做网站的,衢州站扩建目录 环境搭建 Dubbo的3种使用方式: 1. XML配置的方式,一般用于Spring MVC工程 2. 配置文件的方式 (spring boot工程) 3. 注解方式 Dubbo 控制台 环境搭建 本篇将介绍Spring boot zookeeper Dubbo 简易环境的搭建以及使用…

目录

环境搭建

Dubbo的3种使用方式:

1. XML配置的方式,一般用于Spring MVC工程

2. 配置文件的方式 (spring boot工程)

3. 注解方式

Dubbo 控制台


环境搭建

本篇将介绍Spring boot + zookeeper + Dubbo 简易环境的搭建以及使用,首先准备好一台虚拟机。

1. 在虚拟机上安装JDK8及以上版本,可以参考我的另一篇博客 https://www.cnblogs.com/chen1-kerr/p/6907280.html

2. 在虚拟机上安装zookeeper。可以是单机,也可以是集群。可以参考我的文件

单机: zookeeper 单机环境搭建(三)_chen_yao_kerr的博客-CSDN博客 

集群: zookeeper集群(二)_zookeeper集群相互发现_chen_yao_kerr的博客-CSDN博客 

3. 新建2个spring boot工程,一个是Server端,一个是client端,并且引入zookeeper 和 dubbo依赖的jar包。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>dubbo-basic</artifactId><groupId>com.enjoy</groupId><version>1.0</version></parent><modelVersion>4.0.0</modelVersion><artifactId>busi-xml-server-client</artifactId><dependencies><dependency><groupId>com.enjoy</groupId><artifactId>busi-api</artifactId><version>1.0</version></dependency><!-- spring支持 --><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><!-- dubbo --><dependency><groupId>com.alibaba</groupId><artifactId>dubbo-config-spring</artifactId><version>${dubbo.version}</version></dependency><!-- zookeeper注册中心 --><dependency><groupId>com.alibaba</groupId><artifactId>dubbo-registry-zookeeper</artifactId><version>${dubbo.version}</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>dubbo-rpc-dubbo</artifactId><version>${dubbo.version}</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>dubbo-remoting-netty</artifactId><version>${dubbo.version}</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>dubbo-serialization-hessian2</artifactId><version>${dubbo.version}</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit_version}</version></dependency></dependencies>
</project>

 我本次使用的虚拟机IP是 192.168.0.105。 因此,需要到这台虚拟机服务器上启动zookeeper

抽象出接口:

Dubbo的3种使用方式:

1. XML配置的方式,一般用于Spring MVC工程

服务端(Server)

服务端需要去实现这个接口,因为这个实现类要提供具体的业务处理逻辑:

服务端的xml配置

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.1.xsdhttp://code.alibabatech.com/schema/dubbohttp://code.alibabatech.com/schema/dubbo/dubbo.xsd"><!--全局配置--><dubbo:provider timeout="3000" /><!-- 服务提供方应用名称, 方便用于依赖跟踪 --><dubbo:application name="busi-server"/><!-- 使用本地zookeeper作为注册中心 --><dubbo:registry address="zookeeper://192.168.0.105:2181" /><!--name指示使用什么协议监听端口:dubbo/rmi/rest--><dubbo:protocol id="d1"  name="dubbo" port="20880" /><dubbo:protocol id="d2"  name="dubbo" port="20882" /><!-- 通过xml方式配置为bean, 让spring托管和实例化 --><bean id="orderService" class="com.enjoy.service.OrderServiceImpl"/><!-- 声明服务暴露的接口,并暴露服务 --><dubbo:service interface="com.enjoy.service.OrderService" ref="orderService" protocol="d1" />
</beans>

、客户端(Consumer):

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.1.xsdhttp://code.alibabatech.com/schema/dubbohttp://code.alibabatech.com/schema/dubbo/dubbo.xsd"><dubbo:application name="busi-client" /><dubbo:registry address="zookeeper://192.168.0.105:2181" /><dubbo:consumer /><dubbo:reference id="orderService" interface="com.enjoy.service.OrderService" /></beans>

启动Server:

启动客户端:

2. 配置文件的方式 (spring boot工程)

首先在Server端定义配置文件:

开放一个业务类:

服务端加载配置:

在Consumer端定义配置文件

定义一个类,这个类要从远程服务器上拿到需要使用的实例

消费端调用:

3. 注解方式

这种方式其实和第2种方式基本一致,唯一的不同就是少了个配置文件,这个配置文件,多了些注入的类:

服务端代码:

/***   Licensed to the Apache Software Foundation (ASF) under one or more*   contributor license agreements.  See the NOTICE file distributed with*   this work for additional information regarding copyright ownership.*   The ASF licenses this file to You under the Apache License, Version 2.0*   (the "License"); you may not use this file except in compliance with*   the License.  You may obtain a copy of the License at**       http://www.apache.org/licenses/LICENSE-2.0**   Unless required by applicable law or agreed to in writing, software*   distributed under the License is distributed on an "AS IS" BASIS,*   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.*   See the License for the specific language governing permissions and*   limitations under the License.**/package com.enjoy.dubbo.annotation;import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.ProviderConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;public class Provider {public static void main(String[] args) throws Exception {AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ProviderConfiguration.class);context.start();System.out.println("---------dubbo启动成功--------");System.in.read();}@Configuration@EnableDubbo(scanBasePackages = "com.enjoy.service")static class ProviderConfiguration {@Beanpublic ProviderConfig providerConfig() {ProviderConfig providerConfig = new ProviderConfig();providerConfig.setTimeout(1000);return providerConfig;}@Beanpublic ApplicationConfig applicationConfig() {ApplicationConfig applicationConfig = new ApplicationConfig();applicationConfig.setName("busi-provider");return applicationConfig;}@Beanpublic RegistryConfig registryConfig() {RegistryConfig registryConfig = new RegistryConfig();registryConfig.setProtocol("zookeeper");registryConfig.setAddress("192.168.0.105");registryConfig.setPort(2181);return registryConfig;}@Beanpublic ProtocolConfig protocolConfig() {ProtocolConfig protocolConfig = new ProtocolConfig();protocolConfig.setName("dubbo");protocolConfig.setPort(20880);return protocolConfig;}}}

客户端:

/***   Licensed to the Apache Software Foundation (ASF) under one or more*   contributor license agreements.  See the NOTICE file distributed with*   this work for additional information regarding copyright ownership.*   The ASF licenses this file to You under the Apache License, Version 2.0*   (the "License"); you may not use this file except in compliance with*   the License.  You may obtain a copy of the License at**       http://www.apache.org/licenses/LICENSE-2.0**   Unless required by applicable law or agreed to in writing, software*   distributed under the License is distributed on an "AS IS" BASIS,*   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.*   See the License for the specific language governing permissions and*   limitations under the License.**/package com.enjoy.dubbo.annotation;import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ConsumerConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import com.enjoy.action.ServiceConsumer;
import com.enjoy.entity.OrderEntiry;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;public class Consumer {public static void main(String[] args) {AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);context.start();System.out.println("---------dubbo启动成功--------");ServiceConsumer serviceConsumer = context.getBean(ServiceConsumer.class);OrderEntiry entiry = serviceConsumer.getDetail("1");System.out.println("result: " + entiry.getMoney());}@Configuration@EnableDubbo(scanBasePackages = "com.enjoy.action")@ComponentScan(value = {"com.enjoy.action"})static class ConsumerConfiguration {@Beanpublic ApplicationConfig applicationConfig() {ApplicationConfig applicationConfig = new ApplicationConfig();applicationConfig.setName("busi-consumer");return applicationConfig;}@Beanpublic ConsumerConfig consumerConfig() {ConsumerConfig consumerConfig = new ConsumerConfig();consumerConfig.setTimeout(3000);return consumerConfig;}@Beanpublic RegistryConfig registryConfig() {RegistryConfig registryConfig = new RegistryConfig();registryConfig.setProtocol("zookeeper");registryConfig.setAddress("192.168.0.105");registryConfig.setPort(2181);return registryConfig;}}
}

其实,具体怎么用,可根据实际情况进行选择。如果是Spring MVC工程,选第一种方式比较好。如果是Spring boot工程,我个人倾向于选择第二种方式,也就是注解+配置文件的方式。

Dubbo 控制台

dubbo-admin 的地址为: mirrors / apache / dubbo-admin · GitCode 

但是,我使用命令拉下来总是少文件,所有我直接下载的zip包解压。

 修改配置:

 编译并启动:

能打开页面,能够登录即可:

上方介绍了集中服务端启动的方式,选择任意一种方式启动服务端代码,如果在控制台能够发现即可:

http://www.jmfq.cn/news/5356927.html

相关文章:

  • app网站建设方案/下载安装
  • 哈尔滨城乡建设局网站/邢台网站公司
  • 百度云网站建设/下载百度网盘
  • 品牌网站建设公司排名/网络营销软件下载
  • 营销网站建设哪里便宜/今日足球赛事分析推荐
  • 地产公司网站建设方案/深圳网站搜索优化工具
  • 网站建设 小程序开发 营销推广/优化推广关键词
  • 广州百度网站建设公司/一站式自媒体服务平台
  • 怎么网站建设怎么样/有没有免费的广告平台
  • 门户网站团队建设/360站长工具
  • 小型企业网站建设内容/最新域名8xgmvxyz
  • 平江网站建设/软文代发布
  • 网站建设板块/最近一周的新闻
  • 政府网站建设先进个人先进事迹/360竞价推广开户多少钱
  • 江苏省建设网站/客源引流推广
  • 建筑工程网站建设/海洋seo
  • 文山城乡建设部网站首页/百度账号官网
  • 网站建设的调查问卷/在线制作网页网站
  • 长春平面网站建设/怎么推广公司网站
  • 网站建网站建设和优/seo+网站排名
  • 网站建设三网合一/app开发平台开发
  • 护肤品网站建设的摘要/新网
  • 哈尔滨 房产网站建设/网络营销技巧
  • 动态网站建设案例教程电子书下载/百度指数查询app
  • 山东省建设官方网站/万能软文范例800字
  • o2o网站建设新闻/企业宣传册模板
  • 亭湖区建设局网站/推广普通话的意义
  • 推动门户网站建设不断优化升级/百度竞价关键词质量度怎么提升
  • 游戏网站建设收费明细/seo在线优化
  • 房山区住房和城乡建设委员会网站/郑州网络营销公司