Dubbo:Helloword 与springboot整合
1.提出需求
某个电商系统,订单服务需要调用用户服务获取某个用户的所有地址;
我们现在 需要创建两个服务模块进行测试
模块 | 功能 |
订单服务web模块 | 创建订单等 |
用户服务service模块 | 查询用户地址等 |
测试预期结果:
订单服务web模块在A服务器,用户服务模块在B服务器,A可以远程调用B的功能。
idea中包以及类的截图
创建模块
将公共接口放在一个项目下,其他项目中的实现类进行调用,需要在自己项目的pom.xml中引入这个公共接口层!
还需要在其配置文件中 声明注册中心,暴露服务(即其他项目可以调用的接口),声明要调用的接口等配置
gmall-interface:公共接口层
com.tinstu.gamll.bean.userAddress
package com.tinstu.gmall.bean;
import java.io.Serializable;
/**
* 用户地址
* @author lfy
*
*/
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;
}
}
com.tinstu.gmall.service.OrderService
package com.tinstu.gmall.service;
public interface OrderService {
/**
* 初始化订单
* @param userId
*/
public void initOrder(String userId);
}
com.tinstu.gmall.service.UserService
package com.tinstu.gmall.service;
import java.util.List;
import com.tinstu.gmall.bean.UserAddress;
/**
* 用户服务
* @author lfy
*
*/
public interface UserService {
/**
* 按照用户id返回所有的收货地址
* @param userId
* @return
*/
public List<UserAddress> getUserAddressList(String userId);
}
consumer消费者:gmall-user:用户模块(对用户接口的实现)
com.tinstu.gmall.service.impl.OrderServiceImpl
package com.tinstu.gmall.service.impl;
import com.tinstu.gmall.bean.UserAddress;
import com.tinstu.gmall.service.OrderService;
import com.tinstu.gmall.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class OrderServiceImpl implements OrderService {
@Autowired
UserService userService;
@Override
public void initOrder(String userId) {
System.out.println("用户ID:"+userId);
//1.查询用户的收获地址
List<UserAddress> addressList = userService.getUserAddressList(userId);
for(UserAddress userAddress : addressList){
System.out.println(userAddress.getUserAddress());
}
}
}
com.tinstu.gmall.Mainaoolication
package com.tinstu.gmall;
import com.tinstu.gmall.service.OrderService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
public class Mainaoolication {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("classpath:consumer.xml");
OrderService bean = app.getBean(OrderService.class);
bean.initOrder("1");
System.out.println("over");
System.in.read();
}
}
consumer.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<context:component-scan base-package="com.tinstu.gmall.service.impl"></context:component-scan>
<dubbo:application name="order-server-consumer"></dubbo:application>
<dubbo:registry address="zookeeper://120.48.39.100:2181"></dubbo:registry>
<!--声明需要调用的远程服务的接口;生成远程服务代理-->
<dubbo:reference interface="com.tinstu.gmall.service.UserService" id="userService"></dubbo:reference>
</beans>
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.tinstu</groupId>
<artifactId>order-service-consumer</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.tinstu</groupId>
<artifactId>gmall-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency>
</dependencies>
</project>
provider提供者:gmall-order-web:订单模块
com.tinstu.gmall.service.impl.UserServiceImpl
package com.tinstu.gmall.service.impl;
import com.tinstu.gmall.bean.UserAddress;
import com.tinstu.gmall.service.UserService;
import java.util.Arrays;
import java.util.List;
public class UserServiceImpl implements UserService {
@Override
public List<UserAddress> getUserAddressList(String userId) {
UserAddress address1 = new UserAddress(1,"地址1","1","zhangsan","10086","Y");
UserAddress address2 = new UserAddress(2,"地址2","2","lisi","10010","N");
return Arrays.asList(address1,address2);
}
}
com.tinstu.gmall.service.MainApplication
package com.tinstu.gmall.service;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
public class MainApplication {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("classpath:provider.xml");
System.in.read();
}
}
provider.xml
<?xml version="1.0" encoding="UTF-8"?>
<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="user-service-provider"></dubbo:application>
<!--指定注册中心的地址 -->
<dubbo:registry address="zookeeper://120.48.39.100:2181" />
<!--使用dubbo协议,将服务暴露在20880端口 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 指定需要暴露的服务 -->
<dubbo:service interface="com.tinstu.gmall.service.UserService" ref="userServiceImpl" />
<!--服务的实现-->
<bean id="userServiceImpl" class="com.tinstu.gmall.service.impl.UserServiceImpl"></bean>
</beans>
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.tinstu</groupId>
<artifactId>user-service-provider</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.tinstu</groupId>
<artifactId>gmall-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency>
</dependencies>
</project>
测试:
运行Mainaoolication.java与MainApplication.java 查看dubbo-admin
与springboot整合
1、引入spring-boot-starter以及dubbo和curator的依赖
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
2、配置application.properties
示例:
dubbo.application.name=gmall-user
dubbo.registry.protocol=zookeeper
dubbo.registry.address=192.168.67.159:2181
dubbo.scan.base-package=com.atguigu.gmall
dubbo.protocol.name=dubbo
application.name就是服务名,不能跟别的dubbo提供端重复
registry.protocol 是指定注册中心协议
registry.address 是注册中心的地址加端口号
protocol.name 是分布式固定是dubbo,不要改。
base-package 注解方式要扫描的包
使用注解:
1.dubbo提供的@Service 相当于
<!-- 指定需要暴露的服务 -->
<dubbo:service interface="com.tinstu.gmall.service.UserService" ref="userServiceImpl" />
2.dubbo提供的@Reference (不使用@Autowired) 相当于
<!--声明需要调用的远程服务的接口;生成远程服务代理-->
<dubbo:reference interface="com.tinstu.gmall.service.UserService" id="userService"></dubbo:reference>
【如果没有在配置中写dubbo.scan.base-package,还需要使用@EnableDubbo注解】
阅读剩余
版权声明:
作者:Tin
链接:http://www.tinstu.com/1812.html
文章版权归作者所有,未经允许请勿转载。
THE END