I posted the annotation method yesterday. Someone sent me an email hoping to release a version of the XML format. It was originally possible to do it before 12 o'clock, but there was a slight problem with the computer, which caused the idea to crash wildly. I have been doing it for a long time and I won't say much nonsense. I hope everyone can point out any errors and send it to my email address.
Using dubbo is definitely multi-modular, so let's create an aggregation project first
This is the project structure
Dubbo_demo pom is mainly used to aggregate business modules without any business processing.
<?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>com.hzb</groupId> <artifactId>Dubbo_demo</artifactId> <version>0.0.1-SNAPSHOT</version> <!--We are multi-module development, so we need to become pom--> <packaging>pom</packaging> <!--System module--> <modules> <module>hzbdubbo-consumer</module> <module>hzbdubbo-provider</module> </modules> <!--Configuration file properties--> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties></project>
Then follow the above method to create two projects, one is the service provider and the service consumer
If the provider and consumers don’t understand it, click this link to learn about it. http://dubbo.io/
Service Provider Code Snippet:
Generate basic projects according to the first image
Code structure: As long as you don't understand the demo according to the standard structure, you can understand it.
Service provider pom:
<?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>com.hzb.dubbo.provider</groupId><artifactId>hzbdubbo-provider</artifactId><packaging>jar</packaging><name>hzbdubbo-provider</name><description>Demo project for Spring Boot</description><parent> <groupId>com.hzb</groupId> <artifactId>Dubbo_demo</artifactId> <version>0.0.1-SNAPSHOT</version></parent><properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version></properties><dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>1.5.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>1.5.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <version>1.5.2.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.4.10</version> <exclusions> <exclusion> <artifactId>spring</artifactId> <groupId>org.springframework</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> <exclusions> <exclusion> <exclusion> <artifactId>slf4j-log4j12</artifactId> <groupId>org.slf4j</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> </dependencies><build> <plugins> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.5.2.RELEASE</version> </plugin> </plugins></build></project>
Pay attention to what the parent in a pom corresponds to the aggregate pom
Configuration in dubbo.xml:
<?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"> <!-- For configuration, please refer to http://dubbo.io/User+Guide-zh.htm --> <!-- The service provider application name, used to calculate dependencies --> <dubbo:application name="dubbo-provider" owner="dubbo-provider" /> <!-- Define zookeeper registration center address and protocol--> <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" client="zkclient" /> <!-- Define Dubbo The default port of dubbo protocol is 20880. If it is configured as -1 or the port is not configured, a port that is not occupied will be assigned --> <dubbo:protocol name="dubbo" port="-1" /> <!-- Declare the service interface that needs to be exposed --> <dubbo:service interface="com.hzb.dubbo.provider.DemoService" ref="demoService" timeout="10000" /> <!-- Implement the service like local beans --> <bean id="demoService" /> </beans>
To understand the meaning of configuration, click this link: http://dubbo.io/books/dubbo-user-book-en/English version https://help.github.com/categories/github-pages-basics/
Service Provider Startup Class Code:
package com.hzb.dubbo.provider;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.ImportResource;@SpringBootApplication// Use dubbo.xml to configure @ImportResource(value = {"classpath:dubbo.xml"})public class DubboProviderApplication { public static void main(String[] args) { SpringApplication.run(DubboProviderApplication.class, args); System.out.println("Server Provider Launched Successfully"); }}Service service interface code:
Business implementation code: "
Next is to serve consumers:
Project structure:
Consumers should also pay attention to the differences between pom and the above and the dependency.
<?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>com.hzb.dubbo.consumer</groupId> <artifactId>hzbdubbo-consumer</artifactId> <packaging>jar</packaging> <name>hzbdubbo-consumer</name> <description>Demo project for Spring Boot</description> <parent> <groupId>com.hzb</groupId> <artifactId>Dubbo_demo</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>1.5.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>1.5.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version>1.5.2.RELEASE</version> <scope>test</scope> </dependency> <!-- Format Dependencies provided by Alibaba--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.1.41</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.4.10</version> <exclusions> <exclusion> <artifactId>spring</artifactId> <groupId>org.springframework</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> <exclusions> <exclusion> <artifactId>slf4j-log4j12</artifactId> <groupId>org.slf4j</groupId> </exclusion> </exclusion> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <dependency> <groupId>com.hzb.dubbo.provider</groupId> <artifactId>hzbdubbo-provider</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> <build> <plugins> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.5.2.RELEASE</version> </plugin> </plugins> </build></project>
dubbo.xml configuration:
Startup class:
Test call service controller
The next step is to start the service provider first and then start the consumer
Running results
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.