一、Dubbo整合SpringBoot的方式(1)
1)直奔主题,方式一:
pom.xml中引入dubbo-starter依赖,在application.properties配置属性,使用@Service【暴露服务】使用@Reference【引用服务】,选取的是application.properties+部分注解的方式来完成。
2)创建ego_interface接口模块,被提供者和消费者所使用
此模块目录结构如下:
1、实体类UserAddress
package com.sxt.domain; import java.io.Serializable; //实体类 必须实现序列化 public class UserAddress implements Serializable { private Integer id; private String address; private String userId; public UserAddress() { } public UserAddress(Integer id, String address, String userId) { this.id = id; this.address = address; this.userId = userId; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } }
2、提供者接口UserService
package com.sxt.service; import com.sxt.domain.UserAddress; import java.util.List; //提供者接口 public interface UserService { //根据用户id查询用户地址 public ListgetUserAddressByUserId(String userId); }
3、消费者接口OrderService
package com.sxt.service; import com.sxt.domain.UserAddress; import java.util.List; //消费者接口 public interface OrderService{ //初始化订单 public ListinitOrder(String userId); }
3)创建boot-ego-user-service-provider提供者模块
此模块目录结构如下(config是第二种方式用的,此处请先忽略):
4)修改pom.xml加入依赖
添部分关键依赖(其余自动生成):
com.sxt ego_interface 1.0-SNAPSHOT
org.apache.dubbo dubbo-spring-boot-starter ${dubbo.version} org.apache.dubbo dubbo-dependencies-zookeeper ${dubbo.version} pom org.slf4j slf4j-log4j12
1、编写UserServiceImpl
package com.sxt.service.impl; import com.sxt.domain.UserAddress; import com.sxt.service.UserService; import org.apache.dubbo.config.annotation.Service; import java.util.ArrayList; import java.util.List; //此处service是apache.dubbo的 代替了创建和暴露对象 @Service public class UserServiceImpl implements UserService { public static Listaddress=new ArrayList<>(); static { address.add(new UserAddress(1, "天安门广场", "bj")); address.add(new UserAddress(2, "上海迪士尼", "sh")); } @Override public List getUserAddressByUserId(String userId) { return address; } }
2、修改application.properties
#application-name 本模块名字 dubbo.application.name=boot-ego-user-service-provider #registry 指定注册中心地址(www.lcbxiuxiu.tech是我阿里云地址 请换成你自己的服务器地址) dubbo.registry.address=zookeeper://www.lcbxiuxiu.tech:2181 #dubbo protocol 指定dubbo协议 将服务暴露在20880端口 dubbo.protocol.name=dubbo dubbo.protocol.port=20880
3、修改启动类并启动
@SpringBootApplication //此注解为了自动开启dubbo @EnableDubbo public class BootEgoUserServiceProviderApplication { public static void main(String[] args) { SpringApplication.run(BootEgoUserServiceProviderApplication.class, args); } }
4、启动成功后画面在远程dubbo访问
5)创建boot-ego-order-service-comsumer消费者模块
此模块目录结构如下(config是第二种方式用的,此处请先忽略):
1、修改pom.xml
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 org.springframework.boot spring-boot-starter-parent 2.2.2.RELEASE com.sxt boot-ego-order-service-comsumer 1.0 boot-ego-order-service-comsumer springboot集成dubbo的消费者 1.8 2.7.3 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-devtools runtime true org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine com.sxt ego_interface 1.0-SNAPSHOT compile org.apache.dubbo dubbo-spring-boot-starter ${dubbo.version} org.apache.dubbo dubbo-dependencies-zookeeper ${dubbo.version} pom org.slf4j slf4j-log4j12 org.springframework.boot spring-boot-maven-plugin
2、创建OrderServiceImpl
package com.sxt.service.impl; import com.sxt.domain.UserAddress; import com.sxt.service.OrderService; import com.sxt.service.UserService; import org.springframework.stereotype.Service; import org.apache.dubbo.config.annotation.Reference; import java.util.List; @Service //此处是spring的 帮你自动创建对象与注入 public class OrderServiceImpl implements OrderService { @Reference //此处是apache.dubbo 代替引入远程对象 private UserService userService; public void setUserService(UserService userService) { this.userService = userService; } @Override public ListinitOrder(String userId) { return this.userService.getUserAddressByUserId(userId); } }
3、修改启动类并启动
package com.sxt; import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @EnableDubbo public class BootEgoOrderServiceComsumerApplication { public static void main(String[] args) { SpringApplication.run(BootEgoOrderServiceComsumerApplication.class, args); } }
4、在测试类中测试
package com.sxt; import com.sxt.domain.UserAddress; import com.sxt.service.OrderService; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.io.IOException; import java.util.List; @SpringBootTest class BootEgoOrderServiceComsumerApplicationTests { @Autowired OrderService orderService; @Test void contextLoads() throws IOException { ListuserAddresses = orderService.initOrder("sxt"); for (UserAddress userAddress : userAddresses) { System.out.println(userAddress.getId()+" "+userAddress.getAddress()); } //想在dubbo首页显示消费者 阻止程序停止 System.in.read(); } }
5、修改application.properties后启动上面的测试类即可
#application.name dubbo.application.name=boot-ego-order-service-comsumer #address dubbo.registry.address=zookeeper://自己的服务器地址:2181
小结第一种方式:
1.提供者只需要在application.properties中声明:模块名字,注册中心地址,连接规则(使用什么协议,暴露什么端口)。其余的由service实现类中的@service(apache.dubbo)注解帮忙创建和暴露了对象
2.消费者只需要在application.properties中声明:模块名字,注册中心地址。其余由service实现类中的@service(spring的)注解帮忙创建和注入对象,@Reference(apache.dubbo)帮忙引入远程服务,在测试类中就可以使用@Autowired装配对象使用其方法