The background of Dubbo
Dubbo has been around for nearly 10 years from open source to now, and it has been widely used in major domestic enterprises. What kind of magic is it worthy of everyone's pursuit? This article gives you a detailed explanation.
Large-scale servitization requires service governance
When enterprises start large-scale servicing, the disadvantages of remote communication become more and more obvious. For example
- The service link becomes longer, how to track and monitor the service link?
- The large-scale cluster of services makes it necessary for services to rely on a third-party registry to solve the problem of service discovery and service perception
- Exceptions between service communications require a protection mechanism to prevent a node failure from causing a large-scale system failure, so a fault-tolerant mechanism is required
- Clients that serve large-scale clusters need to introduce a load balancing mechanism to achieve request distribution
With these requirements for service governance, traditional RPC technology seems a little weak in such a scenario, so many companies have begun to develop their own RPC frameworks, such as Ali's HSF and Dubbo; JD.com's JSF framework, Dangdang's dubbox, and Sina's motan. , Ant Financial's sofa, etc.
Companies with technical output capabilities will develop an rpc framework suitable for their own scenarios, either from 0 to 1 or based on existing ideas combined with the company's business characteristics for transformation. However, companies that do not have the ability to export technology will give priority to those more mature open source frameworks when they meet the needs of service governance. And Dubbo is one of them
dubbo is mainly a distributed service governance solution, so what is service governance? Service governance is mainly aimed at solving the problems of routing, load balancing, fault tolerance mechanism, and service degradation between services after large-scale service. Dubbo not only realizes remote service communication, but also solves service routing and load. , Degradation, fault tolerance and other functions.
Dubbo's development history
Dubbo is a distributed service governance framework used internally by Alibaba. It was open sourced in 2012. Because Dubbo has undergone a lot of internal verification in the company, it is relatively mature, so it is used by many Internet companies in a short period of time. In addition, after many technical experts from Ali entered various startup companies as the technical architecture, the RPC framework with Dubbo as the main push made Dubbo quickly become the primary choice of many Internet companies. And when many companies use dubbo, they will optimize and improve based on their own business characteristics, so many versions have been derived, such as JD.com's JSF, such as Sina's Motan, such as Dangdang's dubbox.
In October 2014, Dubbo stopped maintenance. Later, in September 2017, Ali announced the restart of Dubbo and prepared for long-term investment in Dubbo. Dubbo has undergone a lot of updates during this time, and the current version has reached 2.7.
On January 8, 2018, Liang Fei, one of the founders of Dubbo, revealed in the Dubbo exchange group that Dubbo 3.0 is under construction. Dubbo 3.0 kernel is completely different from Dubbo 2.0, but compatible with Dubbo 2.0. Dubbo 3.0 will support optional Service Mesh
In February 2018, Dubbo donated to Apache. In addition, Alibaba's improvement of the Spring Cloud Alibaba ecosystem and the Spring Cloud team's support for the entire service governance ecosystem of alibaba, so Dubbo will still be the primary choice for most domestic companies in the future.
Dubbo's overall architecture
Use of Dubbo
First, build two maven projects
user-service
- user-service-api
- user-service-provider
- user-service-consumer
user-service-api
The public contract for the service provided by the user-service, which provides the external service of the user-service.
public interface ILoginService {
String login(String username,String password);
}
user-service-provider
Provide the implementation of ILoginService in the user-service-provider service
public class LoginServiceImpl implements ILoginService{
@Override
public String login(String username, String password) {
if(username.equals("admin")&&password.equals("admin")){
return "SUCCESS";
}
return "FAILED";
}
}
user-service-consumer
public class App
{
public static void main( String[] args ){
ILoginService loginService=null;
System.out.println(loginService.login("admin","admin"));
}
}
The question is, now that the user-service-consumer is a service consumer, how to call the remote service user-service-provider?
According to the previous principle of service remote communication, the service provider must publish the service on the network and provide the corresponding access protocol. The service consumer must access it based on this protocol.
At this time, the dubbo middleware comes in handy. Its most basic function is to provide service publishing and remote access to services.
Introduce Dubbo publishing service
Introduce dubbo dependency package
<dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> <version>2.7.8</version> </dependency>
Add the application.xml file in the /src/main/resource/META-INF/spring directory
<?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"> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="user-service"/> <!-- 使用multicast广播注册中心暴露服务地址 --> <dubbo:registry address="N/A" /> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="com.gupaoedu.demo.ILoginService" ref="loginService" /> <!-- 和本地bean一样实现服务 --> <bean id="loginService" class="com.gupaoedu.demo.LoginServiceImpl" /> </beans>
Start service
public class App { public static void main( String[] args ){ Main.main(args); } }
After the startup is successful, you will see the following log in the console
信息: [DUBBO] Export dubbo service com.gupaoedu.demo.ILoginService to url dubbo://192.168.1.104:20880/com.gupaoedu.demo.ILoginService?anyhost=true&application=user-service&bind.ip=192.168.1.104&bind.port=20880&deprecated=false&dubbo=2.0.2&dynamic=true&generic=false&interface=com.gupaoedu.demo.ILoginService&methods=login&pid=24280&release=2.7.8&side=provider×tamp=1596550697070, dubbo version: 2.7.8, current host: 192.168.152.1 八月 04, 2020 10:18:17 下午 org.apache.dubbo.remoting.transport.AbstractServer info 信息: [DUBBO] Start NettyServer bind /0.0.0.0:20880, export /192.168.1.104:20880, dubbo version: 2.7.8, current host: 192.168.152.1
Through the above steps, it means that ILoginService has been published on the network, based on the form of NettyServer, listening on port 20880 by default
Service consumers introduce dubbo
Add jar package dependency
<dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> <version>2.7.8</version> </dependency>
Add the application.xml file in the /src/main/resources/META-INF/spring directory
<?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"> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="user-service-consumer"/> <!-- 使用multicast广播注册中心暴露服务地址 --> <dubbo:registry address="N/A" /> <dubbo:reference id="loginService" interface="com.gupaoedu.demo.ILoginService"/> </beans>
Modify the main method
- Load spring configuration file through ApplicationContext
- Obtain an ILoginService bean from the container
public class App { public static void main( String[] args ){ ILoginService loginService=null; ApplicationContext applicationContext=new ClassPathXmlApplicationContext("classpath:META-INF/spring/application.xml"); loginService=applicationContext.getBean(ILoginService.class); System.out.println(loginService.login("admin","admin")); } }
Specify the url of the service provider
After the above configuration is completed, the following error will be prompted after running the project
IllegalStateException: No such any registry to reference com.gupaoedu.demo.ILoginService on the consumer 192.168.152.1 use dubbo version 2.7.8, please config <dubbo:registry address="..." /> to your spring config.
The reason is that the registration center specified by the dubbo:registry we configured is N/A, which means that the registration center is not configured.
Secondly, we did not clearly indicate where the service provider is. So there are two ways to solve this problem
- Point to the address of the service provider
- Configure the service registry, register the service provider to the registry, and then the service consumer points to the registry to obtain the service address from the registry
The modification method is as follows, modify the dubbo:reference in the application.xml of the service consumer.
<dubbo:reference id="loginService" interface="com.gupaoedu.demo.ILoginService"
url="dubbo://192.168.1.104:20880/com.gupaoedu.demo.ILoginService"/>
Summarize
To briefly summarize the entire process above, it is not difficult to find that Dubbo, a middleware, provides us with a solution for service remote communication. Through the framework of dubbo, developers can quickly and efficiently build remote communication implementations under the microservice architecture.
I don’t know if you have discovered that when we use dubbo to publish services or consume services, the whole process is completed by spring configuration. The advantage of this is that when we are learning or using dubbo, if you have used the spring framework, Then the difficulty of learning it will be greatly reduced. And we can also see that dubbo is fully integrated with Spring, so when we analyze the source code of dubbo later, there will still be some content related to spring.
And if you have studied my handwritten RPC lesson before, you can basically guess the entire implementation structure of it. You may as well guess some of the implementation details of dubbo boldly to help you better understand dubbo in the future. .
Introduce the registry
Dubbo is not just an RPC framework, it is also a service governance framework, which provides functions such as unified management of services and service routing.
In the above case, we just concealed Dubbo as a point-to-point service for RPC communication, but just like we were learning about the content of spring cloud, how to manage, maintain, and dynamically discover when there are more services?
Moreover, as can be seen from the architecture diagram of Dubbo, Dubbo naturally supports service registration and discovery. The earliest official recommended service registry is zookeeper. Of course, there are already many registries supported by dubbo, such as
consul, etcd, nacos, sofa, zookeeper, eureka, redis, etc. Obviously, Dubbo is already developing an ecosystem of independent microservice solutions.
Integrate Zookeeper as a service registry
Add zookeeper's jar package dependency
<dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-dependencies-zookeeper</artifactId> <version>2.7.8</version> </dependency>
Modify the configuration of service providers and service consumers
<dubbo:registry address="zookeeper://192.168.216.128:2181" />
Integrate Nacos as a service registry
Start nacos
docker run --name nacos -d -p 8848:8848 --privileged=true --restart=always -e JVM_XMS=512m -e JVM_XMX=2048m -e MODE=standalone -e PREFER_HOST_MODE=hostname -v /home/nacos/logs:/home/nacos/logs nacos/nacos-server
- privileged: Using this parameter, the root in the container has real root privileges. Otherwise, the root in the container is just an external normal user authority.
- When Docker restarts, the container automatically restarts
- PREFER_HOST_MODE: ip #If hostname is supported, hostname can be used, otherwise ip is used, and the default is also ip
Add dependency
<dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-client</artifactId> <version>1.2.1</version> </dependency>
Change setting
<dubbo:registry address="nacos://192.168.216.128:8848" timeout="10000" />
Dubbo Spring Cloud
Since we are talking about the topic of Spring Cloud Alibaba, it is necessary for us to understand how Dubbo integrates Spring Cloud to use.
Dubbo Spring Cloud is built on the native Spring Cloud and covers the native features of Spring Cloud. Compared with the native governance of Spring Cloud, Dubbo Spring Cloud provides a more stable and mature implementation.
The specific features are compared as follows:
Why is it called Dubbo Spring Cloud instead of Spring Cloud Dubbo? In my opinion, Dubbo itself is an ecosystem of its own, and it is more prominent than Spring Cloud in terms of its service governance and maturity.
So in fact, Dubbo integrates Spring Cloud, and it is Dubbo's mature ecosystem to embrace the standard system of Spring Cloud.
Dubbo Spring Cloud is developed based on Dubbo Spring Boot 2.7.1[1] and Spring Cloud 2.x. Regardless of whether the developer is a Dubbo user or a Spring Cloud user, they can easily control it and make the application upward at a cost close to "zero". migrate
Starting from 2.7.0, Dubbo Spring Boot and Dubbo are consistent in version
Next, we can use Dubbo Spring Cloud to do a simple case implementation
Create a project
- Create a maven project of spring-cloud-dubbo-example
Add three modules separately
- spring-cloud-dubbo-sample-api
- spring-cloud-dubbo-sample-provider
- spring-cloud-dubbo-sample-consumer
The latter two modules are both spring boot applications.
Modify spring-cloud-dubbo-sample-provider
this module.
- Move the dependencies of the dependencyManagement part to parent pom.xml
Modify the pom.xml in spring-cloud-dubbo-sample-provider to increase the dependency of the parent module
<parent> <groupId>com.gupaoedu.dubbo</groupId> <artifactId>spring-cloud-dubbo-example</artifactId> <version>1.0-SNAPSHOT</version> </parent>
Add maven dependency
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-dubbo</artifactId> </dependency> <dependency> <groupId>com.gupaoedu.dubbo</groupId> <version>1.0-SNAPSHOT</version> <artifactId>spring-cloud-dubbo-sample-api</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency>
Define service interface
In the spring-boot-dubbo-sample-api module, define the interface
public interface IHelloService {
String sayHello();
}
Implement service
In spring-boot-dubbo-sample-provider, implement the IHelloService interface
public class HelloServiceImpl implements IHelloService{
@Override
public String sayHello() {
return "Hello GuPao";
}
}
Add @EnableDiscoveryClient
comment
@EnableDiscoveryClient
@SpringBootApplication
public class SpringCloudDubboSampleProviderApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudDubboSampleProviderApplication.class, args);
}
}
Configure dubbo service publishing
@Service
annotation in the service implementation class@Service public class HelloServiceImpl implements IHelloService{ @Override public String sayHello() { return "Hello GuPao"; } }
Configure dubbo provider information
# dubbo 服务扫描基础包路径 dubbo.scan.base-packages=com.gupaoedu.dubbo.springclouddubbosampleprovider dubbo.protocol.id=dubbo # Dubbo 服务暴露的协议配置,其中子属性 name 为协议名称,port 为协议端口( -1 表示自增端口,从 20880 开始) dubbo.protocol.name=dubbo dubbo.protocol.port=-1 spring.cloud.nacos.discovery.server-addr=192.168.216.128:8848
dubbo.scan.base-packages
: Specify the scan benchmark package for Dubbo service implementation classdubbo.protocol
: The protocol configuration exposed by the Dubbo service, where the sub-attributename
is the protocol name, andport
is the protocol port (-1 means self-incremented port, starting from 20880)dubbo.registry
: Dubbo service registry configuration,address
is "spring-cloud://localhost", indicating that it is mounted to the Spring Cloud registryspring.cloud.nacos.discovery
: Nacos service discovery and registration configuration, where the sub-attribute server-addr specifies the host and port of the Nacos server
Version specification
The version number format of the project is in the form of xxx, where the value type of x is a number, and the value starts from 0, and is not limited to the range of 0-9. When the project is in the incubator stage, the first version number is always 0, that is, the format of the version number is 0.xx.
Since Spring Boot 1 and Spring Boot 2 have great changes in the interface and annotations of the Actuator module, and the upgrade of spring-cloud-commons from version 1.xx to version 2.0.0 also has major changes, we adopt the SpringBoot version A consistent version:
- Version 1.5.x is suitable for Spring Boot 1.5.x
- Version 2.0.x is suitable for Spring Boot 2.0.x
- 2.1.x version is suitable for Spring Boot 2.1.x
- The 2.2.x version is suitable for Spring Boot 2.2.x
Building service consumers
Add jar package dependency
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-dubbo</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-actuator</artifactId> </dependency> <dependency> <groupId>com.gupaoedu.dubbo</groupId> <version>1.0-SNAPSHOT</version> <artifactId>spring-cloud-dubbo-sample-api</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
Add configuration file
spring.application.name=spring-cloud-dubbo-sample-consumer dubbo.application.name=spring-cloud-dubbo-sample-consumer dubbo.cloud.subscribed-services=spring-cloud-dubbo-sample-provider spring.cloud.nacos.discovery.server-addr=192.168.216.128:8848
In addition to the difference spring.application.name
spring-cloud-dubbo-client-sample
adds the setting of dubbo.cloud.subscribed-services
And this value is the service provider application "spring-cloud-dubbo-sample-provider".
Its main function is that the service consumer subscribes to the list of application names of the service provider. If you need to subscribe to multiple applications, use the "," segmentation. It is not recommended to use the default value "*", it will subscribe to all applications.
Write test code
@RestController @EnableDiscoveryClient @SpringBootApplication public class SpringCloudDubboSampleConsumerApplication { public static void main(String[] args) { SpringApplication.run(SpringCloudDubboSampleConsumerApplication.class, args); } @Reference IHelloService helloService; @GetMapping("/say") public String say(){ return helloService.sayHello(); } }
# Multi-registration center support
Compared with spring cloud, dubbo is powerful in that it provides functional support for many different scenarios, such as support for multiple registration centers.
The so-called multi-registration center means that dubbo can configure the addresses of multiple registration centers at the same time, and then register to different registration centers for different types of services.
Dubbo multi-registration center can support several scenarios
One service is deployed to multiple registries
Configuration method based on spring cloud
Add jar package dependency
<dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-dependencies-zookeeper</artifactId> <version>2.7.8</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> <exclusion> <artifactId>log4j</artifactId> <groupId>log4j</groupId> </exclusion> </exclusions> </dependency>
Modify application configuration
dubbo.registries.registry1.address=nacos://192.168.216.128:8848 dubbo.registries.registry1.timeout=10000 dubbo.registries.registry2.address=zookeeper://192.168.216.128:2181 dubbo.registries.registry2.timeout=10000 #spring.cloud.nacos.discovery.server-addr=192.168.216.128:8848 spring.cloud.nacos.discovery.register-enabled=false spring.cloud.nacos.discovery.watch.enabled=false spring.cloud.service-registry.auto-registration.enabled=false
- spring.cloud.service-registry.auto-registration.enabled Turn off automatic registration of spring cloud
- spring.cloud.nacos.discovery.watch.enabled/spring.cloud.nacos.discovery.register-enabled turns off nacos service registration and monitoring
The purpose of this is to circumvent the service registration and discovery mechanism of spring cloud itself, and take the service registration and discovery of dubbo itself.
Modify service configuration
@Service(registry = {"registry1","registry2"}) public class HelloServiceImpl implements IHelloService{ @Override public String sayHello() { return "Hello GuPao"; } }
Multi-registry reference
Modify the application.properties of the consumer
dubbo.registries.registry1.address=nacos://192.168.216.128:8848 dubbo.registries.registry1.timeout=10000 dubbo.registries.registry2.address=zookeeper://192.168.216.128:2181 dubbo.registries.registry2.timeout=10000 spring.cloud.nacos.discovery.register-enabled=false spring.cloud.nacos.discovery.watch.enabled=false spring.cloud.service-registry.auto-registration.enabled=false
Add jar package dependency
<dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-dependencies-zookeeper</artifactId> <version>2.7.8</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> <exclusion> <artifactId>log4j</artifactId> <groupId>log4j</groupId> </exclusion> </exclusions> </dependency>
Integrated Dubbo based on spring boot
In fact, in the use of dubbo spring cloud, it is not very friendly to configure multiple service registries and there are some potential problems. After all, dubbo and spring cloud are essentially completely different ecological couplings. Will cause some compatibility issues. For example, the support for these multiple registration centers that we just configured, it needs to turn off the support for automatic registration and discovery of spring cloud itself. In essence, it is to choose one of the two ecosystems as the main method to use.
Therefore, if you are in the spring cloud ecosystem, you can minimize the use of dubbo's own flexibility and embrace the spring cloud standard ecosystem. Of course, if you want to use dubbo as an independent ecosystem, you can use spring boot+Dubbo to integrate ,
Here is also a quick build for everyone.
In addition, the integration of dubbo into spring boot has another advantage, that is, it can inherit the characteristics of spring boot itself
- Automatic assembly (annotation driven, automatic assembly)
- production-ready (security mechanism, health detection, externalized configuration)
Create project structure
Create a basic project structure
spring-boot-dubbo-example [maven]
- spring-boot-dubbo-sample-api [maven]
- spring-boot-dubbo-sample-provider [spring boot]
- spring-boot-dubbo-sample-consumerp [spring-boot]
Add jar package dependency
Starting from 2.7, the version of dubbo and the version of dubbo-spring-boot are consistent, so you don't have to worry about the version anymore.
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.7</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>1.2.1</version>
</dependency>
Add service and publish
@DubboService
public class SayHelloServiceImpl implements ISayHelloService{
@Override
public String sayHello() {
return "Hello GuPaoEdu.com";
}
}
spring.application.name=spring-boot-dubbo-sample-provider
dubbo.registry.address=nacos://192.168.216.128:8848
dubbo.scan.base-packages=com.gupaoedu.springboot.dubbo.springbootdubbosampleprovider.service
dubbo.protocol.name=dubbo
dubbo.protocol.port=-1
Write service reference code
Add jar package dependency
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.gupaoedu.com</groupId> <version>1.0-SNAPSHOT</version> <artifactId>spring-boot-dubbo-sample-api</artifactId> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.7.7</version> </dependency> <dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-client</artifactId> <version>1.2.1</version> </dependency>
Add web test class
@DubboReference ISayHelloService sayHelloService; @GetMapping("/get") public String get(){ return sayHelloService.sayHello(); }
dubbo.registry.address=nacos://192.168.216.128:8848
Different services are registered to different registration centers
From the above configuration, we can find that we can configure different registration centers for different services. This will no longer waste time to demonstrate.
Cluster of multiple registries
If a service consumer references multiple registries, the first thing the service consumer needs to do at this time is to first load balance the registry, and then obtain a target registry, and then obtain the service provider’s information from the target registry The address list is then accessed by cluster, and the implementation principle is shown in the figure below
Of course, there are three ways to specify the load balancing configuration of the registry
Assign priority
<!-- 来自 preferred=“true” 注册中心的地址将被优先选择,只有该中心无可用地址时才 Fallback 到其他注册中心 --> <dubbo:registry address="zookeeper://${zookeeper.address1}" preferred="true" />
Same zone first
<!-- 选址时会和流量中的 zone key 做匹配,流量会优先派发到相同 zone 的地址 --> <dubbo:registry address="zookeeper://${zookeeper.address1}" zone="beijing" />
Weight polling
<!-- 来自北京和上海集群的地址,将以 10:1 的比例来分配流量 --> <dubbo:registry id="beijing" address="zookeeper://${zookeeper.address1}" weight=”100“ /> <dubbo:registry id="shanghai" address="zookeeper://${zookeeper.address2}" weight=”10“ />
Interface multi-version support
Usually when we develop an interface, we may face an interface modification, but at this time, because some online projects are using this interface, if it is modified directly, it is likely to have a greater impact on online services.
Therefore, for this situation, dubbo provides support for the interface version.
Specific configuration method
- The server provides implementations of different versions for the same interface
And configure the version statement in the dubboservice annotation
@DubboService(registry = {"registry1","registry2"},version = "1.0")
Service consumer specifies the consumer version number
@DubboReference(registry = {"registry1","registry2"},version = "2.0") ISayHelloService sayHelloService;
Multi-protocol support
When the company originally adopted other rpc frameworks, if you want to migrate to the dubbo framework at this time, then the multi-protocol support provided by Dubbo can provide almost zero-cost migration.
For a service, multiple interfaces of different protocols can be released at the same time, or different protocol types can be released for different interfaces. And since 2.7, dubbo has supported some mainstream protocols. Currently, the supported protocols are:
dubbo protocol, hessian protocol, http protocol, thrift, rmi, webservice, grpc, rest, etc. In addition to the first time, dubbo also provides a very flexible scalability mechanism. For companies that have customized requirements or are currently in use, companies that dubbo does not support can expand by themselves.
The overall flexibility and pluggability characteristics are more powerful than spring cloud.
JAX-RS protocol description
The REST (Representational Resource Transfer) support in Dubbo is implemented based on JAX-RS2.0 (Java API for RESTful Web Services).
REST is an architectural style. Simply put, it is a constraint on the api interface. It locates resources based on URL and uses http verbs (GET/POST/DELETE) to describe operations.
REST was proposed very early. In the early days, developers used various tools to implement REST. For example, Servlets were often used to develop RESTful programs. As REST is adopted by more and more developers, the JCP (Java community process) proposed the JAX-RS specification and provided a new annotation-based way to develop RESTful services. With such a specification, developers do not need to care about the communication layer, but only need to focus on resources and data objects.
The implementation of JAX-RS specifications includes: Apache CXF, Jersey (reference implementation of JAX-RS provided by Sun), RESTEasy (implementation of jboss), etc.
The REST implemented in Dubbo is based on the RESTEasy framework provided by Jboss.
We use the RESTful implementation in SpringMVC a lot, and it is also an implementation of the JAX-RS specification
Add REST support
Add jar package dependency
<dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jaxrs</artifactId> <version>3.13.0.Final</version> </dependency> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-client</artifactId> <version>3.13.0.Final</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-server</artifactId> <version>9.4.19.v20190610</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-servlet</artifactId> <version>9.4.19.v20190610</version> </dependency>
Modify the configuration file
dubbo.protocols.dubbo.name=dubbo dubbo.protocols.dubbo.port=-1 dubbo.protocols.rest.name=rest dubbo.protocols.rest.port=8888 dubbo.protocols.rest.server=jetty
Modify the interface definition of api
@Path("/") public interface ISayHelloService { @GET @Path("say") String sayHello(); }
Copyright statement: All articles in this blog, except for special statements, adopt the CC BY-NC-SA 4.0 license agreement. Please indicate the reprint from Mic takes you to learn architecture!
If this article is helpful to you, please help me to follow and like. Your persistence is the motivation for my continuous creation. Welcome to follow the WeChat public account of the same name for more technical dry goods!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。