Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”
Hello everyone, MY name is Melo, a sophomore majoring in software engineering. After a year of fumbling, I am now preparing to develop backstage projects in the studio. In this article, let’s stop talking about data structures and talk about the pitfalls of getting started with distributed. Feel the distributed layer, because the technology is updated so quickly, it seems that it is difficult to get access to first-hand materials (even official documents are not updated in real time, many are not applicable). After several groping, watching videos, turning over documents and looking for blogs, I finally managed to run Dubbo+ ZooKeeper’s small demo successfully. I write this article because there are a lot of holes in it, and I have written the details in the code annotations. I hope I can help more friends, step on a little hole less, and concentrate on technology!
Architecture objectives for large Internet projects
Site performance metrics:
• Response time: The total time it takes to execute a request from the start until the response data is received. • Concurrency: specifies the number of concurrent requests that the system can process at the same time. • Number of concurrent connections: indicates that the client sends a request to the server and establishes a TCP connection. • Number of concurrent users: Number of users in a unit time • Throughput: Number of requests that can be processed by the system in a unit time •QPS: Query Per Second Number of queries Per Second. •TPS: Transactions Per Second Number of Transactions Per Second. • A transaction is the process by which a client sends a request to the server and the server responds. The client starts the timer when it sends a request and ends it when it receives a response from the server to count the time used and the number of transactions completed. • A single visit to a page will only form one TPS; But a single page request, which may result in multiple requests to the server, has multiple QPS
Clustering and distribution
traditional
- Cluster: many people doing the same thing
Many servers, responsible for the same module
- Distributed: Lots of people doing different things, which adds up to one big thing
Many servers, with different modules on top, and finally one big module
- Clustering and distribution often go together
High availability
- The failure of one server does not affect the other, ensuring that the service is always available
High scalability
- As each module is removed as far as possible, the previous coupling relationship between each other is reduced. Therefore, if you want to add more modules to MODULE E, you can directly add them without affecting other modules
High scalability
- Similar to open and close principle, because the coupling degree is not high, so it is not very difficult to expand
Architecture evolution
Assume that the project consists of ABCD base modules E is the common module
Monomer architecture
- On one server, there is a whole project ABCDE
Vertical architecture
- Split the project into ** independent (unrelated) modules such as AB CD, where E has to be deployed multiple times because the two servers are not related. Namely ABE CDE two servers
disadvantages
- Common modules are needed on every server
Distributed architecture
- Vertical architecture looks like different servers do different things, but there is also a common module E, which appears in both servers. If we want to optimize, we can add another server and put E separately
Remote call E(RPC)
- Using HTTP, REST style, etc. (Dubbo helped us encapsulate it)
disadvantages
- Once E has changed, all the places that call it need to change
SOA architecture
There is nothing that can’t be solved with one more layer, another intermediary ESB
Microservices Architecture (in Depth)
Dubbo architecture
Node roles:
•ProviderThe service provider that exposes the service
•Container: Service run container
•Consumer: Service consumer that invokes the remote service
•Registry: registry for service registration and discovery
•Monitor: monitoring center that collects statistics on service invocation times and invocation time
Quick start
Zookeeper download
Website:Zookeeper.apache.org/releases.ht…I chose this version, with the bin directory
Windows
- Straight out of it
- Copy zoo_sample. CFG from the conf directory and rename it zoo.cfg (because this is the default configuration file name)
- Then go back to the installation directory and create an empty data and log
CFG configuration file in conf, change dataDir=/ TMP /zookeeper to the data folder where the ZooKeeper installation directory resides, and add a configuration for adding data logs (the configuration needs to be modified based on your own installation path).5. Double-click zkServer. CMD in the bin directory to start the program:6. Console displayBind to the port 0.0.0.0/0.0.0.0:2181“Indicates that the server is successfully started!7. Double-click zkcli. CMD in the same directory to start the clientWelcome to Zookeeper! “Indicates that the client is successfully started.
Pay attention to
If you need to start ZooKeeper all the time, you cannot turn off the two CMD files. You cannot turn off !!!!!!! To keep running
Linux
CFG vimzoo.cfg: tar -zxvf (name of the package you downloaded) CD (your installation directory) CD conf/ cp zoo_sample. CFG zoo. CFG vimzoo.cfg
Note that you need to modify according to your own installation path, also need to create data and log, modify port, etc
cd ..
cd bin/
sh zkServer.sh start
sh zkCli.sh
- If you download a different version, you may want to look at the help commands yourself
Detailed configuration (SpringBoot)
Multi-module development
There are actually three modules used here, one is the public interface module, one is the Provider module, and one is the consumer module. The latter two jointly rely on the public module, and the Provider relies on the consumer module
For the full source, contact the bloggers in the comments section
Pay special attention to
We are going to @ import two modules are com. Example. Dubbo_interface. Service. StuService;
- Maven’s install command is used to install the dubbo_interface module into a public repository. Maven’s install command is used to install the dubbo_interface module into a public repository. Maven’s install is used to install the dubbo_interface module The eye can quote the content inside!
If you are not familiar with Maven, please refer to this article for details.
Now that you’re all configured, open ZooKeeper first! Open the zookeeper! Open the zookeeper!
Provider provides the server
Yml file
dubbo:
application:
Configure the name of the application (the application can be a provider or a consumer)
name: dubbo_service
# Protocol Center
protocol:
Is only the provider required?
name: dubbo
port: 20882
127.0.0.1 is the IP address and 2181 is the port number
registry:
address: Zookeeper: / / 127.0.0.1:2181
###### here is the key, many tutorials are not configured, resulting in zooKeeper connection failure
timeout: 50000
The package of the service to be exposed (configure packet scanning)
You can also scan the package in comments
# scan:
# base-packages: com.example.dubbo_service.service
# specify the port on which the project module will start
server:
port: 8081
Copy the code
Service Indicates the Service end
package com.example.dubbo_service.service;
import com.example.dubbo_interface.service.StuService;
import org.apache.dubbo.config.annotation.DubboService;
// Expose service
@DubboService
public class StuServiceImpl implements StuService {
@Override
public String login(a) {
return "111"; }}Copy the code
Introduce the Dubbo client that relies on and controls ZooKeeper
Version 2.7.8 is used with Springboot version 2.5.5
Note Depend on interface modules
<! -- Dependent interface project -->
<dependency>
<groupId>com.example</groupId>
<artifactId>dubbo_interface</artifactId>
<version>0.0.1 - the SNAPSHOT</version>
</dependency>
<! -- Dubbo Spring Boot Starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.8</version>
</dependency>
<! - curator dependence - >
<! Curator provides a set of Java class libraries that make ZooKeeper easier to use. -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>5.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>5.1.0</version>
</dependency>
Copy the code
Start the class
The @enableDubbo annotation is usually written on the startup class. By default, it scans all ** @DubBoService ** annotated components in the package path of the current annotation class. If you need to custom scan registered beans (service implementations), you can use the @DubBoComponentScan or scan attribute. In addition, the @enabledubbo annotation is equivalent to using dubo.scan. Base-packages in a configuration file in one step (EnableDubbo + scan dubbo’s service implementation bean)
package com.example.dubbo_service;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
// Enable annotation-based Dubbo while scanning the location of the exposed server
@EnableDubbo(scanBasePackages = "com.example.dubbo_service.service")
@SpringBootApplication
public class DubboServiceApplication {
public static void main(String[] args) { SpringApplication.run(DubboServiceApplication.class, args); }}Copy the code
Consumer terminal
Note that unlike a traditional service, it is also set up as a Web project (with Springboot) in order to be able to start independently.
Yml file
dubbo:
application:
# specify the name of the current service/application.
name: dubbo_controller
Consumer does not need this communication protocol because consumer does not need to be called remotely via Dubbo
# protocol:
# name: dubbo
## port: 20881
127.0.0.1 is the IP address and 2181 is the port number
registry:
address: Zookeeper: / / 127.0.0.1:2181
###### here is the key, many tutorials are not configured, resulting in zooKeeper connection failure
timeout: 50000
## We found that zooKeeper is still unavailable if only consumer is available on the official website
consumer:
This is equivalent to the timeout setting in the DubboService annotation.
timeout: 3000
## This is used for exposure scanning,consumer does not need exposure service
# scan:
# base-packages: com.example.dubbo_service.service
# specify the port on which the project module will start
server:
port: 8082
Copy the code
The Controller consumer
package com.example.dubbo_controller.controller;
import com.example.dubbo_interface.service.StuService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/stu")
public class StuController {
/** * Remote injection * Gets the access URL for the service from the ZooKeeper registry * makes a remote call to RPC * encapsulates the result as a proxy object and assigns values to variables */
// If there is no timeout, there is a problem. Don't even go on to??
// You can also use the timeout of consumer in yML
@DubboReference/*(timeout = 5000)*/
StuService stuService;
@RequestMapping("/login")
public String login(a){
returnstuService.login(); }}Copy the code
Ditto for introducing dependencies
<! -- Dependent interface project -->
<dependency>
<groupId>com.example</groupId>
<artifactId>dubbo_interface</artifactId>
<version>0.0.1 - the SNAPSHOT</version>
</dependency>
<! -- Dubbo Spring Boot Starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.8</version>
</dependency>
<! - curator dependence - >
<! Curator provides a set of Java class libraries that make ZooKeeper easier to use. -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>5.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>5.1.0</version>
</dependency>
Copy the code
Start the class
package com.example.dubbo_controller;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
// Turn on Dubbo annotations
@EnableDubbo
@SpringBootApplication
public class DubboControllerApplication {
public static void main(String[] args) { SpringApplication.run(DubboControllerApplication.class, args); }}Copy the code
Public interface
Since both the Provider and consumer modules need this interface, it is better to extract it as a public interface
package com.example.dubbo_interface.service;
// Public interface
public interface StuService {
String login(a);
}
Copy the code
Dubbo-admin
Life advice: direct use of the old version of the front and back end is not separated, can be directly started using a key, port default in the machine 7001
About the old version of dubbo-admin I am directly using the silicon Valley tutorial inside the Silicon Valley file should be there, if you can not find it can also comment on the message
Modify the application. The properties
server.port
Port for the project to start
registry.address
The address of the registry to monitor
Start ZooKeeper before you start!! (Refer to the zooKeeper installation above for how to start it.)
Access localhost: port number
The default user name and password are root
The final result
How to start the Service module, then the Controller module, and finally the Dubo-admin project with ZooKeeper on
You can see that there is one service number and two applications, one provider and one consumer
Services are our interface modules
The provider
consumers
Access our interface
Complete engineering code
- The poM files of the interdependence of the three modules are different based on the name of the individual project. If you are a little confused about the POM files and dependencies, please feel free to communicate in the comments section on wechat
Write in the last
- The distributed part is still just a beginning, and I hope I can understand more about the principle. I hope I can continue to adhere to this part and add more knowledge about it. Learn more about Dubbo’s advanced features and ZooKeeper. Come back later! (Flag goes up like crazy)
PPT source
- Dark Horse Dubbo tutorial
- Dubo-admin file source: Shang Silicon Valley