不同服务开发实战
1、新建一个空白项目
2、在空白项目下,新建2个springboot-web模块
新建 provider-server 和 consumer-server 模块,模拟提供者和消费者服务。
新建 service 包,包下编写服务接口。
项目目录:
TicketService
package com.allen.service;
public interface TicketService {
public String getTicket();
}
TicketServiceImpl
package com.allen.service;
/**
* @author Allen
* @date 2021/1/5 20:32
*/
public class TicketServiceImpl implements TicketService {
@Override
public String getTicket() {
return "allen";
}
}
UserService
3、导入依赖
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.4.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.allen</groupId>
<artifactId>provider-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>provider-server</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--导入依赖 zookeeper + dubbo-->
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<!--解决log4j依赖冲突-->
<!-- https://mvnrepository.com/artifact/org.apache.curator/curator-framework -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>5.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.curator/curator-recipes -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>5.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.6.2</version>
<!--排除这个slf4j-log4j12-->
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4、配置提供者的 application.properties
server.port=8001
# 服务中心名字
dubbo.application.name=provider-server
# 注册中心地址
dubbo.registry.address=zookeeper://127.0.0.1:2181
# 哪些服务要被注册
dubbo.scan.base-packages=com.allen.service
5、配置注解,启动提供者服务主启动类
package com.allen.service;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;
@DubboService
@Component
public class TicketServiceImpl implements TicketService {
@Override
public String getTicket() {
return "allen";
}
}
启动提供者服务主启动类 ProviderServerApplication
6、开启zookeeper服务,开启dubbo监听
监听界面如下:
7、开启消费者服务
7.1、配置消费者的 application.properties
server.port=8002
# 消费者去哪里拿服务,需要暴露自己的名字
dubbo.application.name=consumer-server
# 注册中心地址
dubbo.registry.address=zookeeper://127.0.0.1:2181
7.2、将提供者的接口 TicketService 拷贝到消费者对应目录下
7.3、编写 UserService 类
UserService(注解)
package com.allen.service;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
/**
* @author Allen
* @date 2021/1/5 20:37
*/
@Service
public class UserService {
//引用,pom坐标
@DubboReference
TicketService ticketService;
public void buyTicket(){
String ticket = ticketService.getTicket();
}
}
7.4、开启消费者服务
界面如下: