1.選擇ZooKeeper作為註冊中心
在linux環境中使用docker安裝ZooKeeper
<code>//拉取zookeeper鏡像
docker pull zookeeper
//啟動zookeeper
docker run -d -p 2181:2181 -v /mysoft/zookeeper/data/:/data/ --name=zookeeper --privileged zookeeper/<code>
使用zookeeper-dev-ZooInspector客戶端連接工具查看
2.創建maven項目統一聲明接口,把所有接口提取到單獨的項目,通過maven引入到其他項目
pom.xml
<code>
<project> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelversion>4.0.0/<modelversion>
<groupid>com.cott.gmail/<groupid>
<artifactid>api-interface/<artifactid>
<version>1.0-SNAPSHOT/<version>
/<project>/<code>
UserAddress.java
<code>package com.cott.gmail.bean;
import java.io.Serializable;
public class UserAddress implements Serializable {
private Integer id;
private String userAddress;
private String userId;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserAddress() {
return userAddress;
}
public void setUserAddress(String userAddress) {
this.userAddress = userAddress;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
}/<code>
OrderService.java
<code>package com.cott.gmail.service;
import com.cott.gmail.bean.UserAddress;
import java.util.List;
public interface OrderService {
List<useraddress> initOrder(String id);
}/<useraddress>/<code>
UserService.java
<code>package com.cott.gmail.service;
import com.cott.gmail.bean.UserAddress;
import java.util.List;
public interface UserService {
List<useraddress> getAddress(String userId);
}/<useraddress>/<code>
3.創建生產者SpringBoot項目
pom.xml引入上文接口項目,引入dubbo依賴
<code>
<project> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelversion>4.0.0/<modelversion>
<parent>
<groupid>org.springframework.boot/<groupid>
<artifactid>spring-boot-starter-parent/<artifactid>
<version>2.2.4.RELEASE/<version>
<relativepath>
/<parent>
<groupid>com.cott.gmail/<groupid>
<artifactid>boot-user-service-provider/<artifactid>
<version>0.0.1-SNAPSHOT/<version>
<name>boot-user-service-provider/<name>
<description>Demo project for Spring Boot/<description>
<properties>
<java.version>1.8/<java.version>
/<properties>
<dependencies>
<dependency>
<groupid>com.cott.gmail/<groupid>
<artifactid>api-interface/<artifactid>
<version>1.0-SNAPSHOT/<version>
/<dependency>
<dependency>
<groupid>com.alibaba.boot/<groupid>
<artifactid>dubbo-spring-boot-starter/<artifactid>
<version>0.2.0/<version>
/<dependency>
<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>
<exclusions>
<exclusion>
<groupid>org.junit.vintage/<groupid>
<artifactid>junit-vintage-engine/<artifactid>
/<exclusion>
/<exclusions>
/<dependency>
/<dependencies>
<build>
<plugins>
<plugin>
<groupid>org.springframework.boot/<groupid>
<artifactid>spring-boot-maven-plugin/<artifactid>
/<plugin>
/<plugins>
/<build>
/<project>/<code>
實現UserService接口,作為dubbo的生產者需要用@Service註解,為了和spring的@Service註解區分,所以這裡用的是@Component註解。更多springboot實戰類文章:SpringBoot內容聚合
UserServiceImpl.java
<code>package com.cott.gmail.bootuserserviceprovider.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.cott.gmail.bean.UserAddress;
import com.cott.gmail.service.UserService;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
@Service
@Component
public class UserServiceImpl implements UserService {
@Override
public List<useraddress> getAddress(String uesrId) {
UserAddress userAddress1 = new UserAddress();
userAddress1.setId(1);
userAddress1.setUserAddress("1");
userAddress1.setUserId("1");
UserAddress userAddress2 = new UserAddress();
userAddress2.setId(2);
userAddress2.setUserAddress("2");
userAddress2.setUserId("2");
return Arrays.asList(userAddress1, userAddress2);
}
}/<useraddress>/<code>
主方法上用@EnableDubbo啟用Dubbo註解
BootUserServiceProviderApplication.java
<code>package com.cott.gmail.bootuserserviceprovider;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableDubbo
@SpringBootApplication
public class BootUserServiceProviderApplication {
public static void main(String[] args) {
SpringApplication.run(BootUserServiceProviderApplication.class, args);
}
}/<code>
配置文件指定應用名稱,註冊中心,服務協議和端口號
application.yml
<code>dubbo:
application:
name: boot-user-service-provider
registry:
address: 192.168.200.128:2181
protocol: zookeeper
protocol:
name: dubbo
port: 20880/<code>
最後啟動程序,在dubbo-admin中查看服務已經註冊上去了。
4.創建消費者SpringBoot項目
pom.xml引入上文接口項目,引入dubbo依賴
<code>
<project> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelversion>4.0.0/<modelversion>
<parent>
<groupid>org.springframework.boot/<groupid>
<artifactid>spring-boot-starter-parent/<artifactid>
<version>2.2.4.RELEASE/<version>
<relativepath>
/<parent>
<groupid>com.cott.gmail/<groupid>
<artifactid>boot-order-service-consumer/<artifactid>
<version>0.0.1-SNAPSHOT/<version>
<name>boot-order-service-consumer/<name>
<description>Demo project for Spring Boot/<description>
<properties>
<java.version>1.8/<java.version>
/<properties>
<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>
<exclusions>
<exclusion>
<groupid>org.junit.vintage/<groupid>
<artifactid>junit-vintage-engine/<artifactid>
/<exclusion>
/<exclusions>
/<dependency>
<dependency>
<groupid>org.springframework.boot/<groupid>
<artifactid>spring-boot-starter-web/<artifactid>
/<dependency>
<dependency>
<groupid>com.cott.gmail/<groupid>
<artifactid>api-interface/<artifactid>
<version>1.0-SNAPSHOT/<version>
<scope>compile/<scope>
/<dependency>
<dependency>
<groupid>com.alibaba.boot/<groupid>
<artifactid>dubbo-spring-boot-starter/<artifactid>
<version>0.2.0/<version>
/<dependency>
/<dependencies>
<build>
<plugins>
<plugin>
<groupid>org.springframework.boot/<groupid>
<artifactid>spring-boot-maven-plugin/<artifactid>
/<plugin>
/<plugins>
/<build>
/<project>/<code>
實現OrderServiece接口,這裡的@Service是spring的,這裡使用@Reference註解聲明UserService是通過遠程調用注入進來
OrderServiceImpl.java
<code>package com.cott.gmail.bootorderserviceconsumer.service.impl;
import com.alibaba.dubbo.config.annotation.Reference;
import com.cott.gmail.bean.UserAddress;
import com.cott.gmail.service.OrderService;
import com.cott.gmail.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class OrderServiceImpl implements OrderService {
@Reference
UserService userService;
@Override
public List<useraddress> initOrder(String id) {
System.out.println("id= " + id);
List<useraddress> list = userService.getAddress("1");
for (UserAddress user : list
) {
System.out.println(user.getUserAddress());
}
return list;
}
}/<useraddress>/<useraddress>/<code>
OrderController.java
<code>package com.cott.gmail.bootorderserviceconsumer.controller;
import com.cott.gmail.bean.UserAddress;
import com.cott.gmail.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
public class OrderController {
@Autowired
OrderService orderService;
@ResponseBody
@GetMapping("/initOrder")
public List<useraddress> initOrder(@RequestParam(name = "id") String id) {
return orderService.initOrder(id);
}
}/<useraddress>/<code>
@EnableDubbo啟用Dubbo註解
BootOrderServiceConsumerApplication.java
<code>package com.cott.gmail.bootorderserviceconsumer;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableDubbo
@SpringBootApplication
public class BootOrderServiceConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(BootOrderServiceConsumerApplication.class, args);
}
}/<code>
配置文件中聲明服務名稱,註冊中心,以及tomcat對外端口號
application.yml
<code>dubbo:
application:
name: order-service-provider
registry:
address: 192.168.200.128:2181
protocol: zookeeper
server:
port: 8081/<code>
至此,一個簡單的dubbo項目已經開發完成,下面啟動消費者項目,在瀏覽器中輸入url,得到返回結果
Java知音,專注於Java實用文章推送,不容錯過!
來源:cnblogs.com/cott/p/12373043.html
閱讀更多 Java知音 的文章