项目搭建思路:
- 使用maven进行项目管理
- 项目整体包括三部分(provider、api、consumer)
- provider和consumer均为springboot项目
- 使用idea作为搭建工具
项目搭建步骤
一、 新建项目父模块
- file -> new -> project
选择maven
-
点击Next,填入GroupId和ArtifactId
- 然后Next ->Finish
-
创建好的项目结构如下,父模块不需要src,删除src目录
二、 新建项目子模块 - provider
-
选中父模块,file -> new -> module
-
点击Next,填入ArtifactId
-
点击Next,然后Finish
三、新建子模块dubbo-api和dubbo-consumer,同步骤二,结果如下
四、添加依赖
- 分别打开provider和consumer下的
pom.xml
,添加api的依赖
<dependencies>
<dependency>
<groupId>com.mk</groupId>
<artifactId>dubbo-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
- 父模块的
pom.xml
添加springboot的parent
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
- 父模块
pom.xml
添加dubbo、zookeeper、log4j等依赖
<dependencies>
<!-- dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
<scope>provided</scope>
</dependency>
<!-- Log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.curator/curator-framework zookeeper client-->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.0.1</version>
</dependency>
</dependencies>
- provide和consumer模块添加springboot-start依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
- 添加maven插件
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
五、新增配置
1. provider
- 在provider模块新增
application.yml
,定义springboot项目的一些参数
#provider
server:
port: 8081
servlet:
context-path: /provider
connection-timeout: 300000
- 新增log4j配置文件
log4j.properties
log4j.rootLogger=info, stdout, file
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=D:\\MDubbo-log\\dubbo-provider\\log4j.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.S} %-5p %c{1}:%L - %m%n
- 新增dubbo配置文件
dubbo-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="MKDubboApp"/>
<dubbo:registry address="zookeeper://127.0.0.1:2181" check="false" subscribe="false"/>
<dubbo:service interface="com.my.HelloService" ref="helloService"/>
<bean id="helloService" class="com.my.service.impl.HelloServiceImpl"/>
</beans>
- springboot配置映射
package com.my.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
@ImportResource("classpath:dubbo-provider.xml")
public class DubboConfig {
}
2. consumer
- 在consume模块新增
application.yml
,定义springboot项目的一些参数
#consumer
server:
port: 8082
servlet:
context-path: /consumer
connection-timeout: 300000
- 新增log4j配置文件
log4j.properties
log4j.rootLogger=info, stdout, console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=D:\\MDubbo-log\\dubbo-consumer\\log4j.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.S} %-5p %c{1}:%L - %m%n
- 新增dubbo配置文件
dubbo-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="MKDubboApp"/>
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<dubbo:reference id="demoService" interface="com.my.HelloService"/>
</beans>
- 添加springboot配置映射
package com.my.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
@ImportResource("classpath:dubbo-consumer.xml")
public class DubboConfig {
}
六、新增服务
- 定义api接口,在api模块新增接口HelloService
package com.my;
/**
* hello service
* @author mk
*/
public interface HelloService {
/**
* dubbo测试接口
* @param param param
* @return result
*/
String hello(String param);
}
- 在provider新增实现类
package com.my.service.impl;
import com.my.HelloService;
/**
* hello service
* @author mk
*/
public class HelloServiceImpl implements HelloService{
@Override
public String hello(String param) {
return "provider get param success,param is " + param;
}
}
- 新增springboot启动类
ProviderApplication.java
package com.my;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class,args);
}
}
- 启动provider,执行
ProviderApplication.java
的main方法,部分启动日志如下,我们可以看到服务注册成功的日志
2018-04-09 10:19:41.146 INFO 210448 --- [ main] o.a.c.f.imps.CuratorFrameworkImpl : Default schema
2018-04-09 10:19:41 INFO ZookeeperRegistry:269 - [DUBBO] Register: dubbo://10.107.76.66:20880/com.my.HelloService?anyhost=true&application=MKDubboApp&dubbo=2.6.1&generic=false&interface=com.my.HelloService&methods=hello&pid=210448&side=provider×tamp=1523240380677, dubbo version: 2.6.1, current host: 10.107.76.66
2018-04-09 10:19:41.149 INFO 210448 --- [127.0.0.1:2181)] org.apache.zookeeper.ClientCnxn : Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session
2018-04-09 10:19:41.214 WARN 210448 --- [127.0.0.1:2181)] org.apache.zookeeper.ClientCnxnSocket : Connected to an old server; r-o mode will be unavailable
2018-04-09 10:19:41.214 INFO 210448 --- [127.0.0.1:2181)] org.apache.zookeeper.ClientCnxn : Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x162a8309ab00002, negotiated timeout = 40000
2018-04-09 10:19:41.220 INFO 210448 --- [ain-EventThread] o.a.c.f.state.ConnectionStateManager : State change: CONNECTED
2018-04-09 10:19:41 INFO ZookeeperRegistry:292 - [DUBBO] Subscribe: provider://10.107.76.66:20880/com.my.HelloService?anyhost=true&application=MKDubboApp&category=configurators&check=false&dubbo=2.6.1&generic=false&interface=com.my.HelloService&methods=hello&pid=210448&side=provider×tamp=1523240380677, dubbo version: 2.6.1, current host: 10.107.76.66
2018-04-09 10:19:41 INFO ZookeeperRegistry:380 - [DUBBO] Notify urls for subscribe url provider://10.107.76.66:20880/com.my.HelloService?anyhost=true&application=MKDubboApp&category=configurators&check=false&dubbo=2.6.1&generic=false&interface=com.my.HelloService&methods=hello&pid=210448&side=provider×tamp=1523240380677, urls: [empty://10.107.76.66:20880/com.my.HelloService?anyhost=true&application=MKDubboApp&category=configurators&check=false&dubbo=2.6.1&generic=false&interface=com.my.HelloService&methods=hello&pid=210448&side=provider×tamp=1523240380677], dubbo version: 2.6.1, current host: 10.107.76.66
2018-04-09 10:19:41.333 INFO 210448 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8081 (http) with context path '/provider'
2018-04-09 10:19:41.337 INFO 210448 --- [ main] com.my.ProviderApplication : Started ProviderApplication in 4.829 seconds (JVM running for 5.482)
七、新增消费者
- 新增controller
HelloController.java
package com.my.controller;
import com.my.HelloService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* hello controller
* @author mk
*/
@RestController
public class HelloController {
@Resource
private HelloService helloService;
@RequestMapping("/hello")
public String hello(HttpServletRequest request, HttpServletResponse response){
String result = helloService.hello("consumer params...");
return result;
}
}
- 新增springboot启动类
ConsumerApplication.java
package com.my;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class,args);
}
}
- 启动consumer,执行
ConsumerApplication.java
的main方法,部分启动日志如下
2018-04-09 10:52:39 INFO ZookeeperRegistry:292 - [DUBBO] Subscribe: consumer://10.107.76.66/com.my.HelloService?application=MKDubboApp&category=providers,configurators,routers&dubbo=2.6.1&interface=com.my.HelloService&methods=hello&pid=15936&side=consumer×tamp=1523242359288, dubbo version: 2.6.1, current host: 10.107.76.66
2018-04-09 10:52:40 INFO ZookeeperRegistry:380 - [DUBBO] Notify urls for subscribe url consumer://10.107.76.66/com.my.HelloService?application=MKDubboApp&category=providers,configurators,routers&dubbo=2.6.1&interface=com.my.HelloService&methods=hello&pid=15936&side=consumer×tamp=1523242359288, urls: [dubbo://10.107.76.66:20880/com.my.HelloService?anyhost=true&application=MKDubboApp&dubbo=2.6.1&generic=false&interface=com.my.HelloService&methods=hello&pid=210448&side=provider×tamp=1523240380677, empty://10.107.76.66/com.my.HelloService?application=MKDubboApp&category=configurators&dubbo=2.6.1&interface=com.my.HelloService&methods=hello&pid=15936&side=consumer×tamp=1523242359288, empty://10.107.76.66/com.my.HelloService?application=MKDubboApp&category=routers&dubbo=2.6.1&interface=com.my.HelloService&methods=hello&pid=15936&side=consumer×tamp=1523242359288], dubbo version: 2.6.1, current host: 10.107.76.66
2018-04-09 10:52:40 INFO AbstractClient:273 - [DUBBO] Successed connect to server /10.107.76.66:20880 from NettyClient 10.107.76.66 using dubbo version 2.6.1, channel is NettyChannel [channel=[id: 0x4bd51d3e, /10.107.76.66:63340 => /10.107.76.66:20880]], dubbo version: 2.6.1, current host: 10.107.76.66
2018-04-09 10:52:40 INFO AbstractClient:91 - [DUBBO] Start NettyClient VIP-20170911VOI/10.107.76.66 connect to the server /10.107.76.66:20880, dubbo version: 2.6.1, current host: 10.107.76.66
2018-04-09 10:52:40 INFO AbstractConfig:425 - [DUBBO] Refer dubbo service com.my.HelloService from url zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?anyhost=true&application=MKDubboApp&check=false&dubbo=2.6.1&generic=false&interface=com.my.HelloService&methods=hello&pid=15936®ister.ip=10.107.76.66&remote.timestamp=1523240380677&side=consumer×tamp=1523242359288, dubbo version: 2.6.1, current host: 10.107.76.66
2018-04-09 10:52:40.510 INFO 15936 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@27adc16e: startup date [Mon Apr 09 10:52:37 CST 2018]; root of context hierarchy
2018-04-09 10:52:40.566 INFO 15936 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto public java.lang.String com.my.controller.HelloController.hello(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-04-09 10:52:40.572 INFO 15936 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-04-09 10:52:40.572 INFO 15936 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-04-09 10:52:40.616 INFO 15936 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-04-09 10:52:40.616 INFO 15936 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-04-09 10:52:40.660 INFO 15936 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-04-09 10:52:40.899 INFO 15936 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-04-09 10:52:40.931 INFO 15936 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8082 (http) with context path '/consumer'
2018-04-09 10:52:40.934 INFO 15936 --- [ main] com.my.ConsumerApplication : Started ConsumerApplication in 3.886 seconds (JVM running for 4.844)