往期回顾
SpringBoot+zk+dubbo架构实践(一):本地部署zookeeper
SpringBoot+zk+dubbo架构实践(二):SpringBoot 集成 zookeeper
SpringBoot+zk+dubbo架构实践(三):部署Dubbo-admin管理平台
sb+zk+dubbo实现效果
模拟了一个provider服务提供方和PC、Web两个服务消费方
前言
先看一下上面的图有个简单的概念,然后开始编码。只需完成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
<?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.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>
<groupId>com.itunion</groupId>
<artifactId>weixin-shop</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<skip_maven_deploy>true</skip_maven_deploy>
</properties>
<modules>
<module>shop-api</module>
<module>shop-ds</module>
<module>shop-web</module>
<module>shop-pc</module>
</modules></project>
shop-api 公共接口-子项目
pom.xml
<?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-shop</artifactId>
<groupId>com.itunion</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>shop-api</artifactId>
<packaging>jar</packaging>
<name>${project.artifactId}</name>
<properties>
<skip_maven_deploy>true</skip_maven_deploy>
</properties></project>
DemoService.java 接口
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
<?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-shop</artifactId>
<groupId>com.itunion</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>shop-ds</artifactId>
<packaging>jar</packaging>
<name>${project.artifactId}</name>
<properties>
<skip_maven_deploy>false</skip_maven_deploy>
</properties>
<dependencies>
<dependency>
<groupId>com.itunion</groupId>
<artifactId>shop-api</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--dubbo-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.0.0</version>
</dependency>
</dependencies></project>
DemoServiceImpl.java 测试接口实现
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.java 测试执行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
<?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-admin 或 dubbo-monitor 会显示这个名字,方便辨识-->
<!--<dubbo:application name="shop-web-provider" owner="programmer" organization="dubbox"/>-->
<dubbo:application name="dubbo-provider" owner="dubbo-provider" />
<!--使用 zookeeper 注册中心暴露服务,注意要先开启 zookeeper-->
<!--<dubbo:registry address="multicast://224.5.6.7:1234"/>-->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880"/>
<!--具体实现该接口的 bean-->
<bean id="demoService" class="com.itunion.shop.service.impl.DemoServiceImpl"/>
<!--使用 dubbo 协议实现定义好的 DemoService 接口-->
<dubbo:service interface="com.itunion.shop.service.DemoService" ref="demoService"/></beans>
shop-web 服务消费方-子项目
pom.xml
<?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-shop</artifactId>
<groupId>com.itunion</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>shop-web</artifactId>
<packaging>jar</packaging>
<name>${project.artifactId}</name>
<properties>
<skip_maven_deploy>false</skip_maven_deploy>
</properties>
<dependencies>
<dependency>
<groupId>com.itunion</groupId>
<artifactId>shop-api</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--dubbo-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.0.0</version>
</dependency>
</dependencies></project>
ConsumerWeb.java 移动消费者入口 测试执行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
<?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">
<!-- consumer's application name, used for tracing dependency relationship (not a matching criterion), don't set it same as provider -->
<dubbo:application name="shop-web-consumer" owner="programmer" organization="dubbed"/>
<!-- use multicast registry center to discover service -->
<!--<dubbo:registry address="multicast://224.5.6.7:1234"/>-->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!-- generate proxy for the remote service, then demoService can be used in the same way as the local regular interface -->
<dubbo:reference id="demoService" check="false" interface="com.itunion.shop.service.DemoService"/></beans>
shop-pc 服务消费方-子项目
pom.xml(可以复制shop-web pom文件修改artifactId 即可)
<artifactId>shop-pc</artifactId>
省略......
ConsumerPC.java 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
<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">
<!-- consumer's application name, used for tracing dependency relationship (not a matching criterion), don't set it same as provider -->
<dubbo:application name="shop-pc-consumer" owner="programmer" organization="dubbed"/>
<!-- use multicast registry center to discover service -->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!-- generate proxy for the remote service, then demoService can be used in the same way as the local regular interface -->
<dubbo:reference id="demoService" check="false" interface="com.itunion.shop.service.DemoService"/></beans>
好了我们的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源码)会自动回复 源码GitHub地址,也可以加入交流群和作者互撩哦~~~
注意:本文归作者所有,未经作者允许,不得转载