springmvc + Dubbo + zookeeper distributed services

springmvc + Dubbo + zookeeper distributed services

   点关注不迷路,欢迎再访!		

Here Insert Picture Description

The introduction of dependence

<dependency>
	<groupId>org.apache.zookeeper</groupId>
	<artifactId>zookeeper</artifactId>
	<version>3.4.9</version>
</dependency>
<!-- dubbo -->
<dependency>
	<groupId>org.apache.dubbo</groupId>
	<artifactId>dubbo</artifactId>
	<version>2.7.3</version>
</dependency>
<dependency>
	<groupId>com.101tec</groupId>
	<artifactId>zkclient</artifactId>
	<version>0.10</version>
</dependency>
<dependency>
	<groupId>org.javassist</groupId>
	<artifactId>javassist</artifactId>
	<version>3.24.1-GA</version>
</dependency>
<dependency>
	<groupId>org.apache.curator</groupId>
	<artifactId>curator-framework</artifactId>
	<version>2.8.0</version>
</dependency>
<dependency>
	<groupId>org.apache.curator</groupId>
	<artifactId>curator-recipes</artifactId>
	<version>2.8.0</version>
</dependency>
<dependency>
	<groupId>org.apache.curator</groupId>
	<artifactId>curator-client</artifactId>
	<version>2.8.0</version>
</dependency>
<dependency>
	<groupId>io.netty</groupId>
	<artifactId>netty-all</artifactId>
	<version>4.0.35.Final</version>
</dependency>

Provider (the consumer)

<?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-admin 或 dubbo-monitor 会显示这个名字,方便识别 -->
	<dubbo:application name="admin-provider" owner="admin" organization="dubbox"/>
	<!-- 使用zookeeper 注册中心暴露服务,注意要先开启 zookeeper -->
	<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
	<!-- 用dubbo协议在20880端口暴露服务 ,端口唯一性,否则会提示被占用 -->
	<dubbo:protocol name="dubbo" port="20880"/>
	<!-- 用dubbo 协议实现定义好的 api 接口 -->
	<dubbo:service interface="com.sun.dubbo.service.IUserService" ref="userService" protocol="dubbo"/>
	<!-- 具体实现接口的bean -->
	<bean id="userService" class="com.sun.dubbo.service.UserServiceImpl"/>	
</beans>

API interface needs to be exposed

/**
 * dubbo 接口
 * @author Andy
 * @date 2019年12月29日
 */
public interface IUserService {
	
	public User getUser();

}

Interface concrete realization

/**
 * @Description: 服务提供者接口的实现
 * @author Andy
 * @date 2019年12月29日
 */
public class UserServiceImpl implements IUserService{
	public User getUser() {
		User user = new User();
		user.setName("andy");
		user.setAge(25);
		user.setJob("JAVA软件开发");
		user.setSex("男");
		return user;
	}
}

Producers

<?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-admin 或 dubbo-monitor 会显示这个名字,方便识别 -->
	<dubbo:application name="admin-consumer" owner="demo" organization="dubbox"/>
	<!-- 向zookeeper 订阅provider 的地址,由zookeeper 定时推送 -->
	<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
	<!-- 使用dubbo 协议调用定义好的 api 接口 -->
	<dubbo:reference id="userService" interface="com.sun.dubbo.service.IUserService"/>	
</beans>

API test class

public class Test {

	public static void main(String[] args) {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
	    context.start();
	    IUserService demoService = context.getBean(IUserService.class);

	    System.out.println(demoService.getUser().toString());
	}
}

Testing phase

1. Start the service provider
Here Insert Picture Description
access to dubbo-admin console will find that service provider interface registration success!

2. Run the test class customer service
Here Insert Picture Description
observed that consumers successful consumer API, partners will find attentive, and did not get to the method name and parameters in the metadata below, according to
a presentation must be dubbo2.7 version above + config center configuration, then we go to the official website to see!

Metadata Configuration

Here Insert Picture Description
I believe many people see here a little confused, do not know what refers registry and configuration center, do not worry. After I found some check it refers to is actually a zookeeper.

Registry configuration and metadata into the center of the center:

Under the new configuration management dubbo configuration, named global representation globally.
Here Insert Picture Description
After entering the console again found Metadata can already get to the API method name and parameter
Here Insert Picture Description
test automation API
Here Insert Picture Description
in this dubbo service exposure and test work has been completed, the need to know more dubbo content can i focus on my progress together!

Published 101 original articles · won praise 33 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_39443053/article/details/103810533