RPC全称为remote procedure call,即远程过程调用。比如两台服务器A和B,A服务器上部署一个应 用,B服务器上部署一个应用,A服务器上的应用想调用B服务器上的应用提供的方法,由于两个应用不 在一个内存空间,不能直接调用,所以需要通过网络来表达调用的语义和传达调用的数据。
Zookeeper的安装(在Lunix系统下安装)下载地址:http://archive.apache.org/dist/zookeeper/
Dubbo管理控制台的dubbo-admin
(1)将资料中的dubbo-admin-2.6.0.war文件复制到tomcat的webapps目录下
(2)启动tomcat,此war文件会自动解压
(3)修改WEB-INF下的dubbo.properties文件,注意dubbo.registry.address对应的值需要对应当前使用的Zookeeper的ip地址和端口号
dubbo.registry.address=zookeeper://192.168.134.129:2181
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest
(4)重启tomcat
访问 http://你的地址:8080/dubbo-admin-2.6.0
创建三个模块:
前两个用springBoot创建,最后一个只是放接口和实体,用maven创建
dubbo-interface
依赖:
org.song
dubbo-interface
1.0-SNAPSHOT
UTF-8
1.8
1.8
org.projectlombok
lombok
1.18.12
provided
实体类
@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserInfo implements Serializable {
private String name;
private String userId;
private String address;
}
接口
public interface CustomerService {
public List initCustomer(String id);
public List getUserInfoList(String id);
}
boot-dubbo-provider 服务提供
依赖:
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.6.3
com.song
boot-dubbo-provider
0.0.1-SNAPSHOT
boot-dubbo-provider
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter
com.alibaba.boot
dubbo-spring-boot-starter
0.2.0
org.projectlombok
lombok
1.18.12
provided
org.song
dubbo-interface
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
配置文件:application.yml
#端口设置
server:
port: 9001
#dubbo的设置
dubbo:
application:
name: boot-dubbo-provider
#dubbo在zookeeper的注册地址
registry:
address: zookeeper://192.168.126.135:2181
#注册 协议和port
protocol:
name: dubbo
port: 20881
核心代码:
//@Service这个注解不是spring框架的注解,是dobbo的注解,表示这是一个对外发布服务
@Service
@Component
public class UserInfoServiceImpl implements UserInfoService {
@Override
public List getUserInfoList(String id) {
UserInfo userInfo1 = new UserInfo("1", "李四", "北京");
UserInfo userInfo2 = new UserInfo("2", "王五", "上海");
return Arrays.asList(userInfo1, userInfo2);
}
}
启动器注解:
/**@EnableDubbo
* 表示开启Dubbo注解
*/
@SpringBootApplication
@EnableDubbo
public class BootDubboProviderApplication {
public static void main(String[] args) {
SpringApplication.run(BootDubboProviderApplication.class, args);
}
}
boot-dubbo-customer 服务消费
依赖:与 boot-dubbo-provider一样
配置文件:application.yml
server:
port: 9005
dubbo:
application:
name: boot-dubbo-customer
registry:
address: zookeeper://192.168.126.135:2181
核心代码:
service层
@Service
public class CustomeServiceImpl implements CustomerService {
/**
* @Reference
* Dubbo提供的注解,动态代理服务提供者对外发布的服务
*/
@Reference
private UserInfoService UserInfo;
@Override
public List initCustomer(String id) {
List userInfoList = UserInfo.getUserInfoList(id);
return userInfoList;
}
}
controller
@RestController
@RequestMapping("customer")
public class CustomerController {
@Resource
public CustomeServiceImpl service;
@RequestMapping("{id}")
public List getUserInfo(@PathVariable("id")String id){
List userInfos = service.initCustomer(id);
return userInfos;
}
}
启动器注解:
@SpringBootApplication
@EnableDubbo
public class BootDubboCustomerApplication {
public static void main(String[] args) {
SpringApplication.run(BootDubboCustomerApplication.class, args);
}
}
测试
访问 :http://192.168.126.135:8080/dubbo-admin-2.6.0
可以看服务提供者与消费者
访问 :http://localhost:9005/customer/2
可以查询到,说明两个微服务间的调用成功