springboot+dubbo+zookeeper小实例(dubbo xml方式配置)

摘要 zookeeper下载、启动, 从官网下载zookeeper的bin包

属于 :API 标签: 发布于:2020-03-25 10:04:03

* springboot+mybatis的部分,参考本站文章 springboot+mybatis+dao+service

1.zookeeper下载、启动, 从官网下载zookeeper的bin包

解压, 或用git bash 执行 tar -zxvf xxx.tar.gz

将 conf中的zoo_sample.cfg复制或重命名为zoo.cfg

运行 bin目录下的 zkServer.cmd 看到输出启动成功的日志。

* 新版zookeeper占用8080端口,解决方法 在zoo.cfg加入一行 

admin.serverPort=8079

2.下在dubbo-admin好查看服务  从github下载 2.5.10,兼容jdk1.8

3.创建两个springboot项目,一个做提供者,一个做消费者

提供者

pom.xml

<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>

  <groupId>com.demo</groupId>
  <artifactId>dubbodemoclient</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>dubbodemoapi</name>
  <url>http://maven.apache.org</url>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.6.RELEASE</version>
	</parent>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <!-- dubbo depend -->
		<dependency>
			<groupId>org.apache.dubbo</groupId>
			<artifactId>dubbo</artifactId>
			<version>2.7.5</version>
		</dependency>
		<dependency>
			<groupId>org.apache.dubbo</groupId>
			<artifactId>dubbo-dependencies-zookeeper</artifactId>
			<version>2.7.5</version>
			<type>pom</type>
			<exclusions>
	            <exclusion>
	                <groupId>org.slf4j</groupId>
	                <artifactId>slf4j-log4j12</artifactId>
	            </exclusion>
	        </exclusions>
		</dependency>
		<!-- springboot -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
  </dependencies>
</project>

application.properties

server.port=8082
# 1.0
#server.context-path=/demo
# 2.0
server.servlet.context-path=/demo
spring.main.allow-bean-definition-overriding=true

dubbo-provider.xml

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    <dubbo:application name="demo1-provider"/>
    
    <dubbo:registry protocol="zookeeper"  address="127.0.0.1:2181"/>
    
    <dubbo:protocol name="dubbo" port="20890"/>
    
    <bean id="usersService" class="com.demo.service.impl.UsersServiceImpl"/>
    
    <dubbo:service interface="com.demo.service.UsersService" ref="usersService"/>
</beans>

启动类

package com.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;

@EnableAutoConfiguration
@ComponentScan(basePackages = {"com.demo"})
@ImportResource({"classpath:dubbo-provider.xml"})
public class App {
     
    public static void main(String[] args) throws Exception { 
        SpringApplication.run(App.class, args);
    }
}

service

package com.demo.service;

public interface UsersService {
	String getUser(int id);
}

service实现类

package com.demo.service.impl;

import org.springframework.stereotype.Service;

import com.demo.service.UsersService;

@Service
public class UsersServiceImpl implements UsersService{

	public String getUser(int id) {
		return "Hi, Jack"+id;
	}

}

启动,App的main方法运行。

在dubbo-admin界面的提供者看到 xxx:20890

消费者

pom.xml , application.properties 一样 ,修改port为8083即可

dubbo-consumer.xml

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    <dubbo:application name="demo1-consumer"/>
    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>
    <dubbo:reference id="usersService" check="false" interface="com.demo.service.UsersService"/>
</beans>

service复制过来,实践中应该将其提出为一个公共模块,然后引入。

package com.demo.service;

public interface UsersService {
	String getUser(int id);
}

Controller

package com.demo.dubbodemoapi;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.demo.service.UsersService;
 
 

@RestController
@RequestMapping("/api")
public class ApiController {
	@Autowired
	private UsersService usersService;
	@RequestMapping("/")
	public String index() {
		return usersService.getUser(23);
	}
}

启动类

package com.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;

@EnableAutoConfiguration
@ComponentScan(basePackages = {"com.demo"})
@ImportResource({"classpath:dubbo-consumer.xml"})
public class App {
     
    public static void main(String[] args) throws Exception { 
        SpringApplication.run(App.class, args);
    }
}

启动,访问 http://localhost:8083/demo/api/  看到输出 Hi, Jack23