From: Mason @ bysocket com
Contents of this article- 1, Why integrate Dubbo for SOA
- 2, Run the springboot Dubbo server and springboot Dubbo client projects
- 3, Detailed configuration of springboot Dubbo server and springboot Dubbo client projects
Spring Boot series: Spring Boot stuff https://www.bysocket.com/springboot
1, Why integrate Dubbo?
Dubbo is not only a high-performance RPC invocation framework, but also a solution for SOA Service governance.
Core:
- Remote communication, calling remote methods like local calls.
- Cluster fault tolerance
- Services are automatically discovered and registered, and service providers can be added or deleted smoothly.
We often use spring boot to expose HTTP services and follow JSON mode. However, the amount is gradually increasing. It is a governance scheme of SOA. This can expose the Dubbo service interface and provide it to the Dubbo consumer for RPC calls. Let's explain in detail how to integrate Dubbo.
2, Run the springboot Dubbo server and springboot Dubbo client projects
Operating environment: JDK 7 or 8, Maven 3.0 + technology stack: SpringBoot 1.5 +, Dubbo 2.5 +, ZooKeeper 3.3+
1.ZooKeeper service registry
ZooKeeper is a distributed, open source distributed application coordination service. It is a software that provides consistency services for distributed applications. Its functions include configuration maintenance, domain name service, distributed synchronization, group service, etc.
Download ZooKeeper, address http://www.apache.org/dyn/closer.cgi/zookeeper Unzip ZooKeeper
tar zxvf zookeeper-3.4.8.tar.gz
Create a new zoo in the conf directory CFG, follow the zoo of the directory_ sample. CFG configuration is as follows.
cd zookeeper-3.3.6/conf vim zoo.cfg
zoo. The CFG code is as follows (specify the log file directory yourself):
tickTime=2000 dataDir=/javaee/zookeeper/data dataLogDir=/javaee/zookeeper/log clientPort=2181
In the bin directory, start ZooKeeper
cd zookeeper-3.3.6/bin ./zkServer.sh start
2. git clone download project springboot learning example
See GitHub – https://github.com/JeffLi1993/springboot-learning-example :
git clone git@github.com:JeffLi1993/springboot-learning-example.git
Then Maven compiles and installs the project:
cd springboot-learning-example mvn clean install
3. Run the springboot Dubbo server Dubbo service provider project
Right click to run the main function of the ServerApplication application startup class of the springboot Dubbo server project. The following items appear in the Console, indicating that the project is started successfully:

