Using frames:

  1. JDK 1.8

  2. Springboot – 2.1.3

  3. Dubbo – 2.6 –

  4. Spring – data – jpa – 2.1.5

I. Developing dubbo service interface:

Follow Dubbo’s official development recommendations to create an interface project that defines only the interface and model classes;

1. Create a Springboot project spring-boot-demo-dubo-interface

Coordinates:

<groupId>com.example</groupId> <artifactId>spring-boot-demo-dubbo-interface</artifactId> < version > 0.0.1 - the SNAPSHOT < / version >Copy the code

Add spring-data-jPA dependencies:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
Copy the code

2. Create model

package com.example.demo.model;

@Entity
public class User implements Serializable{
    private static final long serialVersionUID = 1L;
    
    @Id @GeneratedValue
    private long id;
    private String userName;
    private String password;
    private int age;
    public long getId() {
        returnid; } / / omittedsetThe get methodCopy the code

Create interface:

package com.example.demo.service;

import com.example.demo.model.User;

public interface UserService {
    
    public void save(User user);
    
    public String sayHello(String word);
}
Copy the code

4. Use the clean install command to package and install to maven repository.

Dubbo integrated Springboot open source project provided by Alibaba;

Reference Documents:

Github.com/apache/dubb…

This project adopts the JAR package of the project to inherit:

<dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> The < version > 0.2.0 < / version > < / dependency >Copy the code

Ii. Develop Dubbo service providers:

1. Create a Springboot project spring-boot-Demo-dubbo-provider and configure related dependencies.

pom.xml

<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> <! <dependency> <groupId>com.alibaba. Boot </groupId> < artifactId > dubbo - spring - the boot - starter < / artifactId > < version > 0.2.0 < / version > < / dependency > <! Add zooKeeper client JAR to the registry: --> <dependency> <groupId>com.101tec</ artifactId> zkClient </artifactId> <version>0.10</version> </dependency> <! -- spring-data-jpa --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <! <dependency> <groupId>com.example</groupId> <artifactId> spring-boot-demo-dubo-interface </artifactId> < version > 0.0.1 - the SNAPSHOT < / version > < / dependency > < / dependencies >Copy the code

Dubbo = Springboot; dubbo = Springboot;

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test? serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.datasource.username=root
spring.datasource.password=root

spring.jpa.properties.hibernate.hbm2ddl.auto=create
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.show-sql=true

Access port
server.port=8080
# dubbo configurationDubbo. Application. Name = springboot dubbo - provider dubbo, registry. Address = zookeeper: / / 192.168.146.128:2181Copy the code

3. Develop and write interface implementation classes for Dubbo:

package com.example.demo.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.alibaba.dubbo.config.annotation.Service; import com.example.demo.model.User; import com.example.demo.repository.UserRepository; Public class UserServiceImpl implements UserService {@autoWired private UserRepository userRepository; @Override public void save(User user) { userRepository.save(user); } @Override public String sayHello(String word) {returnword; }}Copy the code

Start Dubbo service provider: add annotation @enableDubbo

package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo; @SpringBootApplication @EnableDubbo public class SpringBootDemoDubboProviderApplication { public static void main(String[] args) { SpringApplication.run(SpringBootDemoDubboProviderApplication.class, args); }}Copy the code

Start main and publish the service to the ZooKeeper registry.

Iii. Develop Dubbo service consumers:

1, create a Springboot project spring-boot-demo-dubo-consumer and configure related dependencies;

2, add springboot and dubbo integration startup dependency :(pom.xml configuration is the same as above)

Note: Service interface dependencies are configured for both the service provider and consumer

Dubbo = Springboot; dubbo = Springboot; dubbo = Springboot;

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test? serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.datasource.username=root
spring.datasource.password=root

# WEB\u670D\u52A1\u7AEF\u53E3
server.port=8081
# dubbo\u914D\u7F6EDubbo. Application. Name = springboot dubbo - consumer dubbo, registry. Address = zookeeper: / / 192.168.146.128:2181Copy the code

4, Write a Controller class to call the remote Dubbo service:

package com.example.demo.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.alibaba.dubbo.config.annotation.Reference; import com.example.demo.model.User; import com.example.demo.service.UserService; @restController Public class UserController {@reference // This annotation is provided by Dubbo private UserService UserService; @RequestMapping("/say")
    public String sayHello(String name) {
        return userService.sayHello(name);
    }
    
    @RequestMapping("/save")
    public void save() {
        User u = new User();
        u.setAge(20);
        u.setPassword("123");
        u.setUserName("zheng"); userService.save(u); }}Copy the code

5, start class add EnableDubbo annotation @enabledubbo

package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo; @SpringBootApplication @EnableDubbo public class SpringBootDemoDubboConsumerApplication { public static void main(String[] args) { SpringApplication.run(SpringBootDemoDubboConsumerApplication.class, args); }}Copy the code

6. Start the main method.

7, call remote interface:

http://localhost:8081/say?name=hello

A SpringBoot service interface based on Dubbo is developed.