Spring boot 集成 Dubbo 快速搭建

架构:

  1、ZooKeeper:服务注册中心

  2、api工程:提供对外暴露的服务API

  3、provider:服务提供者

  4、consumer:服务消费者

示例如下:

(一)新建 Maven 项目 api

1、  pom.xml

<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.dubbo</groupId>
<artifactId>api</artifactId>
<version>1.0.0</version> <build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin> </plugins>
</build>
</project>

2、   TestService.java

package com.dubbo.service;

import java.util.Map;

/**
* 测试获取主机信息
*
* @author Logan
* @version 1.0.0
* @createDate 2019-06-23
*
*/
public interface TestService { Map<String, Object> getHostMessage(); }

(二) 新建Maven项目provider

1、 pom.xml

<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.dubbo</groupId>
<artifactId>provider</artifactId>
<version>1.0.0</version> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent> <properties>
<dubbo.version>2.7.1</dubbo.version>
</properties> <dependencies> <!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
</dependency> <dependency>
<groupId>com.dubbo</groupId>
<artifactId>api</artifactId>
<version>1.0.0</version>
</dependency> </dependencies> <build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin> <plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

2、 ProviderStarter.java

package com.dubbo;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; /**
* 服务提供者启动类
*
* @author Logan
* @version 1.0.0
* @createDate 2019-06-23
*
*/
@EnableDubbo
@SpringBootApplication
public class ProviderStarter { public static void main(String[] args) {
SpringApplication.run(ProviderStarter.class, args);
} }

3、  TestServiceImpl.java

package com.dubbo.service.impl;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map; import org.apache.dubbo.config.annotation.Service; import com.dubbo.service.TestService; /**
* 测试服务提供者
*
* @author Logan
* @version 1.0.0
* @createDate 2019-06-23
*
*/
@Service
public class TestServiceImpl implements TestService { @Override
public Map<String, Object> getHostMessage() {
System.out.println("服务提供者被调用");
Map<String, Object> map = new HashMap<String, Object>();
try {
InetAddress host = InetAddress.getLocalHost();
map.put("hostName", host.getHostName());
map.put("hostAddress", host.getHostAddress());
} catch (UnknownHostException e) {
e.printStackTrace();
map.put("msg", e.getMessage());
}
return map;
} }

4、   application.properties

server.port=8880

# Spring boot application
spring.application.name=dubbo-provider # Base packages to scan Dubbo Component: @org.apache.dubbo.config.annotation.Service
dubbo.scan.base-packages=com.dubbo.service.impl # Dubbo Application
## The default value of dubbo.application.name is ${spring.application.name}
## dubbo.application.name=${spring.application.name} # Dubbo Protocol
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880 ## Dubbo Registry
dubbo.registry.address=zookeeper://127.0.0.1:2181

(三)新建Maven项目consumer

1、 pom.xml

<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.dubbo</groupId>
<artifactId>consumer</artifactId>
<version>1.0.0</version> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent> <properties>
<dubbo.version>2.7.1</dubbo.version>
</properties> <dependencies> <!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
</dependency> <dependency>
<groupId>com.dubbo</groupId>
<artifactId>api</artifactId>
<version>1.0.0</version>
</dependency> </dependencies> <build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin> <plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

2、 ConsumerStarter.java

package com.dubbo;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; /**
* 服务消费端启动类
*
* @author Logan
* @version 1.0.0
* @createDate 2019-06-23
*
*/
@EnableDubbo
@SpringBootApplication
public class ConsumerStarter { public static void main(String[] args) {
SpringApplication.run(ConsumerStarter.class, args);
} }

3、 ConsumerService.java

package com.dubbo.service;

import java.util.Map;

/**
* 消费者端的服务
*
* @author Logan
* @version 1.0.0
* @createDate 2019-06-23
*
*/
public interface ConsumerService { Map<String, Object> getHostMessage(); }

4、 ConsumerServiceImpl.java

package com.dubbo.service.impl;

import java.util.Map;

import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service; import com.dubbo.service.ConsumerService;
import com.dubbo.service.TestService; /**
* 消费端的服务
*
* @author Logan
* @version 1.0.0
* @createDate 2019-06-23
*
*/
@Service // 使用此注解可暴露服务到注册中心,此处只示例调用外部服务
public class ConsumerServiceImpl implements ConsumerService { @Reference
private TestService testService; @Override
public Map<String, Object> getHostMessage() {
Map<String, Object> hostMessage = testService.getHostMessage();
System.out.println(hostMessage); hostMessage.put("flag", "消费端的服务");
return hostMessage;
} }

5、 TestController.java

package com.dubbo.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import com.dubbo.service.ConsumerService; /**
* 测试消费端控制层
*
* @author Logan
* @version 1.0.0
* @createDate 2019-06-23
*
*/
@RestController
public class TestController { @Autowired
private ConsumerService service; @GetMapping("/getHostMessage")
public Map<String, Object> getHostMessage() {
return service.getHostMessage();
} }

6、 application.properties

server.port=8888

# Spring boot application
spring.application.name=dubbo-consumer # Base packages to scan Dubbo Component: @org.apache.dubbo.config.annotation.Service
dubbo.scan.base-packages=com.dubbo.service.impl # Dubbo Application
## The default value of dubbo.application.name is ${spring.application.name}
## dubbo.application.name=${spring.application.name} # Dubbo Protocol
dubbo.protocol.name=dubbo
dubbo.protocol.port=20888 ## Dubbo Registry
dubbo.registry.address=zookeeper://127.0.0.1:2181

(四) 启动测试

1、启动ZooKeeper服务器

ZooKeeper集群环境搭建可参考:https://www.cnblogs.com/jonban/p/zookeeper.html

练习只启动单机服务器即可

2、启动provider 服务提供者

运行 ProviderStarter.java

3. 启动consumer服务消费者

运行ConsumerStarter.java

4、浏览器输入 http://127.0.0.1:8888/getHostMessage

响应内容如下:{"hostName":"Logan","flag":"消费端的服务","hostAddress":"192.168.1.102"}

截图如下:

Spring boot 集成 Dubbo 快速搭建

观察服务提供者控制台信息,打印内容【服务提供者被调用】,服务提供端和消费端均已正常运行。

.

Spring boot 集成 Dubbo 快速搭建

上一篇:玩转Spring Boot 集成Dubbo


下一篇:CSS盒模型和margin重叠