Springboot+zookeeper+dubbo realizes service registration and discovery

Springboot+zookeeper+dubbo realizes service registration and discovery

First, let's look at the project structure diagram

Producer module structure diagram

pom

<?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 http://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.1.2.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.waf</groupId>
	<artifactId>springboot-dubbo-producer</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>springboot-dubbo-producer</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<version>2.5.3</version>
			<exclusions>
				<exclusion>
					<groupId>org.springframework</groupId>
					<artifactId>spring</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
			<version>3.4.6</version>
			<exclusions>
				<exclusion>
					<groupId>org.slf4j</groupId>
					<artifactId>slf4j-log4j12</artifactId>
				</exclusion>
				<exclusion>
					<groupId>log4j</groupId>
					<artifactId>log4j</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>com.github.sgroschupf</groupId>
			<artifactId>zkclient</artifactId>
			<version>0.1</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-autoconfigure</artifactId>
			<version>2.1.2.RELEASE</version>
		</dependency>
	</dependencies>


	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

 

application.properties configure a port number server.port=8888

producer.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 ">
    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
    <dubbo:application name="dubbo-provider" />
    <!-- 使用zookeeper作为注册中心 -->
    <dubbo:registry  protocol="zookeeper" address="zookeeper://192.168.0.0:2181" />
    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <bean id="producerService" class="com.waf.producer.service.impl.DubboProducerServiceImpl"></bean>
    <dubbo:service interface="com.waf.producer.service.DubboProducerService" ref="producerService"></dubbo:service>
</beans>

Interface and implementation
 

package com.waf.producer.service;

public interface DubboProducerService {

    String queryProducerInfo();
}



package com.waf.producer.service.impl;

import com.waf.producer.service.DubboProducerService;
import org.springframework.stereotype.Service;

@Service
public class DubboProducerServiceImpl implements DubboProducerService {
    @Override
    public String queryProducerInfo() {
        return "获取生产者的接口数据";
    }
}

 

Last start class
 

package com.waf.producer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@ImportResource("classpath:producer.xml")
@SpringBootApplication
public class SpringbootDubboProducerApplication {

	public static void main(String[] args) {

		SpringApplication.run(SpringbootDubboProducerApplication.class, args);
		System.out.println("启动服务");
	}

}

 

Log in to dubbo-admin and you can see that there are instances of our publishing interface in the provider

You can see that it has been successfully released, and there is also this node information in zookeeper (ephemeralOwner = ......... is a temporary node ephemeralOwner = 0x0 persistent node) If the client disconnects the session, the life cycle of this temporary node will be invalid

Let's look at consumer
pom must introduce producer jar package

<?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 http://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.1.2.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.waf</groupId>
	<artifactId>springboot-dubbo-consumer</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>springboot-dubbo-consumer</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

        <!-- 引入生产者jar 包-->
		<dependency>
			<groupId>com.waf</groupId>
			<artifactId>springboot-dubbo-producer</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>

		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<version>2.5.3</version>
			<exclusions>
				<exclusion>
					<groupId>org.springframework</groupId>
					<artifactId>spring</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
			<version>3.4.6</version>
			<exclusions>
				<exclusion>
					<groupId>org.slf4j</groupId>
					<artifactId>slf4j-log4j12</artifactId>
				</exclusion>
				<exclusion>
					<groupId>log4j</groupId>
					<artifactId>log4j</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>com.github.sgroschupf</groupId>
			<artifactId>zkclient</artifactId>
			<version>0.1</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

This is the structure diagram

 

consumer.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 ">
    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
    <dubbo:application name="dubbo-consumer" />
    <!-- 使用zookeeper作为注册中心 -->
    <dubbo:registry  protocol="zookeeper" address="zookeeper://192.168.0.0:2181" />
    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <dubbo:reference id="producerService" interface="com.waf.producer.service.DubboProducerService"></dubbo:reference>
</beans>

 

Start class
 

package com.waf.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@ImportResource("classpath:consumer.xml")
@SpringBootApplication
public class SpringbootDubboConsumerApplication {

	public static void main(String[] args) {

		SpringApplication.run(SpringbootDubboConsumerApplication.class, args);
		System.out.println("注册消费者");
	}

}

 

Interface and implementation class. The server layer calls the producer service
 

package com.waf.consumer.service;

public interface DubboConsumerService {

    String queryConsumerInfo();
}




package com.waf.consumer.service.impl;

import com.waf.consumer.service.DubboConsumerService;
import com.waf.producer.service.DubboProducerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DubboConsumerServiceImpl implements DubboConsumerService {

    //此处调用生产者暴露服务的接口
    @Autowired
    DubboProducerService producerService;

    @Override
    public String queryConsumerInfo() {
        return producerService.queryProducerInfo();
    }
}


Control layer access case
 

package com.waf.consumer.controller;

import com.waf.consumer.service.DubboConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/dubbo-server")
public class ConsumerController {

    @Autowired
    private DubboConsumerService consumerService;

    @RequestMapping("/test")
    public String queryInfo(){
        return consumerService.queryConsumerInfo();
    }
}

 

After starting the consumer and visiting localhost:8080/dubbo-server/test, this sentence will definitely appear

 

Take a look at the dubbo-admin console

 

The next article introduces the difference between eureka+feign and zk+dubbo

Guess you like

Origin blog.csdn.net/qq_38108719/article/details/91362073