1. Install the Zookeeper environment
Description of the zk command for setting up the Zookeeper environment
2. Service providers
2.1 pom depends on
<dependencies> <! <dependency> <groupId>org.springframework. Boot </groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <! --> <dependency> <groupId>org.springframework. Boot </groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <! --spring data jpa --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <! --mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> < version > 8.0.8 - DMR < / version > the < scope > compile < / scope > < / dependency > <! Springboot </groupId> <artifactId> Spring-boot-starter-dubbo </artifactId> </dependency> <! --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>com.tyut</groupId> <artifactId>ip-common</artifactId> <version>${ip.version}</version> </dependency> </dependencies>Copy the code
2.2. Service
Outline the project structure:
For convenience, I use springBoot Data JPA as the persistence framework. If not jPA, please see my Springboot JPA integration first.
- SchoolRepository .java
/**
* Created by Fant.J.
*/
@Repository
public interface SchoolRepository extends JpaRepository<School,Integer> {
}
Copy the code
- Schoolservice.java (a normal excuse)
- Implement the schoolServiceImp.java core code
@service (version = "2.0.1") public Class Implements SchoolService {@autoWired private SchoolRepository schoolRepository;Copy the code
The @service annotation here is a dubbo annotation, not a springframework annotation. This annotation is to register the service with ZK.
2.3. The application properties
Server port = 9002 # # Dubbo service provider configuration spring. The Dubbo. Application. The name = school - server spring.dubbo.registry.address=zookeeper://xxx.xxx.xxx.xxx:2181 spring.dubbo.protocol.name=dubbo spring.dubbo.protocol.port=20882 spring.dubbo.scan=com.xxx.school.service spring.datasource.url=jdbc:mysql://xxxxxxxxxx spring.datasource.username=xxxx spring.datasource.password=xxxx spring.datasource.driver-class-name=com.mysql.jdbc.DriverCopy the code
3. Service consumption
To decouple from the Service provider, we need to separate the Service interface class and put it in the client module. We won’t post detailed code here.
3.1 the pom. XML
The dependencies are populated as needed in the Controller classCopy the code
3.2 SchoolController
@restController @RequestMapping("/ SCH ") Public Class SchoolController {@reference (version = "2.0.1") private SchoolService schoolService; @RequestMapping("/all") public ServerResponse getAll(){ return schoolService.selectAll(); }}Copy the code
Note the one-to-one correspondence with the version attribute value of the @service annotation.
3.3 application. The properties
# # Dubbo service consumers to configure spring. Dubbo. Application. Spring. The name = XXX Dubbo. Registry. Address = zookeeper: / / XXXX, XXX, XXX, XXX spring.dubbo.scan=com.xxx.web.controllerCopy the code
Successful screenshot:
Leave a comment below if you have any questions.