源码:https://github.com/shengfenglai/SpringMVCWithDubboAdmin.git
一.准备maven项目
mvn archetype:generate -DgroupId=com.lsf.app -DartifactId=coder-center -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false 创建coder-center项目,服务的具体实现
mvn archetype:generate -DgroupId=com.lsf.app -DartifactId=coder-center-api -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false 创建coder-center-api项目,暴露服务
mvn archetype:generate -DgroupId=cn.lsf.app -DartifactId=appserver -DarchetypeArtifactId=maven-archetype-webapp -DinteractivMode=false 创建appserver web项目,消费各服务
注释:
mvn archetype:generate 固定格式
-DgroupId 组织标识(包名)
-DartifactId 项目名称
-DarchetypeArtifactId 特定ArchetypeId,maven-archetype-quickstart,创建一个Java Project;maven-archetype-webapp,创建一个Web Project
-DinteractiveMode 是否使用交互模式
然后把项目导入Eclipse中,项目结构图如下
二.服务提供者
1.在coder-center-api中定义接口,用来暴露出去
package com.shengfeng.user.service; import com.shengfeng.user.bean.UserBean; public interface UserService { long addUser(); UserBean getUserByUserId(); }
2.在coder-center-api中定义bean,用来暴露出去
package com.shengfeng.user.bean; import java.io.Serializable; public class UserBean implements Serializable{ private static final long serialVersionUID = -5621716534400078000L; private long id ; private String username;//用户名 private String password;//密码 private String level;//等级 private int sex;//性别 private String mobilephone;//手机号 private String img;// 头像 public long getId() { return id; } public void setId(long id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getLevel() { return level; } public void setLevel(String level) { this.level = level; } public int getSex() { return sex; } public void setSex(int sex) { this.sex = sex; } public String getMobilephone() { return mobilephone; } public void setMobilephone(String mobilephone) { this.mobilephone = mobilephone; } public String getImg() { return img; } public void setImg(String img) { this.img = img; } }
3.在coder-center中的pom.xml添加依赖,把需要用的jar引进来
<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.shengfeng</groupId> <artifactId>coder-center</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>coder-center</name> <url>http://maven.apache.org</url> <properties> <!-- spring版本号 --> <spring.version>3.2.11.RELEASE</spring.version> </properties> <dependencies> <!-- 引api的jar包 --> <dependency> <groupId>com.shengfeng</groupId> <artifactId>coder-center-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!-- spring核心包 --> <!-- springframe start --> <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>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> <scope>test</scope> </dependency> <!-- springframe end --> <!-- dubbo的包 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.4.9</version> </dependency> <!-- zookeeper的包 --> <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> </project>
4.在coder-center中实现coder-center-api的接口
package com.shengfeng.user.service.impl; import com.shengfeng.user.bean.UserBean; import com.shengfeng.user.service.UserService; /** * UserService impl * */ public class UserServiceImpl implements UserService { public long addUser() { return 0; } public UserBean getUserByUserId() { UserBean userBean = new UserBean(); userBean.setUsername("shengfeng"); return userBean; } }
5.在coder-center中将实现的服务暴露出来,provider.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:application name="coder-center-provider" /> <!-- 使用zookeeper注册中心暴露服务地址 --> <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" /> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="com.shengfeng.user.service.UserService" ref="userService" /> <bean id="userService" class="com.shengfeng.user.service.impl.UserServiceImpl" /> </beans>
6.服务启动类
package com.shengfeng.user.main; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Provider { public static ClassPathXmlApplicationContext context; public static void main(String[] args) { context = new ClassPathXmlApplicationContext(new String[] { "provider.xml" }); context.start(); System.out.println("coder-center 启动成功"); while (true) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
7.启动main方法之后,去dubbo-admin管理平台上看是否已经将提供的服务暴露到上面
查看提供者
这样就完成了一半,还剩下一个消费者
三.服务消费者
1.在appserver项目中先在pom.xml文件中引入相应的jar包
<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.shengfeng</groupId> <artifactId>appserver</artifactId> <packaging>war</packaging> <version>1.0</version> <name>appserver Maven Webapp</name> <url>http://maven.apache.org</url> <build> <finalName>appserver</finalName> </build> <properties> <!-- spring版本号 --> <spring.version>3.2.11.RELEASE</spring.version> </properties> <dependencies> <!-- 引api的jar包 --> <dependency> <groupId>com.shengfeng</groupId> <artifactId>coder-center-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!-- spring核心包 --> <!-- springframe start --> <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>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> <scope>test</scope> </dependency> <!-- springframe end --> <!-- dubbo的包 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.4.9</version> </dependency> <!-- zookeeper的包 --> <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> </project>
2.消费服务的类UserAction
package com.shengfeng.user.action; import java.io.IOException; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.shengfeng.user.service.UserService; public class UserAction { public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "consumer.xml" }); context.start(); System.out.println("consumer启动成功"); UserService userService = (UserService) context.getBean("userService"); System.out.println("username:" + userService.getUserByUserId().getUsername()); while (true) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
这里需要说一下,不要消费完服务就关闭,这样的话它都到不了注册中心那里,也就是dubbo-admin那里的消费者一直会显示不出来,这里的解决办法是让它睡1秒
3.消费服务的配置文件consumer.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:application name="consumer-appserver" /> <!-- 使用zookeeper注册中心暴露发现服务地址 --> <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" default="true"/> <!-- 引入服务接口 --> <dubbo:reference id="userService" interface="com.shengfeng.user.service.UserService" /> </beans>
4.启动UserAction的main方法,然后去dubbo-admin管理平台上去看是否有消费者的相应信息
查看消费者
然后可以在eclipse控制上看到可以消费到 提供者提供的服务
比较简单,不过可以试试手~