目录
2.1新建一个公共模块——存放生产者和消费者共有的接口和实体类
1.环境准备
1.1安装并配置zookeeper
复制conf目录下的zoo_sample.cfg,重命名为zoo.cfg
打开zoo.cfg
主要设置以下两处
dataDir=/tmp/zookeeper
clientPort=2181 #服务端口号
1.2启动zookeeper
windows下点击 zkServer.cmd
2.创建三个模块
一个提供公共接口,一个生产者,一个消费者
2.1新建一个公共模块——存放生产者和消费者共有的接口和实体类
命名web_interface,存放公共service.
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>
<groupId>com.hck.interface</groupId>
<artifactId>web_interface</artifactId>
<version>1.0-SNAPSHOT</version>
</project>
新建一个service接口
package com.hck.service;
public interface RemoteUserService {
String sayHello(String name);
}
2.2创建生产者模块——提供服务方
1.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>
<groupId>com.hck.web_01</groupId>
<artifactId>web_01</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<!-- SpringBoot 启动父依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
</parent>
<!-- SpringBoot Web 依赖 -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--<version>2.0.5.RELEASE</version>-->
</dependency>
<!-- SpringBoot dubbo 依赖 -->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!-- 引入zkclient -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version> <!--不带版本号会报异常-->
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.22</version>
</dependency>
<!-- 引入公共服务模块 -->
<dependency>
<groupId>com.hck.interface</groupId>
<artifactId>web_interface</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
2.配置文件
server:
port: 8088
spring:
application:
name: web_01_provider #注册到注册中心的名称
dubbo:
server: true
application:
name: provider
registry: #注册中心地址
address: zookeeper://127.0.0.1:2181
protocol: #采用协议和端口号
name: dubbo #采用dubbo协议
port: 20880 #发布dubbo端口号为20880
scan:
basePackages: com
3.启动类
package com;
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@Slf4j
@SpringBootApplication
@EnableDubboConfiguration
public class ApplicationProducer {
public static void main(String[] args) {
log.info("this is producer service");
SpringApplication.run(ApplicationProducer.class, args);
}
}
4.服务提供方
package com.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.hck.service.RemoteUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
@Slf4j
@Component
@Service(version = "1.0.0", timeout = 10000, interfaceClass = RemoteUserService.class)
public class RemoteUserServiceImpl implements RemoteUserService {
@Override
public String sayHello(String name) {
return "Hello:" + name;
}
}
2.3创建消费者模块——服务使用方
1.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>
<groupId>com.hck.web</groupId>
<artifactId>web_02</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>web_02 Maven Webapp</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</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.hck.interface</groupId>
<artifactId>web_interface</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.22</version>
</dependency>
</dependencies>
</project>
2.配置文件
server:
port: 8089
spring:
application:
name: consumer
dubbo:
application:
name: consumer
registry:
address: zookeeper://127.0.0.1:2181
protocol:
name: dubbo
scan:
basePackages: com
3.启动类
package com;
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@Slf4j
@SpringBootApplication
@EnableDubboConfiguration
public class ApplicationConsumer {
public static void main(String[] args) {
log.info("this is consumer service.");
SpringApplication.run(ApplicationConsumer.class, args);
}
}
4.消费者接口/service
package com.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.hck.service.RemoteUserService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 访问方式:http://localhost:8089/dubbo/say/999
*/
@RestController
public class RemoteUserController {
@Reference(version = "1.0.0")
private RemoteUserService remoteUserService;
@RequestMapping(value = "/dubbo/say/{name}")
public String sayHello(@PathVariable("name") String name){
String result = remoteUserService.sayHello(name);
return result;
}
}