Dubbo first use (broadcast format)

Dubbo first use

Distributed projects split all modules into independent projects, which are divided into business layer and service layer.
The business layer and the service layer are not in the same project and cannot be directly connected, while other projects can be dependent on the project, and the dependencies can be imported directly. This presents a problem. When the business layer accesses the service layer, there is no mutual dependence. Without the corresponding class, we need to use Dubbo as middleware.

Understand for yourself:

First, configure dubbo in the service layer, bind the service implementation class and interface together, and put it into the broadcast set by dubbo. When accessing the web, you also need to configure dubbo. Obtain the implementation class object in the broadcast through the interface, and take one id name, this id must be consistent with the interface name of the service introduced by the web layer, and then you can get it

Steps for usage

1. Import dependencies

<dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>dubbo</artifactId>
      <version>${
    
    dubbo-version}</version>
      <!--dubbo默认依赖spring2.5.6,有依赖冲突-->
      <exclusions>
        <exclusion>
          <groupId>org.springframework</groupId>
          <artifactId>spring</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

Dubbo relies on spring by default, which will conflict with the spring dependencies we imported, so we need to exclude it.

2. Configure the service layer interface

package com.ceh.back.service;

public interface TestService {
    
    
    /**
     * 测试dubbo
     * @return
     */
    String getNow();
}

When building the project structure, the interface needs to be proposed separately to form a separate project. Dubbo's official website proposes to do so, because both the web layer and the service layer need to be bound through this interface.

3. Declare the service (service implementation class)

@Service
public class TestServiceImpl implements TestService {
    
    

    @Autowired
    private TestDao testDao;


    @Override
    public String getNow() {
    
    
        Date date = testDao.getNow();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String now = sdf.format(date);
        return now;
    }
}

4. Configure service to dubbo

 <!--配置dubbo:广播的形式(把所有的服务全部暴露在一个地方,需要使用服务就去这里找)-->
    <!--配置服务方名称(随便写,但是要保持唯一)-->
    <dubbo:application name="taotao-back-service"/>
    <!--广播的地址,所有的服务会暴露在这里-->
    <dubbo:registry address="multicast://224.5.6.7:1234"/>
    <!--dubbo的端口号-->
    <dubbo:protocol name="dubbo" port="20880"/>
    <!--配置服务的提供方(service的实现类,已经使用了注解,就不需要再声明了)-->
    <!--<bean id="demoService" class="org.apache.dubbo.demo.provider.DemoServiceImpl"/>-->
    <!--注册服务
    interface:注册的接口,因为web层也引入了interface的依赖,这里直接声明,web就可以直接注入
    ref:引入你服务的类(service的实现类)-->
    <dubbo:service interface="com.ceh.back.service.TestService" ref="testServiceImpl"/>

The last line of configuration is to bind the service layer implementation class and interface together, put them in dubbo, and register, so that the service exists in dubbo.

5. Configure the web layer

@RestController
public class TestController {
    
    


    @Autowired
    private TestService testService;

    @RequestMapping("/getNow")
    public String getNow(){
    
    
        String now = testService.getNow();
        return now;
    }
}

When using @Autowired annotations, because the service and web are separated, this place cannot be injected, so dubbo is needed.

springmvc.xml file configuration

<!--配置dubbo-->
    <!--配置消费方名称(随便写,但唯一)-->
    <dubbo:application name="taotao-back-web"/>
    <!--广播服务存放地址-->
    <dubbo:registry address="multicast://224.5.6.7:1234"/>
    <!--消费
    id:对应web引入service服务的名称(autowired)
    interface:你在web层需要引入的接口
    这样dubbo就会把广播中对应的接口实现类给消费者-->
    <dubbo:reference id="testService"  interface="com.ceh.back.service.TestService"/>

Through the last line of configuration, assign the service that exists in dubbo to id. At this time, the id must be consistent with the service interface name introduced by the web layer. This is when the web receives the service layer object.

Problems encountered:

When configuring dubbo in the applicationContext-service.xml and springmvc.xml files, because there is no header file, an error will be reported. Idea can directly generate the corresponding header file, but if only xmlns:dubbo=”http://code.alibabatech. "com/schema/dubbo"
will appear "cannot find the declaration of dubbo: application" error, delete the above header file again, and regenerate it again to see if this will appear in the end http://code.alibabatech.com /schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd, the declaration file, the declaration is out and the error is resolved. (I don’t know why idea will not automatically generate the address below for the first time)

In the form of direct connection (usually used in testing and development environments)

Just need to modify the configuration file in the form of the above broadcast

applicationContext-service.xml

<!--配置dubbo:广播的形式(把所有的服务全部暴露在一个地方,需要使用服务就去这里找)-->
    <!--配置服务方名称(随便写,但是要保持唯一)-->
    <dubbo:application name="taotao-back-service"/>
    <!--dubbo的端口号-->
    <dubbo:registry address="N/A"/>
    <dubbo:protocol name="dubbo" port="20880"/>
    <!--注册服务
    interface:注册的接口,因为web层也引入了interface的依赖,这里直接声明,web就可以直接注入
    ref:引入你服务的类(service的实现类)-->
    <dubbo:service interface="com.ceh.back.service.TestService" ref="testServiceImpl"/>

springmvc.xml

<!--配置dubbo-->
    <!--配置消费方名称(随便写,但唯一)-->
    <dubbo:application name="taotao-back-web"/>
    <!--消费
    id:对应web引入service服务的名称(autowired)
    interface:你在web层需要引入的接口
    这样dubbo就会把广播中对应的接口实现类给消费者-->
    <dubbo:reference url="dubbo://127.0.0.1:20880" id="testService"  interface="com.ceh.back.service.TestService"/>

Guess you like

Origin blog.csdn.net/weixin_43431123/article/details/112133732
Recommended