技术标签: spring java springboot
地址:https://archive.apache.org/dist/zookeeper/
这下面是我的配置,可以参考一下:
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=D:\devtools\zookeeper-3.4.14\data
dataLogDir=D:\devtools\zookeeper-3.4.14\log
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
点击bin目录下的zkServer.cmd启动,前提是安装好了java环境
这里我直接将我打包好的dubbo-admin献上, 点击下载
下载之后进入包里面将,dubbo-admin-0.0.1-SNAPSHOT.jar\BOOT-INF\classes目录下的applciaiton.properties文件的dubbo.registry.address改为
dubbo.registry.address=zookeeper://127.0.0.1:2181
java -jar dubbo-admin-0.0.1-SNAPSHOT.jar
打开lcoalhost:7001,用户名密码都是root,就可以对dubbo服务的服务提供者,服务消费者进行监控了。
如下图:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yYjbTM9t-1576571683257)(https://img.vim-cn.com/0b/003cfa2d2ae13dd0186be5bcee6ef72bf88fde.png)]
工具:idea,jdk8,dubbo-admin,zookeeper,lombok
项目结构:[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uuJe5YBZ-1576571683259)(https://img.vim-cn.com/8c/7e481b9e4d184c735babc5af8a1bd2147b0382.png)]
dubbo架构:
将公共接口和bean文件放在common-api包下,实现还是在原来的包里面
首先我们在common-api下建一个bean文件:
package top.juntech.commonapi.bean;
import lombok.*;
import java.io.Serializable;
@Setter
@Getter
@ToString
@AllArgsConstructor
@NoArgsConstructor
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-否
}
有2个服务:userservice,orderservice
1.服务消费者服务
package top.juntech.commonapi.service;
import top.juntech.commonapi.bean.UserAddress;
import java.util.List;
public interface OrderService {
//初始化订单
public List<UserAddress> initOrder(String userId);
}
2.服务提供者服务
package top.juntech.commonapi.service;
import top.juntech.commonapi.bean.UserAddress;
import java.util.List;
public interface UserService {
List<UserAddress> getUserAddressList(String userId);
}
pom文件:
引入一个lombok就行了
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
spring配置方式:
包下面建立一个实现userservice的实现类:userserviceimpl,
在这里我们就不连接数据库了,直接自己早一点数据来测试就行了
package top.juntech.serviceprovider.service.impl;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;
import top.juntech.commonapi.bean.UserAddress;
import top.juntech.commonapi.service.UserService;
import java.util.Arrays;
import java.util.List;
@Service
@Component
public class UserServiceImpl implements UserService {
@Override
public List<UserAddress> getUserAddressList(String userId) {
//模拟获取数据过程,这里为简化,自定义两个地址对象返回
UserAddress address1 = new UserAddress(1, "北京市昌平区宏福科技园综合楼3层", "1", "李老师", "010-56253825", "Y");
UserAddress address2 = new UserAddress(2, "深圳市宝安区西部硅谷大厦B座9层", "1", "王老师", "010-56253825", "N");
return Arrays.asList(address1,address2);
}
}
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://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://dubbo.apache.org/schema/dubbo
http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- 1、指定当前服务/应用的名字(同样的服务名字相同,不要和别的服务同名) -->
<dubbo:application name="user-provider"></dubbo:application>
<!-- 2、指定注册中心的位置 -->
<!-- <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry> -->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry>
<!-- 3、指定通信规则(通信协议 通信端口) -->
<dubbo:protocol name="dubbo" port="20880"></dubbo:protocol>
<!-- 4、暴露服务 ref:指向服务的真正的实现对象 -->
<dubbo:service interface="top.juntech.commonapi.service.UserService" ref="userServiceImpl"></dubbo:service>
<!-- 服务的实现对象 -->
<bean id="userServiceImpl" class="top.juntech.serviceprovider.service.impl.UserServiceImpl"></bean>
</beans>
在这里我们需要 dubbo ,zkclient,以及curator,curator-recipes…,同时还要导入common-api的pom坐标
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--dubbo-->
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.4.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<!--zookeeper 日志排除-->
<!-- https://mvnrepository.com/artifact/org.apache.curator/curator-framework -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.curator/curator-recipes -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.12.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.7</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>top.juntech</groupId>
<artifactId>common-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
package top.juntech.serviceprovider;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ServiceProviderApplication {
public static void main(String[] args) throws Exception{
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
context.start();
System.in.read();
}
}
实现类:
package top.juntech.serviceconsumer.service.impl;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.juntech.commonapi.bean.UserAddress;
import top.juntech.commonapi.service.OrderService;
import top.juntech.commonapi.service.UserService;
import java.util.List;
@Service
@Component
public class OrderServiceImpl implements OrderService {
@Autowired
private UserService userService;
@Override
public List<UserAddress> initOrder(String userId) {
System.out.println("用户id:"+userId);
List<UserAddress> addressList = userService.getUserAddressList(userId);
for (UserAddress userAddress : addressList) {
System.out.println(userAddress.getUserAddress());
}
return addressList;
}
}
consumer.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 开启包扫描 -->
<context:component-scan base-package="top.juntech.serviceconsumer.service.impl"></context:component-scan>
<!-- 1、指定当前服务/应用的名字(同样的服务名字相同,不要和别的服务同名) -->
<dubbo:application name="order-consumer"></dubbo:application>
<!-- 2、指定注册中心的位置 -->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry>
<!-- 3、声明需要调用的远程服务的接口;生成远程服务代理 -->
<dubbo:reference id="userService" interface="top.juntech.commonapi.service.UserService"></dubbo:reference>
</beans>
pom.xml 与服务提供者的基本一致,只需要添加 spring-web
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</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>
<!--dubbo-->
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.4.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<!--zookeeper 日志排除-->
<!-- https://mvnrepository.com/artifact/org.apache.curator/curator-framework -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.curator/curator-recipes -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.12.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.7</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>top.juntech</groupId>
<artifactId>service-provider</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>top.juntech</groupId>
<artifactId>common-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
启动类:
package top.juntech.serviceconsumer;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import top.juntech.commonapi.service.OrderService;
public class ServiceConsumerApplication {
public static void main(String[] args) throws Exception{
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml");
OrderService orderService = context.getBean(OrderService.class);
orderService.initOrder("1");
System.out.println("调用完成....");
System.in.read();
}
}
创建2个springboot工程
如下:[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-picMOJaB-1576571683260)(https://img.vim-cn.com/29/ba595f47b550126f1f0059a0958eb797afa906.png)]
过程和spring整合dubbo差不多,在这里没有使用注解的方式,还是使用的xml方式配置dubbo,注解方式老是出错,显示地址被占用,很坑
package top.juntech.springbootserviceprovider.service.impl;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;
import top.juntech.commonapi.bean.UserAddress;
import top.juntech.commonapi.service.UserService;
import java.util.Arrays;
import java.util.List;
@Service
@Component
public class UserServiceImpl implements UserService {
@Override
public List<UserAddress> getUserAddressList(String userId) {
//模拟获取数据过程,这里为简化,自定义两个地址对象返回
UserAddress address1 = new UserAddress(1, "北京市昌平区宏福科技园综合楼3层", "1", "李老师", "010-56253825", "Y");
UserAddress address2 = new UserAddress(2, "深圳市宝安区西部硅谷大厦B座9层", "1", "王老师", "010-56253825", "N");
return Arrays.asList(address1,address2);
}
}
启动类
package top.juntech.springbootserviceprovider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
//@EnableDubbo
@ImportResource(locations = "classpath:provider.xml")
public class SpringbootServiceProviderApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootServiceProviderApplication.class, args);
}
}
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://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://dubbo.apache.org/schema/dubbo
http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- 1、指定当前服务/应用的名字(同样的服务名字相同,不要和别的服务同名) -->
<dubbo:application name="user-provider"></dubbo:application>
<!-- 2、指定注册中心的位置 -->
<!-- <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry> -->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry>
<!-- 3、指定通信规则(通信协议 通信端口) -->
<dubbo:protocol name="dubbo" port="20880"></dubbo:protocol>
<!-- 4、暴露服务 ref:指向服务的真正的实现对象 -->
<dubbo:service interface="top.juntech.commonapi.service.UserService" ref="userServiceImpl"></dubbo:service>
<!-- 服务的实现对象 -->
<bean id="userServiceImpl" class="top.juntech.springbootserviceprovider.service.impl.UserServiceImpl"></bean>
</beans>
pom文件与之前的一样就不在重复了
package top.juntech.springbootserviceconsumer.service.impl;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;
import top.juntech.commonapi.bean.UserAddress;
import top.juntech.commonapi.service.OrderService;
import top.juntech.commonapi.service.UserService;
import java.util.List;
@Component
//这里是dubbo的注解service,作用是暴露服务
@Service
public class OrderServiceImpl implements OrderService {
//显示被引用的服务
@Reference
private UserService userService;
@Override
public List<UserAddress> initOrder(String userId) {
return userService.getUserAddressList(userId);
}
}
启动类:
package top.juntech.springbootserviceconsumer;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
//@EnableDubbo
@ImportResource(locations = "classpath:consumer.xml")
public class SpringbootServiceConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootServiceConsumerApplication.class, args);
}
}
方便测试,写了一个controller:
package top.juntech.springbootserviceconsumer.controller;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import top.juntech.commonapi.bean.UserAddress;
import top.juntech.commonapi.service.OrderService;
import top.juntech.commonapi.service.UserService;
import java.util.List;
@RestController
@RequestMapping("/consumer")
public class ConsumerController {
@Autowired
private OrderService orderService;
@ResponseBody
@RequestMapping("/initOrder")
public List<UserAddress> initOrder(@RequestParam("uid") String userId){
return orderService.initOrder(userId);
}
}
启动服务提供者,服务消费者,前提是先启动了zookeeper
打开可视化界面观察服务:
127.0.0.1:7001
界面如下:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Kc1TJpV9-1576571683261)(https://img.vim-cn.com/38/3946004a3edabd2d5ed94e98c7a94a4fd2702b.png)]
至此,spring/springboot整合dubbo就结束了。
更多详情请访问:传送门
文章浏览阅读2.9k次,点赞26次,收藏82次。多态需要两个特性:(1)方法重写(override):父类与子类具有函数签名完全相同的方法。(2)向上类型转换(upcasting):用一个父类指针指向子类对象的时候,假如调用的是虚函数,会自动暂时将该指针转换为子类类型的指针。虚函数的存在就是为了类型转换,即使没有虚函数也能重写方法。虚函数并不是为了解决函数重写问题的。假如你去在父类和子类中都写上函数签名相同的方法,同样也能重写函数。例如 我们先写一个没有虚函数的例子结果为是完全没问题的!是的,即使不加virtual,也能实现方法重写!假如我用子类_虚表指针
文章浏览阅读5k次。1、命令格式ffmpeg [global_options] {[input_file_options] -i input_url} ... {[output_file_options] output_url} ...global_options:全局选项input_file_options:输入文件相关的选项output_file_options:输出文件相关的选项-i input_url:输入文件,可以有多个输入文件,每个输入文件前都要加“-i”选项output_url:输出文件2、简述_ffmpeg -vf命令
文章浏览阅读130次。原来想既然STemWin对此要求不高,随便找个版本就行,从VC6.0到VC2015,结果遇到莫名奇妙的许多问题(可能不是正版造成的吧),后来去微软官网下载了VC2022的专业版又有1个月的使用限制,唉!简单使用的方法,使我们对STemWin快捷的感受了一下它的尊容。实际它的内容还很丰富!要想达到能做点事的水平,需要踏踏实实的学习一番。StemWin提供了丰富的DEMO例程,重点就是学这个啦!该公司做了大量的编写及归纳整理工作,实际这里的基本就够啦。(1) 笔记本或一台电脑,我用的Win10。_emwin demo
文章浏览阅读417次。 实现原理:创建多个div,div之间通过css实现层叠,每个div放置当前表格的克隆。例如:需要行冻结时,创建存放冻结行表格的div,通过设置z-index属性和position属性,让冻结行表格在数据表格的上层。同理,需要列冻结时,创建存放冻结列表格的div,并放置在数据表格的上层。如果需要行列都冻结时,则除了创建冻结行、冻结列表格的div,还需要创建左上角的固定行列表格的d..._列冻结列解冻jquery
文章浏览阅读1.2k次。问题及描述:/* *Copyright(c++)2015,烟台大学计算机学院 *All rights reserved, *文件名称:test.cpp *作 者:程梦莹 *完成日期:2015年9月12日 *版本号:v1.0 *问题描述:计算任一输入的正整数的各位数字之和,并分析算法的时间复杂度 *输入描述:一个整数 */#include_输入一个整数求各位数字之和时间复杂度
文章浏览阅读3.6k次。成都理工大学微型计算机原理及接口技术实验报告学 院 : 核技术与自动化工程学院专 业 : 电气工程及其自动化班 级 :学 号 :姓 名 :指导老师 :完成时间 :实验一 EMU 8086软件的使用1、实验目的通过对emu8086的使用,来理解《微型计算机原理及接口技术》课本上的理论知识,加深对知识的运用,以及emu8086交互式学习汇编语言(Assembly ..._微机原理与接口技术emu8086课题总结
文章浏览阅读1w次。各位听得最多的段子:老司机们,上车了。没错,不管是在电脑端还是移动端,都需要一款好用的浏览器。那么,老司机们都用什么浏览器,什么样的浏览器才是老司机必备的浏览器呢?来咯,这些手机浏览器比UC浏览器还好用,功能可媲美桌面浏览器,一起来看看吧。_比uc好用的手机浏览器
文章浏览阅读1.3k次。7-35 jmu-Java&Python-统计文字中的单词数量并按出现次数排序_jmu-java&python-统计文字中的单词数量并按出现次数排序
文章浏览阅读3.5w次,点赞16次,收藏39次。b=number/10%10=520/10%10=52%10,“%”是取余符号,即52%10=5*10余2,%符号取得就是这个2。a=number/100=520/100,如果是正常计算的话应该等于5.2,但是“a”是int型属于整形,也就是说取它的整数部分。c=number%10;printf("百位数是%d\n",a);printf("十位数是%d\n",b);printf("个位数是%d\n",c);printf("请输入一个三位数\n");..._输入一个三位数,输出它的百位,十位,个位
文章浏览阅读2k次。一、效果二、知识点CircularReveal动画、透明主题、转场动画(非必须)三、方案假设有两个Activity A和B。Reveal圆形Activity转场动画效果先从A到B,那么基本方案如下:确定要显示的圆形动画中心起点位置通过Intent将起点位置从Activity A传递BActivity B主题需要是透明的,同时先隐藏布局视图在Activity A中启动Activi..._reveal动画
文章浏览阅读4.7k次。pako.js压缩和解压请求参数和响应数据_pako.js
文章浏览阅读4.8k次,点赞6次,收藏25次。自用CSDN的markdown模板_markdown 模板