Dubbox服务demo

一、安装虚拟机,安装所需要的jdk、zookeeper并启动zookeeper,虚拟机的ip+zookeeper默认端口号2181

二、编写Service服务方

  1.创建Maven项目 

2.编写接口   

public interface UserService {

public String getUserName();
}

接口实现类

import com.alibaba.dubbo.config.annotation.Service;
import com.lf.service.UserService;
@Service
public class UserServiceImpl implements UserService {

@Override
public String getUserName() {
// TODO Auto-generated method stub
return "zookeeperServicedemo";
}

}

3. 创建配置文件applicationContext-service.xml

  加入如下配置:


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">





 

4.在WEB-INF下加入web.xml

  添加如下配置:  


xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">



contextConfigLocation
classpath:applicationContext*.xml


org.springframework.web.context.ContextLoaderListener

 

5.pom.xml添加依赖

 


4.2.4.RELEASE





org.springframework
spring-context
${spring.version}


org.springframework
spring-beans
${spring.version}


org.springframework
spring-webmvc
${spring.version}


org.springframework
spring-jdbc
${spring.version}


org.springframework
spring-aspects
${spring.version}


org.springframework
spring-jms
${spring.version}


org.springframework
spring-context-support
${spring.version}




com.alibaba
dubbo
2.8.4


org.apache.zookeeper
zookeeper
3.4.6


com.github.sgroschupf
zkclient
0.1



javassist
javassist
3.11.0.GA






org.apache.maven.plugins
maven-compiler-plugin
2.3.2

1.7
1.7



org.apache.tomcat.maven
tomcat7-maven-plugin


8081

/



6.启动服务方服务

  配置 clean tomcat7:run 启动服务

三、编写服务消费方

1.创建maven消费方

2.把服务方的接口复制到消费方

public interface UserService {

public String getUserName();
}

编写控制层代码

@Controller
@RequestMapping("/user")
public class UserController {

@Reference//引用接口
private UserService userService;

@RequestMapping("/getName")
@ResponseBody
public String showName(){
return userService.getUserName();
}
}

3加入mvc配置springmvc.xml

   添加如下配置:  

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">












4.在WEB-INF下加入web.xml


xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">


CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter

encoding
utf-8


forceEncoding
true



CharacterEncodingFilter
/*



springmvc
org.springframework.web.servlet.DispatcherServlet


contextConfigLocation
classpath:springmvc.xml




springmvc
*.do

5.添加pom.xml的依赖


4.2.4.RELEASE





org.springframework
spring-context
${spring.version}


org.springframework
spring-beans
${spring.version}


org.springframework
spring-webmvc
${spring.version}


org.springframework
spring-jdbc
${spring.version}


org.springframework
spring-aspects
${spring.version}


org.springframework
spring-jms
${spring.version}


org.springframework
spring-context-support
${spring.version}




com.alibaba
dubbo
2.8.4


org.apache.zookeeper
zookeeper
3.4.6


com.github.sgroschupf
zkclient
0.1



javassist
javassist
3.11.0.GA






org.apache.maven.plugins
maven-compiler-plugin
2.3.2

1.7
1.7



org.apache.tomcat.maven
tomcat7-maven-plugin


8082

/



6.启动消费方

7.访问服务

http://localhost:8082/user/getName.do

页面响应:zookeeperServicedemo

四、虚拟机中安装Tomcat,并在Tomcat下webapps中添加dubbox源码编译打包后的dubbox.admin.war

启动Tomcat,访问http://192.168.25.128:8080/dubbo-admin

输入账号、密码: root\root

查看服务

新增  批量倍权 批量半权 批量禁用 批量启用 批量删除

机器IP:   权重:   类型:  所有 动态 静态 状态:  所有 已启用 已禁用 检查:  所有 正常 警告 出错 操作
169.254.253.253:20880 100 动态 已启用 正常 编辑 复制 倍权 半权 禁用

你可能感兴趣的:(Dubbox服务demo)