随笔,自己记录一下。
一、先创建一个maven项目 dubbo-pom,里面包含两个module (dubbo-api)和(dubbo-consumer)
如下:
dubbo-pom.xml :
4.0.0
pom
dubbo-api
dubbo-consumer
org.springframework.boot
spring-boot-parent
2.0.3.RELEASE
com.ivan.testDubbo
dubbo-pom
1.0-SNAPSHOT
com.ivan.testDubbo
dubbo-api
1.0-SNAPSHOT
dubbo-api :
1、dubbo-api.xml :
dubbo-pom
com.ivan.testDubbo
1.0-SNAPSHOT
4.0.0
dubbo-api
2、在dubbo-api中新建一个接口:
dubbo-cunsumer :
1、dubbo-cunsumer.xml:
dubbo-pom
com.ivan.testDubbo
1.0-SNAPSHOT
4.0.0
dubbo-consumer
org.springframework.boot
spring-boot-starter
com.alibaba.boot
dubbo-spring-boot-starter
0.2.0
org.springframework.boot
spring-boot-starter-data-jpa
mysql
mysql-connector-java
com.alibaba
druid
1.1.10
com.ivan.testDubbo
dubbo-api
2、在dubbo-cunsumer中新建po类、WechatRepository JPA接口类、springboot启动类、LoadDBTask定时推送类:
WechatLoginPo :
package com.ivan.dubbo.consumer;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
/**
* @author ivan
* @date 2018-07-31
*/
@Entity
@Table(name = "xm_wechat_login")
public class WeChatLoginPo implements Serializable {
@Id
@Column(name = "appKey")
String appKey;
@Column(name = "appId")
String appId;
@Column(name = "appSecret")
String appSecret;
public String getAppKey() {
return appKey;
}
public void setAppKey(String appKey) {
this.appKey = appKey;
}
public String getAppId() {
return appId;
}
public void setAppId(String appId) {
this.appId = appId;
}
public String getAppSecret() {
return appSecret;
}
public void setAppSecret(String appSecret) {
this.appSecret = appSecret;
}
}
WechatRepository :
package com.ivan.dubbo.consumer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
/**
* @author ivan
* @date 2018-07-31
*/
@Repository
public interface WechatRepository extends JpaRepository {
}
LoadDBTask定时推送类:
package com.ivan.dubbo.consumer;
import com.alibaba.dubbo.config.annotation.Reference;
import com.ivan.dubbo.api.AppSetting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @author ivan
* @date 2018-07-31
*/
@Component
public class LoadDBTask {
@Autowired
WechatRepository repository;
@Reference(version = "1.0.0")
AppSetting appSettingService;
@Scheduled(fixedRate = 10000L)
public void execute() {
List all = repository.findAll();
for (WeChatLoginPo po : all) {
appSettingService.push(po.getAppKey(), po.getAppId(), po.getAppSecret());
}
}
}
springboot启动类:
package com.ivan.dubbo.consumer;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.scheduling.annotation.EnableScheduling;
/**
* @author ivan
* @date 2018-07-31
*/
@SpringBootApplication
@EnableScheduling
public class BootStartup {
public static void main(String[] args) throws Exception {
new SpringApplicationBuilder(BootStartup.class).web(WebApplicationType.NONE)
.run(args);
}
}
还有springboot的配置文件:
dubbo.application.name=dubbo-consumer
dubbo.registry.address=zookeeper://--------------按自己实际的来
dubbo.registry.id=dubbo-consumer-register
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://--------------按自己实际的来
spring.datasource.username=mysql
spring.datasource.password=password
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.hibernate.use-new-id-generator-mappings=false
到这里,我们的消费者就写好了。
提供者的代码,这边项目之前就写好了,所以dubbo的配置文件我也不太懂,今天只是加了一行提供者的代码,基本就可以运行了:
还写了一个AppSettingImp 实现类,用来调用消费者中的AppSetting接口 push()方法:
package open.xmeye.net.gateway.wechat.api.interfaces.services.user.wechat;
import open.xmeye.net.wx.api.AppSetting;
import org.springframework.stereotype.Service;
@Service("appSettingService")
public class AppSettingImp implements AppSetting {
@Override
public void push(String s, String s1, String s2) {
System.out.println(s + "--" + s1 + "---" + s2);
}
}
两个项目开启后,控制台就会每10秒打印出被推送过来的数据库中app表的信息。
主要用于巩固、梳理一下,今天同事给我讲的这些代码,理清消费者、提供者、dubbo的简单用法,以便于下次使用时能更快上手。