主要内容以及实现的功能和上一篇博客类似,只是以springboot的配置来实现完成用户信息的获取。
可查看上一篇博客了解功能需求。
https://blog.csdn.net/qq_36654629/article/details/90052908
现在说一下整合的思想,这里一样是分别创建三个springboot工程
gmall-interface和之前一样,仍然是存放公共接口,这里就不提供代码了,和上一篇博客一样。所以创建另外两个项目的时候,得导入这个模块,不然添加依赖会报错。
boot-user-service:用户服务模块
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.4.RELEASE
com.springboot.dubbo
boot-user-service
0.0.1-SNAPSHOT
boot-user-service
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
com.atguigu.gmall
gmall-interface
0.0.1-SNAPSHOT
com.alibaba.boot
dubbo-spring-boot-starter
0.2.0
org.springframework.boot
spring-boot-maven-plugin
package com.springboot.dubbo.bootuserservice.service.impl;
import java.util.Arrays;
import java.util.List;
import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.gmall.bean.UserAddress;
import com.atguigu.gmall.service.UserService;
import org.springframework.stereotype.Component;
@Service //暴露接口
@Component
public class UserServiceImpl implements UserService{
public List getUserAddressList(String userId) {
// TODO Auto-generated method stub
System.out.println("UserServiceImpl.....old...");
// TODO Auto-generated method stub
UserAddress address1 = new UserAddress(1, "北京市昌平区宏福科技园综合楼3层", "1", "李老师", "010-56253825", "Y");
UserAddress address2 = new UserAddress(2, "深圳市宝安区西部硅谷大厦B座3层(深圳分校)", "1", "王老师", "010-56253825", "N");
/*try {
Thread.sleep(4000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
return Arrays.asList(address1,address2);
}
}
#指定当前服务/应用名字(同样的服务名字相同,不要和别的服务同名
dubbo.application.name=user-service-provider
#指定注册中心的位置
dubbo.registry.address=127.0.0.1:2181
dubbo.registry.protocol=zookeeper
#指定通信规则(通信协议、通信端口)
dubbo.protocol.name=dubbo
dubbo.protocol.port=20881
#表示从注册中心发现监控中心地址,否则直连监控中心
dubbo.monitor.protocol=registry
#dubbo.scan.base-packages=com.spring
package com.springboot.dubbo.bootuserservice;
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 BootUserServiceApplication {
public static void main(String[] args) {
SpringApplication.run(BootUserServiceApplication.class, args);
}
}
boot-order-service:订单服务模块
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.4.RELEASE
com.springboot.order-service
boot-order-service
0.0.1-SNAPSHOT
boot-order-service
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
com.atguigu.gmall
gmall-interface
0.0.1-SNAPSHOT
com.alibaba.boot
dubbo-spring-boot-starter
0.2.0
org.springframework.boot
spring-boot-maven-plugin
然后导入gmall-interface模块,和boot-user-service导入步骤一样。
package com.springboot.orderservice.bootorderservice.service.impl;
import java.util.List;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;
import com.atguigu.gmall.bean.UserAddress;
import com.atguigu.gmall.service.OrderService;
import com.atguigu.gmall.service.UserService;
@Service
public class OrderServiceImpl implements OrderService{
//@Autowired
@Reference //远程引用userService服务
UserService userService;
public List initOrder(String userId) {
// TODO Auto-generated method stub
//1、查询用户的收货地址
List addressList = userService.getUserAddressList(userId);
return addressList;
}
}
package com.springboot.orderservice.bootorderservice.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.gmall.bean.UserAddress;
import com.atguigu.gmall.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author JP
* @title: OrderController
* @projectName boot-order-service
* @description:
* @date 2019/5/10 0010
*/
@RestController
public class OrderController {
@Autowired
OrderService orderService;
@RequestMapping("/getOrder")
public List getList(String uid){
System.out.println("用户id:"+uid);
List addressList = orderService.initOrder(uid);
for (UserAddress userAddress : addressList) {
System.out.println(userAddress.getUserAddress());
}
return addressList;
}
}
#申明消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样
dubbo.application.name=order-service-consumer
#使用zookeeper广播注册中心暴露发现服务地址
dubbo.registry.address=127.0.0.1:2181
dubbo.registry.protocol=zookeeper
#表示从注册中心发现监控中心地址,否则直连监控中心。
dubbo.monitor.protocol=registry
#dubbo.scan.base-packages=com.spring
#这里改了一下tomcat启动端口,因为dubbo监控中心启动时使用了jetty启动,占用了8080端口
server.port=8081
package com.springboot.orderservice.bootorderservice;
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 BootOrderServiceApplication {
public static void main(String[] args) {
SpringApplication.run(BootOrderServiceApplication.class, args);
}
}