springboot integration simple case of dubbo

Use Framework:

  jdk 1.8

  springboot-2.1.3 

  dubbo-2.6

  spring-data-jpa-2.1.5

First, the development dubbo service interface:

According to Dubbo official development proposals to create an interface project, which only defines interfaces and model class;

1. Create springboot engineering springboot-demo-dubbo-interface

coordinate:

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

Add spring-data-jpa dependency:

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

2. Create a 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() { return id; } //省略set get 方法

3, create an 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);
}

4, use the command to install package clean install maven repository.

 

 

Alibaba provides dubbo integrated springboot open source projects;

Reference documents:

https://github.com/apache/dubbo-spring-boot-project/blob/0.2.x/README_CN.md

This project uses the project's jar package inherit:

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

 

Second, the development dubbo Service Provider:

1, create a project Springboot spring-boot-demo-dubbo-provider and configure specific dependency;

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>
        
        
        <!-- 加入springboot与dubbo集成的起步依赖 -->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version>
        </dependency>

        <!-- 由于使用了zookeeper作为注册中心,则需要加入zookeeper的客户端jar包: -->
        <dependency>
            <groupId>com.101tec</groupId>
            <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> 
        <-! Add interface services ->
        </ dependency>
        
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>spring-boot-demo-dubbo-interface</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

2, the configuration information dubbo in the core configuration file application.properties Springboot in:

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

# 访问端口
server.port=8080
# dubbo配置
dubbo.application.name=springboot-dubbo-provider
dubbo.registry.address=zookeeper://192.168.146.128:2181

3, development interface implementation class written in 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;

@Component //注册为spring bean
@Service // 这注解是dubbo提供的 
public class UserServiceImpl implements UserService {
    
    @Autowired
    private UserRepository userRepository;

    @Override
    public void save(User user) {
        userRepository.save(user);
    }

    @Override
    public String sayHello(String word) {
        return word;
    }
}

4, main entrance program starts Dubbo Service Provider: add annotations @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);
    }

}

 Start main, zookeeper service published to the registry.

 

Third, the development dubbo service consumers:

1, create a project Springboot spring-boot-demo-dubbo-consumer and configuring the related dependencies;

2, integrated with added springboot dubbo starting dependence: (pom.xml configuration supra)

 Note: Service providers and consumers need to configure the service interface dependence 

3, the configuration information dubbo in the core configuration file application.properties Springboot in:

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\u7F6E
dubbo.application.name=springboot-dubbo-consumer
dubbo.registry.address=zookeeper://192.168.146.128:2181 

 

4, write a Controller class, call the remote service Dubbo:

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 //该注解是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);
    }
}

 

 

5, start the class to add comment @EnableDubbo open dubbo

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);
    }
}

 

  6, start the main method.

  7, calling the remote interface:

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

A simple springboot complete service-based interface development dubbo.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  

Guess you like

Origin www.cnblogs.com/qq99514925/p/10980041.html