Springboot integrates Dubbo/ZooKeeper to explain SOA case in detail

Outline of this paper
1. Why integrate Dubbo to implement SOA
2. Run the springboot-dubbo-server and springboot-dubbo-client projects
3. Detailed explanation of springboot-dubbo-server and springboot-dubbo-client project configuration
 

1. Why integrate Dubbo to implement SOA

Dubbo is not only a high-performance RPC calling framework, but also a solution for SOA service governance.
Core :
1. Remote communication, calling a remote method like a local call.
2. Cluster fault tolerance
3. Automatic service discovery and registration, which can smoothly add or delete service providers.
 
We often use Springboot to expose HTTP services and go in JSON mode. But slowly the amount is large, a kind of SOA governance scheme. This exposes the Dubbo service interface and provides Dubbo consumers to make RPC calls. Below we explain in detail how to integrate Dubbo.
 

2. Run the springboot-dubbo-server and springboot-dubbo-client projects

Operating environment: JDK 7 or 8, Maven 3.0+
Technology stack: SpringBoot 1.5+, Dubbo 2.5+, ZooKeeper 3.3+

 

 
1. ZooKeeper Service Registry
ZooKeeper is a distributed, open source coordination service for distributed applications. It is a software that provides consistent services for distributed applications. The functions provided include: configuration maintenance, domain name service, distributed synchronization, group service, etc.
Download ZooKeeper, address http://www.apache.org/dyn/closer.cgi/zookeeper
 
Unzip ZooKeeper
 
tar zxvf zookeeper-3.4.8.tar.gz
 
Create a new zoo.cfg in the conf directory, and configure it as follows according to the zoo_sample.cfg in the directory.
 
cd zookeeper-3.3.6/conf
came zoo.cfg
 
The zoo.cfg code is as follows (specify the log file directory by yourself):
 
tickTime=2000
dataDir=/javaee/zookeeper/data 
dataLogDir=/javaee/zookeeper/log
clientPort=2181
 
In the bin directory, start ZooKeeper:
 
cd zookeeper-3.3.6/bin
./zkServer.sh start
 
2. git clone to download the project springboot-learning-example
See GitHub for the project address -  https://github.com/JeffLi1993/springboot-learning-example :
git clone [email protected]:JeffLi1993/springboot-learning-example.git
 

Then, Maven compiles and installs the project:

cd springboot-learning-example
mvn clean install
 
 
3. Run the springboot-dubbo-server Dubbo service provider project
Right-click to run the main function of the ServerApplication application startup class of the springboot-dubbo-server project. The following appears in the Console to indicate that the project was started successfully:
This means that the Dubbo service has been successfully started and registered in ZK (ZooKeeper).
 
 
4. Run the springboot-dubbo-client Dubbo service consumer project

Right-click to run the main function of the Springboot-dubbo-client project ClientApplication application startup class. The console appears as follows:

...
2017-03-01 16:31:38.473  INFO 9896 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2017-03-01 16:31:38.538  INFO 9896 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8081 (http)
2017-03-01 16:31:38.547  INFO 9896 --- [           main] org.spring.springboot.ClientApplication  : Started ClientApplication in 6.055 seconds (JVM running for 7.026)
City{id=1, provinceId=2, cityName='Wenling', description='is my hometown'}
 
The last printed city information is obtained by calling the Dubbo service interface. The successful operation is successful. The following is a detailed explanation of each code and configuration.
 
 

3. Detailed explanation of springboot-dubbo-server and springboot-dubbo-client project configuration

 
1. Explain the springboot-dubbo-server Dubbo service provider project in detail
springboot-dubbo-server project directory structure
Pom── pom.xml
└── src
    └── main
        ├── java
        │   └── org
        │       └── spring
        │           └── springboot
        │               ├── ServerApplication.java
        │               ├── domain
        │               │   └── City.java
        │               └── dubbo
        │                   ├── CityDubboService.java
        │                   └── impl
        │                       └── CityDubboServiceImpl.java
        └── resources
            └── application.properties
 
 
a.pom.xml configuration

pom.xml relies on the spring-boot-starter-dubbo project, whose address is  https://github.com/teaey/spring-boot-starter-dubbo . pom.xml is configured as follows

