手把手教你Dubbo与SpringBoot常用两种方式整合
一、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 list<useraddress> getuseraddressbyuserid(string userid); }
3、消费者接口orderservice
package com.sxt.service; import com.sxt.domain.useraddress; import java.util.list; //消费者接口 public interface orderservice{ //初始化订单 public list<useraddress> initorder(string userid); }
3)创建boot-ego-user-service-provider提供者模块
此模块目录结构如下(config是第二种方式用的,此处请先忽略):
4)修改pom.xml加入依赖
添部分关键依赖(其余自动生成):
<!--加入对ego_interface依赖-->
<!--这个是上面第二步创建的你所要使用的接口类的依赖-->
<dependency> <groupid>com.sxt</groupid> <artifactid>ego_interface</artifactid> <version>1.0-snapshot</version> </dependency>
<!-- dubbo spring boot starter --> <dependency> <groupid>org.apache.dubbo</groupid> <artifactid>dubbo-spring-boot-starter</artifactid> <version>${dubbo.version}</version> </dependency> <!-- 使用zk 做注册中心,dubbo 需要的依赖 --> <dependency> <groupid>org.apache.dubbo</groupid> <artifactid>dubbo-dependencies-zookeeper</artifactid> <version>${dubbo.version}</version> <type>pom</type> <exclusions> <exclusion> <groupid>org.slf4j</groupid> <artifactid>slf4j-log4j12</artifactid> </exclusion> </exclusions> </dependency>
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 list<useraddress> address=new arraylist<>(); static { address.add(new useraddress(1, "*广场", "bj")); address.add(new useraddress(2, "上海迪士尼", "sh")); } @override public list<useraddress> 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
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>2.2.2.release</version> <relativepath/> <!-- lookup parent from repository --> </parent> <groupid>com.sxt</groupid> <artifactid>boot-ego-order-service-comsumer</artifactid> <version>1.0</version> <name>boot-ego-order-service-comsumer</name> <description>springboot集成dubbo的消费者</description> <properties> <java.version>1.8</java.version> <dubbo.version>2.7.3</dubbo.version> </properties> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-devtools</artifactid> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupid>org.projectlombok</groupid> <artifactid>lombok</artifactid> <optional>true</optional> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> <exclusions> <exclusion> <groupid>org.junit.vintage</groupid> <artifactid>junit-vintage-engine</artifactid> </exclusion> </exclusions> </dependency> <!--加入对ego_interface依赖--> <dependency> <groupid>com.sxt</groupid> <artifactid>ego_interface</artifactid> <version>1.0-snapshot</version> <scope>compile</scope> </dependency> <!-- dubbo spring boot starter --> <dependency> <groupid>org.apache.dubbo</groupid> <artifactid>dubbo-spring-boot-starter</artifactid> <version>${dubbo.version}</version> </dependency> <!-- 使用zk 做注册中心,dubbo 需要的依赖 --> <dependency> <groupid>org.apache.dubbo</groupid> <artifactid>dubbo-dependencies-zookeeper</artifactid> <version>${dubbo.version}</version> <type>pom</type> <exclusions> <exclusion> <groupid>org.slf4j</groupid> <artifactid>slf4j-log4j12</artifactid> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> </project>
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 list<useraddress> initorder(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 { list<useraddress> useraddresses = 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装配对象使用其方法
上一篇: goweb-go语言基础
下一篇: thinkphp5基础