2019 has arrived, I hope I have a new beginning like everyone else. No matter how much trouble and unbearable I have had in the past 18 years, I just need to walk the road under my feet.
Now microservices are more and more common, and new projects are basically using microservices. Here we will not discuss the benefits of microservices. Our common microservices architecture is SpirngCloud, which integrates various components, making our development very convenient indeed. In fact, there is another architectural style, SpringBoot + Dubbo, so today we will talk about how SpringBoot integrates Dubbo to do microservices architecture.
The project structure
We want to develop a sound microservice architecture, so it is very important to do a good job of the dependencies between modules, so let’s take a look at our project structure first.
In fact, we built this project mainly to simulate such a process, in the order module to call the interface of the membership module. Here I will not say how to install ZooKeeper and Dubbo, this thing is a hundred hundred, if there is a problem, please leave a message at the bottom, I will see the reply. (It is recommended that you try to install in the Linux environment)
Build the project
Next we first build our parent project (SpringBoot2.0-Dubbo-parent) to see which Maven coordinates we need to introduce. Here are some maven coordinates we need for SpringBoot2.0 to integrate Dubbo. I didn’t put the entire POM file up here. It’s too long to look at, but I’ve uploaded it to Github.
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> The < version > 2.0.1. RELEASE < / version > < / parent > <! - write a unified version control - > < properties > < project. Build. SourceEncoding > utf-8 < / project. Build. SourceEncoding > Piler < maven.com. Source > 1.8 < / maven.com piler. Source > < maven.com piler. Target > 1.8 < / maven.com piler. Target > < Java version - > 1.8 < / Java version > < curator - framework > 4.0.1 < / curator - framework > < zookeeper version > 3.4.13 < / zookeeper version > < dubbo. Starter. Version > 0.2.0 < / dubbo. Starter. Version > < / properties > <dependencies> <! <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>${dubbo.starter.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>Copy the code
The next step is to build our interface management module (SpringBoot2.0-Dubo-public-apI-service), under which all module interface definitions must be placed. There are no special Maven coordinates to import from this module.
Next, build the Member interface module (SpringBoot2.0-Dubo-public-apI-member-service). This module is a sub-module of the interface management module. There are no maven coordinates in this module that need to be introduced in particular.
Springboot2.0-dubo-public-api-member-service-impl (” member module “); this module is part of the parent project and does not exist under the interface management module. This module needs to introduce a Maven coordinate, because it needs to implement the interface defined by the member module, so it must introduce the member module.
< the dependency > < groupId > com. Xiaotao < / groupId > < artifactId > springboot2.0 dubbo - public - API - member - service < / artifactId > < version > 1.0 - the SNAPSHOT < / version > < / dependency >Copy the code
Finally, we will build the presentation layer of the order module (SpringBoot2.0-Dubo-order-web). This module will also introduce a Maven, because it will call the interface of the member module, so we will introduce the Maven of the member module.
< the dependency > < groupId > com. Xiaotao < / groupId > < artifactId > springboot2.0 dubbo - public - API - member - service < / artifactId > < version > 1.0 - the SNAPSHOT < / version > < / dependency >Copy the code
Project to improve
At this point, the entire module is created, and all we have left to do is refine our project. The order module calls the member module interface, so we should first create an interface in the member interface module.
Public interface IMemberService {// Member interface String getUser(); }Copy the code
The implementation module of the member interface implements the interface
@Service
public class MemberServiceImpl implements IMemberService {
@Override
public String getUser() {
System.out.println("Order service invokes member service");
return "Order service invokes member service"; }}Copy the code
Note: We used @service to register our Service in the container, but now we are using Dubbo so we are registering our Service in Dubbo, so be careful when you guide the package here.
The module is to be started, so we need to write our startup class
@EnableDubbo @SpringBootApplication public class AppMember { public static void main(String[] args) { SpringApplication.run(AppMember.class, args); }}Copy the code
Note: the @enableDubbo annotation is used to enable the Dubbo service. Without this annotation, the service will not be registered in Dubbo.
Finally, we need to configure our Dubbo. Where will our Dubbo register the service? We haven’t configured this yet
server:
port: 9002
dubbo:
application:
########### Register to the name of the registry ############
name: member
########### Uses the protocol and port number ################
protocol:
########### Uses the Dubbo protocol ####################
name: dubbo
########### The port number of dubbo is 20880###########
port: 20881
registry:
########### Registry address #####################Address: zookeeper: / / 192.168.50.128:2181 spring: the output: ANSI: enabled: alwaysCopy the code
Here we order the module even if done, will be up members to implement the module, and then access the Dubbo address (http://192.168.50.128:8080/dubbo-admin-2.6.0/), here is my address, you want to access to your address.
We can see from the console that the Dubbo service has been registered.
Looking at the Dubbo control panel, we can see that the service is indeed registered.
Finally, let’s talk about how we call in the order module. First we write an interface in the order module to call our member module interface.
@RestController
public class OrderController {
@Reference
private IMemberService memberService;
@GetMapping("/orderToMember")
public String orderToMember() {
String user = memberService.getUser();
System.out.println(user);
returnuser; }}Copy the code
Note: the @reference annotation is the same as the @autowired annotation we used earlier, but we registered the service in Dubbo, so we need to fetch the service from Dubbo, so we have to use this annotation.
Start the class
@SpringBootApplication @EnableDubbo public class AppOrder { public static void main(String[] args) { SpringApplication.run(AppOrder.class, args); }}Copy the code
The last step is under configuration application.yml
server:
port: 8001
dubbo:
application:
##### The name of the registration service
name: order
##### Registry addressRegistry: address: zookeeper: / / 192.168.50.128:2181##### Timeout period for invoking the service
consumer:
timeout: 5000
spring:
output:
ansi:
enabled: alwaysCopy the code
At this point we are done writing the entire project, so finally we start the order module to see if the call succeeds.
Finally, through the test, we can find that there is no problem. Let’s see if Dubbo’s control panel has customers.
It turned out that there was indeed a consumer.
At this point our SpringBoot2.0 integration with Dubbo is complete.
The address of the project: https://github.com/liangbintao/springboot2.0-dubbo