Introduction of Dubbo
Dubbo is an open source high-performance service framework of Alibaba, which enables applications to realize the output and input functions of services through high-performance RPC, and can be seamlessly integrated with the Spring framework.
It provides three core competencies:
- Interface oriented remote method invocation
- Intelligent fault tolerance and load balancing
- Services are automatically registered and discovered.
You can see that there are five types of roles:
- Provider: indicates the service Provider
- B: We serve consumers
- Registry: service Registry
- Monitor: indicates the service monitoring center
- Container: The Container in which the service runs
The process can be explained like this:
- Start the service Provider in the Container
- The Provider of the service registers the service as an interface to the Registry
- A Consumer of a service subscribes to the registry
- The registry returns the service interface provided by the service provider to the service consumer (asynchronously)
- The consumer of the service invokes the provider of the service (synchronization)
- The monitoring center collects statistics on the number and time of service invocation.
Here we use ZooKeeper as the service registry. Here I check the official documentation and find that Redis can also be used as the Dubbo registry, but it is rare in the application, and the official documentation also states:
If we want to use it, we just need to change the value of dubo.registry. addrss to redis://127.0.0.1:6379.
Here is the theory, let us enter the actual combat it ~
The installation of a zookeeper
Here I am installing on a Mac OS environment. You can do this by installing a virtual machine locally.
First we need to go to the official website to download the installation package.
# 0. DecompressionThe tar - ZXVF zookeeper - 3.4.13. Tar. Gz# 1. Switch to the decompressed directory
cdZookeeper 3.4.13 /# 2. Create data and logs directories
mkdir data
mkdir logs
# 3. Copy zoo_sample. CFG to zoo.cfg
cd conf/
cp zoo_sample.cfg zoo.cfg
# 4. Modify configurationVi zoo. CFG dataDir= Absolute path of the newly created data directorylogDir= Absolute path to the newly created data directory# 5. Start
cd ../bin
./zkServer.sh start
# 6. Check whether the startup is successful
./zkServer.sh status
Copy the code
The instance
Write the Provider
Introducing Maven dependencies for our project:
<! --Don't get the bag wrong here. I tried Ali'sstarter, found a lot of problems, not as good as this-->
<dependency>
<groupId>io.dubbo.springboot</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>1.0.0</version>
</dependency>
Copy the code
Then configure the registry and exposed service interface packages
Spring. Dubbo. Application. The name = the provider spring. The dubbo. Registry. Address = zookeeper: / / 127.0.0.1:2181 spring.dubbo.protocol.name=dubbo spring.dubbo.protocol.port=27899 spring.dubbo.scan=indi.viyoung.dubbo.provider.serviceCopy the code
Write the Service interface and implementation class:
public interface TestService {
String test(a);
}
Copy the code
@Service
public class TestServiceImpl implements TestService {
@Override
public String test(a) {
return "Hello Wolrd"; }}Copy the code
Note that the Service annotation must be dubbo.
Write a Consumer
Also introduce dependencies:
<dependency>
<groupId>io.dubbo.springboot</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>1.0.0</version>
</dependency>
Copy the code
The Consumer configuration is less:
Spring. Dubbo. Application. The name = consumer spring. The dubbo. Registry. Address = zookeeper: / / 127.0.0.1:2181Copy the code
Directly referenced in Controller
public class TestController {
@Reference
private TestService testService;
@RequestMapping(a)public String hello(a) {
returntestService.test(); }}Copy the code
Note that the @reference annotation is not wrong, it should be under Dubbo package!
Then after the project is started, we visit:
Nice, access successful!
Let’s look at the use of dubo-admin
Dubbo-Admin
Dubo-admin gave me the impression of what it would look like without UI:
git clone https://github.com/apache/incubator-dubbo-ops.git
Copy the code
First pull the project down from Git and open it with IDEA:
Just add a configuration with a port number and use the Maven packaging tool on the left:
Then go to the target directory of dubo-admin-server and use the following command:
Java -jar dubo-admin-server-0.1. jar >run.log &Copy the code
Then open your browser and type localhost:7070
Compared to the previous UI is simply countless times stronger, and thief convenient!
The specific function we can follow the operation to discuss, here will not expand to say.