[Unity学习教程] SOFABoot——根本利用(笔记)

[复制链接]
查看1244 | 回复0 | 2023-8-23 12:08:27 | 显示全部楼层 |阅读模式 来自 中国北京
一、前言

SOFABoot 是蚂蚁金服开源的基于 Spring Boot 的研发框架。
具体可见


  • SOFABoot
  • SOFABoot官方文档
版本关系

二、快速开始

2.1 根本搭建

依官方文档所言,为顺利从中心堆栈拉取 SNAPSHOT 包我们在Maven的setting.xml中参加如下配置:
  1. <profile>
  2.     <id>default</id>
  3.     <activation>
  4.         <activeByDefault>true</activeByDefault>
  5.     </activation>
  6.     <repositories>
  7.         <repository>
  8.             <snapshots>
  9.                 <enabled>true</enabled>
  10.             </snapshots>
  11.             <id>maven-snapshot</id>
  12.             <url>https://oss.sonatype.org/content/repositories/snapshots</url>
  13.         </repository>
  14.     </repositories>
  15.     <pluginRepositories>
  16.         <pluginRepository>
  17.             <snapshots>
  18.                 <enabled>true</enabled>
  19.             </snapshots>
  20.             <id>maven-snapshot</id>
  21.             <url>https://oss.sonatype.org/content/repositories/snapshots</url>
  22.         </pluginRepository>
  23.     </pluginRepositories>
  24. </profile>
复制代码
创建Maven项目
以如下依靠为参考
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5.     <modelVersion>4.0.0</modelVersion>
  6.     <groupId>org.example</groupId>
  7.     <artifactId>test</artifactId>
  8.     <version>1.0-SNAPSHOT</version>
  9.     <properties>
  10.         <maven.compiler.source>8</maven.compiler.source>
  11.         <maven.compiler.target>8</maven.compiler.target>
  12.     </properties>
  13.     <parent>
  14.         <groupId>com.alipay.sofa</groupId>
  15.         <artifactId>sofaboot-dependencies</artifactId>
  16.         <version>2.3.1</version>
  17.     </parent>
  18.     <dependencies>
  19.         <dependency>
  20.             <groupId>com.alipay.sofa</groupId>
  21.             <artifactId>healthcheck-sofa-boot-starter</artifactId>
  22.         </dependency>
  23.         <dependency>
  24.             <groupId>org.springframework.boot</groupId>
  25.             <artifactId>spring-boot-starter-web</artifactId>
  26.         </dependency>
  27.     </dependencies>
  28.     <build>
  29.         <plugins>
  30.             <plugin>
  31.                 <groupId>org.springframework.boot</groupId>
  32.                 <artifactId>spring-boot-maven-plugin</artifactId>
  33.             </plugin>
  34.         </plugins>
  35.         <resources>
  36.             <resource>
  37.                 <directory>src/main/java</directory>
  38.                 <includes>
  39.                     <include>**/*.xml</include>
  40.                 </includes>
  41.             </resource>
  42.             <resource>
  43.                 <directory>src/main/resources</directory>
  44.                 <includes>
  45.                     <include>**/*.properties</include>
  46.                     <include>**/*.xml</include>
  47.                     <include>**/*.yml</include>
  48.                 </includes>
  49.             </resource>
  50.         </resources>
  51.     </build>
  52. </project>
复制代码
同SpringBoot一样,完成MainApplication的创建,然后运行
可参考Spring Boot2——根本利用(笔记)
  1. package com.test;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class MainApplication {
  6.     public static void main(String[] args){
  7.         SpringApplication.run(MainApplication.class,args);
  8.     }
  9. }
复制代码
依据滚网,参加如下配置一下yml文件:
  1. server:
  2.   port: 8080
  3. spring:
  4.   application:
  5.     name: testApplication
  6. logging:
  7.   path: ./logs
复制代码
2.2 测试是否成功



  • 查询如下网址,SOFABoot 中利用 Maven 插件天生的版本信息汇总
  1. http://localhost:8080/sofaboot/versions
  2. (在SOFABoot 3.x 中调整了 endpoint 路径,sofaboot/versions 更改为 actuator/versions)
复制代码
点击直接查询(3.x 前)
点击直接查询(3.x 后)
  1. [
  2.     {
  3.         "GroupId": "com.alipay.sofa",
  4.         "Doc-Url": "https://github.com/alipay/sofa-boot",
  5.         "ArtifactId": "infra-sofa-boot-starter",
  6.         "Bulit-Time": "2018-04-18T22:19:09+0800",
  7.         "Commit-Time": "2018-04-18T22:07:52+0800",
  8.         "Commit-Id": "466f0e039b250ff7b201dc693eec7fa07eb21ad7",
  9.         "Version": "2.3.1"
  10.     }
  11. ]
