This article introduces the code integration and release of Springboot integrated Dubbo, and shares it with you, as follows:
1. boot-dubbo-api related
Open the boot-dubbo-api project, create a package under src/main/java, and create the interface TestService.java that you need to expose dubbo, and create an entity class to test User.java. As shown in the figure below:
Create file and package structure
User.java
package com.boot.domain;import lombok.Data;import java.io.Serializable;@Datapublic class User implements Serializable { private Integer id; private String username; private String password; private Integer age; private Integer gender;}TestService.java
package com.boot.service;import com.boot.domain.User;public interface TestService { String saysHello(String str); User findUser();}2. boot-dubbo-provider related
First, let’s take a look at the total content that needs to be written and the hierarchy diagram of the file.
boot-dubbo-provider project structure diagram
Step 1: We first implement the interface we defined on boot-dubbo-api, create a TestServiceImpl class and implement TestService
package com.boot.service.impl;import com.alibaba.dubbo.config.annotation.Service;import com.boot.domain.User;import com.boot.service.TestService;import java.text.SimpleDateFormat;import java.util.Date;@Service(version = "1.0.0")public class TestServiceImpl implements TestService { @Override public String saysHello(String str) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); return dateFormat.format(new Date()) + ": " + str; } @Override public User findUser() { User user = new User(); user.setId(1001); user.setUsername("scott"); user.setPassword("tiger"); user.setAge(20); user.setGender(0); return user; }}Note: The @Service annotation in the code is com.alibaba.dubbo.config.annotation.Service.
Step 2: Create a config folder under resources and create a spring-dubbo.xml configuration file under config.
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <dubbo:application name="provider"/> <!-- ip address of the registration center --> <dubbo:registry address="zookeeper://127.0.0.1:2181"/> <!-- Scan the annotation package path, multiple packages are separated by commas, and do not fill in pacakge to indicate scanning all classes in the current ApplicationContext --> <dubbo:annotation package="com.boot.service.impl"/></beans>
Step 3: Create a new Springboot entry class under the com.boot package and create a ProviderApplication.java file.
package com.boot;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.ImportResource;import java.io.IOException;@SpringBootApplication@ImportResource({"classpath:config/spring-dubbo.xml"})public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); try { System.in.read(); } catch (IOException e) { e.printStackTrace(); } }}Step 4: Finally, create application.yml, Springboot configuration file under the resources folder.
# Write springboot configuration information here
3. boot-dubbo-consumer related
First, let’s take a look at the total content that needs to be written and the hierarchy diagram of the file.
boot-dubbo-consumer project structure diagram
Step 1: Write our Controller control class, create a new TestController class under the com.boot.controller package, and write the access address.
package com.boot.controller;import com.alibaba.dubbo.config.annotation.Reference;import com.boot.domain.User;import com.boot.service.TestService;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/")public class TestController { @Reference(version = "1.0.0") private TestService testService; @GetMapping("hello") public String hello() { return testService.sayHello("Hello springboot and dubbo!"); } @GetMapping("user") public User user() { return testService.findUser(); }}Step 2: Create a config folder under resources and create a spring-dubbo.xml configuration file under config.
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <dubbo:application name="consumer"/> <dubbo:registry address="zookeeper://127.0.0.1:2181"/> <dubbo:annotation package="com.boot.controller"/></beans>
Step 3: Create a new Springboot entry class under the com.boot package and create a ConsumerApplication.java file.
package com.boot;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.ImportResource;@SpringBootApplication@ImportResource({"classpath:config/spring-dubbo.xml"})public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); }}Step 4: Finally, create application.yml, Springboot configuration file under the resources folder.
# Write springboot configuration information here server: port: 8080 context-path: /
At this point, the code part has been written! ! !
4. Install the zookeeper registration center into the computer
Download address: zookeeper download address
Click to download the version that suits you, as shown in the picture
Download zookeeper
After downloading, unzip the file, enter the conf folder, copy a copy of zoo_sample.cfg, and generate the zoo.cfg file in this directory.
Copy and generate zoo.cfg file
Enter the bin directory and run the zkServer.cmd file.
Run zkServer.cmd
Click to run and appear as shown in the following figure
Run successfully
5. Now we can finally run our project
First run the main function of our ProviderApplication.java file, and then run the main function of the ConsumerApplication.java file.
Open a browser to access
http://localhost:8080/hello
http://localhost:8080/user
Well, the exciting time has come, and we have finally completed the integration of Springboot and Dubbo.
Project address: springboot-dubbo project GitHub address
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.