Overview:
Node Role Description
node | Role Description |
---|---|
Provider | Expose service provider service |
Consumer | Call the remote service consumer services |
Registry | Service registration and discovery registries |
Monitor | The number of calls statistics monitoring center services and call time |
Container | Container services running |
Call relationship Description
- Container is responsible for starting the service container, load, run service provider.
- Service provider Provider-initiated time, Registry registration services they provided to the registry.
- Consumer Consumer Services at startup, Registry subscription services need to the registry.
- Registry Registry return address of the service provider to the consumer of a list, if there is a change registry data will change based on long connection push to consumers.
- Consumer service provider address from the list, soft load balancing algorithm to select a provider be invoked if the call fails then select another one.
- Consumers and service providers in memory statistics the number of calls and call time, time sent once per minute statistical data to the monitoring center Monitor.
Construction of the project
Development environment mainly related to the following aspects:
- Spring Boot
- JDK 8
- Dubbo 2.7.1
- Zookeeper
You can view the specific code github dubbo module: github.com/UniqueDong/...
Dubbo API
Define the service interface, labeled jar package so that consumers rely on, service providers implement the interface. The project only interface definitions and model objects. @Data belong characteristics lombok open source library provides for easy development.
- model object definitions:
@Data
public class User implements Serializable {
private Long id;
private String username;
}
复制代码
- provider interface definition:
public interface UserProvider {
List<User> listUser();
}
复制代码
Provider Service Provider
- pom-dependent:
The introduction of spring-boot-starter, dubbo-api jar interface defines the interface that we mentioned above, dubbo-spring-boot-starter, dubbo-dependencies-zookeeper.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-logging</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>zero.springboot.study</groupId>
<artifactId>dubbo-api</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<!--dubbo start-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.1</version>
</dependency>
<!-- Zookeeper dependencies -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>2.7.1</version>
<type>pom</type>
<exclusions>
<exclusion>
<artifactId>log4j</artifactId>
<groupId>log4j</groupId>
</exclusion>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<!--dubbo end-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.57</version>
</dependency>
</dependencies>
复制代码
- Profile yaml definition:
spring:
application:
name: dubbo-provider
#自定义配置
embedded:
zookeeper:
# zookeeper 服务连接端口
port: 2181
# dubbo 配置
dubbo:
# 注册中心配置
registry:
id: dubbo-provider
address: zookeeper://127.0.0.1:${embedded.zookeeper.port}
group: local
application:
name: dubbo-provider
id: dubbo-provider
logger: slf4j
qosEnable: true
qosPort: 22224
qosAcceptForeignIp: false
# dubbo 协议配置
protocol:
# -1 表示使用随机未被占用的端口
port: -1
name: dubbo
scan:
# dubbo 服务提供者实现类所在包
base-packages: com.zero.provider.impl
复制代码
Implement the interface defined api
Note @Service is Dubbo, do not import the Spring of.
import com.google.common.collect.Lists;
import com.zero.api.model.User;
import com.zero.api.provider.UserProvider;
import org.apache.dubbo.config.annotation.Service;
import java.util.List;
@Service(interfaceClass = UserProvider.class)
public class UserProviderImpl implements UserProvider {
@Override
public List<User> listUser() {
User user = new User();
user.setId(1L);
user.setUsername("青龙");
return Lists.newArrayList(user);
}
}
复制代码
Consumer
- Pom definition:
We have to rely on spring-boot-starter-web provides front-end interfaces to invoke http rest. Meanwhile Dubbo achieved through internal provider RPC call service.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>zero.springboot.study</groupId>
<artifactId>dubbo-api</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<!--dubbo start-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.1</version>
</dependency>
<!-- Zookeeper dependencies -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>2.7.1</version>
<type>pom</type>
<exclusions>
<exclusion>
<artifactId>log4j</artifactId>
<groupId>log4j</groupId>
</exclusion>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<!--dubbo end-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
复制代码
- yaml definition:
server:
# web 日更年期端口
port: 9005
spring:
application:
name: dubbo-comsumer
#自定义配置
embedded:
zookeeper:
port: 2181
# dubbo 配置
dubbo:
registry:
id: dubbo-comsumer
address: zookeeper://127.0.0.1:${embedded.zookeeper.port}
group: local
application:
name: dubbo-comsumer
id: dubbo-comsumer
logger: slf4j
qosEnable: false
qosPort: 22223
qosAcceptForeignIp: false
protocol:
port: -1
name: dubbo
# 是否检查服务提供者有效
consumer:
check: false
复制代码
- Service consumer calls the service producer
import com.zero.api.model.User;
import com.zero.api.provider.UserProvider;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
@Reference
private UserProvider userProvider;
public List<User> listUser() {
return userProvider.listUser();
}
}
复制代码
- Through a RESTfull interface that provides front-end to call.
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService userService;
@GetMapping
public Object listUser() {
List<User> list = userService.listUser();
return list;
}
}
复制代码
to sum up
A variety of specific agreements, registration center, multi-registry, overtime and other configurations can view the official document dubbo.apache.org/zh-cn/docs/...
Welcome to the discussion correct, email: [email protected] . The thumbs and concern micro-channel public number is our motivation.
_ Joint communications scan code search pattern - green micro letter Standard Edition .png