Build dubbo registration center (zookeeper) and dubbo monitoring center combined with spring

Construction of dubbo registration center and monitoring center

Construction of dubbo registration center (zookeeper)

Download link: https://zookeeper.apache.org/releases.html
Note: Download the version with bin when downloading

  1. Unzip
    attention, attention, attention! ! ! ! You can't decompress it directly under the window, otherwise you can't delete it, but you can save the country in a curve. Put the downloaded installation package into Linux, unzip it under Linux, and put it back in the window

Because all kinds of software were installed in Linux before, the memory of Linux virtual machine is not enough. (Well, in fact, the Linux command is too troublesome, and it is comfortable to use without a window visualization window).
The directory decompressed to the window is as follows:
Insert picture description here
Insert picture description here
when starting zkServer.cmd. In a flash, you need to start from cmd.
Switch to the installation directory of your apache-zookeeper-3.5.5-bin and start zkServer.cmd. You can see that the error message is that there is no /conf/zoo.cfg
Insert picture description here2. Under conf, copy and rename it.
Insert picture description here
As shown below:
Insert picture description here
3. Add disk d in front of the path. Save and exit Now
Insert picture description here
starting zkServer.cmd. Start successfully
Insert picture description here

dubbo monitoring center installation

Find a dubbo-admin project on the Internet, copy it, and package it into a jar package in your own idea

Insert picture description here
Note: Check the registered address of zookeepr and whether it is a local registered address in the configuration file. If it is not, errors will still occur later.
Insert picture description here
Note: First use the doc command to start zookeeper, and then use the doc command to start the management center.

java -jar where you typed as jar

There is a packaged dubbo-admin-0.0.1-SNAPSHOT.jar in the resource
Insert picture description here

After startup, enter http:localhost:7001 in the browser to see if you can access the dubbo management center.
Insert picture description here
If you can access it, it means that the management center is installed.

dubbo combined with spring

Create a maven project.
Configuration in pom.xml under mavaen project

	<properties>
        <project.build.sourceEncodiing>UTF-8</project.build.sourceEncodiing>
        <maven.compile.source>1.8</maven.compile.source>
        <maven.compile.target>1.8</maven.compile.target>
        <dubbo.version>2.7.3</dubbo.version>
    </properties>

Built by dubbo service provider

Create a module subproject
pom.xml under the maven project

 <properties>
        <springframework.version>4.3.11.RELEASE</springframework.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.7</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.6</version>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.3</version>
        </dependency>
        <dependency>
            <groupId>com.itzz</groupId>
            <artifactId>dubbo01-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

Configure in applicationContext.xml after importing the jar package

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://code.alibabatech.com/schema/dubbo
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!--使用zookeeper注册中心暴露当前地址-->

    <dubbo:registry address="zookeeper://localhost:2181"></dubbo:registry>

    <!-- 用dubbo协议在20880端口暴露服务 当前应用通过20880暴露服务-->
    <dubbo:protocol name="dubbo" port="20880" />

    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="dubbo01-provider" />

    <!--定义实现类的bean-->
    <bean id="userService" class="com.itzz.provider.service.Impl.UserServiceImpl"></bean>

    <!--声明暴露的接口-->
    <dubbo:service interface="com.itzz.provider.service.UserService"  ref="userService" version="1.0.0"></dubbo:service>
</beans>

In the main class

public static void main(String[] args) throws IOException {
    
    
        ClassPathXmlApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml ");
        context.start();
        System.in.read();
    }

Create a method in the implementation class of the service layer.

public class UserServiceImpl implements UserService {
    
    
    public void sayHello() {
    
    
        System.out.println("你好,我是提供者。---1");
    }

    public User findUser() {
    
    
        User user = new User();
        user.setId(1);
        user.setUsername("张三");
        user.setAddress("北京");
        return user;
    }
}

Create an interface api module

such as:

public interface UserService {
    
    
    void sayHello();
    User findUser();
}

pojo
creates a User.java

public class User implements Serializable {
    
    
    private int id;
    private String username;
    private String address;
    //setter getter
}

Create a sub-consumer module

pom.xml

    <properties>
        <springframework.version>4.3.11.RELEASE</springframework.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.7</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.6</version>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.3</version>
        </dependency>
        <dependency>
            <groupId>com.itzz</groupId>
            <artifactId>dubbo01-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://code.alibabatech.com/schema/dubbo
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!--使用zookeeper注册中心暴露当前地址-->

    <dubbo:registry protocol="zookeeper" address="localhost:2181"></dubbo:registry>
    
    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="dubbo01-consumer" />
    
    <!--定义参照接口-->
    <dubbo:reference interface="com.itzz.provider.service.UserService"  version="1.0.0" id="userService" check="false"></dubbo:reference>


</beans>

Consumer main category

package com.itzz.provider;


import com.itzz.provider.pojo.User;
import com.itzz.provider.service.UserService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;


public class ConsumerMain {
    
    
    public static void main(String[] args) throws IOException {
    
    
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //启动容器
        context.start();
        //获取bean
        UserService userService = context.getBean(UserService.class);
        userService.sayHello();

        //
        User user = userService.findUser();
        System.out.println(user.toString());

        System.in.read();
    }

}

test

Run, can you see registered consumers and providers in the control center of dubbo

Insert picture description here
Insert picture description here
This shows that dubbo combined with spring configuration is successful. You can also create multiple consumers and multiple providers. (You only need to change the configuration of the configuration file, such as the provider's name and port, and the consumer's name) is to copy the module to the project, and then change and add the module structure.
Insert picture description here
You can have multiple providers. That is, the cluster implements the same functions, but there are two to provide services to consumers.

Guess you like

Origin blog.csdn.net/qq_39095899/article/details/107323323