Dubbo microservices are simple and practical

  The previous blog updated the installation of the Dubbo environment. Today, I will start to write a simple project for actual combat.
  
  First create an empty project, and then build our project in this empty project; to
  create a submodule, you can create maven or create it directly The springboot project creates sub-modules for service providers and consumers. The project structure is as follows. After the
Insert picture description here
  project is created, write services for the two modules, customize the service content
  
  and start the dubbo configuration, first configure the service provider

1. Import the jar package
<!-- dubbo -->
<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo-spring-boot-starter</artifactId>
    <version>2.7.8</version>
</dependency>
<!-- zookeeper -->
<dependency>
    <groupId>com.github.sgroschupf</groupId>
    <artifactId>zkclient</artifactId>
    <version>0.1</version>
</dependency>
<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-framework</artifactId>
    <version>5.1.0</version>
</dependency>
<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-recipes</artifactId>
    <version>5.1.0</version>
</dependency>
<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>3.6.2</version>
    <!-- 排除日志jar包 -->
    <exclusions>
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </exclusion>
    </exclusions>
</dependency>
2. Configure dubbo related configuration
server.port=8081
# 服务应用名称,消费者通过这个名称去找到这个服务
dubbo.application.name=provider-server
# 注册中心地址
dubbo.registry.address=zookeeper://192.168.0.109:2181
# 需要被注册的服务
dubbo.scan.base-packages=com.provider.service
# 注册超时时间
dubbo.registry.timeout=20000

## 注意,zookeeper的默认超时时间是2秒,如果两秒内连上了就没问题,项目会启动
## 如果两秒内没连上,项目会启动失败
## 所以建议将超时时间设置大一点,或者去更改zookeeper的配置文件:tickTime 参数
3. Add annotations to the scanning service
/**
 *	注意这个服务类的注解,新版使用的是DubboService
 *	之前的版本使用的是Service,引入包的时候选择dubbo下面的,否则会扫描失败
 *	或者使用万能注解Component
 **/
import org.apache.dubbo.config.annotation.DubboService;
@DubboService
public class ProviderService implements IProviderService {
    
    
    @Override
    public String demo(String str) {
    
    
        String result = "传了一个参数:" + str;
        System.out.println(result);
        return result;
    }
}

  After the project is started, you can see that the service we registered in the background control interface of dubbo is
Insert picture description here
  now the service provider is running normally, and then we will start to configure the service consumer

1. Import the jar package
<!-- dubbo -->
<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo-spring-boot-starter</artifactId>
    <version>2.7.8</version>
</dependency>
<!-- zookeeper -->
<dependency>
    <groupId>com.github.sgroschupf</groupId>
    <artifactId>zkclient</artifactId>
    <version>0.1</version>
</dependency>
<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-framework</artifactId>
    <version>5.1.0</version>
</dependency>
<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-recipes</artifactId>
    <version>5.1.0</version>
</dependency>
<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>3.6.2</version>
    <!-- 排除日志jar包 -->
    <exclusions>
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </exclusion>
    </exclusions>
</dependency>
2. Configure dubbo related configuration
server.port=8082
# 消费者名称
dubbo.application.name=consumer-service
# 注册中心地址
dubbo.registry.address=zookeeper://192.168.0.109:2181
# 连接超时时间
dubbo.registry.timeout=20000

## 注意,zookeeper的默认超时时间是2秒,如果两秒内连上了就没问题,项目会启动
## 如果两秒内没连上,项目会启动失败
## 所以建议将超时时间设置大一点,或者去更改zookeeper的配置文件:tickTime 参数
3. Write service call remote

There are two ways to call remote services. One is to create an interface class with the same package and the same name as the service provider in the service consumer project, which must be exactly the same, otherwise it will fail when calling, and the
other is to import it in the pom file The coordinates of the service provider are used in actual development to introduce the coordinates

<!-- 服务提供者的坐标 -->
<dependency>
    <groupId>com.provider</groupId>
    <artifactId>provider-server</artifactId>
    <version>1.0</version>
</dependency>
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
@Service //注意,这里在本地使用,直接加入到本地spring容器
public class ConsumerService implements IConsumerService {
    
    
    @DubboReference //引用远程服务
    private IProviderService providerService;

    @Override
    public void consumerDemo() {
    
    
        System.out.println(providerService.demo("测试"));
    }
}

  Then test by unit test
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_45481406/article/details/109728364