Dubbo distributed framework combat

Dubbo distributed framework combat

Dubbo: Alibaba's open source distributed service framework in 2011.
Three main functions:
(1) Remote interface call: Dubbo provides RPC based on high-performance interface, which is transparent to users.
(2) Load balancing and fault tolerance: Dubbo supports multiple load balancing strategies out of the box, which can sense the status of downstream services to reduce overall delay and improve system throughput.
(3) Service registration and service discovery: Dubbo supports multiple service registration forms, which can detect online/offline services immediately.

Dubbo architecture:

Provider: the service provider that exposes the service
Consumer: the service consumer that calls the remote service
Registry: the registry for service registration and discovery
Monitor: the monitoring center that counts the number of service calls and calling time
Container: the service running container

Dubbo example (based on maven project):

1. Create 3 sub-packages in the root directory :
(1) dubbo-interfacce: general service interface
(2) dubbo-provider: rely on dubbo-interface, develop implementation classes, configure spring files, and publish services
(3) dubbo-consumer : Rely on dubbo-interface, configure spring configuration file, call service

2. Introduce common dependencies in the pom files of dubbo-provider and dubbo-consumer:

   <dependencies>
        <dependency>
            <groupId>com.qiuzelin</groupId>
            <artifactId>dubbo-interface</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!--引入dubbo依赖-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.8</version>
        </dependency>
        <!-- zookeeper客户端依赖 -->
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>
    </dependencies>

4.dubbo-interface:
Create interface:

package com.qiuzelin.service;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: qiuzelin
 * @Date: 2020/09/29/11:02
 * @Description:
 */
public interface IUserService {
    
    
    public String hello();
}

5. Dubbo-provider configuration:
(1) Create interface service implementation class:

package com.qiuzelin.service.impl;

import com.qiuzelin.service.IUserService;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: qiuzelin
 * @Date: 2020/09/29/11:07
 * @Description:
 */
public class UserServiceImpl implements IUserService {
    
    
    public String hello() {
    
    
        return "hello,dubbo!";
    }
}

(2) Create dubbo-provider.xml configuration file in resources

<!--1.定义应用的名称    -->
    <dubbo:application name="dubbo-provider"/>
<!--2.定义协议端口    -->
    <dubbo:protocol port="28800"/>
<!--3.定义注册中心地址    -->
    <dubbo:registry protocol="zookeeper" address="192.168.157.128:2181"/>
<!--4.定义服务提供者    -->
    <bean id="userService" class="com.qiuzelin.service.impl.UserServiceImpl"/>
    <dubbo:service interface="com.qiuzelin.service.IUserService" ref="userService"/>

(3) Create the Provider class (by starting the Spring container, publishing services):

package com.qiuzelin.service.impl;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: qiuzelin
 * @Date: 2020/09/29/15:19
 * @Description: 启动spring容器,自动发布服务
 */
public class Provider  {
    
    
    public static void main(String[] args) throws IOException {
    
    
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {
    
    "classpath:dubbo-provider.xml"});
        context.start();
        System.out.println("Provider started!");
        System.in.read(); // 按任意键退出
    }
}

Run the main method test of the Provider class:
Insert picture description here

5.dubbo-consumer configuration:
(1) Create dubbo-consumer.xml configuration file in resources:

<!--1.定义应用的名称    -->
    <dubbo:application name="dubbo-consumer"/>
    <!--2.定义注册中心地址    -->
    <dubbo:registry protocol="zookeeper" address="192.168.157.128:2181"/>
    <!--3.引用服务    -->
    <dubbo:reference interface="com.qiuzelin.service.IUserService" id="userService"/>

(2) Create a Consumer class and call remote services

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: qiuzelin
 * @Date: 2020/09/29/15:38
 * @Description:
 */
public class Consumer {
    
    
    public static void main(String[] args) throws IOException {
    
    
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {
    
    "classpath:dubbo-consumer.xml"});
        context.start();
       //获取远程服务
        IUserService userService=context.getBean(IUserService.class);
        System.out.println("获取远程服务端反馈信息"+userService.hello());
    }
}

Run the main method test:
Insert picture description here

Note: When testing Consumer, the Provider process cannot be terminated, otherwise an error will be reported.

Enter the server address and tomcat port number to view:
Insert picture description here
there are providers and consumers in the application process.

`

Guess you like

Origin blog.csdn.net/weixin_41570890/article/details/108871861