This indicates that the Dubbo service has been started successfully and registered with ZK (ZooKeeper).
4. Run the springboot Dubbo client Dubbo service consumer project
Right click to run the main function of the springboot Dubbo client project ClientApplication application startup class. The following appears in Console:
... 2017-03-01 16:31:38.473 INFO 9896 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 2017-03-01 16:31:38.538 INFO 9896 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8081 (http) 2017-03-01 16:31:38.547 INFO 9896 --- [ main] org.spring.springboot.ClientApplication : Started ClientApplication in 6.055 seconds (JVM running for 7.026) City{id=1, provinceId=2, cityName='Wenling', description='It's my hometown'}
The last printed city information is obtained through Dubbo service interface call. It runs successfully. The following details each code and configuration.
3, Detailed configuration of springboot Dubbo server and springboot Dubbo client projects
The code is on GitHub, https://github.com/JeffLi1993/springboot-learning-example .
1. Explain the springboot Dubbo server Dubbo service provider project in detail
Springboot Dubbo server project directory structure
├── pom.xml └── src └── main ├── java │ └── org │ └── spring │ └── springboot │ ├── ServerApplication.java │ ├── domain │ │ └── City.java │ └── dubbo │ ├── CityDubboService.java │ └── impl │ └── CityDubboServiceImpl.java └── resources └── application.properties
a.pom.xml configuration
pom.xml relies on the spring boot starter Dubbo project. The project address is https://github.com/teaey/spring-boot-starter-dubbo . pom. The XML configuration is as follows
<?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>springboot</groupId> <artifactId>springboot-dubbo-server</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot-dubbo Server:: integration Dubbo/ZooKeeper Explain in detail SOA case</name> <!-- Spring Boot Start parent dependency --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> </parent> <properties> <dubbo-spring-boot>1.0.0</dubbo-spring-boot> </properties> <dependencies> <!-- Spring Boot Dubbo rely on --> <dependency> <groupId>io.dubbo.springboot</groupId> <artifactId>spring-boot-starter-dubbo</artifactId> <version>${dubbo-spring-boot}</version> </dependency> <!-- Spring Boot Web rely on --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Boot Test rely on --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- Junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies> </project>
b.application.properties configuration
## Dubbo service provider configuration spring.dubbo.application.name=provider spring.dubbo.registry.address=zookeeper://127.0.0.1:2181 spring.dubbo.protocol.name=dubbo spring.dubbo.protocol.port=20880 spring.dubbo.scan=org.spring.springboot.dubbo
The address and port configured by ZK here is the ZK built locally above. If you have your own ZK, you can modify the following configuration. The configuration is explained as follows:
- spring.dubbo.application.name application name
- spring.dubbo.registry.address registry address
- spring.dubbo.protocol.name agreement name
- spring.dubbo.protocol.port protocol port
- spring.dubbo.scan dubbo service class package directory
c.CityDubboServiceImpl.java city business Dubbo service layer implementation layer class
// Register as Dubbo service @Service(version = "1.0.0") public class CityDubboServiceImpl implements CityDubboService { public City findCityByName(String cityName) { return new City(1L,2L,"Wenling","It's my hometown"); } }
@The Service annotation identifies the Dubbo Service and specifies the version number through version.
d.City.java city entity class
If the entity class is called through RPC between Dubbo services, the serialization interface needs to be implemented. It is best to specify a serialVersionUID value.
2. Explain the springboot Dubbo client consumer service project in detail
Springboot Dubbo client project directory structure
├── pom.xml └── src └── main ├── java │ └── org │ └── spring │ └── springboot │ ├── ClientApplication.java │ ├── domain │ │ └── City.java │ └── dubbo │ ├── CityDubboConsumerService.java │ └── CityDubboService.java └── resources └── application.properties
pom.xml , CityDubboService.java,City.java has not changed. Dubbo consumers call the Dubbo interface by introducing an interface.
a.application.properties configuration
## Avoid conflicts with server project ports server.port=8081 ## Dubbo service consumer configuration spring.dubbo.application.name=consumer spring.dubbo.registry.address=zookeeper://127.0.0.1:2181 spring.dubbo.scan=org.spring.springboot.dubbo
Because the springboot Dubbo server project takes up port 8080, port 8081 is set here.
b.CityDubboConsumerService.java City Dubbo service consumers
@Component public class CityDubboConsumerService { @Reference(version = "1.0.0") CityDubboService cityDubboService; public void printCity() { String cityName="Wenling"; City city = cityDubboService.findCityByName(cityName); System.out.println(city.toString()); } }
@Reference(version = "1.0.0") subscribes to the interface version of 1.0.0 through this annotation Dubbo service for 0.
The CityDubboConsumerService is injected into the Spring container to obtain the Bean more conveniently, and then verify whether the Dubbo call is successful.
c.ClientApplication.java client startup class
@SpringBootApplication public class ClientApplication { public static void main(String[] args) { // Program startup entry // Start the embedded Tomcat and initialize the Spring environment and its Spring components ConfigurableApplicationContext run = SpringApplication.run(ClientApplication.class, args); CityDubboConsumerService cityService = run.getBean(CityDubboConsumerService.class); cityService.printCity(); } }
Explain this logic, which is to obtain the urban Dubbo service consumer bean from the bean container after startup. The Bean method is then invoked to verify whether the Dubbo call is successful.
Code example
The example readers of this article can view the module project name in the following warehouse: 2-x-spring-boot-weblux-handling-errors:
- Github: https://github.com/JeffLi1993/springboot-learning-example
- Gitee: https://gitee.com/jeff1993/springboot-learning-example
If you are interested in these, you are welcome to support star, follow, collect and forward!