标签:远程 host display list tle 一个 catch 关于 inf
入门级demo,并没有数据库之类的!
1、项目结构
一个empty project下有两个module: boot-user-service-provider(服务提供者)
和 boot-order-service-consumer(服务消费者)
至于怎么在一个空项目下创建多个module自己去百度
2、服务提供者
创建SpringBoot工程:boot-user-service-provider
1)依赖 【注意】坐标gmall-interface中包含了bean,和远程调用的接口,需要install进maven仓库中
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>com.atguigu</groupId> <artifactId>gmall-interface</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.0</version> </dependency> </dependencies>
2)启动类
@SpringBootApplication @EnableDubbo //开启基于dubbo注解的开发 public class UserServiceApplication { public static void main(String[] args) { SpringApplication.run(UserServiceApplication.class,args); } }
3)服务提供者向zookeeper暴露的服务
@Service //指出暴露的服务 【注意】这是dubbo的注解。引入包时别弄错 @Component public class UserServiceImpl implements UserService { public List<UserAddress> getUserAddressList(String userId) { 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); } }
4)application.yml : dubbo关于服务提供方的一些配置
dubbo: application: name: user-service-provider registry: address: 172.20.10.14:2181 protocol: zookeeper protocol: name: dubbo port: 20080
3、服务消费者
1)依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.atguigu</groupId> <artifactId>gmall-interface</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.0</version> </dependency> </dependencies>
2)启动类
@EnableDubbo @SpringBootApplication public class OrderServiceApplication { public static void main(String[] args) { SpringApplication.run(OrderServiceApplication.class,args); } }
3)控制器 controller和业务逻辑层
@Controller public class OrderController { @Autowired private OrderService orderService; @ResponseBody @RequestMapping("/initOrder") public List<UserAddress> initOrder(String uid){ List<UserAddress> userAddresses = orderService.initOrder(uid); return userAddresses; } } @Service public class OrderServiceImpl implements OrderService { //@Autowired @Reference //去注册中心寻找对应的服务 UserService userService; @Override public List<UserAddress> initOrder(String userId) { //1、查询用户的收货地址 System.out.println("用户id:"+userId); List<UserAddress> addressList = userService.getUserAddressList(userId); return addressList; } }
4)application.xml
dubbo: application: name: order-service-provider registry: address: 172.20.10.14:2181 protocol: zookeeper server: port: 8085
4、依赖的工程
【注意】 要打成jar放入maven中
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-否 public UserAddress() { super(); // TODO Auto-generated constructor stub } public UserAddress(Integer id, String userAddress, String userId, String consignee, String phoneNum, String isDefault) { super(); this.id = id; this.userAddress = userAddress; this.userId = userId; this.consignee = consignee; this.phoneNum = phoneNum; this.isDefault = isDefault; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUserAddress() { return userAddress; } public void setUserAddress(String userAddress) { this.userAddress = userAddress; } public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getConsignee() { return consignee; } public void setConsignee(String consignee) { this.consignee = consignee; } public String getPhoneNum() { return phoneNum; } public void setPhoneNum(String phoneNum) { this.phoneNum = phoneNum; } public String getIsDefault() { return isDefault; } public void setIsDefault(String isDefault) { this.isDefault = isDefault; } } public interface OrderService { public List<UserAddress> initOrder(String userId); } public interface UserService { /** * 按照用户id返回所有的收货地址 * @param userId * @return */ public List<UserAddress> getUserAddressList(String userId); }
5、测试
启动两个工程
在浏览器访问:http://localhost:8085/initOrder?uid=1 成功
标签:远程 host display list tle 一个 catch 关于 inf
原文地址:https://www.cnblogs.com/houchen/p/13371625.html