This paper will build on already good dubbo and zookeeper, create producer services, and registered with the zookeeper.
1, environmental constraints
- win10 64 operating system
- idea2018.1.5
- maven-3.0.5
- jdk-8u162-windows-x64
2, software download
Baidu network disk:
Link: https://pan.baidu.com/s/1gfnI8NqUYgYK1g0ULGIV2w
extraction code: q9pl
3, the premise of restraint
- zookeeper and dubbo has been installed and started
- This album will be "03 to create a common project maven" do finish
4. Create a new module fbs-service, the following steps:![Creating fbs-service module](https://upload-images.jianshu.io/upload_images/16204070-df20b5f54b3e05b2.gif?imageMogr2/auto-orient/strip)
In pom.xml to add a dependency:
<dependency>
<groupId>net.wanho.fenbushi</groupId>
<artifactId>fbs-interface</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
pom.xml build tag modified as follows:
<build>
<finalName>fbs-service</finalName>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<path>/</path>
<port>8081</port>
</configuration>
</plugin>
</plugins>
</build>
5, the module in the src / main folder java added below, to Sources Root, net.wanho.fenbushi.service.impl created in this folder package, the package added UserServiceImpl.java category below.
package net.wanho.fenbushi.service.impl;
import net.wanho.fenbushi.pojo.User;
import net.wanho.fenbushi.service.UserService;
import java.util.ArrayList;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Override
public List<User> queryUsers() {
List<User> list = new ArrayList<>();
//这里面查询数据库并不是我们关注的主要矛盾
list.add(new User(1,"ali"));
list.add(new User(2,"zhangli"));
list.add(new User(3,"xiaoli"));
return list;
}
}
6, the module in the src / main Add the following resources folder set Resources Root, applicationContext-service.xml created in this folder.
<?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/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<context:component-scan base-package="net.wanho.fenbushi.service"></context:component-scan>
<!-- 发布dubbo服务 -->
<dubbo:application name="fenbushi-service"/>
<!-- 提供依赖信息,这是我们zookeeper的地址 -->
<dubbo:registry protocol="zookeeper" address="192.168.100.192:2181" />
<!-- 暴露一个服务在20880端口 -->
<dubbo:protocol name="dubbo" port="20882"/>
<!-- 暴露一个实际服务,ref是接口实现类的首字母小写而成 -->
<dubbo:service interface="net.wanho.fenbushi.service.UserService" ref="userServiceImpl" timeout="30000"/>
</beans>
7, modify src / main / webapp / WEB-INF / web.xml, parent container can be arranged
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-service.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
Project is structured as follows:
8, start-up and testing
Open your browser and enter http://192.168.100.192:8080/dubbo, proceed as follows:
We see, producer services has been the lack of consumers, here we create web layer, as a service to consumers.