1. What is Dubbo?
Apache Dubbo is a microservice development framework that provides two key capabilities: RPC communication and microservice governance. This means that the microservices developed by Dubbo will have the ability to remotely discover and communicate with each other, and at the same time, the rich service governance capabilities provided by Dubbo can be used to achieve service governance requirements such as service discovery, load balancing, and traffic scheduling. At the same time, Dubbo is highly extensible, and users can customize their own implementation at almost any function point to change the default behavior of the framework to meet their business needs.
2. ZooKeeper environment setup
Dubbo uses zookeeper as the registry, so zookeeper needs to be installed.
version: '3'
services:
zookeeper:
image: zookeeper:3.7.0
container_name: zookeeper
restart: unless-stopped
volumes:
- "./zookeeper/data:/data"
- "./zookeeper/datalog:/datalog"
ports:
- "2181:2181"
# webui
zookeeper-webui:
image: tobilg/zookeeper-webui
container_name: zookeeper-webui
restart: unless-stopped
environment:
ZK_DEFAULT_NODE: zookeeper:2181
depends_on:
- zookeeper
links:
- zookeeper
ports:
- "8089:8080"
initiate
docker-compose -f docker-compose-zookeeper.yml -p zookeeper up -d
The access address of the visual interface is [http://ip address: 8089] and enter [{host IP}:2181/] to enter PrettyZoo, a desktop visualizer: https://github.com/vran-dev/PrettyZoo
3. Code engineering
Purpose: To implement remote call to dubbo
dubbo-samples-spring-boot-interface
API layer, which is exposed to consumer and provider references
<!--api -->
<dependency>
<groupId>com.et</groupId>
<artifactId>dubbo-samples-spring-boot-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
package com.et.api.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User implements Serializable {
private static final long serialVersionUID = -4294369157631461921L;
Long userId;
String userName;
String userInfo;
}
package com.et.api;
import com.et.api.entity.User;
public interface UserService {
String getUserInfo();
User getUserById(String userId);
}
dubbo-samples-spring-boot-provider
The service provisioning layer registers the service with ZooKeeper
package com.et.provider.service;
import com.et.api.UserService;
import com.et.api.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;
/**
* @service is org.apache.dubbo.config.annotation.Service. not spring
*/
@Service
@Component
@Slf4j
public class UserServiceImpl implements UserService {
@Override
public String getUserInfo() {
log.info("this is a test");
return "userTest";
}
@Override
public User getUserById(String userId) {
log.info("invoke getUserById method");
User user = new User();
user.setUserId(Long.valueOf(userId));
user.setUserInfo("test");
user.setUserName("lin");
return user;
}
}
package com.et.provider;
import org.apache.dubbo.config.spring.context.annotation.DubboComponentScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* DubboComponentScan service package
*/
@SpringBootApplication
@DubboComponentScan("com.et.provider.service")
public class DubboProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DubboProviderApplication.class, args);
}
}
dubbo:
application:
name: dubbo-provider
protocol:
name: dubbo
port: 20880
registry:
address: zookeeper://127.0.0.1:2181
server:
port: 8081
dubbo-samples-spring-boot-consumer
Consume the application and call the provider service
package com.et.consumer.controller;
import com.et.api.UserService;
import com.et.api.entity.User;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user")
public class ConsumerController {
/**
* @Reference invoker dubbo-provider
*/
@Reference
private UserService userService;
@GetMapping("/info")
public String getUserById() {
return userService.getUserInfo();
}
@GetMapping("/{id}")
public User getUserById(@PathVariable String id) {
return userService.getUserById(id);
}
}
The above are just some of the key codes, all of which can be found in the repositories below
Code repositories
4. Testing
Start the provider and consumer applications