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

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

属于 :API 标签: 发布于:2020-03-25 15:15:49

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

* xml方式配置参考本站文章 springboot+dubbo+zookeeper小实例(dubbo xml方式配置)

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

application.yml

dubbo:
  application:
    name: duboo-provider1
  # 指定注册协议和注册地址  dubbo推荐使用zookeeper作为注册中心,并且在start依赖中引入了zookeeper的java客户端Curator
  registry:
    protocol: zookeeper
    address: 127.0.0.1:2181
  protocol:
     name: dubbo
     port: 20800

启动类

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"})
@EnableDubbo
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实现类

* 代码里面的注解是dubbo的service注解。

package com.demo.service.impl;


import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;

import com.demo.service.UsersService;

@Service(timeout = 5000,version="1.0.0",interfaceClass = UsersService.class)
@Component
public class UsersServiceImpl implements UsersService{

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

}

启动,App的main方法运行。

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

消费者

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

application.yml

dubbo:
  application:
    name: duboo-consumer1
  # 指定注册协议和注册地址  dubbo推荐使用zookeeper作为注册中心,并且在start依赖中引入了zookeeper的java客户端Curator
  registry:
    protocol: zookeeper
    address: 127.0.0.1:2181
  protocol:
     name: dubbo
     port: 20800
  # 关闭所有服务的启动时检查 (没有提供者时报错)视实际情况设置
  consumer:
    check: false

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

package com.demo.service;

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

Controller

package com.demo.dubbodemoapi;

import org.apache.dubbo.config.annotation.Reference;
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 {
	@Reference(version = "1.0.0")
	private UsersService usersService;
	
	@RequestMapping("/")
	public String index() {
		return usersService.getUser(23);
	}
}

启动类

package com.demo;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;

@EnableAutoConfiguration
@ComponentScan(basePackages = {"com.demo"})
@EnableDubbo
public class App {
     
    public static void main(String[] args) throws Exception { 
        SpringApplication.run(App.class, args);
    }
}

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