一. 参考我上面博客,在本机搭建启动好Zookeeper服务。
启动Zookeeper:
cd /Users/sunww/Documents/JAVA/Dubbo/zookeeper-3.4.12/bin/
启动命令 ./zkServer.sh start 关闭命令 ./zkServer.sh stop
二. Spring工程配置Dubbo服务(如下pom.xml)
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.LJSpring</groupId>
<artifactId>LJSpring</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>LJSpring Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!--lib setting-->
<spring.version>4.3.3.RELEASE</spring.version>
<!--<spring.version>3.2.3.RELEASE</spring.version>-->
<jackson.version>2.6.3</jackson.version>
</properties>
<dependencies>
<!--spring依赖-->
<!--1)核心依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.4</version>
</dependency>
</dependencies>
<build>
<finalName>LJSpring</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
三:项目演示
1. 启动服务成功
2.消费服务成功(在浏览器中输入 http://localhost:8080/test/getLoginfo )
在此处调用服务提供方提供的dubbo接口 ProviderServiceDemo,然后可以看到下面成功打印了消费信息
3. 在tomcat下运行可视化dubbo服务平台,可以看到有服务提供者和消费者信息
tomcat 启动
cd /usr/local/tomcat/bin sh startup.sh
登录dubbo-admin管理系统 http://localhost:9001/dubbo-admin/
用户名: root 密码:root
附录
服务提供方spring-dubboProvider.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:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 定义了提供方应用信息,用于计算依赖关系。在dubbo-admin 或 dubbo-monitor 会显示这个名字,方便识别 -->
<dubbo:application name="admin-provider" owner="admin" organization="dubbox"/>
<!-- 使用zookeeper 注册中心暴露服务,注意要先开启 zookeeper -->
<!--<dubbo:registry address="zookeeper://127.0.0.1:2181"/>-->
<dubbo:registry address="zookeeper://10.50.62.53:2181"/>
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880"/>
<!-- 用dubbo 协议实现定义好的 api 接口 -->
<dubbo:service interface="com.lujing.dubbo.ProviderServiceDemo" ref="providerService" protocol="dubbo"/>
<!-- 具体实现接口的bean -->
<bean id="providerService" class="com.lujing.dubbo.ProviderServiceDemoImpl"/>
</beans>
消费方spring-dubboConsumer.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:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<dubbo:application name="consumer-of-helloworld-app" />
<!-- 使用multicast广播注册中心暴露发现服务地址 -->
<!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
<!-- 使用Linux服务器上的Zookeeper -->
<!-- <dubbo:registry address="zookeeper://192.168.125.100:2181" /> -->
<!-- 使用本机的Zookeeper(Windows)-->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="demoService" interface="com.lujing.dubbo.ProviderServiceDemo" />
</beans>
---------------------
作者:水桶前辈
来源:CSDN
原文:https://blog.csdn.net/robinson_911/article/details/87638503
版权声明:本文为博主原创文章,转载请附上博文链接!
(责任编辑:IT) |