Overview
First of all, this article is to learn the types of blog content, if there is a similar purely learning. The standard mainly combines zookeeper and Dubbo to make a simple example. At present, the general website architecture with the development of business, logic more and more complex, more and more data volume, more and more interaction after the conventional program evolution process.
Secondly, what service governance do we need to do when more and more services are available?
Dubbo mainly deals with services, constraining the relationship between service providers and consumers. Dubbo's relationship with consumers, providers and registrations is as follows:
Zookeeper use
Although ZooKeeper is a coordinated service for distributed systems, it is also a distributed application. ZooKeeper follows a simple client-server model where the client is the node that uses the service (that is, the machine), and the server is the node that provides the service. The collection of ZooKeeper servers forms a ZooKeeper aggregate (ensemble), where simple recording ZooKeeper is used;
: Http://www.apache.org/dyn/closer.cgi/zookeeper after downloading, Decompression found inside there are double-click zkserver.cmd (Window Double-click, Linux needs zkserver.sh) start zookeeper. The default open is 2181 port, double-click zkcli.cmd (Window Environment) to start the client under the test server. The usual zookeeper directives can be entered on the client:
1, display root directory, file: LS/use the LS command to view what is contained in the current ZooKeeper2, show root directory, file: LS2/View data for current node data and to see the number of updates3, create a file, and set the initial content create [-S] [-e] path data ACL where-s or-e specify node attributes, sequential or temporary nodes, if not specified, represent persistent nodes;
ACLs are used for permission control. For example, use the following: Create/zk"Test"Create a new znode node "ZK" and the string associated with it4, get the contents of the file:Get/ZK confirms if Znode contains the string we created5, modify the contents of the file:Set/zk"Zkbak"set the string associated with ZK6, delete file: delete/ZK Delete the znode you just created7, quit client: Quit8, Help commands:
Of course, you can also use the client management software to query the current zookeeper usage. Download: https://issues.apache.org/jira/secure/attachment/12436620/ZooInspector.zip; Click zooinspector\build\ after decompression Zookeeper-dev-zooinspector.jar Click on the green button in the upper left corner, enter the address and port of ZK server, and then you will be able to see the ZK node data information after successful connection.
Code writing
Parent encoding
Define the parent public project, where the Pom.xml content is as follows
<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.justin</groupId> <artifactId>parent</artifactId> < Version>1.0-snapshot</version> <packaging>pom</packaging> <name>parent</name> <url>http://maven.apache.org</url><parent> <groupId>org.springframework.boot</groupId> <artifactId> Spring-boot-starter-parent</artifactid> <version>1.5.2. release</version> </parent> <properties> <project.build.sourceencoding>utf-8</project.build.sourceEncoding> <dubbo-spring-boot>1.0.0</dubbo-spring-boot> </properties> <dependencies> <!--spring boot Dubbo Dependent-<dependency > <groupId>io.dubbo.springboot</groupId> <artifactid>spring-boot-starter-dubbo</ Artifactid> <version>${dubbo-spring-boot}</version> </dependency> <!--spring boot Web dependent--& Gt <dependency> <groupId>org.springframework.boot</groupId> <artifactId> Spring-boot-starter-web</artifactid> </dependency> <!--Spring Boot Test dependency--<dependency> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-starter-test</ artifactid> <scope>test</scope> </dependency> <dependency> <groupId>junit< /groupid> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies></project>
Define a common entity
Package Com.justin.dubbo.domain;import java.io.Serializable; Public classCity implements Serializable {Private StaticFinalLongSerialversionuid =-1L; /** * City number*/ PrivateLong ID; /** * Province number*/ PrivateLong Provinceid; /** * City name*/ PrivateString CityName; /** * Description*/ PrivateString description; PublicCity () {} PublicCity (Long ID, long Provinceid, string cityname, string description) { This. ID =ID; This. Provinceid =Provinceid; This. CityName =CityName; This. Description =description; } PublicLong getId () {returnID; } Public voidsetId (Long id) { This. ID =ID; } PublicLong Getprovinceid () {returnProvinceid; } Public voidSetprovinceid (Long provinceid) { This. Provinceid =Provinceid; } PublicString Getcityname () {returnCityName; } Public voidsetcityname (String cityname) { This. CityName =CityName; } PublicString getdescription () {returndescription; } Public voidsetdescription (String description) { This. Description =description; } @Override PublicString toString () {return "city{"+"id="+ ID +", provinceid="+ Provinceid +", Cityname= '"+ CityName +'\ ''+", description= '"+ Description +'\ ''+'}'; }}
Define a common interface
Package Com.justin.dubbo.service;import com.justin.dubbo.domain.City; Public Interface Cityservice { public city findcitybyname (String cityname);}
Provider service Provider Code
Implementing a common interface
Package Com.justin.dubbo.serviceimpl;import Com.justin.dubbo.domain.city;import Com.justin.dubbo.service.cityservice;import Com.alibaba.dubbo.config.annotation.Service; @Service (Version="1.0.0") Public classCityserviceimpl implements Cityservice { PublicCity Findcitybyname (String cityname) {return NewCity (1L,2L,"Wenling","it's my hometown."); }}
Main program
@SpringBootApplication Public class App { publicstaticvoid main (string[] args) { System. out " Hello world! Start Service ... " ); Springapplication.run (App.class); }}
Application.properties Content
# # Dubbo Service provider configuration # App name spring.dubbo.application.name=provider# Registry address spring.dubbo.registry.address=zookeeper:// 127.0.0.1:2181# protocol name spring.dubbo.protocol.name=dubbo# Protocol port spring.dubbo.protocol.port=20880# Service Class package directory Spring.dubbo.scan=com.justin.dubbo.serviceimplcus service provider code
Customer Consumer calls service code
Invoking a service Code entry
import Com.alibaba.dubbo.config.annotation.reference;import com.justin.dubbo.MyCityService.MyServiceCity; Import Com.justin.dubbo.domain.city;import Com.justin.dubbo.service.cityservice;import org.springframework.stereotype.Component; @Component Public classCityserviceimpl implements myservicecity{@Reference (version="1.0.0")//Invoke service itemPrivateCityservice Cityservice; @Override Public voidprintcity () {String CityName="Wenling"; City City=Cityservice.findcitybyname (CityName); System. out. println (City.tostring ()); }}
Main program
import Com.justin.dubbo.mycityservice.myservicecity;import Com.justin.dubbo.serviceimpl.CityServiceImpl; Import Org.springframework.boot.springapplication;import Org.springframework.boot.autoconfigure.springbootapplication;import Org.springframework.context.ConfigurableApplicationContext;/** * Hello world! **/@SpringBootApplication Public classApp { Public Static voidMain (string[] args) {System. out. println ("Hello world!" ); Configurableapplicationcontext Run= Springapplication.run (App.class, args); Myservicecity Cityservice= Run.getbean (Cityserviceimpl.class); Cityservice.printcity (); }}
Configuring Application.properties Content
# # Avoid and server engineering port conflicts server.port=8081# # Dubbo Service Consumer configuration spring.dubbo.application.name= Consumerspring.dubbo.registry.address=zookeeper://127.0.0.1:2181spring.dubbo.scan= Com.justin.dubbo.service
Reference articles
Zookeeper Introduction
Springboot Integration Dubbo/zookeeper Detailed SOA case
Build a Dubbo+zookeeper platform from the start
Example Source: Demo Download
Create Maven Springboot + Zookeeper +dubbo instance on idea