dubbo-springboot
一、服务提供者boot-user-service-provider
服务提供者boot-user-service-provider代码结构如下:
1、服务提供者boot-user-service-provider引入spring-boot-starter以及dubbo和curator的依赖
注意starter版本适配:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
引入spring-boot-starter以及dubbo和curator的依赖后的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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.lina02.gmall</groupId> <artifactId>boot-user-service-provider</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>boot-user-service-provider</name> <description>服务提供者</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>com.lina02.gmall</groupId> <artifactId>gmall-interface</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2、配置文件application.properties
#服务名,不能跟别的dubbo服务提供者重复 dubbo.application.name=boot-user-service-provider #指定注册中心的地址和端口号 dubbo.registry.address=127.0.0.1:2181 #指定注册中心协议 dubbo.registry.protocol=zookeeper #指定通信规则(通信协议&通信端口) dubbo.protocol.name=dubbo dubbo.protocol.port=20880 #连接监控中心 dubbo.monitor.protocol=registry
3、UserService接口实现UserServiceImpl
package com.lina02.gmall.service.impl; import com.lina02.gmall.bean.UserAddress; import com.lina02.gmall.service.UserService; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; import java.util.Arrays; import java.util.List; @com.alibaba.dubbo.config.annotation.Service //暴露服务 //@Service @Component public class UserServiceImpl implements UserService { @Override public List<UserAddress> getUserAddressList(String userId) { System.out.println("UserServiceImpl..3....."); UserAddress address1 = new UserAddress(1, "北京市昌平区宏福科技园综合楼3层", "1", "李老师", "010-56253825", "Y"); UserAddress address2 = new UserAddress(2, "深圳市宝安区西部硅谷大厦B座3层(深圳分校)", "1", "王老师", "010-56253825", "N"); return Arrays.asList(address1,address2); } }
4、主程序
package com.lina02.gmall; import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @EnableDubbo //开启dubbo注解 @SpringBootApplication public class BootUserServiceProviderApplication { public static void main(String[] args) { SpringApplication.run(BootUserServiceProviderApplication.class, args); } }
二、服务消费者boot-order-service-consumer
服务消费者boot-order-service-consumer代码结构如下:
1、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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.lina02.gmall</groupId> <artifactId>boot-order-service-consumer</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>boot-order-service-consumer</name> <description>服务消费者</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>com.lina02.gmall</groupId> <artifactId>gmall-interface</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2、配置文件application.propertise
#配置服务端口 server.port=8081 #服务名,不能跟别的dubbo服务提供者重复 dubbo.application.name=boot-order-service-consumer #指定注册中心协议、地址、端口号 dubbo.registry.address=zookeeper://127.0.0.1:2181 #连接监控中心 dubbo.monitor.protocol=registry
3、OrderService接口实现OrderServiceImpl
package com.lina02.gmall.service.impl; import com.alibaba.dubbo.config.annotation.Reference; import com.lina02.gmall.bean.UserAddress; import com.lina02.gmall.service.OrderService; import com.lina02.gmall.service.UserService; import org.springframework.stereotype.Service; import java.util.List; /** * 1、将服务提供者注册到注册中心(暴露服务) * 1)、导入dubbo依赖(2.6.2)\操作zookeeper的客户端(curator) * 2)、配置服务提供者 * * 2、让服务消费者去注册中心订阅服务提供者的服务地址 */ @Service public class OrderServiceImpl implements OrderService { //@Autowired @Reference //订阅的服务 UserService userService; @Override public List<UserAddress> initOrder(String userId) { System.out.println("用户id:"+userId); //1、查询用户的收货地址 List<UserAddress> addressList = userService.getUserAddressList(userId); return addressList; } }
4、OrderController
package com.lina02.gmall.controller; import com.lina02.gmall.bean.UserAddress; import com.lina02.gmall.service.OrderService; 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 java.util.List; @Controller public class OrderController { @Autowired OrderService orderService; @ResponseBody @RequestMapping("/initOrder") public List<UserAddress> initOrder(@RequestParam("uid") String userId){ return orderService.initOrder(userId); } }
5、主程序
package com.lina02.gmall; import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @EnableDubbo //开启dubbo注解 @SpringBootApplication public class BootOrderServiceConsumerApplication { public static void main(String[] args) { SpringApplication.run(BootOrderServiceConsumerApplication.class, args); } }