赞
踩
二、准备dubbo-admin(管理控制台)和zookeeper(注册中心)
2.下载安装dubbo-admin(dubbo的管理控制台)
Apache Dubbo 是一款微服务开发框架,它提供了 RPC通信与微服务治理两大关键能力。这意味着,使用 Dubbo 开发的微服务,将具备相互之间的远程发现与通信能力,同时利用 Dubbo 提供的丰富服务治理能力,可以实现诸如服务发现、负载均衡、流量调度等服务治理诉求。同时Dubbo 是高度可扩展的,用户几乎可以在任意功能点去定制自己的实现,以改变框架的默认行为来满足自己的业务需求。
zookeeper官网:https://zookeeper.apache.org/
下载安装后复制conf目录下的zoo_sample.cfg文件,改名为zoo.cfg,并修改配置
默认8080端口启动,这里添加admin.serverPort=9999将启动端口改成9999,防止端口占用
然后打开bin目录下的zkServer.cmd,启动zookeeper服务端
启动成功页面
进入官网导航的GitHub,下滑找到Dubbo-admin分支并进入
直接下载zip安装包
安装后进入dubbo-admin-master目录找到dubbo-admin-server
将dubbo-admin-server打成jar包,两种方式选择
(1)maven命令打包:cmd窗口切换到根目录用mvn clean package+项目名打包
(2)IDEA中maven打包:用IDEA打开dubbo-admin-server项目,用maven插件package打包
打好jar包后,推荐复制放在dubbo、zookeeper根目录下
用cmd命令行切换到当前目录,使用java -jar+包名 运行
如果没有报错的话,此时dubbo-admin的服务端已经启动了,由于dubbo的管理控制台是个前后端分离的项目,我们还需要让前端服务也跑起来:
进入到dubbo-admin-master目录找到dubbo-ui
打开命令行切换到当前目录,先执行下npm install命令,然后用npm run serve运行项目
此时前端也已经跑起来了,我们可以直接用前端的url http://localhost:8082/进入控制台
默认账号密码都是root,在dubbo-admin-master的配置文件application.properties中
application.properties配置文件
登录成功后的页面
分布式微服务主要是将应用的各项功能拆分成不同的模块,(根据不同的功能模块划分成不同的项目)发布在不同的服务器上,然后将需要向外提供的服务api做成分包,模块之间的相互调用通过分包api完成,dubbo的作用在于我们只需要将服务提供者的接口暴露给dubbo托管,服务消费者只要向registry注册中心注册并指定需要调用的服务,即可完成服务间调用,无需关心服务提供者的内部结构是怎样的,用户也感知不到调用了任何外部服务。
项目工程结构
整体思路
这里主要模拟实现用户和会话的功能,用户模块主要负责对用户信息的管理,而一个用户则可以对于多个聊天会话,现在的需求是需要通过id查询用户的信息,并查询该用户所有的会话。在此实例中,则用户服务(user-service)是服务消费者(consumer),会话服务是服务提供者(provider)。
(1)创建一个空项目(主要作为容器)
(2)创建分包api(用于整合服务提供者和消费者)
创建一个maven的quickstart模板项目即可
这里命名为data-api
导入依赖
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.11</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <version>1.18.22</version>
- </dependency>
- </dependencies>
将功能所需要的实体和接口都定义在这个项目里,后面服务的提供者和消费者只需要关注具体实现即可
创建实体类
- package com.api.entity;
-
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import lombok.NoArgsConstructor;
-
- import java.io.Serializable;
- import java.util.List;
-
- @Data
- @AllArgsConstructor
- @NoArgsConstructor
- public class User implements Serializable {
-
- private long user_id;
- private String username;
- private String password;
- private Boolean online_status;
- private String create_time;
- private Integer user_status;
- private List<Chat> chatList;
- }
- package com.api.entity;
-
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import lombok.NoArgsConstructor;
-
- import java.io.Serializable;
- import java.util.List;
-
- @Data
- @AllArgsConstructor
- @NoArgsConstructor
- public class Chat implements Serializable {
- private long chat_id;
- private String chat_status;
- private String chat_name;
- private Integer chat_type;
- private String create_time;
- private long creator_id;
- private long to_id;
- private Boolean is_deleted;
- private String last_send_time;
- private String last_message;
-
- }
对应的数据表
定义接口
- package com.api.service;
-
- import com.api.entity.Chat;
-
- import java.util.List;
-
- public interface ChatService {
- List<Chat> findChatByUser(long user_id);
-
- }
- package com.api.service;
-
- import com.api.entity.User;
-
- public interface UserService {
- User findUserById(long user_id);
- User findInfoById(long user_id);
- }
整体结构
(3)创建会话服务项目(提供者)
在空项目中创建module
选择springboot的模板即可
项目配置
依赖选择,我这里选了lombok和web
项目命名,我这里命名为chat-service
创建成功后,导入依赖(注意记得把刚才创建的分包api maven依赖导进来)
注意:这里dubbo和zookeeper的版本一定要匹配,不然整合了跑不起来噢
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <optional>true</optional>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- <!-- 数据库-->
- <dependency>
- <groupId>org.mybatis.spring.boot</groupId>
- <artifactId>mybatis-spring-boot-starter</artifactId>
- <version>2.2.2</version>
- </dependency>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>8.0.20</version>
- </dependency>
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid</artifactId>
- <version>1.2.1</version>
- </dependency>
- <dependency>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- <version>1.2.17</version>
- </dependency>
-
- <!-- Dubbo Spring Boot Starter -->
- <dependency>
- <groupId>org.apache.dubbo</groupId>
- <artifactId>dubbo-spring-boot-starter</artifactId>
- <version>2.7.8</version>
- </dependency>
- <dependency><!--zookerper版本一定要匹配! -->
- <groupId>org.apache.dubbo</groupId>
- <artifactId>dubbo-registry-zookeeper</artifactId>
- <version>2.7.8</version>
- </dependency>
- <!--这个是分包api的maven依赖-->
- <dependency>
- <groupId>org.example</groupId>
- <artifactId>data-api</artifactId>
- <version>1.0-SNAPSHOT</version>
- </dependency>
- </dependencies>
配置durid连接池
- package com.chat.config;
-
- import com.alibaba.druid.pool.DruidDataSource;
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
-
- import javax.sql.DataSource;
-
- @Configuration
- public class DruidConfig {
-
- /*
- 将自定义的 Druid数据源添加到容器中,不再让 Spring Boot 自动创建
- 绑定全局配置文件中的 druid 数据源属性到 com.alibaba.druid.pool.DruidDataSource从而让它们生效
- @ConfigurationProperties(prefix = "spring.datasource"):作用就是将 全局配置文件中
- 前缀为 spring.datasource的属性值注入到 com.alibaba.druid.pool.DruidDataSource 的同名参数中
- */
- @ConfigurationProperties(prefix = "spring.datasource")
- @Bean
- public DataSource druidDataSource() {
- return new DruidDataSource();
- }
-
- }
创建mapper接口
- package com.chat.mapper;
-
- import com.api.entity.Chat;
- import org.apache.ibatis.annotations.Mapper;
- import org.apache.ibatis.annotations.Param;
- import org.springframework.stereotype.Repository;
-
- import java.util.List;
-
- @Mapper
- @Repository
- public interface ChatMapper {
- List<Chat> findChatByUser(@Param("user_id") long user_id);
- }
在resource目录下创建MyBatis的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="com.chat.mapper.ChatMapper">
- <select id="findChatByUser" resultType="Chat">
- SELECT * FROM chat WHERE creator_id=#{user_id}
- UNION SELECT * FROM chat WHERE to_id=#{user_id}
- </select>
- </mapper>
创建ChatService实现类
这里dubbo主要的注解是@DubboService 用于将服务暴露给dubbo
- package com.chat.service;
-
- import com.api.entity.Chat;
- import com.api.service.ChatService;
- import com.chat.mapper.ChatMapper;
- import org.apache.dubbo.config.annotation.DubboService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- import java.util.List;
-
- @Service
- @DubboService//将服务暴露给dubbo
- public class ChatServiceImpl implements ChatService {
- @Autowired
- private ChatMapper chatMapper;
-
- @Override
- public List<Chat> findChatByUser(long user_id) {
- return chatMapper.findChatByUser(user_id);
- }
- }
配置启动类,一定要加上@EnableDubbo注解,用于将dubbo相关的配置bean加载到spring容器
- package com.chat;
-
- import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
- import org.mybatis.spring.annotation.MapperScan;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
-
- @SpringBootApplication
- @MapperScan("com.chat.mapper")
- @EnableDubbo//打开dubbo
- public class DemoApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(DemoApplication.class, args);
- }
-
- }
修改yml配置文件
- spring:
- datasource:
- username: root
- password: 123456
- url: jdbc:mysql://localhost:3306/spring_im?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
- driver-class-name: com.mysql.cj.jdbc.Driver
- type: com.alibaba.druid.pool.DruidDataSource
- initialSize: 5
- minIdle: 5
- maxActive: 20
- maxWait: 60000
- timeBetweenEvictionRunsMillis: 60000
- minEvictableIdleTimeMillis: 300000
- validationQuery: SELECT 1 FROM DUAL
- testWhileIdle: true
- testOnBorrow: false
- testOnReturn: false
- poolPreparedStatements: true
- filters: stat,wall,log4j
- maxPoolPreparedStatementPerConnectionSize: 20
- useGlobalDataSourceStat: true
- connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
- server:
- port: 8882
- mybatis:
- type-aliases-package: com.api.entity
- mapper-locations: mappers/*.xml
- dubbo:
- application:
- name: chat-service
- registry:
- protocol: zookeeper
- address: 127.0.0.1:2181
- protocol:
- name: dubbo
- port: 20884
会话服务项目结构
(4)创建用户服务项目(消费者)
创建项目的过程同上,创建成功后,先导依赖,再配置durid数据连接池(同上)
创建mapper接口
- package com.user.mapper;
-
- import com.api.entity.User;
- import org.apache.ibatis.annotations.Mapper;
- import org.apache.ibatis.annotations.Param;
- import org.springframework.stereotype.Repository;
-
- @Mapper
- @Repository
- public interface UserMapper {
- User findUserById(@Param("user_id")long user_id);
- }
在resource目录下创建MyBatis的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="com.user.mapper.UserMapper">
- <select id="findUserById" resultType="User">
- SELECT * FROM user WHERE user_id=#{user_id}
- </select>
-
- </mapper>
创建UserService实现类,主要的注解是@DubboReference,用于指定需要调用(依赖)的服务
- package com.user.service;
-
- import com.api.entity.User;
- import com.api.service.ChatService;
- import com.api.service.UserService;
- import com.user.mapper.UserMapper;
- import org.apache.dubbo.config.annotation.DubboReference;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- @Service
- public class UserServiceImpl implements UserService {
- @Autowired
- private UserMapper userMapper;
- @DubboReference
- private ChatService chatService;
- @Override
- public User findUserById(long user_id) {
- return userMapper.findUserById(user_id);
- }
-
- @Override
- public User findInfoById(long user_id) {
- User user=userMapper.findUserById(user_id);
- user.setChatList(chatService.findChatByUser(user_id));
- return user;
- }
- }
创建UserController,用于测试服务
- package com.user.controller;
-
- import com.alibaba.fastjson.JSONObject;
- import com.api.service.UserService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- @RequestMapping("/user")
- @RestController
- public class UserController {
- @Autowired
- private UserService userService;
- @RequestMapping("/queryUser")
- public String queryUser(long user_id){
- return JSONObject.toJSONString(userService.findUserById(user_id));
- }
- @RequestMapping("/queryUserChat")
- public String queryUserChat(long user_id){
- return JSONObject.toJSONString(userService.findInfoById(user_id));
- }
- }
配置启动类
- package com.user;
-
- import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
- import org.mybatis.spring.annotation.MapperScan;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
-
- @SpringBootApplication
- @MapperScan("com.user.mapper")
- @EnableDubbo
- public class DemoApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(DemoApplication.class, args);
- }
-
- }
配置yml文件
- spring:
- datasource:
- username: root
- password: 123456
- url: jdbc:mysql://localhost:3306/spring_im?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
- driver-class-name: com.mysql.cj.jdbc.Driver
- type: com.alibaba.druid.pool.DruidDataSource
- initialSize: 5
- minIdle: 5
- maxActive: 20
- maxWait: 60000
- timeBetweenEvictionRunsMillis: 60000
- minEvictableIdleTimeMillis: 300000
- validationQuery: SELECT 1 FROM DUAL
- testWhileIdle: true
- testOnBorrow: false
- testOnReturn: false
- poolPreparedStatements: true
- filters: stat,wall,log4j
- maxPoolPreparedStatementPerConnectionSize: 20
- useGlobalDataSourceStat: true
- connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
- server:
- port: 8882
- mybatis:
- type-aliases-package: com.api.entity
- mapper-locations: mappers/*.xml
- dubbo:
- application:
- name: user-service
- registry:
- protocol: zookeeper
- address: 127.0.0.1:2181
- protocol:
- name: dubbo
- port: 20885
user-service项目结构
至此,所有准备工作搭建完成,启动两个项目即可(建议先启动服务提供者,再启动消费者)
启动chat-service(前提:zookeeper服务端已经运行,建议dubbo-admin前后端也跑起来)
启动后可以在dubbo-admin控制台查看
启动user-service
此时服务提供者和消费者都已经ok了,可以测试下功能
访问一下http://localhost:8881/user/queryUser?user_id=1 仅查询用户信息
访问http://localhost:8881/user/queryUserChat?user_id=1 查询用户和用户的会话信息
在user-service项目里面我们成功的调用了chat-service里面的查询会话服务,如果不是分布式微服务的项目,我们通常会将这两个功能写一起,内部相互调用,耦合性比较高。如果其中任何一个环节出了问题,那整个应用都将无法使用。而在本项目中,我们将业务做了拆分,而用户是感知不到的,单个环节出现问题并不影响另一个模块功能的使用,同时也为我们开发高并发、高可用、分布式的应用服务提供了快速高效的解决方案。使用 Dubbo 构建的微服务遵循云原生思想,能更好的复用底层云原生基础设施、贴合云原生微服务架构。创作不易,觉得有用的话点个赞吧!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。