往期回顾
SpringBoot+zk+dubbo架构实践(一):本地部署zookeeper
SpringBoot+zk+dubbo架构实践(二):SpringBoot 集成 zookeeper
SpringBoot+zk+dubbo架构实践(三):部署Dubbo-admin管理平台
sb+zk+dubbo实现效果
前言
先看一下上面的图有个简单的概念,然后开始编码。只需完成2件事情。
1、Spring boot + zk + dubbo 框架搭建(1个主项目4个子模块)
2、编写测试类,实现暴露服务的服务提供方、调用远程服务的服务消费方和服务注册与发现的注册中心 功能。
项目目录和结构图
项目说明
weixin-shop 主项目
shop-api 公共接口
shop-ds 服务提供方(provider)
shop-pc 服务消费方1(consumer)-模拟PC端请求入口
shop-web 服务消费方2(consumer)-模拟移动端请求入口
项目结构
备注:目录结构仅供参考,但是配置文件是必不可少的。
weixin-shop 主项目
pom.xml
4.0.0
org.springframework.boot
spring-boot-starter-parent
1.5.10.RELEASE
com.itunion
weixin-shop
1.0-SNAPSHOT
pom
UTF-8
1.8
true
shop-api
shop-ds
shop-web
shop-pc
shop-api 公共接口-子项目
pom.xml
weixin-shop
com.itunion
1.0-SNAPSHOT
4.0.0
shop-api
jar
${project.artifactId}
true
DemoService 接口
package com.itunion.shop.service;
/**
* 测试demo
* Created by lin on 2018年04月16日21:38:07
*/
public interface DemoService {
String sayHello(String name);
}
shop-ds 服务提供方-子项目
pom.xml
weixin-shop
com.itunion
1.0-SNAPSHOT
4.0.0
shop-ds
jar
${project.artifactId}
false
com.itunion
shop-api
${project.parent.version}
org.springframework.boot
spring-boot-starter-web
com.alibaba
dubbo
2.6.0
com.101tec
zkclient
0.10
org.apache.curator
curator-framework
4.0.0
DemoServiceImpl 测试接口实现
package com.itunion.shop.service.impl;
import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.alibaba.dubbo.rpc.RpcContext;
import com.itunion.shop.service.DemoService;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 测试demo-服务提供方
* Created by lin on 2018年04月16日21:38:07
*/
public class DemoServiceImpl implements DemoService {
private final static Logger LOGGER = LoggerFactory.getLogger(DemoServiceImpl.class);
@Override
public String sayHello(String name) {
System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
return "Hello " + name + ", response form provider: " + RpcContext.getContext().getLocalAddress();
}
}
Provider 测试执行main
package com.itunion.shop.service.impl;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Provider {
public static void main(String[] args) throws Exception {
System.setProperty("java.net.preferIPv4Stack", "true");
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"shop-ds-rovider.xml"});
context.start();
System.out.println("服务提供方已经启动...");
System.in.read(); // press any key to exit
}
}
dubbo.properties
dubbo.qos.port=33333
log4j.properties(后面子项目一样用这个)
###set log levels###
log4j.rootLogger=info, stdout
###output to the 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{dd/MM/yy hh:mm:ss:sss z}] %t %5p %c{2}: %m%n
shop-ds-rovider.xml
shop-web 服务消费方-子项目
pom.xml
weixin-shop
com.itunion
1.0-SNAPSHOT
4.0.0
shop-web
jar
${project.artifactId}
false
com.itunion
shop-api
${project.parent.version}
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
com.alibaba
dubbo
2.6.0
com.101tec
zkclient
0.10
org.apache.curator
curator-framework
4.0.0
ConsumerWeb 移动消费者入口 测试执行main
package com.itunion.shop.web.controller;
import com.itunion.shop.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ConsumerWeb {
@Autowired
DemoService demoService;
public static void main(String[] args) {
System.setProperty("java.net.preferIPv4Stack", "true");
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"shop-web-consumer.xml"});
context.start();
System.out.println("微商城移动端 消费方(Consumer)启动......");
DemoService demoService = (DemoService) context.getBean("demoService"); // get remote service proxy
System.out.println("消费方(Consumer)");
while (true) {
try {
Thread.sleep(1000);
String hello = demoService.sayHello("第2个:我是移动端"); // call remote method
System.out.println(hello); // get result
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
}
}
dubbo.properties
dubbo.qos.port=11111
log4j.properties(用上面shop-ds那个)
省略....
shop-web-consumer.xml
shop-pc 服务消费方-子项目
pom.xml(可以复制shop-web pom文件修改artifactId 即可)
shop-pc
省略......
ConsumerPC PC消费之业务入口 测试执行main
package com.itunion.shop.web.controller;
import com.itunion.shop.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ConsumerPC {
@Autowired
DemoService demoService;
public static void main(String[] args) {
System.setProperty("java.net.preferIPv4Stack", "true");
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"shop-pc-consumer.xml"});
context.start();
System.out.println("微商城PC端-消费方(Consumer)启动......");
DemoService demoService = (DemoService) context.getBean("demoService"); // get remote service proxy
System.out.println("消费方(Consumer)");
while (true) {
try {
Thread.sleep(1000);
String hello = demoService.sayHello("第1个:我是PC端消费方"); // call remote method
System.out.println(hello); // get result
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
}
}
dubbo.properties
dubbo.qos.port=22222
log4j.properties(用上面那个)
省略....
shop-pc-consumer.xml
好了我们的sb+zk+dubbo 框架已经搭建好了,接下来我们执行一下看看结果!(zookeeper 服务记得启动哈)
启动shop-ds 暴露服务的服务提供方
启动shop-pc 服务消费方 -PC端启动
启动shop-web 服务消费方 -移动端启动
最后预告
如上面几张控制台截图我们模拟的 shop-ds(服务提供方)、shop-web(移动)和shop-pc(PC)消费方 都已经跑起来了,效果也达到我们预期的目的,那么还剩下最后一部分内容 我们会在 spring boot + zookeeper + dubbo 框架基础上 集成 mybatis + swagger 来实现增、删、改、查业务。
关注我们
更多精彩内容请关注“IT实战联盟”公众号,如果需要源码的话可以关注公众号并留言(sb+zk+boot源码)会自动回复 源码Git地址,也可以加入交流群和作者互撩哦~~~