SpringBoot集成SOFA-RPC+Zookeeper

1. 快速开始

1.1 环境准备

  • JDK7+
  • 需要采用 Apache Maven 3.2.5 或者以上的版本来编译

创建工程

直接在 https://start.spring.io/ 构建Spring Boot工程。构建时可以添加一些常用依赖,例如:添加 Web 的依赖,以便最后在浏览器中查看效果。

1.png

1.2 pom文件引入依赖

替换前:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
替换后:
<parent>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofaboot-dependencies</artifactId>
<version>2.3.1</version>
</parent>

dependencies下添加一下依赖:

 <!--SOFABoot 健康检查 -->
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>healthcheck-sofa-boot-starter</artifactId>
</dependency>
<!-- SOFA-RPC 环境 -->
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>rpc-sofa-boot-starter</artifactId>
</dependency>
<!-- Zookeeper 作为服务注册列表 -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
<version>0.10</version>
</dependency>

完整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>
<parent>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofaboot-dependencies</artifactId>
<version>2.3.1</version>
</parent>
<groupId>com.sofa.demo</groupId>
<artifactId>sofa-client-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sofa-client-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--SOFABoot 健康检查 -->
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>healthcheck-sofa-boot-starter</artifactId>
</dependency>
<!-- SOFA-RPC 环境 -->
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>rpc-sofa-boot-starter</artifactId>
</dependency>
<!-- Zookeeper 作为服务注册列表 -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
<version>0.10</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</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-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

1.4 application.properties配置

##当前应用端口
server.port=8081
##当前应用名称(不能删除,作为eureka服务时,名称不能带下划线“_”,可为“-”,否则注册中心访问不到指定服务)
spring.application.name=SOFA-BIP-Client
## logging path
logging.path=./logs
## zookeeper address list
com.alipay.sofa.rpc.registry.address=zookeeper://127.0.0.1:2181

com.alipay.sofa.rpc.registry.address属性指:配置zookeeper地址。

1.5 编写sofa服务接口和实现类

sofa提供者工程结构图:

2.png

HelloSyncService.java

package com.uisftech.bip.service;
public interface HelloSyncService {
String saySync(String string);
}

HelloSyncServiceImpl.java

package com.sofa.demo.service.impl;
import com.sofa.demo.service.HelloSyncService;
public class HelloSyncServiceImpl implements HelloSyncService {
@Override
public String saySync(String sync) {
// TODO Auto-generated method stub
return sync;
}
}

1.6 编写sofa服务提供方配置文件

bip-sofa-service.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:sofa="http://sofastack.io/schema/sofaboot"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://sofastack.io/schema/sofaboot http://sofastack.io/schema/sofaboot.xsd"
default-autowire="byName">
<bean id="helloSyncServiceImpl"
class="com.sofa.demo.service.impl.HelloSyncServiceImpl" />
<!-- 以多种通信协议发布服务 -->
<sofa:service ref="helloSyncServiceImpl"
interface="com.sofa.demo.service.HelloSyncService">
<sofa:binding.bolt />
<sofa:binding.rest />
<sofa:binding.dubbo />
</sofa:service>
</beans>
  • 通过sofa:service元素将该服务发布,其中ref属性表示发布的服务实例,interface 属性表示该服务的接口。
  • bolt: 服务通过 bolt 协协议通道发布,底层基于 Netty 实现。
  • rest: 服务通过 http 协议发布。
  • dubbo: 服务基于 dubbo 的协议通道发布。

1.7 编写服务提供方启动程序

package com.sofa.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
@ImportResource({ "classpath:bip-sofa-service.xml" })
public class SimpleServerApplication {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(SimpleServerApplication.class);
ApplicationContext applicationContext = springApplication.run(args);
}
}

以上是sofa服务提供者创建步骤,接下来新建sofa服务消费方工程。

创建消费方工程以及pom文件修改、application.properties文件配置直接参考服务提供者工程1.2-1.4步骤,内容一样的。需要注意的是两个工程的application.properties文件中的server.port端口不能一样。

sofa消费方工程结构图:

3.png

1.1 新建sofa服务接口

从工程图中可以看出,我们需要消费谁提供的接口,就必须在自己工程中按提供者接口原路径新建一个一模一样的接口类。

一般都是提供者会把接口定义类会给我们,我们新建好包路径直接把提供者接口类放进去就行,包路径必须一致。

1.2 编写sofa服务消费方配置文件

bip-sofa-client.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:sofa="http://sofastack.io/schema/sofaboot"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://sofastack.io/schema/sofaboot http://sofastack.io/schema/sofaboot.xsd"
default-autowire="byName">
<!-- bolt引用 -->
<sofa:reference id="boltHelloSyncServiceReference"
interface="com.uisftech.bip.service.HelloSyncService">
<sofa:binding.bolt />
</sofa:reference>
<!-- rest引用 -->
<sofa:reference id="restHelloSyncServiceReference"
interface="com.uisftech.bip.service.HelloSyncService">
<sofa:binding.rest />
</sofa:reference>
<!-- dubbo引用 -->
<sofa:reference id="dubboHelloSyncServiceReference"
interface="com.uisftech.bip.service.HelloSyncService">
<sofa:binding.dubbo />
</sofa:reference>
</beans>

1.3 编写服务消费方启动程序

package com.uisftech.bip;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ImportResource;
import com.uisftech.bip.service.HelloSyncService;
@ImportResource({ "classpath:bip-sofa-client.xml" })
@SpringBootApplication
public class SimpleClientApplication {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(SimpleClientApplication.class);
ApplicationContext applicationContext = springApplication.run(args);
HelloSyncService boltHelloSyncService = (HelloSyncService) applicationContext
.getBean("boltHelloSyncServiceReference");
HelloSyncService restHelloSyncService = (HelloSyncService) applicationContext
.getBean("restHelloSyncServiceReference");
HelloSyncService dubboHelloSyncService = (HelloSyncService) applicationContext
.getBean("dubboHelloSyncServiceReference");
System.out.println("Bolt result:" + boltHelloSyncService.saySync("bolt"));
System.out.println("Rest result:" + restHelloSyncService.saySync("rest"));
System.out.println("Dubbo result:" + dubboHelloSyncService.saySync("dubbo"));
}
}

注意:因SOFA注册机制,测试时需要两台机器,一台启动sofa服务提供者,一台启动sofa服务消费者。

分别启动服务端和客户端,客户端控制台输出日志如下:

启动之前一定要记得启动zookeeper注册中心

4.png

  1. 注意:maven聚合工程,父级的pom需要引入sofaboot-dependencies依赖:
<!--sofa开源集成zookeeper -->
<parent>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofaboot-dependencies</artifactId>
<version>2.3.1</version>
</parent>
  1. maven聚合工程,子工程pom依赖则不要引入sofaboot-dependencies,其它sofa依赖引入和上面提到的一样,具体引入依赖参考如下代码:
<!--***sofa开源集成zookeeper*** -->
<!--SOFABoot 健康检查 -->
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>healthcheck-sofa-boot-starter</artifactId>
</dependency>
<!-- SOFA-RPC 环境 -->
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>rpc-sofa-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava-version}</version>
</dependency>
<!-- Zookeeper 作为服务注册列表 -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
<version>0.10</version>
</dependency>
<!--***sofa开源集成zookeeper-end*** -->

竟然都看到最后了,给小编点个关注吧,小编还会持续更新的,只收藏不点关注的都是在耍流氓!

发表评论
留言与评论(共有 0 条评论)
   
验证码:

相关文章

推荐文章

'); })();