复制代码


  • 查询如下网址,查察应用 Readiness Check 的状态
  1. http://localhost:8080/health/readiness
  2. (在 SOFABoot 3.x 中调整了 endpoint 路径,health/readiness 更改为 actuator/readiness)
复制代码
点击直接查询(3.x 前)
点击直接查询(3.x 后)
  1. {
  2.     "status": "UP",
  3.     "sofaBootComponentHealthCheckInfo": {
  4.         "status": "UP"
  5.     },
  6.     "springContextHealthCheckInfo": {
  7.         "status": "UP"
  8.     },
  9.     "DiskSpaceHealthIndicator": {
  10.         "status": "UP",
  11.         "total": 53685972992,
  12.         "free": 47562289152,
  13.         "threshold": 10485760
  14.     }
  15. }
复制代码
status: “UP” ,表示应用 Readiness Check 康健的。


  • 利用如下查询,查察应用的运行时康健状态
  1. http://localhost:8080/health
  2. (在 SOFABOOT 3.X 中调整了 endpoint 路径,/health 更改为 /actuator/health)
复制代码
点击直接查询(3.x 前)
点击直接查询(3.x 后)
2.3 其他部门

日志

日志文件布局如下
  1. ./logs
  2. ├── health-check
  3. │   ├── sofaboot-common-default.log
  4. │   └── sofaboot-common-error.log
  5. ├── infra
  6. │   ├── common-default.log
  7. │   └── common-error.log
  8. └── spring.log
复制代码
测试

在倒霉用SOFABoot的类隔离本事时,可以倒霉用SOFABoot的测试包。即,原来的测试包。
但如果要利用类隔离本事的时间,我们必须利用如下引用。(类隔离本事见后文)
  1. <dependency>
  2.     <groupId>com.alipay.sofa</groupId>
  3.     <artifactId>test-sofa-boot-starter</artifactId>
  4. </dependency>
复制代码
对应的单元测试注解为:
  1. @SofaBootRunner
  2. @SofaJUnit4Runner
复制代码
可参考Spring官网-Test scope dependencies
利用如下
  1.                 <dependency>
  2.             <groupId>com.alipay.sofa</groupId>
  3.             <artifactId>test-sofa-boot-starter</artifactId>
  4.             <scope>test</scope>
  5.         </dependency>
  6.         <dependency>
  7.             <groupId>org.springframework.boot</groupId>
  8.             <artifactId>spring-boot-starter-test</artifactId>
  9.             <scope>test</scope>
  10.         </dependency>
