一.結構圖
二.啓動順序
1.啓動zookeeper
配置:\zookeeper\conf\zoo.cfg
執行 : zookeeper\bin\zkServer.cmd
2.啓動服務提供者(dubbo服務包含在內)
啓動tomcat即可
spring 配置文件
<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
">
<!-- 具體的實現bean -->
<bean id="demoService" class="com.springapp.mvc.service.impl.DemoServiceImpl" />
<!-- 提供方應用信息,用於計算依賴關係 -->
<dubbo:application name="xs_provider" />
<!-- 使用multicast廣播註冊中心暴露服務地址 -->
<!--<dubbo:registry address="multicast://224.5.6.7:1234" /> -->
<!-- 使用zookeeper註冊中心暴露服務地址 -即zookeeper的所在服務器ip地址和端口號 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 用dubbo協議在20880端口暴露服務 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 聲明需要暴露的服務接口 -->
<dubbo:service interface="com.springapp.mvc.service.DemoService"
ref="demoService" />
</beans>
web.xml配置文件
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Application</display-name>
<welcome-file-list>
<welcome-file>test.html</welcome-file>
</welcome-file-list>
<!--<servlet>-->
<!--<servlet-name>mvc-dispatcher</servlet-name>-->
<!--<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>-->
<!--<load-on-startup>1</load-on-startup>-->
<!--</servlet>-->
<!--<servlet-mapping>-->
<!--<servlet-name>mvc-dispatcher</servlet-name>-->
<!--<url-pattern>/</url-pattern>-->
<!--</servlet-mapping>-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/mvc-*.xml
</param-value>
</context-param>
</web-app>
pom文件
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.springapp</groupId>
<artifactId>springMvc</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>springMvc</name>
<properties>
<spring.version>4.1.1.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>springMvc</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/*Tests.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</project>
java代碼
package com.springapp.mvc.service;
import java.util.List;
public interface DemoService {
String sayHello(String name);
public List getUsers();
}
package com.springapp.mvc.service.impl;
import com.springapp.mvc.service.DemoService;
import java.util.ArrayList;
import java.util.List;
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
return "Hello " + name;
}
@Override
public List getUsers() {
return new ArrayList();
}
}
3.啓動dubbo_web_admin
將war包放在tomcat 的webapp目錄下即可
訪問地址:http://localhost:8080/ 端口根據自己tomcat配置爲準
配置文件 \tomcat7\webapps\dubbo-admin-2.5.3\WEB-INF\dubbo.properties root和guest爲登錄賬號密碼 zookeeper://127.0.0.1:2181 爲zookeeper地址
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest
4.啓動服務消費者
pom.xml同服務提供者
項目結構
spring配置文件
<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:application name="hjy_consumer" />
<!-- 使用zookeeper註冊中心暴露服務地址 -->
<!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 生成遠程服務代理,可以像使用本地bean一樣使用demoService -->
<dubbo:reference id="demoService"
interface="com.springapp.mvc.service.DemoService" />
</beans>
Java代碼
package com.springapp.mvc.service;
import java.util.List;
public interface DemoService {
String sayHello(String name);
public List getUsers();
}
直接獲取spring上下文,調用服務提供者的服務
package com.springapp.mvc;
import com.springapp.mvc.service.DemoService;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import java.util.List;
/**
* Created by acer on 2017/3/2.
*/
public class Consumer {
public static void main(String[] args) throws Exception {
FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("D:\\project\\druid-master\\consumer\\src\\main\\webapp\\WEB-INF\\mvc-dispatcher-servlet.xml");
context.start();
DemoService demoService = (DemoService) context.getBean("demoService");
String hello = demoService.sayHello("hejingyuan");
System.out.println(hello);
List list = demoService.getUsers();
if (list != null && list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
System.in.read();
}
}
5.查看服務調用結果
所用到的安裝包等資源
dubbo-admin-2.5.3.war:http://download.csdn.net/detail/tangyali516/9219461
zookeeper-3.3.6.tar.gz :http://download.csdn.net/download/zknxx/9629211