Preface
With the development of the Internet, the scale of website applications has been continuously expanded, and conventional vertical application architectures can no longer cope with it. Distributed service architecture and mobile computing architecture are imperative. Dubbo is a distributed service framework, which was born in this case. Now the core business is extracted as an independent service, so that front-end applications can respond more quickly and stably.
What is Dubbo
Dubbo is Alibaba's open source distributed service framework. Its biggest feature is that it is structured in a layered manner. This method can decouple (or maximize loose coupling) between layers. From the perspective of service model, Dubbo adopts a very simple model, either the provider provides services or the consumer consumes services. Therefore, based on this point, we can abstract the two roles of service provider and service consumer. About registration center, agreement support, service monitoring and other contents
What can Dubbo do
When the website becomes larger, it is inevitable that the application will be split and serviced (microservices) will be needed to improve development efficiency, optimize performance, save key competitive resources, etc.
As more and more services, the URL address information of the service will explode, configuration management becomes very difficult, and the single point pressure of the F5 hardware load balancer is getting greater and greater.
When further development, the inter-service dependencies become complicated and even cannot tell which application is started before which application, architects cannot fully describe the application's architectural relationship.
Then, the number of calls of services becomes larger and larger, and the service capacity problem is exposed. How much machine support does this service require? When should I add the machine? etc……
When encountering these problems, you can use Dubbo to solve them.
What I share with you this time is the initial configuration of the dubbo framework application and the use of the zookeeper registration center; when it comes to the registration center, there are only two types of things I have used: zookeeper and Eureka. I use zk with dubbo, and Eureka with springcloud. Therefore, I will share with you some chapters about microservices later, hoping it will be helpful to you.
Install the registration center zookeeper
Dubbo framework provider and consumer
dubbo-admin deployment
Install the registration center zookeeper
First of all, we need to search the zookeeper download address online. I am on the Linux system, so we downloaded the zookeeper-3.3.6.tar.gz package and unzipped it through tar -zxvf zookeeper-3.3.6.tar.gz. It should be noted that the configuration files in the general package are the default sample version, and the default configuration file name of zookeeper is only zoo.cfg. In order not to specify the file name every time, we need to create a configuration file named zoo.cfg in the conf directory. The file content can be copied from zoo_sample.cfg or rename the file. The content is as follows vim zoo.cfg:
# The number of million seconds of each ticktickTime=2000 #Heartbeat frequency# The number of ticks that the initial # synchronization phase can takeinitLimit=10 #Restrict connection# The number of ticks that can pass between # sending a request and getting an acknowledgementsyncLimit=5# the directory where the snapshot is stored.dataDir=/tmp/zookeeper #Data storage folder# the port at which the clients will connectclientPort=2081 #zookeeper external port
I won't modify the parameter information here, and use the default; briefly introduce several commonly used commands on how to edit file content in Linux:
vim zoo.cfg: View file content
insert: Execute the insert command
esc: Cancel the command, then press:q: Exit, :wq: Save and exit
When we have zoo.cfg, we only need to enter its bin directory to find the zkServer.sh file, and start the zookeeper registration center by executing the ./zkServer.sh start command. The normal startup prompt is as follows:
Usually, zookeeper is on a separate server as a registration center, and the program (here refers to my local) needs to call another registration center. In addition, the registration center needs to check whether the open port of zookeeper can be connected. My local is Windows 10, so you don’t need to verify the port through the zookeeper client. You only need to use telnet ip 2081 to detect whether the port is open;
Dubbo framework provider and consumer
First of all, for convenience, we need to define a unified interface, which is the public interface extracted by the business. We package this interface separately into a module. Here is dubbo_api. We define the following interface in this module:
public interface UserService { List<MoUser> getUsers(); }Then, create a provider module, which depends on the dubbo_api module and implements the UserService interface, the code is as follows:
public class UserServiceImpl implements UserService { @Value("${server.port}") private int port; /** * @return */ @Override public List<MoUser> getUsers() { List<MoUser> list = new ArrayList<>(); for (int i = 0; i < 5; i++) { MoUser user = new MoUser(); user.setUserName("shenniu" + i); user.setUserPwd("port:" + port); list.add(user); } return list; }}As a service provider, to use the dubbo framework, you need to first introduce dubbo and then do some configuration. First, we need to introduce dubbo dependencies through maven in the dubbo_provider module. Zookeeper is used in the registration center, so we also need to introduce corresponding dependencies through maven:
<dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.8</version> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.3</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.example</groupId> <artifactId>dubbo_api</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
After completing the dependency, the rest is to configure dubbo's provider, create the configuration file of resources/dubbo-conf/server.xml, and the file content:
<?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"> <!-- Provider name--> <dubbo:application name="demo-provider" /> <!-- Service address of zookeeper registration center--> <dubbo:registry protocol="zookeeper" address="192.168.168.168:2081" /> <!-- Use dubbo protocol to expose the service on port 20880, protocol communication port--> <dubbo:protocol name="dubbo" port="20880" /> <!-- User service interface--> <dubbo:service interface="service.UserService" ref="userService" /> <!-- User Service Interface Implementation--> <bean id="userService"/></beans>
Add resource import @ImportResource("classpath:dubbo-conf/*.xml") in the DubboProviderApplication portal, and our service provider is completed here. Check the startup log:
There is no exception when the service provider starts. Let’s look at the caller to create a module of dubbo_consumer, and also introduce the module dependencies of dubbo_api public interface, then create a UserController, and expose a get user interface:
@RestControllerpublic class UserController { @Autowired private UserService userService; @GetMapping("/users") public List<MoUser> getUsers(){ return userService.getUsers(); }}The UserService here is the injection of the public interface; after the encoding is completed, the consumer side introduces dubbo and zookeeper dependencies:
<dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.8</version> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.3</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> </exclusions> </dependency>
We also create a configuration file with the name resources/dubbo-conf/client.xml and introduce this resource in the application entry:
@ImportResource("classpath:dubbo-conf/*.xml")@SpringBootApplicationpublic class DubboConsumerApplication { public static void main(String[] args) { SpringApplication.run(DubboConsumerApplication.class, args); }}The configuration content of client.xml is as follows:
<?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"> <!-- Service name--> <dubbo:application name="demo-consumer" /> <!-- zookeeper registration center exposes service address--> <dubbo:registry protocol="zookeeper" address="192.168.168.168:2081" /> <!-- User Service Interface--> <dubbo:reference id="userService" interface="service.UserService" /></beans>
At this time, a simple service provider, service caller configuration and encoding are completed, and the 3 modules are as follows:
Then start the provider and consumer respectively; then request the interface through the controller interface exposed by the consumer. My address here is http://localhost:8082/users:
The effect is that Consumer gets the result returned by Provider through the injected UserService interface call getUsers(), which also means that the simple use of the dubbo framework has been considered successful.
dubbo-admin deployment
As a popular rpc framework dubbo has this open source monitoring tool dubbo-admin. I have to say that the dubbo-admin.war package that is ready-made on the Internet can either not be used now or needs CSSDN points to download. There are many versions and it is a bit tricky. Decisively go to git to pull the source code and package it yourself. The git address is: https://github.com/apache/incubator-dubbo-ops. After pulling it down, we only need to pay attention to the dubbo-admin project, and other things can be ignored for the time being; after opening the project, we only need to modify the following screenshot configuration (you can also modify the packaged configuration file):
#zookeeper configuration address and port dubbo.registry.address=zookeeper://127.0.0.1:2081#backend login password dubbo.admin.root.password=rootdubbo.admin.guest.password=guest
After executing the package, you can get the name: dubbo-admin-2.0.0.war package, then put it in tomcat, and browse through the browser. The account and password are both root; after logging in, you can see the provider and consumer we started before. This is the interface of dubbo-admin monitoring registration center service. You can directly click on these services. If you don’t say much, try it yourself.
git address: https://github.com/shenniuboxing3 nuget publishing package: https://www.nuget.org/profiles/shenniuboxing3
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.