首先介绍一下我们的需求:
在开心乐园我们要开心的购物一番,于是乐园老板让我去写一套系统【其实就是最简单的生产 消费!没那么牛逼】 Ozz!
其次介绍一下我们的工程结构:
-
定义主父工程:【happyland】 开心乐园父工程!这个父工程主要作用是定义一套公共的POM依赖
-
定义子工程一:【happyland-customer】开心乐园顾客工程!这个子工程主要是提供消费者代码
-
定义子工程一:【happyland-market】开心乐园小商店工程!这个子工程主要是提供生产者代码
实现部分:
一、pom结构
编写代码前先把结构定义清楚:
下面我们定义父POM【happyland】的pom
注意pom中
<modules>
<module>happyland-customer</module>
<module>happyland-market</module>
</modules>
<?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>
<packaging>pom</packaging>
<modules>
<module>happyland-customer</module>
<module>happyland-market</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
</parent>
<groupId>com.happyland</groupId>
<artifactId>happyland</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.1.1.RELEASE</spring-boot.version>
<dubbo.version>2.6.5</dubbo.version>
<zkclient.version>0.2</zkclient.version>
<zookeeper.version>3.4.9</zookeeper.version>
<curator-framework.version>2.12.0</curator-framework.version>
<netty-all.version>4.1.31.Final</netty-all.version>
<!-- Maven plugins -->
<maven-jar-plugin.version>3.0.2</maven-jar-plugin.version>
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
<maven-source-plugin.version>3.0.1</maven-source-plugin.version>
<maven-jacoco-plugin.version>0.8.1</maven-jacoco-plugin.version>
<maven-gpg-plugin.version>1.5</maven-gpg-plugin.version>
<apache-rat-plugin.version>0.12</apache-rat-plugin.version>
<maven-release-plugin.version>2.5.3</maven-release-plugin.version>
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
<alibaba-spring-context-support.version>1.0.2</alibaba-spring-context-support.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>${curator-framework.version}</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>${netty-all.version}</version>
</dependency>
<!-- Dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Alibaba Spring Context extension -->
<dependency>
<groupId>com.alibaba.spring</groupId>
<artifactId>spring-context-support</artifactId>
<version>${alibaba-spring-context-support.version}</version>
</dependency>
<!-- ZK -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>${zookeeper.version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>${zkclient.version}</version>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
<exclusion>
<artifactId>log4j</artifactId>
<groupId>log4j</groupId>
</exclusion>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
</plugins>
</build>
</project>
下面我们定义子POM【happyland-market】的pom
<?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>com.happyland</groupId>
<artifactId>happyland</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.happyland</groupId>
<artifactId>happyland-market</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>happyland-market</name>
<packaging>pom</packaging>
</project>
下面我们定义子POM【happyland-customer】的pom
注意要依赖Market工程,因为我们要用Market提供的购物接口去远程消费
<?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>com.happyland</groupId>
<artifactId>happyland</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.happyland</groupId>
<artifactId>happyland-customer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>happyland-customer</name>
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>com.happyland</groupId>
<artifactId>happyland-market</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
二、生产者消费者Springboot配置的实现
(1)生产者配置 application.properties
# Spring boot application 请注意端口的生产者和消费者端口必须不一样
spring.application.name = market_webserver
server.port = 8082
# Base packages to scan Dubbo Components (e.g., @Service, @Reference)
# 这里你要写能扫描到的Dubbo的Service和Reference的包路径
dubbo.scan.basePackages = com.happyland.happylandmarket
# Dubbo Config properties
## ApplicationConfig Bean
dubbo.application.name = product
## ProtocolConfig Bean
dubbo.protocol.name = dubbo
## RegistryConfig Bean 请配置你们的zookeeper路径
dubbo.registry.address = zookeeper://192.168.3.104:2181
(2)消费者配置 application.properties
# Spring boot application 消费者配置
spring.application.name = customer_webserver
server.port = 8081
# Base packages to scan Dubbo Components (e.g., @Service, @Reference)
dubbo.scan.basePackages = com.happyland.happylandcustomer.test
# Dubbo Config properties
## ApplicationConfig Bean
dubbo.application.name = customer
## ProtocolConfig Bean
dubbo.protocol.name = dubbo
## RegistryConfig Bean
dubbo.registry.address = zookeeper://192.168.3.104:2181
三、构建生产者消费者工程包和类
四、生产者Springboot启动类及服务提供方的编写
一、请注意这里一定要加上EnableDubbo注解,这样才能正常启动Dubbo
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
public class HappylandMarketApplication {
public static void main(String[] args) {
SpringApplication.run(HappylandMarketApplication.class, args);
}
}
二、编写我们对外提供服务的实现类,请特别注意这里的Service使用的是Dubbo的Service注解
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
@Service
public class MarketImpl implements MarketService{
@Autowired
public String printHello(){
System.out.println("helloworld");
return "helloworld";
}
}
三、编写我们要对外提供服务的接口
public interface MarketService {
public String printHello();
}
五、消费者Springboot启动类及服务提供方的编写
一、启动类,@ComponentScan("com.happyland.happylandcustomer.test")我们可以自定义要扫描的包, 默认扫描该@SpringBootApplication注解下所有包含的包及项目
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import com.happyland.happylandcustomer.test.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan("com.happyland.happylandcustomer.test")
@EnableDubbo
public class HappylandCustomerApplication {
public static void main(String[] args) {
SpringApplication.run(HappylandCustomerApplication.class, args);
}
}
二、编写一个Controller,我们通过页面请求的方式获取内容
@RequestMapping("getHello")
@Controller
public class TestController {
@Autowired
private TestService testServiceImpl;
@RequestMapping("/getHello")
@ResponseBody
public String getHello(){
return testServiceImpl.printHello();
}
}
三、编写Service
public interface TestService {
public String printHello();
}
四、编写实现,请注意这里的Reference注解,他表示我要获取的服务接口。当你想通过dubbo获取服务的时候,请先通过pom依赖它。MarketService即是生产者提供的接口。
import com.alibaba.dubbo.config.annotation.Reference;
import com.happyland.happylandmarket.MarketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("testServiceImpl")
public class TestServiceImpl implements TestService{
@Reference
private MarketService marketService;
@Override
public String printHello() {
return marketService.printHello();
}
}
六、先启动Zookeeper,再生产者工程,最后启动消费者工程
我的zookeeper安装在虚拟机上,安装zk和duboo-admin的方式请自行百度吧。
附上启动命令:
start zookeeper server: ./zookeeper/zookeeper-3.4.12/bin/zkServer.sh start
statr duboo admin: ./tomcat/apache-tomcat-8.5.35/bin/startup.sh
七、测试
当然这只是很简陋的一段代码
但是我们实现了开心乐园的最基本dubbo通讯功能
剩下的就是你自己垒业务,垒自己喜欢的代码,加油