SpringBoot+zk+dubbo架构实践(一):本地部署zookeeper
SpringBoot+zk+dubbo架构实践(二):SpringBoot 集成 zookeeper
SpringBoot+zk+dubbo架构实践(三):部署Dubbo-admin管理平台
先看一下上面的图有个简单的概念,然后开始编码。只需完成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)-模拟移动端请求入口
项目结构
备注:目录结构仅供参考,但是配置文件是必不可少的。
xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>1.5.10.RELEASEversion>
parent>
<groupId>com.ituniongroupId>
<artifactId>weixin-shopartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>pompackaging>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<java.version>1.8java.version>
<skip_maven_deploy>trueskip_maven_deploy>
properties>
<modules>
<module>shop-apimodule>
<module>shop-dsmodule>
<module>shop-webmodule>
<module>shop-pcmodule>
modules>
project>
xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>weixin-shopartifactId>
<groupId>com.ituniongroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>shop-apiartifactId>
<packaging>jarpackaging>
<name>${project.artifactId}name>
<properties>
<skip_maven_deploy>trueskip_maven_deploy>
properties>
project>
package com.itunion.shop.service;
/**
* 测试demo
* Created by lin on 2018年04月16日21:38:07
*/
public interface DemoService {
String sayHello(String name);
}
xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>weixin-shopartifactId>
<groupId>com.ituniongroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>shop-dsartifactId>
<packaging>jarpackaging>
<name>${project.artifactId}name>
<properties>
<skip_maven_deploy>falseskip_maven_deploy>
properties>
<dependencies>
<dependency>
<groupId>com.ituniongroupId>
<artifactId>shop-apiartifactId>
<version>${project.parent.version}version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>dubboartifactId>
<version>2.6.0version>
dependency>
<dependency>
<groupId>com.101tecgroupId>
<artifactId>zkclientartifactId>
<version>0.10version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-frameworkartifactId>
<version>4.0.0version>
dependency>
dependencies>
project>
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();
}
}
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.qos.port=33333
###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
xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="dubbo-provider" owner="dubbo-provider" />
<dubbo:registry address="zookeeper://localhost:2181"/>
<dubbo:protocol name="dubbo" port="20880"/>
<bean id="demoService" class="com.itunion.shop.service.impl.DemoServiceImpl"/>
<dubbo:service interface="com.itunion.shop.service.DemoService" ref="demoService"/>
beans>
xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>weixin-shopartifactId>
<groupId>com.ituniongroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>shop-webartifactId>
<packaging>jarpackaging>
<name>${project.artifactId}name>
<properties>
<skip_maven_deploy>falseskip_maven_deploy>
properties>
<dependencies>
<dependency>
<groupId>com.ituniongroupId>
<artifactId>shop-apiartifactId>
<version>${project.parent.version}version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>dubboartifactId>
<version>2.6.0version>
dependency>
<dependency>
<groupId>com.101tecgroupId>
<artifactId>zkclientartifactId>
<version>0.10version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-frameworkartifactId>
<version>4.0.0version>
dependency>
dependencies>
project>
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.qos.port=11111
省略....
xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="shop-web-consumer" owner="programmer" organization="dubbed"/>
<dubbo:registry address="zookeeper://localhost:2181"/>
<dubbo:reference id="demoService" check="false" interface="com.itunion.shop.service.DemoService"/>
beans>
<artifactId>shop-pcartifactId>
省略......
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.qos.port=22222
省略....
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="shop-pc-consumer" owner="programmer" organization="dubbed"/>
<dubbo:registry address="zookeeper://localhost:2181"/>
<dubbo:reference id="demoService" check="false" interface="com.itunion.shop.service.DemoService"/>
beans>
好了我们的sb+zk+dubbo 框架已经搭建好了,接下来我们执行一下看看结果!(zookeeper 服务记得启动哈)
如上面几张控制台截图我们模拟的 shop-ds(服务提供方)、shop-web(移动)和shop-pc(PC)消费方 都已经跑起来了,效果也达到我们预期的目的,那么还剩下最后一部分内容 我们会在 spring boot + zookeeper + dubbo 框架基础上 集成 mybatis + swagger 来实现增、删、改、查业务。
更多精彩内容请关注“IT实战联盟”公*众*号,如果需要源码的话可以关注*公*众*号并留言(sb+zk+boot源码)会自动回复 源码GitHub地址,也可以加入交流群和作者互撩哦~~~