<?xml version="1.0" encoding="UTF-8"?>
<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>springboot</groupId>
<artifactId>springboot-dubbo-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-dubbo server:: Integrate Dubbo/ZooKeeper to explain SOA case</name>

<!-- Spring Boot starts parent dependencies -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>

<properties>
<dubbo-spring-boot>1.0.0</dubbo-spring-boot>
</properties>

<dependencies>

<!-- Spring Boot Dubbo 依赖 -->
<dependency>
<groupId>io.dubbo.springboot</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>${dubbo-spring-boot}</version>
</dependency>

<!-- Spring Boot Web Dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- Spring Boot Test dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<!-- Junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
 
 

b.application.properties configuration

## Dubbo service provider configuration
spring.dubbo.application.name=provider
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
spring.dubbo.scan=org.spring.springboot.dubbo
 
 
The address and port configured by ZK here are the ZK built by the machine above. If you have your own ZK, you can modify the following configuration. The configuration is explained as follows:
spring.dubbo.application.name application name
spring.dubbo.registry.address registry address
spring.dubbo.protocol.name protocol name
spring.dubbo.protocol.port protocol port
spring.dubbo.scan dubbo service class package directory
 
c.CityDubboServiceImpl.java City business Dubbo service layer implementation layer class
 
// Register as a Dubbo service
@Service(version = "1.0.0")
public class CityDubboServiceImpl implements CityDubboService {

    public City findCityByName(String cityName) {
        return new City(1L,2L,"Wenling","is my hometown");
    }
}
 
The @Service annotation identifies the Dubbo service and specifies the version number through version.
 
d.City.java city entity class
The entity class needs to implement the serialization interface for RPC calls between Dubbo services. It is better to specify the serialVersionUID value.
 
2. Explain the springboot-dubbo-client Dubbo service consumer project in detail
springboot-dubbo-client project directory structure
 
Pom── pom.xml
└── src
    └── main
        ├── java
        │   └── org
        │       └── spring
        │           └── springboot
        │               ├── ClientApplication.java
        │               ├── domain
        │               │   └── City.java
        │               └── dubbo
        │                   ├── CityDubboConsumerService.java
        │                   └── CityDubboService.java
        └── resources
            └── application.properties
 
pom.xml, CityDubboService.java, City.java have not changed. Dubbo consumers implement Dubbo interface calls by introducing interfaces.
 
a.application.properties configuration
 
## Avoid conflict with the server project port
server.port=8081

## Dubbo service consumer configuration
spring.dubbo.application.name=consumer
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.scan=org.spring.springboot.dubbo
 
Because the springboot-dubbo-server project starts using port 8080, the port is set to 8081 here.
 
b.CityDubboConsumerService.java City Dubbo service consumer
 
@Component
public class CityDubboConsumerService {

    @Reference(version = "1.0.0")
    CityDubboService cityDubboService;

    public void printCity() {
        String cityName="Wenling";
        City city = cityDubboService.findCityByName(cityName);
        System.out.println(city.toString());
    }
}
@Reference(version = “1.0.0”) Through this annotation, subscribe to the Dubbo service whose interface version is 1.0.0.
Here, the CityDubboConsumerService is injected into the Spring container in order to obtain the bean more conveniently, and then verify whether the Dubbo call is successful.
 
c.ClientApplication.java client startup class
 
@SpringBootApplication
public class ClientApplication {

    public static void main(String[] args) {
        // program startup entry
        // Start the embedded Tomcat and initialize the Spring environment and its various Spring components
        ConfigurableApplicationContext run = SpringApplication.run(ClientApplication.class, args);
        CityDubboConsumerService cityService = run.getBean(CityDubboConsumerService.class);
        cityService.printCity();
    }
}
Explain this logic, that is, get the city Dubbo service consumer bean from the bean container after startup. Then call the bean method to verify that the Dubbo call was successful.
 

4. Summary

There are also monitoring and governance related to services. This has nothing to do with SpringBoot in essence, so I won't introduce them one by one here. Thanks to Ali teaey for the starter-dubbo project.
 
Recommend " Spring boot those things "
Original article, please indicate:  Reprinted from Concurrent Programming Network – ifeve.com Link to this article:   Springboot integrates Dubbo/ZooKeeper to explain SOA case in detail

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326444739&siteId=291194637