中国建设银行春季招聘网站网站seo技术能不能赚钱
目录
环境搭建
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包解压。
修改配置:
编译并启动:
能打开页面,能够登录即可:
上方介绍了集中服务端启动的方式,选择任意一种方式启动服务端代码,如果在控制台能够发现即可: