1,课程回顾
2,本章重点
父子工程的使用,子工程相互调用
springboot+mybatis+dubbo+zookeeper+mysql
部署一个dubbo_admin治理项目(可以对提供者和消费者进行管理)
dubbo负载均衡功能
3,具体内容
3.1 springboot,mybatis整合dubbo
3.1.1创建父项目(springboot+mybatis方式创建),pom.xml引入jar:
<!--引入springboot 和dependencies并列--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.7</version> <relativePath/> <!-- lookup parent from repository --> </parent> <!--springboot web包--> <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> <!-- Spring Boot Dubbo 依赖 --> <dependency> <groupId>com.alibaba.spring.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency> <!-- zookeeper依赖 --> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency>
3.1.2 创建子项目common
创建公共的实体类和服务层接口
3.1.3创建子项目consumer
pom.xml <dependency> <groupId>com.aaa</groupId> <artifactId>common</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
新建启动类并加入dubbo支持:
package com.aaa.pc; import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration; import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringBootConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @ fileName:PowerConsumerApplication * @ description: * @ author:zhz * @ createTime:2022/1/5 9:51 * @ version:1.0.0 */ @SpringBootApplication @EnableDubboConfiguration //开启dubbo配置 public class PowerConsumerApplication { public static void main(String[] args) { //run方法 SpringApplication.run(PowerConsumerApplication.class,args); } }
编写controller,引入Service:
@Reference(check=false)
Dubbo 缺省会在启动时检查依赖的服务是否可用,不可用时会抛出异常,阻止 Spring 初始化完成,以便上线时,能及早发现问题,默认 check=“true”.
可以通过 check=“false” 关闭检查,比如,测试时,有些服务不关心,或者出现了循环依赖,必须有一方先启动。
package com.aaa.pc.controller; import com.aaa.common.controller.BaseController; import com.aaa.common.entity.Menu; import com.aaa.common.service.MenuService; import com.aaa.common.util.Result; import com.alibaba.dubbo.config.annotation.Reference; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import java.util.Map; /** * 菜单权限表(Menu)表控制层 * * @author makejava * @since 2022-01-05 09:45:33 */ @RestController @RequestMapping("menu") public class MenuController extends BaseController { /** * 服务对象 */ @Reference(check = false) //没有配置文件,直接使用引用注解 private MenuService menuService; /** * 通过主键查询单条数据 * * @param id 主键 * @return 单条数据 */ @GetMapping("selectOne") public Result selectOne(Integer id) { return this.success(this.menuService.queryById(id)); } /** * 添加 * * @param menu * @return */ @PutMapping("add") public Result add(@RequestBody Menu menu) { return this.success(this.menuService.insert(menu)); } /** * 更新 * * @param menu * @return */ @PutMapping("update") public Result update(@RequestBody Menu menu) { return this.success(this.menuService.update(menu)); } /** * 根据编号删除 * * @param id * @return */ @DeleteMapping("deleteById") public Result deleteById(Integer id) { return this.success(this.menuService.deleteById(id)); } } application.properties: #访问端口号 server.port=18090 #dubbo - customer名称 spring.dubbo.application.name=power_customer
zookeeper 注册中心
spring.dubbo.registry.address=zookeeper://192.168.170.41:2181
#dubbo 通信端口
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20881
3.1.4创建子项目provider
pom.xml <dependency> <groupId>com.aaa</groupId> <artifactId>common</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.10</version> </dependency> <!--druid包--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.8</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.8</version> </dependency>
启动类加入:
package com.aaa.pp; import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.transaction.annotation.EnableTransactionManagement; /** * @ fileName:PowerProviderApplication * @ description: * @ author:zhz * @ createTime:2022/1/5 10:32 * @ version:1.0.0 */ @SpringBootApplication @EnableDubboConfiguration //@EnableTransactionManagement public class PowerProviderApplication { public static void main(String[] args) { SpringApplication.run(PowerProviderApplication.class,args); } } 编写服务实现类加入 @server @com.alibaba.dubbo.config.annotation.Service @Service 为dubbo 注解,注解暴露服务 application.properties: spring.datasource.druid.url=jdbc:mysql://localhost:3306/db_qy141 spring.datasource.druid.username=root spring.datasource.druid.password=root spring.datasource.druid.initial-size=5 spring.datasource.druid.max-active=20 spring.datasource.druid.min-idle=10 spring.datasource.druid.max-wait=10 #端口号 server.port=19090 #dubbo 服务名称 spring.dubbo.application.name=power_provider
zookeeper 注册中心
spring.dubbo.registry.address=zookeeper://192.168.170.41:2181
#zookeeper 通信端口
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20881
3.2 服务治理
3.2.1 多台zk集群
前面配置过,直接启动
3.2.2 配置治理项目,并且测试
配置tomcat
tar -xzvf /root/software/apache-tomcat-9.0.52.tar.gz -C /usr/
vim /etc/profile
修改为:
export PATH=$PATH:$JAVA_HOME/bin:$ZK_HOME/bin:/usr/apache-tomcat-9.0.52/bin
生效:
source /etc/profile
startup.sh
查看是否启动成功
jps 或者 ps -ef |grep tomcat |grep -v grep
jps 可以看到Bootstrap进程
复制准备好dubbo-admin项目到tomcat下
使用xft 把dubbo-admin.war上传到/usr/apache-tomcat-9.0.52/webapps/
修改配置
vim /usr/apache-tomcat-9.0.52/webapps/dubbo-admin/WEB-INF/dubbo.properties
修改第一行:
127.0.0.1 修改为 192.168.xxx.41 (zk所在的机子IP)
启动zk集群:
/root/shelldir/zk-start-stop.sh
测试
http://192.168.170.41:8080/dubbo-admin/
3.2.3 启动提供者和消费者,查看admin项目变化
3.3 负载均衡,集群容错, 服务降级(半权) 路由规则,动态配置,访问控制,权重调节
打包生成多个服务:
cd soft/
使用xftp 把provider_jar上传
cd provider_jar/
java -jar
java -jar provider.jar
运行出错:slf4j-log4j12-1.7.26.jar
修改META-INF/MANIFEST.MF文件,删除slf4j-log4j12-1.7.26.jar包,再次打包运行
再次rebuild 时,要再次修改META-INF/MANIFEST.MF 文件中多余的jar
4,知识点总结
5,本章面试题