## 1.準備工作 首先創建一個空的項目,然後再項目里創建三個模塊,分別為springboot-dubbo-interface,springboot-dubbo-provider,springboot-dubbo-consumer,其中springboot-dubbo-interface模塊只是一個 ...
1.準備工作
首先創建一個空的項目,然後再項目里創建三個模塊,分別為springboot-dubbo-interface,springboot-dubbo-provider,springboot-dubbo-consumer,其中springboot-dubbo-interface模塊只是一個簡單的maven工程,用來存放介面,在這裡我把實體類也放在了該模塊,而其他的兩個是springboot項目。三個模塊如下:
2.springboot-dubbo-interface實現
模塊結構如下:
User.java:
package org.example.entity;
import java.io.Serializable;
public class User implements Serializable {
private String name;
private Integer age;
private Integer id;
private String address;
private String phone;
private String email;
private String sex;
public User() {
}
public User(String name, Integer age, Integer id, String address, String phone, String email, String sex) {
this.name = name;
this.age = age;
this.id = id;
this.address = address;
this.phone = phone;
this.email = email;
this.sex = sex;
}
/**
* 獲取
* @return name
*/
public String getName() {
return name;
}
/**
* 設置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 獲取
* @return age
*/
public Integer getAge() {
return age;
}
/**
* 設置
* @param age
*/
public void setAge(Integer age) {
this.age = age;
}
/**
* 獲取
* @return id
*/
public Integer getId() {
return id;
}
/**
* 設置
* @param id
*/
public void setId(Integer id) {
this.id = id;
}
/**
* 獲取
* @return address
*/
public String getAddress() {
return address;
}
/**
* 設置
* @param address
*/
public void setAddress(String address) {
this.address = address;
}
/**
* 獲取
* @return phone
*/
public String getPhone() {
return phone;
}
/**
* 設置
* @param phone
*/
public void setPhone(String phone) {
this.phone = phone;
}
/**
* 獲取
* @return email
*/
public String getEmail() {
return email;
}
/**
* 設置
* @param email
*/
public void setEmail(String email) {
this.email = email;
}
/**
* 獲取
* @return sex
*/
public String getSex() {
return sex;
}
/**
* 設置
* @param sex
*/
public void setSex(String sex) {
this.sex = sex;
}
public String toString() {
return "User{name = " + name + ", age = " + age + ", id = " + id + ", address = " + address + ", phone = " + phone + ", email = " + email + ", sex = " + sex + "}";
}
}
註意:User類必需實現Serializable介面,因為後面User對象會從服務提供者傳輸到消費者,dubbo會序列化User對象。
UserService介面:
package org.example.service;
import org.example.entity.User;
public interface UserService {
public User getUser();
}
最後記得把該模塊install一下。
3.springboot-dubbo-provider實現
模塊結構如下:
pom.xml引入依賴:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--引入介面工程-->
<dependency>
<groupId>org.example</groupId>
<artifactId>springboot-dubbo-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--SpringBoot框架web項目起步依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--Dubbo集成SpringBoot起步依賴-->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!--zookeeper註冊中心依賴-->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.11</version>
</dependency>
</dependencies>
application.properties:
#設置內嵌Tomcat埠號
server.port=8082
#設置上下文根
server.servlet.context-path=/
#設置dubbo的配置
#服務提供者聲明名稱:必須保證服務名稱的唯一性,它的名稱是dubbo內部使用的唯一標識
spring.application.name=springboot-integration-dubbo-service-provider
#聲明當前工程是一個服務提供者
spring.dubbo.server=true
#設置訪問服務的協議及埠號,dubbo官方推薦使用的是dubbo協議,埠號預設20880
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
#指定zookeeper註冊中心地址和埠號
spring.dubbo.registry=zookeeper://127.0.0.1:2181
UserServiceImpl.java:
package com.example.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import org.example.entity.User;
import org.example.service.UserService;
import org.springframework.stereotype.Component;
@Component
@Service(timeout = 3000,retries = 2) // 當前服務三秒超時,超時後有兩次重試,如果還是不成功,就不再重試
public class UserServiceImpl implements UserService {
@Override
public User getUser() {
User user = new User();
user.setId(10001);
user.setAddress("河南****");
user.setAge(18);
user.setName("張三");
user.setEmail("[email protected]");
user.setPhone("158361217272");
user.setSex("男");
return user;
}
}
此處的@Service不再是以前的了,他是dubbo提供的,不要用錯了,最後在啟動類加上@EnableDubboConfiguration註解。
4.springboot-dubbo-consumer實現
模塊結構如下:
pom文件和服務提供者一樣,然後再引入一個lombok依賴:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.26</version>
<scope>provided</scope>
</dependency>
application.properties:
#設置內嵌Tomcat埠號
server.port=8083
#設置上下文根
server.servlet.context-path=/
#設置dubbo的配置
#服務提供者聲明名稱:必須保證服務名稱的唯一性,它的名稱是dubbo內部使用的唯一標識
spring.application.name=springboot-integration-dubbo-consumer
#指定zookeeper註冊中心地址和埠號
spring.dubbo.registry=zookeeper://127.0.0.1:2181
UserController:
package com.example.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
import org.example.entity.User;
import org.example.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
@Reference
private UserService userService;
@GetMapping
public User getUser(){
log.info("請求了一次用戶數據!");
return userService.getUser();
}
}
註入的時候使用的是@Reference,他是dubbo提供的,不要用錯了。
5.測試
- 啟動zookeeper
- 啟動provider
- 啟動consumer
然後在瀏覽器訪問127.0.0.1:8083/user,結果如下:
{"name":"張三","age":18,"id":10001,"address":"河南****","phone":"158361217272","email":"[email protected]","sex":"男"}