1,Dubbo
1.1 introduction to Dubbo
Apache Dubbo is a high-performance Java RPC framework. Its predecessor is Alibaba's open source and lightweight open source Java RPC framework, which can be seamlessly integrated with the Spring framework. In 2018, Alibaba donated this framework to the apache foundation
1.2 Dubbo architecture
dubbo consists of Container, Provider, Registry, Consumer and Monitor.
Container: service running container
Provider: the service provider that exposes the service
Registry: the registry for service registration and discovery
Consumer: the service consumer that calls the remote service
Container: the monitoring center that counts the number and time of service calls
2. Service registry Zookeeper
2.1 introduction to Zookeeper
Zookeeper is a sub project of Apache Hadoop. It is a tree type directory service that supports change push. It is suitable to be used as the registry of Dubbo service. It has high industrial intensity, can be used in production environment, and is recommended.
2.2 Zookeeper Download
Installation steps:
- Check jdk environment
- Unzip the download file
- Create a data folder under the Zookeeper root directory to store data
- Modify the zoo in the conf directory_ Change the name of sample.cfg to zoo.cfg
- Modify configuration file zoo.cfg: dataDir = point to create data directory
2.3 start and stop of Zookeeper
Under Linux environment, enter the bin directory of Zookeeper
Start service command:. / zkServer.sh start
Stop service command:. / zkServer.sh stop
View service status:. / zkServer.sh status
Client connection:. / zkCli.sh
3. Dubbo quick start
As an RPC framework, Dubbo's core function is to realize cross network remote call. This section is to create two applications, one as a service provider and the other as a service consumer. Dubbo enables the service consumer to remotely invoke the service provider
Square method.
3.1 service provider development
pom.xml configuration
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <spring.version>5.0.5.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <!-- dubbo relevant --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.0</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.7</version> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <dependency> <groupId>javassist</groupId> <artifactId>javassist</artifactId> <version>3.12.1.GA</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <configuration> <!-- Specify port --> <port>8081</port> <!-- Request path --> <path>/</path> </configuration> </plugin> </plugins> </build>
web.xml configuration
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
Create service interface
public interface HelloService { public String sayHello(String name); }
Create interface implementation class
import com.alibaba.dubbo.config.annotation.Service; @Service public class HelloServiceImpl implements HelloService { public String sayHello(String name) { return "hello " + name; } }
Note: @ Service package is an alibaba package, not a spring package
Create applicationContext-service.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- The current application name is used to calculate the dependency between applications in the registry. Note: the application names of consumers and providers are different --> <dubbo:application name="dubbodemo_provider" /> <!-- Connect service registry zookeeper ip by zookeeper Server ip address--> <dubbo:registry address="zookeeper://192.168.134.129:2181"/> <!-- Registration agreement and port --> <dubbo:protocol name="dubbo" port="20881"></dubbo:protocol> <!-- Scan the specified package and join@Service Annotated classes are published as services --> <dubbo:annotation package="com.lxs.service.impl" /> </beans>
Maven startup service: tomcat:run
3.2 development of service consumers
pom.xml ditto
web.xml configuration
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- Specify the configuration file to load through parameters contextConfigLocation load --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext-web.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
Copy and publish the service of the project
Write Controller
@Controller @RequestMapping("/demo") public class HelloController { @Reference private HelloService helloService; @RequestMapping("/hello") @ResponseBody public String getName(String name){ //Remote call String result = helloService.sayHello(name); System.out.println(result); return result; } }
Create applicationContext-web.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- The current application name is used to calculate the dependency between applications in the registry. Note: the application names of consumers and providers are different --> <dubbo:application name="dubbodemo-consumer" /> <!-- Connect service registry zookeeper ip by zookeeper Server ip address--> <dubbo:registry address="zookeeper://192.168.134.129:2181"/> <!-- Expose the interface by scanning --> <dubbo:annotation package="com.lxs.service.impl" /> </beans>
maven: tomcat:run
Enter in the browser http://localhost:8082/demo/hello.do?name=Jack , view the browser output