We created two new projects to simulate the service provider and service consumers, the Spring boot integration Dubbo is divided into the following steps:
1. Installing zookeeper
Docker installation is recommended and can be done using the following commands
(1) Docker pull zookeeper:3.4.11
(2) Docker run--name zookeeper-p 2181:2181--restart always-d 56d414270ae3 (-D followed by Mirror ID)
2. Add Dubbo and Zookeeper dependencies to the project of service providers and service consumers, respectively
<Dependency> <groupId>Com.alibaba.boot</groupId> <Artifactid>Dubbo-spring-boot-starter</Artifactid> <version>0.1.0</version> </Dependency> <!--https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient - <Dependency> <groupId>Com.github.sgroschupf</groupId> <Artifactid>Zkclient</Artifactid> <version>0.1</version> </Dependency>
3. Install Dubbo Management Background
There is a Dubbo-admin project in Dubbo released project, you can make this project into a war package, prepare a tomcat, delete the contents of the root folder under Wabapps, Unzip the Dubbo-admin.war to root, modify the zookeeper address in the dubbo.properties under Wen-inf to change to its own, in order to prevent port conflicts can be modified under the Conf of Tomcat Server.xml And then run Startup.bat under the bin to start the Dubbo admin background, enter the username and password (root/root or guest/guest)
4. Writing the service Code
(1) Create a new interface that indicates the services we want to provide
Package Com.springboot.service.ticket; Public Interface Ticketservice { public String getticket ();}
(2) Implement the service interface and use Dubbo's @service to annotate the service and inject the service into the IOC container as a component
Package Com.springboot.service.ticket; Import org.springframework.stereotype.Component; Import Com.alibaba.dubbo.config.annotation.Service, @Service @component Public class Implements ticketservice{ @Override public String getticket () { // TODO auto-generated Method stub return "Mission Spy 6"; }
(3) Configuration services
#服务名称
Dubbo.application.name=springboot-dubbo-provider#zookeeper Address
dubbo.registry.address=zookeeper://192.168.1.215:2181
#服务的扫描路径, the @service annotations will be treated as a service release dubbo.scan.base-packages=com.springboot.servicedubbo.protocol.name= dubbodubbo.protocol.port=20880
5. Writing consumer-side code
(1) Create a new interface identical to the server, including the same path as the interface
(2) Call the service. Injecting the service interface using @reference
PackageCom.springboot.service.user;ImportOrg.springframework.stereotype.Service;Importcom.alibaba.dubbo.config.annotation.Reference;ImportCom.springboot.service.ticket.TicketService; @Service Public classuserservice {@Reference ticketservice ticketservice; Publicstring Buyticket () {string ticket=Ticketservice.getticket (); System.out.println ("Buy a Movie ticket:" +ticket); returnticket; }}
(3) configuration
Server.port=8081dubbo.protocol.name=dubbodubbo.protocol.port=20880dubbo.application.name= springboot-dubbo-consumerdubbo.registry.address=zookeeper://192.168.1.215:2181
(4) Call consumer services
PackageCom.springboot.contrller;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.web.bind.annotation.GetMapping;ImportOrg.springframework.web.bind.annotation.RestController;ImportCom.springboot.service.user.UserService; @RestController Public classBuycontroller {@Autowired userservice userservice; @GetMapping ("/buy") PublicString Buy () {returnUserservice.buyticket (); }}
6. Publish the service, subscribe to the service.
Start service-side projects and consumer projects separately, and see service providers and consumers in the Dubbo management background
Invoking consumer Services
Spring boot-18. Publishing distributed services using Dubbo