Springboot統合dubboの簡単なケース
13923 ワード
フレームワークの使用:
jdk 1.8
springboot-2.1.3
dubbo-2.6
spring-data-jpa-2.1.5
一、dubboサービスインタフェースの開発:
Dubboの公式開発提案に従って、インタフェースとmodelクラスのみを定義するインタフェースプロジェクトを作成します.
1、springbootプロジェクトspring-boot-demo-dubbo-interfaceの作成
座標:
com.example spring-boot-demo-dubbo-interface 0.0.1-SNAPSHOT
spring-data-jpa依存を追加するには:
org.springframework.boot spring-boot-starter-data-jpa
2、モデルの作成
アリババが提供するdubbo集積springbootオープンソースプロジェクト;
参照ドキュメント:
https://github.com/apache/dubbo-spring-boot-project/blob/0.2.x/README_CN.md
このプロジェクトは、このプロジェクトのjarパッケージを継承します.
com.alibaba.boot dubbo-spring-boot-starter 0.2.0
二、dubboサービスプロバイダの開発:
1、Springbootプロジェクトspring-boot-demo-dubbo-providerを作成し、関連する依存を構成する.
pom.xml
三、dubboサービス消費者の開発:
1、Springbootプロジェクトspring-boot-demo-dubbo-consumerを作成し、関連する依存を構成する.
2、springbootとdubboの統合の開始依存を加える:(pom.xml構成は同じ)
注意:サービスプロバイダと消費者は、サービスインタフェース依存性を構成する必要があります.
3.Springbootのコアプロファイルアプリケーション.propertiesでdubboを構成する情報:
4、コントローラクラスを作成し、リモートのDubboサービスを呼び出す:
5、起動クラス追加オープンdubbo注記@EnableDubbo
6、mainメソッドを起動します.
7、リモートインタフェースを呼び出す:
jdk 1.8
springboot-2.1.3
dubbo-2.6
spring-data-jpa-2.1.5
一、dubboサービスインタフェースの開発:
Dubboの公式開発提案に従って、インタフェースとmodelクラスのみを定義するインタフェースプロジェクトを作成します.
1、springbootプロジェクトspring-boot-demo-dubbo-interfaceの作成
座標:
com.example spring-boot-demo-dubbo-interface 0.0.1-SNAPSHOT
spring-data-jpa依存を追加するには:
org.springframework.boot spring-boot-starter-data-jpa
2、モデルの作成
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、インタフェースの作成: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、コマンドclean installを使用してmaven倉庫にパッケージインストールする.アリババが提供するdubbo集積springbootオープンソースプロジェクト;
参照ドキュメント:
https://github.com/apache/dubbo-spring-boot-project/blob/0.2.x/README_CN.md
このプロジェクトは、このプロジェクトのjarパッケージを継承します.
com.alibaba.boot dubbo-spring-boot-starter 0.2.0
二、dubboサービスプロバイダの開発:
1、Springbootプロジェクトspring-boot-demo-dubbo-providerを作成し、関連する依存を構成する.
pom.xml
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
com.alibaba.boot
dubbo-spring-boot-starter
0.2.0
com.101tec
zkclient
0.10
org.springframework.boot
spring-boot-starter-data-jpa
mysql
mysql-connector-java
runtime
com.example
spring-boot-demo-dubbo-interface
0.0.1-SNAPSHOT
2.Springbootのコアプロファイルアプリケーション.propertiesでdubboを構成する情報: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、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プログラム起動Dubboサービスプロバイダ:注釈を追加@EnableDubbopackage 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);
}
}
mainを起動し、zookeeper登録センターにサービスをパブリッシュします.三、dubboサービス消費者の開発:
1、Springbootプロジェクトspring-boot-demo-dubbo-consumerを作成し、関連する依存を構成する.
2、springbootとdubboの統合の開始依存を加える:(pom.xml構成は同じ)
注意:サービスプロバイダと消費者は、サービスインタフェース依存性を構成する必要があります.
3.Springbootのコアプロファイルアプリケーション.propertiesでdubboを構成する情報:
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、コントローラクラスを作成し、リモートの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、起動クラス追加オープンdubbo注記@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);
}
}
6、mainメソッドを起動します.
7、リモートインタフェースを呼び出す:
http://localhost:8081/say?name=hello
springboot dubbo 。