赞
踩
1.maven引用dubbo2.7
- <dependency>
- <groupId>org.apache.dubbo</groupId>
- <artifactId>dubbo-spring-boot-starter</artifactId>
- <version>2.7.1</version>
- </dependency>
- <dependency>
- <groupId>org.apache.dubbo</groupId>
- <artifactId>dubbo</artifactId>
- <version>2.7.1</version>
- </dependency>
- <dependency>
- <groupId>org.apache.dubbo</groupId>
- <artifactId>dubbo-dependencies-zookeeper</artifactId>
- <version>2.7.1</version>
- <type>pom</type>
- </dependency>
2.application.yml配置dubbo 注意到:dubbo是一级节点,不要放在spring节点下面。
- dubbo:
- application: #应用配置,用于配置当前应用信息,不管该应用是提供者还是消费者。
- name: robot_provide
- registry: #注册中心配置,用于配置连接注册中心相关信息。
- address: zookeeper://47.84.225.001:2181
- metadata-report:
- address: zookeeper://47.84.225.001:2181
- protocol: #协议配置,用于配置提供服务的协议信息,协议由提供方指定,消费方被动接受。
- name: dubbo
- port: 20888
- version: 1.0.0
- scan:
- base-packages: com.kp.robot.service.dubbo #服务暴露与发现消费所在的package
3.服务提供方
注意点:1>@Service引入的是org.apache.dubbo.config.annotation.Service包 。
2>文件内引用服务最好使用@Autowired。
- import org.apache.dubbo.config.annotation.Service;
- import org.springframework.beans.factory.annotation.Autowired;
- @Service(version = "${dubbo.version}")
- public class DubboProvidService implements IManeuverService {
-
- @Autowired
- private ManeuverService maneuverService;
-
- /**
- * 更新状态
- * @param userManeId 用户ID
- * @param status 状态 1有效 2无效
- * @return
- */
- @Override
- public boolean updateCoinManeStatus(int userManeId, int status) {
- return maneuverService.updateCoinManeStatus(userManeId,status) ;
- }
- }
4.启动入口
- package com.kp.robot;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
- @SpringBootApplication
- @EnableAutoConfiguration
- @EnableDubbo
- public class RobotApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(RobotApplication.class, args);
- }
-
- }
-
5,消费者 application.yml配置
- dubbo:
- application: #应用配置,用于配置当前应用信息,不管该应用是提供者还是消费者。
- name: robot_provide
- registry: #注册中心配置,用于配置连接注册中心相关信息。
- address: zookeeper://11.76.235.122:2181
- metadata-report:
- address: zookeeper://11.76.235.122:2181
- protocol: #协议配置,用于配置提供服务的协议信息,协议由提供方指定,消费方被动接受。
- name: dubbo
- port: 20888
- version: 1.0.0
6,消费者代码 @Reference同样是apache.dubbo
- package com.robot.web.service.dubbo;
- import org.apache.dubbo.config.annotation.Reference;
- import org.springframework.stereotype.Service;
- import com.robot.interfaces.IManeuverService;
- @Service
- public class DubboConsumerService{
- @Reference(version="${dubbo.version}",check=true,interfaceClass=IManeuverService.class)
- private IManeuverService dubboProvidService;
- /**
- * 更新策略状态
- * @param id
- * @param status 状态 1有效 2无效
- * @return
- */
- public boolean updateCoinManeStatus(int id,int status){
- return dubboProvidService.updateCoinManeStatus(id, status);
- }
- }
7,消费者启动类 @EnableDubbo
- package com.robot;
-
- import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
-
- @SpringBootApplication
- @EnableAutoConfiguration
- @EnableDubbo(scanBasePackages="com.robot.web.service.dubbo")
- public class RobotApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(RobotApplication.class, args);
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。