往期回顧
SpringBoot+zk+dubbo架構(gòu)實踐(一):本地部署zookeeper
SpringBoot+zk+dubbo架構(gòu)實踐(二):SpringBoot 集成 zookeeper
SpringBoot+zk+dubbo架構(gòu)實踐(三):部署Dubbo-admin管理平臺
sb+zk+dubbo實現(xiàn)效果

模擬了一個provider服務提供方和PC、Web兩個服務消費方.gif
前言
先看一下上面的圖有個簡單的概念,然后開始編碼。只需完成2件事情。
1、Spring boot + zk + dubbo 框架搭建(1個主項目4個子模塊)
2、編寫測試類,實現(xiàn)暴露服務的服務提供方、調(diào)用遠程服務的服務消費方和服務注冊與發(fā)現(xiàn)的注冊中心 功能。

dubbo.jpeg
項目目錄和結(jié)構(gòu)圖

項目目錄.png
項目說明
weixin-shop 主項目
shop-api 公共接口
shop-ds 服務提供方(provider)
shop-pc 服務消費方1(consumer)-模擬PC端請求入口
shop-web 服務消費方2(consumer)-模擬移動端請求入口
項目結(jié)構(gòu)

項目結(jié)構(gòu).png
備注:目錄結(jié)構(gòu)僅供參考,但是配置文件是必不可少的。
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>
<!--<name>${project.artifactId}</name>-->
<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 接口
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 測試接口實現(xiàn)
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 測試執(zhí)行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("服務提供方已經(jīng)啟動...");
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">
<!--定義了提供方應用信息,用于計算依賴關(guān)系;在 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協(xié)議在20880端口暴露服務 -->
<dubbo:protocol name="dubbo" port="20880"/>
<!--具體實現(xiàn)該接口的 bean-->
<bean id="demoService" class="com.itunion.shop.service.impl.DemoServiceImpl"/>
<!--使用 dubbo 協(xié)議實現(xiàn)定義好的 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 移動消費者入口 測試執(zhí)行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 PC消費之業(yè)務入口 測試執(zhí)行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 框架已經(jīng)搭建好了,接下來我們執(zhí)行一下看看結(jié)果?。▃ookeeper 服務記得啟動哈)
啟動shop-ds 暴露服務的服務提供方

服務提供方啟動.jpg
啟動shop-pc 服務消費方 -PC端啟動

shop-pc消費方調(diào)用.jpg
啟動shop-web 服務消費方 -移動端啟動

shop-web消費方調(diào)用.jpg
最后預告
如上面幾張控制臺截圖我們模擬的 shop-ds(服務提供方)、shop-web(移動)和shop-pc(PC)消費方 都已經(jīng)跑起來了,效果也達到我們預期的目的,那么還剩下最后一部分內(nèi)容 我們會在 spring boot + zookeeper + dubbo 框架基礎(chǔ)上 集成 mybatis + swagger 來實現(xiàn)增、刪、改、查業(yè)務。
關(guān)注我們
更多精彩內(nèi)容請關(guān)注“IT實戰(zhàn)聯(lián)盟”公眾號,如果需要源碼的話可以關(guān)注公眾號并留言(sb+zk+boot源碼)會自動回復 源碼Git地址,也可以加入交流群和作者互撩哦~~~

IT實戰(zhàn)聯(lián)盟.jpg