复制代码
  1. package com.test;
  2. import org.junit.Test;
  3. import org.junit.runner.RunWith;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. import org.springframework.boot.test.context.SpringBootTest;
  7. import org.springframework.test.context.junit4.SpringRunner;
  8. @RunWith(SpringRunner.class) //SpringJUnit4ClassRunner.class 也可以
  9. @SpringBootTest(classes = MainApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
  10. public class test {
  11.     private static Logger log = LoggerFactory.getLogger(test.class);
  12.     @Test
  13.     public void UnitTest(){
  14.         int a = 33;
  15.         log.info("你好{}",a);
  16.     }
  17. }
复制代码

Spring:SpringRunner 和 SpringJUnit4ClassRunner
异步启动

SOFABoot 在 v2.6.0 开始提供异步初始化 Spring Bean 本事,引入如下 Starter 即可:
  1. <dependency>
  2.     <groupId>com.alipay.sofa</groupId>
  3.     <artifactId>runtime-sofa-boot-starter</artifactId>
  4. </dependency>
复制代码
三、SOFABoot的模块化开辟

官网的文档中,项目分为如下4个模块。官方样例下载地点-sofa-boot-guides
  1. .
  2. ├── service-facade
  3. ├── service-provider
  4. ├── service-consumer
  5. └── sofa-boot-run
复制代码


  • service-facade: 演示 JVM 服务发布与引用的 API 包
  • service-provider: 演示 XML 方式、Annotation 方式、API 方式发布 JVM 服务
  • service-consumer: 演示 XML 方式、Annotation 方式、API 方式引用 JVM 服务
  • sofa-boot-run: 启动包罗 SOFABoot 模块的 SOFA Boot 应用
3.1 基于Spring上下文的隔离

常见的模块化情势:


  • 基于代码构造上的模块化:

    • 在开辟期,将差别功能的代码放在差别 Java 工程下,在编译期被打进差别 jar 包
    • 在运行期,全部 Java 类都在一个 classpath 下,没做任何隔离

  • 基于 Spring 上下文隔离的模块化:

    • 借用 Spring 上下文来做差别功能模块的隔离,在开辟期和编译期,代码和配置也会分在差别 Java 工程中,但在运行期,差别模块间的 Spring Bean 相互不可见
    • DI(Dependency Injection,依靠注入)只在同一个上下文内部发生,但是全部的 Java 类还是在同一个 ClassLoader 下;

  • 基于 ClassLoader 隔离的模块化:

    • 借用 ClassLoader 来做隔离,每个模块都有独立的 ClassLoader,模块与模块之间的 classpath 差别

SOFABoot 模块化开辟属于模块化情势 —— 基于 Spring 上下文隔离的模块化。每个 SOFABoot 模块利用独立的 Spring 上下文,克制差别 SOFABoot 模块间的 BeanId 辩论,有用低落企业级多模块开辟时团队间的沟通资源。

取自 蚂蚁金服的业务体系模块化之模块化隔离方案
我们经常见到一个体系中的模块会按照如下的方式举行分层,如下图中的左边部门所示,一个体系就简单地分为 Web 层、Service 层、DAL 层。
当这个体系承载的业务变多了之后,体系大概演化成上图中右边的这种方式。在上图的右边的部门中,一个体系承载了两个业务,一个是 Cashier(收银台),另一个是 Pay(支付),这两个业务大概会有一些依靠的关系,Cashier 需要调用 Pay 提供的本事去做支付。

但是在这种模块化的方案内里,Spring 的上下文依然是同一个,类也没有任何做隔离,这就意味着,Pay Service 这个模块内里的任何的一个 Bean,都可以被 Cashier Service 这个模块所依靠。极端的环境下,大概会出现下面这种环境:


  • Cashier Service 错误地调用了 Pay Service 中的一个内部的 Bean,造成了两个模块之间的紧耦合。

SOFA的模块化如下

可以看到每一个小模块单独有一个Spring上下文.通过这种方式将他们逼迫分开,而不是同之前一样,由开辟者自发服从。

以 SOFABoot 模块为单元的模块化方式为开辟者提供了以下功能:


  • 运行时,每个 SOFABoot 模块的 Spring 上下文是隔离的,模块间界说的 Bean 不会相互影响
  • 每个 SOFABoot 模块是功能完备且自包罗的,可以很轻易在差别的 SOFABoot 应用中举行模块迁移和复用,只需将 SOFABoot 模块整个拷贝已往,调解 Maven 依靠,即可运行。
3.2 Root Application Context

SOFABoot 应用运行时,自己会产生一个 Spring Context,我们把它叫做 Root Application Context,它是每个 SOFABoot 模块创建的 Spring Context 的 Parent。
这样计划的目的是为了保证每个 SOFABoot 模块的 Spring Context 都能发现 Root Application Context 中创建的 Bean,这样当应用新增 Starter 时,不光 Root Application Context 能够利用 Starter 中新增的 Bean,每个 SOFABoot 模块的 Spring Context 也能利用这些 Bean。
也就是一些全局的Bean。
3.3 模块并行化启动

每个 SOFABoot 模块都是独立的 Spring 上下文,多个 SOFABoot 模块支持并行化启动,与 Spring Boot 的单 Spring 上下文模式相比,模块并行化启动能够加速应用的启动速率。
3.4 JVM服务与RPC服务的发布与引用

上下文隔离后,模块与模块间的 Bean 无法直接注入,模块间需要通过 SOFA 服务举行通讯,目前SOFABoot 提供了两种情势的服务发布和引用,用于解决差别级别的模块间调用的问题:


  • JVM 服务发布和引用:解决一个 SOFABoot 应用内部各个 SOFABoot 模块之间的调用问题, JVM 服务发布与引用
  • RPC 服务发布和引用:解决多个 SOFABoot 应用之间的远程调用问题,RPC 服务发布与引用。
3.5 模块配置

                                         S                            O                            F                            A                            B                            o                            o                            t                            模                            块                            =                            普                            通                            的                            J                            a                            r                            包                            +                            S                            O                            F                            A                            B                            o                            o                            t                            特                            有                            的                            配                            置                                  SOFABoot 模块 = 平凡的 Jar 包 + SOFABoot 特有的配置                     SOFABoot模块=平凡的Jar包+SOFABoot特有的配置
共有4种配置文件
  1. Module-Name = com.alipay.test.biz.service.impl
  2. Spring-Parent = com.alipay.test.common.dal
  3. Require-Module = com.alipay.test.biz.shared
  4. Module-Profile = dev
复制代码
Module-Name

Module-Name 是 SOFABoot 模块的名称,也是 SOFABoot 模块的唯一标示符。
在一个 SOFABoot 应用中,一个SOFABoot 模块的 Module-Name 必须和其他的 SOFABoot 模块的 Module-Name 不一样。
需要注意的一点是,一个 SOFABoot 应用运行时的 SOFABoot 模块,不光仅只包罗本应用的模块,还包括依靠了其他应用的 SOFABoot 模块,确定是否唯一的时间需要把这些 SOFABoot 模块也考虑进去。
  1. 其实就是配置的服务的实现,通过com.alipay.test.biz.service.impl,就很容易发现,其实就是xxxServiceImpl
  2. 因为在SOFABoot中,它们被分在了不同的Spring上下文。通过配置文件指定接口的实现。
复制代码
Require-Module

Require-Module 用于界说模块之间的依靠顺序,值是以逗号分隔的 SOFABoot 模块名列表,比如上面的配置中,就表示本模块依靠于 com.alipay.test.biz.shared 模块。
对于这种依靠关系的处理,SOFABoot 会将 com.alipay.test.biz.shared 模块在本模块之前启动,即com.alipay.test.biz.shared 模块将先启动 Spring 上下文。

一样平常环境下,是不需要为模块界说 Require-Module 的,只有当模块的 Spring 上下文的启动依靠于另一个模块的 Spring 上下文的启动时,才需要界说 Require-Module。
举一个例子,如果你在 A 模块中发布了一个 SOFA JVM Service。在 B 模块的某一个 Bean 的 init 方法内里,需要利用 SOFA Reference 调用这个 JVM Service。假设 B 模块在 A 模块之前启动了,那么 B 模块的 Bean 就会由于 A 模块的 JVM Service 没有发布而 init 失败,导致 Spring 上下文启动失败。这个时间,我们就可以利用 Require-Module 来逼迫 A 模块在 B 模块之前启动。
Spring-Parent

在 SOFABoot 应用中,每一个 SOFABoot 模块都是一个独立的 Spring 上下文,而且这些 Spring 上下文之间是相互隔离的。
固然这样的模块化方式可以带来诸多好处,但是,在某些场景下还是会有一些未便,这个时间,你可以通过 Spring-Parent 来打通两个 SOFABoot 模块的 Spring 上下文。
Spring-Parent 属性可以配置一个模块的名称,比如上面的配置中,就将 com.alipay.test.common.dal 的 Spring 上下文设置为当前模块的 Spring 上下文的父 Spring 上下文。
由于 Spring 的限定,一个模块的 Spring-Parent 只能有一个模块。关于 Spring 的父上下文的作用可以看 Spring 的 BeanFactory 的阐明:Interface BeanFactory
Module-Profile

支持 SOFABoot Profile 本事,详情参考- SOFABoot Profile
也就是配置运行环境(dev、test、product),当处于对应的环境时,模块就会启动,大概说激活。
3.6 JVM 服务发布与引用实例

有三种方式,可参考 JVM 服务发布与引用


  • XML 方式
  • Annotation 方式
  • 编程 API 方式
对于同一个服务,不能混用XML与Annotation方式
我们可以依据分类创建如下布局,下图是官方样例中的内容。



  • boot:启动模块,含有MainApplication与控制层
  • facade:Service接口地点
  • provide:Service方法实现
  • consumer:引用Service,让其可被作为Bean利用
我们另建一个项目。

在上图pom.xml文件中参加如下代码
  1.         <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5.     <modelVersion>4.0.0</modelVersion>
  6.     <groupId>org.example</groupId>
  7.     <artifactId>sofaBootTest</artifactId>
  8.     <packaging>pom</packaging>
  9.     <version>1.0-SNAPSHOT</version>
  10.     <modules>
  11.         <module>service-consumer</module>
  12.         <module>service-provider</module>
  13.         <module>sofa-boot-run</module>
  14.         <module>service-facade</module>
  15.     </modules>
  16.     <properties>
  17.         <maven.compiler.source>8</maven.compiler.source>
  18.         <maven.compiler.target>8</maven.compiler.target>
  19.         <maven.surefire.plugin>2.21.0</maven.surefire.plugin>
  20.     </properties>
  21.     <parent>
  22.         <groupId>com.alipay.sofa</groupId>
  23.         <artifactId>sofaboot-dependencies</artifactId>
  24.         <version>3.2.0</version>
  25.     </parent>
  26.     <dependencyManagement>
  27.         <dependencies>
  28.             <dependency>
  29.                 <groupId>com.alipay.sofa</groupId>
  30.                 <artifactId>isle-sofa-boot-starter</artifactId>
  31.                 <version>3.2.0</version>
  32.             </dependency>
  33.             <dependency>
  34.                 <groupId>org.example</groupId>
  35.                 <artifactId>service-provider</artifactId>
  36.                 <version>${project.version}</version>
  37.             </dependency>
  38.             <dependency>
  39.                 <groupId>org.example</groupId>
  40.                 <artifactId>service-facade</artifactId>
  41.                 <version>${project.version}</version>
  42.             </dependency>
  43.             <dependency>
  44.                 <groupId>org.example</groupId>
  45.                 <artifactId>service-consumer</artifactId>
  46.                 <version>${project.version}</version>
  47.             </dependency>
  48.             <dependency>
  49.                 <groupId>org.example</groupId>
  50.                 <artifactId>sofa-boot-run</artifactId>
  51.                 <version>${project.version}</version>
  52.             </dependency>
  53.         </dependencies>
  54.     </dependencyManagement>
  55.     <build>
  56.         <plugins>
  57.             <plugin>
  58.                 <groupId>org.springframework.boot</groupId>
  59.                 <artifactId>spring-boot-maven-plugin</artifactId>
  60.             </plugin>
  61.         </plugins>
  62.     </build>
  63.     <profiles>
  64.         <profile>
  65.             <id>jdk8</id>
  66.             <activation>
  67.                 <jdk>1.8</jdk>
  68.             </activation>
  69.             <build>
  70.                 <plugins>
  71.                     <plugin>
  72.                         <groupId>org.apache.maven.plugins</groupId>
  73.                         <artifactId>maven-surefire-plugin</artifactId>
  74.                         <version>${maven.surefire.plugin}</version>
  75.                         <dependencies>
  76.                             <dependency>
  77.                                 <groupId>org.ow2.asm</groupId>
  78.                                 <artifactId>asm</artifactId>
  79.                                 <version>6.2</version>
  80.                             </dependency>
  81.                         </dependencies>
  82.                         <configuration>
  83.                             <reuseForks>false</reuseForks>
  84.                         </configuration>
  85.                     </plugin>
  86.                 </plugins>
  87.             </build>
  88.         </profile>
  89.         <profile>
  90.             <id>jdk11</id>
  91.             <activation>
  92.                 <jdk>11</jdk>
  93.             </activation>
  94.             <build>
  95.                 <plugins>
  96.                     <plugin>
  97.                         <groupId>org.apache.maven.plugins</groupId>
  98.                         <artifactId>maven-surefire-plugin</artifactId>
  99.                         <version>${maven.surefire.plugin}</version>
  100.                         <dependencies>
  101.                             <dependency>
  102.                                 <groupId>org.ow2.asm</groupId>
  103.                                 <artifactId>asm</artifactId>
  104.                                 <version>6.2</version>
  105.                             </dependency>
  106.                         </dependencies>
  107.                         <configuration>
  108.                             <reuseForks>false</reuseForks>
  109.                             <argLine>-Djdk.attach.allowAttachSelf --add-opens
  110.                                 java.base/jdk.internal.loader=ALL-UNNAMED
  111.                             </argLine>
  112.                         </configuration>
  113.                     </plugin>
  114.                 </plugins>
  115.             </build>
  116.         </profile>
  117.     </profiles>
  118. </project>
复制代码
3.6.1 facade模块声明接口

于facade模块中创建一个服务
  1. package com.facadeTest;
  2. public interface SampleService {
  3.     String message();
  4. }
复制代码
3.6.2 provide中实现接口与根本配置

创建 sofa-module.properties 文件如图

并在文件中,添加如下配置,为我们模块的命名
  1. Module-Name=com.service-provider
复制代码
3.6.3 consumer根本配置

同理伪consumer创建一个sofa-module.properties 文件。
  1. Module-Name=com.service-consumer
  2. Require-Module=com.service-provider
复制代码
在 sofa-module.properties 文件中需要指定 Require-Module,保证 service-provider 模块在 service-consumer 模块之前革新。
3.6.4 BootRun根本配置

我们在BootRun中创建好SpringBoot的根本架构

导入如下文件即可:
  1.          <dependencies>
  2.         <dependency>
  3.             <groupId>org.springframework.boot</groupId>
  4.             <artifactId>spring-boot-starter-web</artifactId>
  5.         </dependency>
  6.         <dependency>
  7.             <groupId>com.alipay.sofa</groupId>
  8.             <artifactId>healthcheck-sofa-boot-starter</artifactId>
  9.         </dependency>
  10.         <dependency>
  11.             <groupId>com.alipay.sofa</groupId>
  12.             <artifactId>test-sofa-boot-starter</artifactId>
  13.             <scope>test</scope>
  14.         </dependency>
  15.         <dependency>
  16.             <groupId>org.springframework.boot</groupId>
  17.             <artifactId>spring-boot-starter-test</artifactId>
  18.             <scope>test</scope>
  19.         </dependency>
  20.         <dependency>
  21.             <groupId>com.alipay.sofa</groupId>
  22.             <artifactId>isle-sofa-boot-starter</artifactId>
  23.         </dependency>
  24.         <dependency>
  25.             <groupId>org.example</groupId>
  26.             <artifactId>service-provider</artifactId>
  27.         </dependency>
  28.         <dependency>
  29.             <groupId>org.example</groupId>
  30.             <artifactId>service-facade</artifactId>
  31.         </dependency>
  32.         <dependency>
  33.             <groupId>org.example</groupId>
  34.             <artifactId>service-consumer</artifactId>
  35.         </dependency>
  36.     </dependencies>
  37.         <build>
  38.         <plugins>
  39.             <!-- http://docs.spring.io/spring-boot/docs/current/maven-plugin/usage.html -->
  40.             <plugin>
  41.                 <groupId>org.springframework.boot</groupId>
  42.                 <!-- http://docs.spring.io/spring-boot/docs/current/maven-plugin/usage.html -->
  43.                 <artifactId>spring-boot-maven-plugin</artifactId>
  44.                 <version>1.4.2.RELEASE</version>
  45.                 <configuration>
  46.                     <!-- executable fat jar -->
  47.                     <outputDirectory>../target/boot</outputDirectory>
  48.                     <classifier>executable</classifier>
  49.                 </configuration>
  50.                 <executions>
  51.                     <execution>
  52.                         <goals>
  53.                             <goal>repackage</goal>
  54.                         </goals>
  55.                     </execution>
  56.                 </executions>
  57.             </plugin>
  58.         </plugins>
  59.     </build>
复制代码
别的注意肯定要yml大概properties文件配置应用名和日志路径,否则会报错。(可见第二部门)
  1. package com.sofaBootRun;
  2. import com.alipay.sofa.runtime.api.annotation.SofaReference;
  3. import com.facadeTest.SampleService;
  4. import org.junit.Assert;
  5. import org.junit.Test;
  6. import org.junit.runner.RunWith;
  7. import org.springframework.boot.test.context.SpringBootTest;
  8. import org.springframework.test.context.junit4.SpringRunner;
  9. @RunWith(SpringRunner.class)
  10. @SpringBootTest(classes = MainApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
  11. public class test {
  12.     @SofaReference
  13. (uniqueId = "sampleService")
  14.     private SampleService sampleJvmService;
  15.    @SofaReference
  16. (uniqueId = "annotationImpl")
  17.     private SampleService sampleJvmServiceAnnotationImpl;
  18.    @SofaReference
  19. (uniqueId = "serviceClientImpl")
  20.     private SampleService sampleJvmServiceClientImpl;
  21.     @Test
  22.     public void test() {
  23.         Assert.assertEquals("Hello, jvm service xml implementation.", sampleJvmService.message());
  24.         Assert.assertEquals("Hello, jvm service annotation implementation.",
  25.                 sampleJvmServiceAnnotationImpl.message());
  26.         Assert.assertEquals("Hello, jvm service service client implementation.",
  27.                 sampleJvmServiceClientImpl.message());
  28.     }
  29. }
复制代码
3.6.5 XML方式发布与引用

我们起首于provide模块中实现该接口
  1. package com.providerTest;
  2. import com.facadeTest.SampleService;
  3. public class SampleServiceImpl implements SampleService {
  4.     private String message;
  5.     public String getMessage() {
  6.         return message;
  7.     }
  8.     public void setMessage(String message) {
  9.         this.message = message;
  10.     }
  11.     @Override
  12.     public String message() {
  13.         System.out.println(message);
  14.         return message;
  15.     }
  16. }
复制代码
在provider服务内里,同Spring,创建如下格式文档 service-provider.xml

并参加如下内容,将对象放入Bean中
  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.        xmlns:sofa="http://sofastack.io/schema/sofaboot"
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  5.             http://sofastack.io/schema/sofaboot http://sofastack.io/schema/sofaboot.xsd"
  6.        default-autowire="byName">
  7.     <bean id="sampleService" class="com.providerTest.SampleServiceImpl">
  8.         <property name="message" value="Hello, jvm service xml implementation."/>
  9.     </bean>
  10.     <sofa:service ref="sampleService" interface="com.facadeTest.SampleService">
  11.         <sofa:binding.jvm/>
  12.     </sofa:service>
  13. </beans>
复制代码
与Spring相同,注意ID和ref内里的同等性。
在consumer内里,同前输入如下内容,然后通过 SOFA 提供的 Spring 扩展标签来将上面的 Bean 发布成一个 SOFA JVM 服务
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.        xmlns:sofa="http://sofastack.io/schema/sofaboot"
  5.        xmlns:context="http://www.springframework.org/schema/context"
  6.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  7.             http://sofastack.io/schema/sofaboot http://sofastack.io/schema/sofaboot.xsd"
  8.        default-autowire="byName">
  9.     <sofa:reference id="sampleServiceRef" interface="com.facadeTest.SampleService">
  10.         <sofa:binding.jvm/>
  11.     </sofa:reference>
  12. </beans>
复制代码
如果有多个实现,需要配置unique-id,比如如下方式在SOFABoot层面将两个Bean关联:
  1. <sofa:service interface="com.alipay.sofa.runtime.test.service.SampleService" ref="sampleService" unique-id="ss2">
  2.        <sofa:binding.jvm/>
  3. </sofa:service>
  4. <sofa:reference interface="com.alipay.sofa.runtime.test.service.SampleService" id="sampleService" unique-id="ss2">
  5. </sofa:reference>
复制代码
3.6.6 注解方式发布与引用

3.6.6.1 通过XML管理

于provider处增长一实现
  1. package com.providerTest;
  2. import com.alipay.sofa.runtime.api.annotation.SofaService;
  3. import com.facadeTest.SampleService;
  4. @SofaService(uniqueId = "annotationImpl")
  5. public class SampleJvmServiceAnnotationImpl implements SampleService {
  6.     @Override
  7.     public String message() {
  8.         String message = "Hello, jvm service annotation implementation.";
  9.         System.out.println(message);
  10.         return message;
  11.     }
  12. }
复制代码
将 SampleJvmServiceAnnotationImpl 配置成一个 Spring Bean:
  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.        xmlns:sofa="http://sofastack.io/schema/sofaboot"
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  5.             http://sofastack.io/schema/sofaboot http://sofastack.io/schema/sofaboot.xsd"
  6.        default-autowire="byName">
  7.     <bean id="sampleJvmServiceAnnotation" class="com.providerTest.SampleJvmServiceAnnotationImpl"/>
  8. </beans>
复制代码
不在需要于Consumer中配置,利用时只需要
  1. @SofaReference
复制代码
3.6.6.2 @Bean方式

自 SOFABoot v2.6.0 及 v3.1.0 版本起可利用
通过如下方式,我们可以直接注入Bean中
  1. 之前的XML配置也可以使用该方式`
复制代码
需要注意,差别包中,其SpringBoot注解不会生效
可以用一下方式解决


在Main地点项目将其导入
可以利用@Import注解
  1. @SpringBootApplication
  2. @Import(com.providerTest.SampleSofaServiceConfiguration.class)
  3. public class MainApplication {
  4.     public static void main(String[] args){
  5.         SpringApplication.run(MainApplication.class,args);
  6.     }
  7. }
复制代码


在Main部门地点项目将其扫入
  1. @SpringBootApplication(scanBasePackageClasses = com.providerTest.SampleSofaServiceConfiguration.class)
  2. public class MainApplication {
  3.     public static void main(String[] args){
  4.         SpringApplication.run(MainApplication.class,args);
  5.     }
  6. }
复制代码

provider部门
  1. package com.providerTest;
  2. import com.alipay.sofa.runtime.api.annotation.SofaService;
  3. import com.facadeTest.SampleService;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. @Configuration
  7. public class SampleSofaServiceConfiguration {
  8.     @Bean("sampleSofaService")
  9.     @SofaService(uniqueId = "annotationImpl")
  10.     SampleService service() {
  11.         return new SampleService() {
  12.             @Override
  13.             public String message() {
  14.                 return "Hello, jvm service annotation implementation.";
  15.             }
  16.         };
  17.     }
  18. }
复制代码
3.6.6.3 多接口

通过如下方式,指定抽象接口
  1. @SofaService(interfaceType=SampleInterface.class)
  2. public class SampleImpl implements SampleInterface, Serializable {
  3.    public void test() {
  4.    }
  5. }
复制代码
3.6.7 代码方式发布与引用

consumer和provider服务需要参加如下包:
  1.                 <dependency>
  2.             <groupId>com.alipay.sofa</groupId>
  3.             <artifactId>runtime-sofa-boot-starter</artifactId>
  4.         </dependency>
复制代码
provider
  1. package com.providerTest;
  2. import com.alipay.sofa.runtime.api.aware.ClientFactoryAware;
  3. import com.alipay.sofa.runtime.api.client.ClientFactory;
  4. import com.alipay.sofa.runtime.api.client.ServiceClient;
  5. import com.alipay.sofa.runtime.api.client.param.ServiceParam;
  6. import com.facadeTest.SampleService;
  7. public class PublishServiceWithClient implements ClientFactoryAware {
  8.     private ClientFactory clientFactory;
  9.     public void init() {
  10.         ServiceClient serviceClient = clientFactory.getClient(ServiceClient.class);
  11.         ServiceParam serviceParam = new ServiceParam();
  12.         serviceParam.setInstance(new SampleServiceImpl(
  13.             "Hello, jvm service service client implementation."));
  14.         serviceParam.setInterfaceType(SampleService.class);
  15.         serviceParam.setUniqueId("serviceClientImpl");
  16.         serviceClient.service(serviceParam);
  17.     }
  18.     @Override
  19.     public void setClientFactory(ClientFactory clientFactory) {
  20.         this.clientFactory = clientFactory;
  21.     }
  22. }
复制代码
放入Spring中
  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.        xmlns:sofa="http://sofastack.io/schema/sofaboot"
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  5.             http://sofastack.io/schema/sofaboot http://sofastack.io/schema/sofaboot.xsd"
  6.        default-autowire="byName">
  7.     <bean id="publishServiceWithClient" class="com.providerTest.PublishServiceWithClient" init-method="init"/>
  8. </beans>
复制代码
consumer
  1. package com.consumerTest;
  2. import com.alipay.sofa.runtime.api.aware.ClientFactoryAware;
  3. import com.alipay.sofa.runtime.api.client.ClientFactory;
  4. import com.alipay.sofa.runtime.api.client.ReferenceClient;
  5. import com.alipay.sofa.runtime.api.client.param.ReferenceParam;
  6. import com.facadeTest.SampleService;
  7. public class JvmServiceConsumer implements ClientFactoryAware {
  8.     private ClientFactory    clientFactory;
  9.     public void init() {
  10.         ReferenceClient referenceClient = clientFactory.getClient(ReferenceClient.class);
  11.         ReferenceParam<SampleService> referenceParam = new ReferenceParam<SampleService>();
  12.         referenceParam.setInterfaceType(SampleService.class);
  13.         referenceParam.setUniqueId("serviceClientImpl");
  14.         SampleService sampleJvmServiceClientImpl = referenceClient.reference(referenceParam);
  15.         sampleJvmServiceClientImpl.message();
  16.     }
  17.     public void setClientFactory(ClientFactory clientFactory) {
  18.         this.clientFactory = clientFactory;
  19.     }
  20. }
复制代码
放入Spring中
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.        xmlns:sofa="http://sofastack.io/schema/sofaboot"
  5.        xmlns:context="http://www.springframework.org/schema/context"
  6.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  7.             http://sofastack.io/schema/sofaboot http://sofastack.io/schema/sofaboot.xsd"
  8.        default-autowire="byName">
  9.     <bean id="consumer" class="com.consumerTest.JvmServiceConsumer" init-method="init" />
  10. </beans>
复制代码
参考文档

SOFABoot
SOFABoot官方文档
蚂蚁金服的业务体系模块化之模块化隔离方案

来源:https://blog.csdn.net/weixin_46949627/article/details/132272697
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则