-
配置zookeeper注册中心:
-
下载并解压zookeeper
-
编辑zookeeper解压目录conf下的配置文件zoo.cf(将zoo_sample.cfg复制为zoo.cfg):
更改dataDir的值为:dataDir=../data
-
执行zkServer.cmd文件启动zookeeper
-
-
安装dubbo控制台
- 下载dubbo源码:https://github.com/satansk/incubator-dubbo-ops
- 解压并进入dubbo-admin\src\main\resources下修改application.properties的注册中心地址
- 将dubbo-admin打包:mvn clean install
- 运行打好的jar包(先运行zookeeper):java -jar XXX.jar
- 访问localhost:7001,用户名密码均为root
-
服务提供者:
-
消息提供者和消费者都需要引入依赖
<dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>2.12.0</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.2</version> </dependency>
-
写接口
public interface SayHelloService { public void sayHello(String str); }
-
写实现类
public class SayHelloServiceImpl implements SayHelloService{ public void sayHello(String str) { System.out.println("hello"+str); } }
-
用 Spring 配置声明暴露服务
<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"> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="provider" /> <!-- 广播注册中心暴露服务地址 --> <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" /> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="com.xdd.SayHelloService" ref="sayHelloService" /> <!-- 和本地bean一样实现服务 --> <bean id="sayHelloService" class="com.xdd.SayHelloServiceImpl"></bean> </beans>
-
加载 Spring 配置
public class App { public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("provider.xml"); context.start(); System.in.read(); } }
-
-
服务消费者:
-
引入依赖
-
通过 Spring 配置引用远程服务
<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-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 --> <dubbo:application name="consumer" /> <!-- 广播注册中心暴露服务地址 --> <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" /> <!--生成远程服务代理,可以像本地bean一样实现服务 --> <dubbo:reference id="sayHelloService" interface="com.xdd.SayHelloService"></dubbo:reference> </beans>
-
加载Spring配置,并调用远程服务
public class App { public static void main(String[] args) { ApplicationContext context=new ClassPathXmlApplicationContext("consumer.xml"); SayHelloService sayHelloService= (SayHelloService) context.getBean("sayHelloService"); sayHelloService.sayHello("111"); } }
-
-
安装监控中心(simple_monitor)
-
下载dubbo源码:https://github.com/satansk/incubator-dubbo-ops
-
解压并将dubbo-monitor-simple打包:mvn clean install
-
编辑打包的dubbo-monitor-simple-2.0.0-assembly.tar.gz/conf/dubbo.properties:修改zookeeper地址
-
运行dubbo-monitor-simple-2.0.0-assembly\dubbo-monitor-simple-2.0.0\assembly.bin\start.bat
-
访问localhost:8080
-
分别在提供者和消费者配置文件中配置监控中心:
<dubbo:monitor protocol="registry"></dubbo:monitor> <!--<dubbo:monitor address="127.0.0.1:7070"></dubbo:monitor>-->
-
-
整合springboot
-
提供者和消费者都需要添加dubbo-springboot依赖
<dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.0</version> </dependency>
-
服务提供者:
@Service //暴露服务,dubbo下的service @Component public class UserServiceImpl implements UserService { @Override public List<UserAddress> getAllAddress(int userId) { List<UserAddress> list=new ArrayList<UserAddress>(); list.add(new UserAddress(1,"xdd","187XXXXXXX","河南-南阳")); list.add(new UserAddress(1,"sl","182XXXXXXX","河南-商丘")); return list; } }
application.properties
dubbo.application.name=boot-user-service-provider dubbo.registry.address=zookeeper://127.0.0.1:2181 dubbo.protocol.name=dubbo dubbo.protocol.port=20880 dubbo.monitor.address=127.0.0.1:7070
启动类
@EnableDubbo @SpringBootApplication public class Provider { public static void main(String[] args) throws Exception { SpringApplication.run(Provider.class,args); } }
-
消费者(web项目)
@Service public class OrderServiceImpl implements OrderService { @Reference //发现并自动注入 UserService userService; @Override public List<UserAddress> initOrder(int id) { List<UserAddress> list=userService.getAllAddress(id); return list; } }
控制器:
@Controller public class OrderController { @Autowired OrderService orderService; @RequestMapping("init") @ResponseBody public List<UserAddress> initOrder(@RequestParam("id") int id) { List<UserAddress> list = orderService.initOrder(id); return list; } }
启动类:与提供者相同,修改类名
application.properties
dubbo.application.name=boot-order-service-consumer dubbo.registry.address=zookeeper://127.0.0.1:2181 dubbo.monitor.protocol=registry server.port=8081 //修改tomcat的端口号
** 暴露服务使用@service,发现服务用@reference,但都要在启动类上添加@EnableDubbo
-
-
覆盖策略(dubbo配置加载顺序)
- 虚拟机属性配置:-Ddubbo.protocol.port=20880 ↑
- dubbo.xml:<dubbo:protocol port=“20881”> ↑
- dubbo.protocol.port=20882 ↑
-
启动检查check:
<dubbo:reference interface="XXX" check="false"> //关闭某个服务启动时检查 (没有提供者时报错) <dubbo:consumer check="false"> //关闭所有服务的启动时检查 (没有提供者时报错) <dubbo:registry check="false" /> //关闭注册中心启动时检查 (注册订阅失败时报错):
-
超时timeout&配置覆盖关系:
-
超时:
<dubbo:method name="XXX" timeout="1000"> //配置具体方法的超时时间 <dubbo:reference interface="XXX" check="false" timeout="1000">
-
覆盖关系:
方法级优先,接口级次之,全局配置再次之
如果级别一样,则消费方优先,提供方次之
-
-
重试次数retries:(一般和超时同时配置)
<dubbo:reference interface="XXX" check="false" timeout="1000" retries="3">
-
多版本version: (指定调用服务的版本,*表示随机调用)
<dubbo:reference interface="XXX" check="false" timeout="1000" retries="3" version="1.0.0">
-
Dubbo高可用:(zookeeper宕机&Dubbo直连)
- zookeeper宕机:是指zookeeper关闭后消费者与提供者仍能通过本地缓存通信,
- Dubbo直连:@Reference(url=“127.0.0.1:20880”),绕过注册中心直接连接
-
负载均衡(loadbalance):
<dubbo:service interface="XXX" loadbalance="XXX"> //暴露服务时 <dubbo:reference interface="XXX" check="false" timeout="1000" retries="3" version="1.0.0" loadbalance="random"> //引用服务时
1.负载均衡策略
Random LoadBalance - - - - - //随机,根据权重设置随机概率
RoundRobin LoadBalance - - - - - //轮询,按公约后的权重设置轮询比率
LeastActive LoadBalance - - - - - //最少活跃调用数,使慢的提供者收到更少请求
ConsistentHash LoadBalance - - - - - //一致性 Hash,相同参数的请求总是发到同一提供者 -
服务降级:
mock=force:return+null:表示消费者调用的方法都返回空,不发起远程调用(在控制台开启屏蔽)
mock=fail:return+null:表示消费者调用方法失败时返回空,发起远程调用(在控制台开启容错) -
容错hystrix:
-
集群容错模式:
Failover Cluster:失败自动切换,当出现失败,重试其它服务器
Failfast Cluster:快速失败,只发起一次调用,失败立即报错
Failsafe Cluster:失败安全,出现异常时,直接忽略,通常用于写入审计日志等操作
Failback Cluster:失败自动恢复,后台记录失败请求,定时重发
Forking Cluster:并行调用多个服务器,只要一个成功即返回
Broadcast Cluster:广播调用所有提供者,逐个调用,任意一台报错则报错 -
集群容错模式配置
<dubbo:service cluster="failsafe" />或者<dubbo:reference cluster="failsafe" />
-
整合hystrix
-
引入hystrix依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> <version>2.0.2.RELEASE</version> </dependency>
-
在提供者容错的方法上添加注解:@HystrixCommand,在启动类上添加注解:@EnableHystrix
-
在消费者调用方法的方法上添加注解 @HystrixCommand(fallbackMethod = “XXX”) ,并编写回调方法XXX,在启动类上添加**@EnableHystrix**
-
-
Dubbo
最新推荐文章于 2024-08-20 19:00:00 发布