Dubbo是阿里巴巴开源的基于Java的高性能RPC(一种远程调用)分布式服务框架(SOA),致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案
节点
角色说明
Provider
暴露服务的服务提供方
Consumer
调用远程服务的服务消费方
Registry
服务注册与发现的注册中心
Monitor
统计服务的调用次数和调用时间的监控中心
Container
服务运行容器
调用关系说明
0.服务容器负责启动,加载,运行服务提供者
1.服务提供者在启动时,向注册中心注册自己提供的服务
2.服务消费者在启动时,向注册中心订阅自己所需的服务
3.注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者
4.服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用
5.服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心
ZooKeeper地址配置为:IP:Port1,IP:Port2
,需要根据实际情况修改
public interface UserService {
String sayHello();
}
pom.xml:
dubbo
com.hand
1.0-SNAPSHOT
4.0.0
user-service
2.1.1.RELEASE
2.7.3
org.springframework.boot
spring-boot-dependencies
${spring-boot.version}
pom
import
org.apache.dubbo
dubbo-dependencies-bom
${dubbo.version}
pom
import
org.apache.dubbo
dubbo
${dubbo.version}
org.springframework
spring
javax.servlet
servlet-api
log4j
log4j
org.springframework.boot
spring-boot-starter
com.hand
user-api
1.0-SNAPSHOT
org.apache.dubbo
dubbo-spring-boot-starter
${dubbo.version}
org.apache.dubbo
dubbo
org.apache.dubbo
dubbo-dependencies-zookeeper
${dubbo.version}
pom
org.slf4j
slf4j-log4j12
application.properties:
#dubbo.application.name默认值为spring.application.name
spring.application.name=dubbo-auto-configuration-provider-demo
#要扫描dubbo组件的基本包
dubbo.scan.base-packages=com.hand.service.impl
#dubbo协议配置
dubbo.protocol.name=dubbo
dubbo.protocol.port=8081
#dubbo注册中心地址
dubbo.registry.address=zookeeper://IP:Port1,IP:Port2
主启动类:
package com.hand.service;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@EnableAutoConfiguration
public class ServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceApplication.class);
}
}
接口实现类:
package com.hand.service.impl;
import com.hand.user.service.UserService;
import org.apache.dubbo.config.annotation.Service;
@Service(version = "1.0.0")
public class UserServiceImpl implements UserService {
public String sayHello() {
return "hello world";
}
}
pom.xml:
dubbo
com.hand
1.0-SNAPSHOT
4.0.0
user-web
2.1.1.RELEASE
2.7.3
org.springframework.boot
spring-boot-dependencies
${spring-boot.version}
pom
import
org.apache.dubbo
dubbo-dependencies-bom
${dubbo.version}
pom
import
org.apache.dubbo
dubbo
${dubbo.version}
org.springframework
spring
javax.servlet
servlet-api
log4j
log4j
com.hand
user-api
1.0-SNAPSHOT
org.apache.dubbo
dubbo-spring-boot-starter
${dubbo.version}
org.apache.dubbo
dubbo
${dubbo.version}
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-web
org.apache.dubbo
dubbo-dependencies-zookeeper
${dubbo.version}
pom
org.slf4j
slf4j-log4j12
application.properties:
#dubbo.application.name默认值为spring.application.name
spring.application.name=dubbo-auto-configure-consumer-demo
#dubbo注册中心地址
dubbo.registry.address=zookeeper://IP:Port1,IP:Port2
主启动类:
package com.hand.user;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class WebApplication {
public static void main(String[] args) {
SpringApplication.run(WebApplication.class);
}
}
消费者:
package com.hand.user.web;
import com.hand.user.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Reference(version = "1.0.0")
private UserService userService;
@RequestMapping("/sayHello")
public String sayHello() {
return userService.sayHello();
}
}
GitHub地址:https://github.com/hxt970311/dubbo-springboot-annotation-demo
新增provider.xml,在里面配置服务提供方相应的信息
去掉具体服务实现类中@Service注解
package com.hand.service.impl;
import com.hand.user.service.UserService;
public class UserServiceImpl implements UserService {
public String sayHello() {
return "hello world";
}
}
在启动类中新增@ImportResource注解
package com.hand.service;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ImportResource;
@ImportResource("provider.xml")
@EnableAutoConfiguration
public class ServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceApplication.class);
}
}
新增consumer.xml
在启动类中新增@ImportResource注解
package com.hand.user;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@ImportResource("consumer.xml")
@SpringBootApplication
public class WebApplication {
public static void main(String[] args) {
SpringApplication.run(WebApplication.class);
}
}
使用Spring的@Autowired注解代替@Reference
package com.hand.user.web;
import com.hand.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/sayHello")
public String sayHello() {
return userService.sayHello();
}
}
GitHub地址:https://github.com/hxt970311/dubbo-springboot-xml-demo