Dubbo+Zookeeper

1.启动Zookeeper服务

zkServer.cmd

2.新建project->maven,删除src文件夹

3.新建module->maven->quickstart->common

module:common
编写:model实体类,service层接口
依赖:

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.2.3</version>
</dependency>

4.新建module->spring ini

module:provider
编写:dao层,service’层实现类
依赖:

<!--依赖的common模块-->
<dependency>
	<groupId>cn.yinjian</groupId>
	<artifactId>common</artifactId>
	<version>1.0-SNAPSHOT</version>
</dependency>
<!--这是微服dubbo的核心服务包-->
<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>dubbo</artifactId>
	<version>2.5.3</version>
	<exclusions>
		<exclusion>
			<artifactId>spring</artifactId>
			<groupId>org.springframework</groupId>
		</exclusion>
	</exclusions>
</dependency>
<!--这是zookeeper的服务操作包 但主要这里的版本最好和你的zookeeper版本一致-->
<dependency>
	<groupId>org.apache.zookeeper</groupId>
	<artifactId>zookeeper</artifactId>
	<version>3.4.6</version>
	<exclusions>
		<exclusion>
			<artifactId>slf4j-log4j12</artifactId>
			<groupId>org.slf4j</groupId>
		</exclusion>
	</exclusions>
</dependency>
<!--这里是zookeeper的zkCli的操作包 我们读写服务全靠这个包-->
<dependency>
	<groupId>com.github.sgroschupf</groupId>
	<artifactId>zkclient</artifactId>
	<version>0.1</version>
</dependency>

spring-provider.xml
main方法入口上添加注解:

  1. @MapperScan(“cn.yinjian.dao”)
  2. @ImportResource(“classpath:spring-provider.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">

    <!-- 1,指定当前服务/应用的名字 -->
    <dubbo:application name="myprovider" />

    <!-- 2.指定注册中心的位置,使用zookeeper注册中心暴露服务地址,我的zookeeper是架在本地的 -->
    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" timeout="60000"/>

    <!-- 3,指定通信协议&通信规则,用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />

    <!-- 4. 暴露服务,ref指向真正的实现类 -->
    <dubbo:service interface="cn.yinjian.service.EmployeeService" ref="employeeService"/>
    <!--服务的实现-->
    <bean id="employeeService" class="cn.yinjian.service.EmployeeServiceImpl"/>
</beans>


application.yml

server:
  port: 7070
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mycurd
    username: root
    password: abc123
mybatis:
  type-aliases-package: cn.yinjian.entity
  mapper-locations: mapper/*.xml
pagehelper:
  helper-dialect: mysql

5.新建module->spring ini->consumer

module:consumer
编写:Controller层
依赖:

<dependency>
	<groupId>cn.yinjian</groupId>
	<artifactId>common</artifactId>
	<version>1.0-SNAPSHOT</version>
</dependency>
<!--这是微服dubbo的核心服务包-->
<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>dubbo</artifactId>
	<version>2.5.3</version>
	<exclusions>
		<exclusion>
			<artifactId>spring</artifactId>
			<groupId>org.springframework</groupId>
		</exclusion>
	</exclusions>
</dependency>
<!--这是zookeeper的服务操作包 但主要这里的版本最好和你的zookeeper版本一致-->
<dependency>
	<groupId>org.apache.zookeeper</groupId>
	<artifactId>zookeeper</artifactId>
	<version>3.4.6</version>
	<exclusions>
		<exclusion>
			<artifactId>slf4j-log4j12</artifactId>
			<groupId>org.slf4j</groupId>
		</exclusion>
	</exclusions>
</dependency>
<!--这里是zookeeper的zkCli的操作包 我们读写服务全靠这个包-->
<dependency>
	<groupId>com.github.sgroschupf</groupId>
	<artifactId>zkclient</artifactId>
	<version>0.1</version>
</dependency>

spring-consumer.xml
main方法入口上添加注解:

  1. @ImportResource(“classpath:spring-provider.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">
    <!-- 1,指定当前服务/应用的名字 -->
    <dubbo:application name="dubbo-consumer"/>
    <!-- 2.指定注册中心的位置,使用zookeeper注册中心暴露服务地址,我的zookeeper是架在本地的 -->
    <dubbo:registry check="false" address="zookeeper://127.0.0.1:2181"/>
    <!-- 3.声明需要调用的远程服务接口,生成远程服务代理-->
    <dubbo:reference interface="cn.yinjian.service.EmployeeService" id="employeeService"/>
    <!-- <dubbo:reference> 标签的缺省值设置
         启动时检查  check=false
         timeout    默认是1000ms
         retries="" 重试次数,不包含第一次调用,0代表不重试,幂等(设置重试次数,如查询修改删除),非幂等(不设置重试次数,如新增)
    -->
    <dubbo:consumer check="false" timeout="3000"/>
</beans>

application.yml

server:
  port: 9090
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mycurd
    username: root
    password: abc123
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值