一、在指定的服务器启动zookeeper作为服务注册中心
二、创建服务提供者
1.添加依赖,完整依赖类似如下:
<?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>org.example</groupId>
<artifactId>dubbo-service-provider</artifactId>
<version>1.0-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>
<dubbo.version>3.1.8</dubbo.version>
<spring-boot.version>2.7.10</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-bom</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
2.配置文件中配置dubbo相关信息(使用dubbo协议)和zookeeper的地址
server:
port: 9811
dubbo:
application:
name: dubbo-springboot-demo-provider
protocol:
name: dubbo
port: -1
registry:
address: zookeeper://xx.xx.xx.xx:2181
3.创建服务定义接口
package cn.edu.tju.service;
public interface DemoService {
String sayHello(String name);
}
4.定义服务实现,使用注解@DubboService将服务注册到zookeeper
package cn.edu.tju.service;
import org.apache.dubbo.config.annotation.DubboService;
@DubboService
public class DemoServiceImpl implements DemoService {
public String sayHello(String name) {
return "Hello " + name;
}
}
5.定义spring boot 主类,加上@EnableDubbo注解
package cn.edu.tju;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
public class Start {
public static void main(String[] args) {
SpringApplication.run(Start.class, args);
}
}
三、创建服务消费者
1.添加依赖,和服务生产者类似
2.配置dubbo信息和zookeeper地址
server:
port: 9812
dubbo:
application:
name: dubbo-springboot-demo-consumer
protocol:
name: dubbo
port: -1
registry:
address: zookeeper://xx.xx.xx.xx:2181
3.创建服务定义接口,和服务提供者端相同
package cn.edu.tju.service;
public interface DemoService {
String sayHello(String name);
}
4.创建服务实现,通过@DubboReference注解使用服务提供者所提供的服务
package cn.edu.tju.service;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.util.Date;
@Service
public class RemoteService {
@DubboReference
private DemoService demoService;
public String callRemoteService(){
try {
Thread. sleep(1000);
return new Date() + " Receive result ======> " + demoService.sayHello("world");
} catch (InterruptedException e) {
e.printStackTrace();
return e.getMessage();
}
}
}
5.定义controller,注入service
package cn.edu.tju.cn.edu.tju;
import cn.edu.tju.service.RemoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DemoController {
@Autowired
private RemoteService remoteService;
@RequestMapping("/getDubbo")
public String getDubbo(){
return remoteService.callRemoteService();
}
}
6.定义spring boot 主类,加上@EnableDubbo注解
package cn.edu.tju;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
public class Start {
public static void main(String[] args) {
SpringApplication.run(Start.class, args);
}
}
7.分别启动生产者和消费者,然后调用接口进行测试