springboot+dubbo+myBatis實現微服務整合
程式碼下載:https://download.csdn.net/download/typ1805/10485048
微服務架構成了當下的技術熱點,實現微服務是要付出很大成本的,但也許是因為微服務的優點太過於吸引人,以至於大部分開發者都將它當成未來的發展趨勢。
微服務架構的演進過程
dubbo的使用者手冊中介紹了服務化架構的進化過程,如下圖:
微服務的技術關注點
要實現一個微服務的架構,我們需要關注的技術點包括:服務註冊、發現、負載均衡和健康檢查,前端路由(閘道器),容錯,服務框架的選擇,動態配置管理等模組。這些模組可以組成一個簡化的微服務架構圖如下:
使用dubbo+zookeeper實現簡化的微服務架構
第一步:zookeeper叢集的部署
使用zookeeper作為dubbo的註冊中心,部署起來並不麻煩。為了保持註冊中心的高可用性,在生產環境下我們需要配置多個zookeeper協同執行。在叢集模式下,zookeeper會基於Paxos演算法從叢集中選擇一臺作為leader,其他機器作為follower,註冊中心的資料都以leader為準。一臺zk機器成為leader的條件是超過這臺機器是可用的,且被超過半數的機器選舉為leader。基於這種實現方式,我們選擇zk叢集的數量時最好為奇數個,最少為3個,這樣只要有超過半數的zk機器存活那註冊中心就是可用的。
注:如果我們選擇2臺機器作為zk的叢集,那隻要有一臺zk掛掉,另一臺機器就無法得到超過半數的選票,那麼這個zk叢集就直接失效了。因此選2臺機器作為zk叢集的穩定性在理論上不如一臺機器作為註冊中心的穩定性。
以3臺機器作為zk叢集為例,每臺zk的具體部署方式為:
1、下載安裝包並解壓到安裝目錄,zookeeper安裝包的下載地址為:http://www.apache.org/dist/zookeeper/
2、進入解壓目錄的conf資料夾,配置zookeeper啟動的基本引數。
在conf資料夾下有一個 zoo_sample.cfg的檔案,是zk啟動的配置樣例,zookeeper程序在啟動的時候會找zoo.cfg檔案作為預設的配置檔案,所以我們複製一個名稱為zoo.cfg的檔案,並編輯其中的配置資訊如下:
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=D:/zookeeper-3.4.12/data
dataLogDir=D:/zookeeper-3.4.12/logs
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
# zookeeper叢集的部署
# server.A=B:C:D
# A為數字,標識這條配置為第幾個zk伺服器,即機器id
# B為host名,標識這個伺服器的主機地址
# C和D為zk叢集的成員用於選舉leader時的通訊埠
server.1=zoo1:2888:3888
server.2=zoo2:2888:3888
server.3=zoo3:2888:3888
執行zookeeper程序,在zookeeper目錄下的bin資料夾中,執行
./zkServer.sh start
ps: 停止方法——執行 ./zkServer.sh stop
1
2
第二步:開發前端服務和後端通用服務,maven模組結構,如圖:
springboot-dubbo-parent的pom.xml檔案:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>springboot-dubbo-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>springboot-dubbo-parent</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modules>
<module>springboot-dubbo-server</module>
<module>springboot-dubbo-client</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<!-- 在properties中統一控制依賴包的版本,更清晰-->
<dubbo.version>2.5.3</dubbo.version>
<zk.version>3.4.5</zk.version>
<zkclient.version>0.1</zkclient.version>
</properties>
<dependencyManagement> <!--dependencyManagement用於管理依賴版本號-->
<dependencies>
<!--刪除spring-boot-starter和spring-boot-starter-test,-->
<!--因為parent中繼承的祖先中已經有了,並且一般dependencyManagement管理的依賴都要寫版本號-->
<!--<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.9.RELEASE</version>
</dependency>
<!--新增後續dubbo專案中所需依賴,dubbo、zk-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version><!--使用properties中配置的版本號-->
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>${zk.version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>${zkclient.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
springboot-dubbo-server的pom.xml檔案:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>springboot-dubbo-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>springboot-dubbo-server</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.example</groupId>
<artifactId>springboot-dubbo-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modules>
<module>springboot-dubbo-api</module>
<module>springboot-dubbo-provider</module>
</modules>
</project>
springboot-dubbo-provider的pom.xml檔案:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.demo.provider</groupId>
<artifactId>apringboot-dubbo-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>apringboot-dubbo-provider</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.example</groupId>
<artifactId>springboot-dubbo-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<properties>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--新增後續dubbo專案中所需依賴,dubbo、zk。
父模組pom中使用dependencyManagement來管理依賴版本號,子模組pom中不需要再寫版本號,exclusion也不需要-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.example.admo.api</groupId>
<artifactId>springboot-dubbo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
dubbo配置檔案provider.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 提供方應用資訊,用於計算依賴關係 -->
<dubbo:application name="demo-provider" />
<!-- 使用zookeeper註冊中心暴露服務地址 -->
<dubbo:registry protocol="zookeeper" address="localhost:2181" timeout="60000"/>
<!-- 用dubbo協議在20880埠暴露服務 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 暴露dubbo服務的方式一-->
<!-- 使用註解方式暴露介面,會自動掃描package下所有包中dubbo相關的註解,這樣就不用在xml中再針對每個服務介面配置dubbo:service interface-->
<!--<dubbo:annotation package="com.example.demo.api.service"/>-->
<!--另外一種是全域性新修改的辦法(timeout時間可以自己調整)-->
<dubbo:provider delay="-1" timeout="12000" retries="0"/>
<!-- 暴露dubbo服務的方式二 -->
<!-- 使用xml配置方式申明暴露一個介面服務,在程式啟動的時候會自動註冊到zookeeper。
等同於在類上打@service註解,打了註解就必須要用annotation指定啟動掃描路徑,使用這種方式,就不需要指定annotation了-->
<dubbo:service interface="com.example.demo.api.service.UserService" ref="userService"/>
<!-- 具體的實現bean,id與上面的ref要一致-->
<bean id="userService" class="com.example.demo.provider.api.impl.UserServiceImpl"/>
</beans>
mysql資料庫配置檔案application.properties:
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-active=10
spring.datasource.max-idle=5
spring.datasource.min-idle=0
服務層介面實現類 UserServiceImpl.java檔案:
package com.example.demo.provider.api.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.example.demo.api.model.User;
import com.example.demo.api.service.UserService;
import com.example.demo.provider.dao.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
/**
* 路徑:com.example.demo.api.impl
* 類名:
* 功能:
* 備註:該Service註解是dubbo的註解,不是spring的。若使用xml配置方式暴露介面,則不需要該註解。
* 建立人:
* 建立時間:
* 修改人:
* 修改備註:
* 修改時間:
*/
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public User findOneById(Integer id) {
return userMapper.findOneById(id);
}
}
dao層介面Usermapper.java檔案:
package com.example.demo.provider.dao;
import com.example.demo.api.model.User;
/**
* 路徑:com.example.demo.dao
* 類名:
* 功能:《用一句話描述一下》
* 備註:
* 建立人:
* 建立時間:
* 修改人:
* 修改備註:
* 修改時間:
*/
public interface UserMapper {
User findOneById(Integer id);
}
myBatis的XML檔案:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.example.demo.provider.dao.UserMapper" >
<select id="findOneById" resultType="com.example.demo.api.model.User">
select id,user_name from user
</select>
</mapper>
啟動類ApringbootDubboProviderApplication.java檔案:
package com.example.demo.provider;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ImportResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
/**
* 路徑:com.example.demo.provider
* 類名:
* 功能:《用一句話描述一下》
* 備註:
* 建立人:
* 建立時間:
* 修改人:
* 修改備註:
* 修改時間:
*/
@SpringBootApplication
@MapperScan("com.example.demo.provider.dao")
@ImportResource("classpath:provider.xml")
public class ApringbootDubboProviderApplication {
@Bean
@ConfigurationProperties(prefix="spring.datasource")
public DataSource dataSource() {
return new org.apache.tomcat.jdbc.pool.DataSource();
}
@Bean
public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource());
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/mapper/*.xml"));
return sqlSessionFactoryBean.getObject();
}
@Bean
public PlatformTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSource());
}
public static void main(String[] args) throws InterruptedException {
SpringApplication.run(ApringbootDubboProviderApplication.class, args);
//pom中沒有加spring-boot-starter-web依賴,啟動時沒有tomcat容器,會自動退出,所以加了一個sleep防止自動退出
Thread.sleep(Long.MAX_VALUE);
}
}
springboot-dubbo-api的pom.xml檔案:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.admo.api</groupId>
<artifactId>springboot-dubbo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot-dubbo-api</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.example</groupId>
<artifactId>springboot-dubbo-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
api服務介面UserService.java檔案:
package com.example.demo.api.service;
import com.example.demo.api.model.User;
/**
* 路徑:com.example.demo.api.service
* 類名:
* 功能:
* 備註:
* 建立人:
* 建立時間:
* 修改人:
* 修改備註:
* 修改時間:
*/
public interface UserService {
User findOneById(Integer id);
}
實體類User.java檔案:
package com.example.demo.api.model;
import java.io.Serializable;
/**
* 路徑:com.example.demo.api.model
* 類名:
* 功能:《用一句話描述一下》
* 備註:
* 建立人:
* 建立時間:
* 修改人:
* 修改備註:
* 修改時間:
*/
public class User implements Serializable {
private Integer id;
private String user_name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
}
springboot-dubbo-client的pom.xml檔案:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>springboot-dubbo-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot-dubbo-client</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.example</groupId>
<artifactId>springboot-dubbo-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<properties>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--新增後續dubbo專案中所需依賴,dubbo、zk。
父模組pom中使用dependencyManagement來管理依賴版本號,子模組pom中不需要再寫版本號,exclusion也不需要-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
</dependency>
<dependency>
<groupId>com.example.admo.api</groupId>
<artifactId>springboot-dubbo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
dubbo配置檔案consumer.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!--消費方應用名,用於計算依賴關係,不是匹配條件,不要與提供方一樣 -->
<dubbo:application name="demo-consumer"/>
<!--zookeeper註冊中心 -->
<dubbo:registry protocol="zookeeper" address="localhost:2181" timeout="60000"/>
<!--關閉服務消費方所有服務的啟動檢查。dubbo預設會在啟動時檢查依賴的服務是否可用,不可用時會丟擲異常,阻止Spring初始化完成。-->
<dubbo:consumer check="false" />
<!-- 使用註解方式建立遠端服務代理-->
<dubbo:annotation package="com.example.demo.controller"/>
<!-- 使用xml配置方式建立遠端服務代理,id即為provider.xml中暴露的服務的id-->
<!-- 等同於dubbo:annotation 加上程式碼裡的@Reference註解-->
<!--<dubbo:reference id="userService" interface="com.example.demo.api.service.UserService"/>-->
</beans>
UserController.java檔案:
package com.example.demo.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.example.demo.api.model.User;
import com.example.demo.api.service.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
/**
* 路徑:com.example.demo.controller
* 類名:
* 功能:《用一句話描述一下》
* 備註:
* 建立人:
* 建立時間:
* 修改人:
* 修改備註:
* 修改時間:
*/
@RestController
@RequestMapping("/user")
public class UserController {
/**
* 注入服務提供方暴露的介面,通過@Reference註解,dubbo會在掃描的時候自動代理介面,然後通過rpc呼叫遠端服務。
* 如果用xml配置方式,需要將@Reference換成@Autowired。
*/
@Reference
UserService userService;
@RequestMapping("/find")
@ResponseBody
public User findOneById(Integer id){
System.out.println("------Controller--------id------"+id);
User user = userService.findOneById(id);
if(user == null){
user = new User();
}
return user;
}
}
啟動類 SpringbootDubboClientApplication.java檔案:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
/**
* 路徑:com.example.demo
* 類名:
* 功能:《用一句話描述一下》
* 備註:
* 建立人:
* 建立時間:
* 修改人:
* 修改備註:
* 修改時間:
*/
@SpringBootApplication
@ImportResource("classpath:consumer.xml")
public class SpringbootDubboClientApplication {
public static void main(String[] args) throws Exception{
SpringApplication.run(SpringbootDubboClientApplication.class, args);
}
}
啟動成功,如圖:
瀏覽器訪問,如圖:
注意(遠端呼叫失敗問題):
Caused by: com.alibaba.dubbo.rpc.RpcException: Failed to invoke the method subscribe in the service com.alibab
a.dubbo.registry.RegistryService. Tried 3 times of the providers[172.168.1.167:2181] (1/1) from the registry
172.168.1.167:2181 on the consumer 169.254.249.102 using the dubbo version2.4.9. Last error is: Invoke remote
method timeout.
解決方法一:
這個是由於dubbo介面中的的傳輸物件沒有被序列化而導致的,只需要要檢查dubbo介面中引數中的實體類實現序列化(implementsSerializable)就可以解決這一個異常.
解決方法二:
解決這個問題這個只需要重新暴露服務,在Zk上註冊服務提供者資訊即可。具體就是要排查服務提供者可能出現的問題,解決後部署並重啟服務提供者即可。
解決方法三:
1.基本就是dubbo provider沒有啟動會造成這種情況。
2.api和service是同一個專案,並不是倆專案。
通過啟動api,run on server是不能啟動service的,必須通過config/startConfig.java,debug as java application啟動。
3.啟動思路總結:
1.api通過run on server啟動
2.service通過startConfig.java啟動。debug as java application
要想了解更多的資訊可以通過官方文件進行學習:
dubbo中文文件:http://dubbo.io/
zookeeper文件:http://zookeeper.apache.org/