dubbo,zookeeper
1. 准备环境
(1)配置并启动zookeeper
|
1.解压zookeeper,修改conf下的配置文件名称为zoo.cfg 2.修改配置文件中的数据存储(日志)位置dataDir=D:\zookeeperdata之后直接运行 3.启动注册中心zkServer.cmd |
(2)dubbo控制台
|
dubbo控制台访问地址: |
2.定义
|
RPC(Remote Procedure Call)远程过程调用,简单的理解是一个节点请求另一个节点提供的服务
|
3.创建工程及目录
|
common--------------maven工程---------------quickstart provider---------------springboot工程 consumer---------------springboot工程 |
4.common
(1) entity包
|
public class City implements Serializable { private Integer id; private String cityName;
@Override public String toString() { return "City{" + "id=" + id + ", cityName='" + cityName + '\'' + '}'; }
public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
public String getCityName() { return cityName; }
public void setCityName(String cityName) { this.cityName = cityName; }
public City() { }
public City(Integer id, String cityName) { this.id = id; this.cityName = cityName; } } |
(2) service包(service接口)
|
public interface FlightService { public List<City> getAllCity(); } |
5.provider
(1) pom.xml
|
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.3</version> </dependency>
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.7.5</version> </dependency> <!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient --> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version>
</dependency>
<dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>4.2.0</version> <!--<exclusions> <exclusion> <artifactId>zookeeper</artifactId> <groupId>org.apache.zookeeper</groupId> </exclusion> </exclusions>--> </dependency>
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.13</version> </dependency>
<dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.9</version> <exclusions> <exclusion> <artifactId>slf4j-log4j12</artifactId> <groupId>org.slf4j</groupId> </exclusion> </exclusions> </dependency>
<dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>4.2.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>cn.kgc</groupId> <artifactId>common</artifactId> <version>1.0-SNAPSHOT</version> <scope>compile</scope> </dependency> </dependencies> |
(2) yml
|
spring: datasource: url: jdbc:mysql:///kgc?serverTimezone=Asia/Shanghai username: root password: ok driver-class-name: com.mysql.jdbc.Driver jackson: date-format: yyyy-MM-dd HH:mm:ss time-zone: GMT+8 mybatis: configuration: call-setters-on-nulls: true log-impl: org.apache.ibatis.logging.stdout.StdOutImpl mapper-locations: classpath:mapper/*.xml dubbo: application: name: provider[1] registry: address: zookeeper://127.0.0.1:2181[2] scan: base-packages[3] : cn.kgc.service protocol: name: dubbo port: 21881[4] server: port: 8083 |
(3) mapper包
|
@Mapper public interface FlightMapper { @Select("select * from city") public List<City> getAllCity(); } |
(4) service包(serviceImpl实现类)
import org.apache.dubbo.config.annotation.Service;
|
@Service[5] @Component @Transactional public class FlightServiceImpl implements FlightService { @Resource private FlightMapper flightMapper;
@Override public List<City> getAllCity() { return flightMapper.getAllCity(); } } |
(5)resources/mapper包(XML)
|
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="cn.kgc.mapper.BookMapper"> <select id="findAll" resultType="map" > select * from book </select> </mapper> |
6.consumer
(1) pom.xml
|
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.3</version> </dependency>
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.13</version> </dependency>
<dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.7.5</version> </dependency> <!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient --> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version>
</dependency>
<dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>4.2.0</version> <!--<exclusions> <exclusion> <artifactId>zookeeper</artifactId> <groupId>org.apache.zookeeper</groupId> </exclusion> </exclusions>--> </dependency>
<dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.9</version> <exclusions> <exclusion> <artifactId>slf4j-log4j12</artifactId> <groupId>org.slf4j</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka</artifactId> </dependency>
<dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>4.2.0</version> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>cn.kgc</groupId> <artifactId>common</artifactId> <version>1.0-SNAPSHOT</version> <scope>compile</scope> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> |
(2) yml
|
dubbo: application: name: consumer registry: address: zookeeper://127.0.0.1:2181 mybatis: configuration: call-setters-on-nulls: true log-impl: org.apache.ibatis.logging.stdout.StdOutImpl server: port: 8081 |
(3) controller包
|
@RestController public class FlightController { @Reference private FlightService service; @RequestMapping("/getAllCity") public List<City> getAllCity(){ return service.getAllCity(); } } |
(4) static包
|
引入js包,引入jquery类库 |
|
findAll.html |
|
· |
(5) 启动类
|
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class) |
注册到zk中的名称
zk的地址
扫描哪些包中的哪些业务暴露出去(dubbo的@Service注解对应包的位置)
提供者暴露服务的端口号
import org.apache.dubbo.config.annotation.Service;

浙公网安备 33010602011771号