SpringBoot和Dubbo+ZooKeeper组合案例
Posted 几度平凡人
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot和Dubbo+ZooKeeper组合案例相关的知识,希望对你有一定的参考价值。
SpringBoot融合了maven的特点,所以可以和maven完美整合。
接下来要做一个分布式项目,首先要有共享的接口:
先建一个maven项目:
详情:
UserAddress.java
package com.changping.bean; import java.io.Serializable; public class UserAddress implements Serializable { private Integer id; private String userAddress; //用户地址 private String userId; //用户id private String consignee; //收货人 private String phoneNum; //电话号码 private String isDefault; //是否为默认地址 Y-是 N-否 public UserAddress() { super(); } public UserAddress(Integer id, String userAddress, String userId, String consignee, String phoneNum, String isDefault) { super(); this.id = id; this.userAddress = userAddress; this.userId = userId; this.consignee = consignee; this.phoneNum = phoneNum; this.isDefault = isDefault; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUserAddress() { return userAddress; } public void setUserAddress(String userAddress) { this.userAddress = userAddress; } public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getConsignee() { return consignee; } public void setConsignee(String consignee) { this.consignee = consignee; } public String getPhoneNum() { return phoneNum; } public void setPhoneNum(String phoneNum) { this.phoneNum = phoneNum; } public String getIsDefault() { return isDefault; } public void setIsDefault(String isDefault) { this.isDefault = isDefault; } }
OrderService.java
package com.changping.service; import java.util.List; import com.changping.bean.UserAddress; public interface OrderService { public List<UserAddress> initOrder(String userId); }
UserService.java
package com.changping.service; import java.util.List; import com.changping.bean.UserAddress; public interface UserService { public List<UserAddress> getUserAddressList(String userId); }
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.changping.mall</groupId> <artifactId>mall_interface</artifactId> <version>0.0.1-SNAPSHOT</version> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.5.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> <dependencies> <!-- 引入dubbo --> <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.2</version> </dependency> <!-- 注册中心使用的是zookeeper,引入操作zookeeper的客户端端 --> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>2.12.0</version> </dependency> <!--Dubbo Spring Boot Starter,一定要针对springboot2.6.2版本 --> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.0</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>org.apache.solr</groupId> <artifactId>solr-solrj</artifactId> <version>4.10.1</version> </dependency> <!-- 1.连接池 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.37</version> </dependency> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.2</version> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <!-- 2.Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>4.0.0.RELEASE</version> </dependency> <!-- 3.MyBatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.8</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.2</version> </dependency> <!-- 4.SpringMVC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.0.0.RELEASE</version> </dependency> <!-- 5.辅助 --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.7</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.7</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.2.4</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.1.3-b06</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.11</version> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.1</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.3.2</version> </dependency> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.7</version> </dependency> <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <classifier>jdk15</classifier> </dependency> <!-- 6.webservice-cxf --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.0.5</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>3.0.5</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-ws-security</artifactId> <version>3.0.5</version> </dependency> <dependency> <groupId>org.apache.ws.security</groupId> <artifactId>wss4j</artifactId> <version>1.5.2</version> </dependency> <!-- 7.事务 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- 1 spring容器事务 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.6.8</version> </dependency> <dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version>1.0</version> </dependency> </dependencies> </project>
以上项目会作为下面两个项目的依赖。
新建springboot项目用来提供服务
UserServiceImpl.java
package com.changping.mall.userserviceimpl; import java.util.Arrays; import java.util.List; import org.springframework.stereotype.Component; import com.alibaba.dubbo.config.annotation.Service; import com.changping.bean.UserAddress; import com.changping.service.UserService; @Service //dubbo注解暴露接口 @Component //spring注解 public class UserServiceImpl implements UserService { public List<UserAddress> getUserAddressList(String userId) { System.out.println("UserServiceImpl.....1..."); UserAddress address1 = new UserAddress(1, "花果山福地", "1", "齐天大圣", "123456", "Y"); UserAddress address2 = new UserAddress(2, "水帘洞洞天", "1", "六耳猕猴", "654321", "N"); return Arrays.asList(address1,address2); } }
SpringbootUserserviceProviderApplication (启动类)
package com.changping.mall; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo; @SpringBootApplication @EnableDubbo //开启基于注解的dubbo功能 public class SpringbootUserserviceProviderApplication { public static void main(String[] args) { SpringApplication.run(SpringbootUserserviceProviderApplication.class, args); } }
application.properties(配置信息)
dubbo.application.name=user-service-provider dubbo.registry.protocol=zookeeper dubbo.registry.address=127.0.0.1:2181 dubbo.protocol.name=dubbo dubbo.protocol.port=-1 dubbo.monitor.protocol=registry
SpringbootUserserviceProviderApplicationTests (单元测试)
package com.changping.mall; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class)//用于指定junit运行环境,是junit提供给其它框架测试环境接口的扩展 @SpringBootTest public class SpringbootUserserviceProviderApplicationTests { @Test public void contextLoads() { } }
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <groupId>com.changping.mall</groupId> <artifactId>springboot-userservice-provider</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot-userservice-provider</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>com.changping.mall</groupId> <artifactId>mall_interface</artifactId> <version>0.0.1-SNAPSHOT</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> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
此时可以启动zkServer、zkCli、然后打包发布到web上
在该路径下执行:zkServer.cmd
zkCli.cmd
在该路径下执行:java -jar dubbo-admin-0.0.1-SNAPSHOT.jar
接下来去浏览器访问
http://localhost:7001
首页
接着启动服务提供者
console信息
SLF4J: Class path contains multiple SLF4J bindings. :: Dubbo Spring Boot (v0.2.0) : https://github.com/apache/incubator-dubbo-spring-boot-project 2020-03-30 15:26:11.180 INFO 13804 --- [ main] e.OverrideDubboConfigApplicationListener : Dubbo Config was overridden by externalized configuration {dubbo.application.name=order-service-consumer, dubbo.monitor.protocol=registry, dubbo.registry.address=zookeeper://127.0.0.1:2181} . ____ _ __ _ _ 2020-03-30 15:26:11.661 INFO 13804 --- [ main] pringbootOrderserviceConsumerApplication : Starting SpringbootOrderserviceConsumerApplication on DESKTOP-NF046H7 with PID 13804 (D:\\workspace\\springboot\\springboot-orderservice-consumer\\target\\classes started by YZ in D:\\workspace\\springboot\\springboot-orderservice-consumer) |
可以看到浏览器
新建springboot项目提供消费者
OrderController.java
package com.changping.mall.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import com.changping.bean.UserAddress; import com.changping.service.OrderService; @Controller public class OrderController { @Autowired OrderService orderService;//引入maven项目里的mall_interface里的OrderService接口 @ResponseBody//返回json @RequestMapping("initOrder") public List<UserAddress> initOrder(@RequestParam("uid") String userId)//前端传值 { List<UserAddress> addressList = orderService.initOrder(userId); return addressList; } }
OrderServiceImp.java
package com.changping.mall.orderserviceimp; import java.util.List; import org.springframework.stereotype.Service; import com.alibaba.dubbo.config.annotation.Reference; import com.changping.bean.UserAddress; import com.changping.service.OrderService; import com.changping.service.UserService; @Service public class OrderServiceImp implements OrderService{ @Reference //dubbo里引用远程服务的注解 UserService userService; public List<UserAddress> initOrder(String userId) { List<UserAddress> userAddress = userService.getUserAddressList(userId); return userAddress; } }
SpringbootOrderserviceConsumerApplication(启动)
package com.changping.mall; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo; @EnableDubbo @SpringBootApplication //@SpringBootApplication(scanBasePackages = {"com.changping.service.UserService"}) public class SpringbootOrderserviceConsumerApplication { public static void main(String[] args) { SpringApplication.run(SpringbootOrderserviceConsumerApplication.class, args); } }
application.properties(配置文件)
dubbo.application.name=order-service-consumer dubbo.registry.address=zookeeper://127.0.0.1:2181 dubbo.monitor.protocol=registry
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <groupId>com.changping.mall</groupId> <artifactId>springboot-orderservice-consumer</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot-orderservice-consumer</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>com.changping.mall</groupId> <artifactId>mall_interface</artifactId> <version>0.0.1-SNAPSHOT</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> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
需要双击启动start.bat
首先看一下配置文件,其中jetty.port之前为8080,需要修改一下为8081,避免与tomecat冲突:
双击start.bat后:
可以去浏览器访问一下,如图是注册中心
可以看一下services,此时还没有消费者
SpringbootOrderserviceConsumerApplication启动消费者应用
console信息
SLF4J: Class path contains multiple SLF4J bindings. :: Dubbo Spring Boot (v0.2.0) : https://github.com/apache/incubator-dubbo-spring-boot-project 2020-03-30 17:44:00.517 INFO 12428 --- [ main] e.OverrideDubboConfigApplicationListener : Dubbo Config was overridden by externalized configuration {dubbo.application.name=order-service-consumer, dubbo.monitor.protocol=registry, dubbo.registry.address=zookeeper://127.0.0.1:2181} . ____ _ __ _ _ 2020-03-30 17:44:00.761 INFO 12428 --- [ main] pringbootOrderserviceConsumerApplication : Starting SpringbootOrderserviceConsumerApplication on DESKTOP-NF046H7 with PID 12428 (D:\\workspace\\springboot\\springboot-orderservice-consumer\\target\\classes started by YZ in D:\\workspace\\springboot\\springboot-orderservice-consumer) |
注册中心
这样就也有注册中心了
分别将提供者和消费者再启动一次,仔细观察一下吧(我的电脑有些hold不住了,如果三个生产者,三个消费者,就基本可以和博客说拜拜了):
先从监控中心开始
首页服务治理
系统管理
服务
应用
机器
提供者
消费者
路由规则
动态配置
访问控制
权重调节
负载均衡
负责人
系统管理
dubbo版本
系统快照
系统状态
系统日志(有130条)
系统环境
接下来看看注册中心
Home
Application
Service
Hosts
Registries
Servers
Status
Log
dubbo-admin的安装