question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SOFABoot quick start documentation issue

See original GitHub issue

I walked through the SOFABoot quick start guides and have some suggestions.

1. It’s not always easy to change the parent pom, so it’s better to document an alternative way, e.g. import the sofaboot-dependencies in dependencyManagement

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>com.alipay.sofa</groupId>
        <artifactId>sofaboot-dependencies</artifactId>
        <version>${sofa.boot.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

2. XML configuration is not widely used now, we’d better update the quick start documents using annotation examples

3. The annotation should be more user friendly

Take the example below:

  1. after the @SofaService is annotated, the @Component should not be necessary
  2. The interfaceType is not mandatory, our sample could remove it
  3. It’s not good to let users specify binding type as string, it’s better to provide some enums for bindings.
@SofaService(interfaceType = HelloSyncService.class, bindings = {@SofaServiceBinding(bindingType = "bolt")})
@Component
public class HelloSyncServiceImpl implements HelloSyncService {
  ...
}

4. Registry instructions should be added in the quick start doc

Currently there is no such instruction, so the user could not run the demo following the quick start doc from scratch.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:17 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
Darian1996commented, Dec 21, 2021

一开始我想认领这个 @SofaService 的任务。

我的想法是,

  • 可以参考 Dubbo 的 ServiceAnnotationPostProcessor 如何做的

    1. 实现:BeanDefinitionRegistryPostProcessor 接口,实现 postProcessBeanDefinitionRegistry方法 在这个方法内部来做扫描,注册到 Spring 容器中 的 Bean
    2. 通过,实现 ClassPathBeanDefinitionScanner,ClassPathBeanDefinitionScanner#scan(String...) 方法可以扫描出来 Bean, 然后注册到容器中
    3. 后边才走到 构造动态 SDK 等。

扫描要实现,对对应的包进行扫描。有两种实现方式。

  1. 如何拿到 扫描 @SofaService 的 scanerPackageList 可以参考 https://github.com/Nepxion/Discovery/blob/6.x.x/discovery-plugin-strategy/discovery-plugin-strategy-starter/src/main/java/com/nepxion/discovery/plugin/strategy/extractor/StrategyPackagesExtractor.java 内部使用 AutoConfigurationPackages#get(BeanFactory) 拿到了 scan 的包的列表

    • 但是很遗憾。。 org.springframework.boot.autoconfigure.AutoConfigurationPackages 类在, Maven: org.springframework.boot:spring-boot-autoconfigure:2.3.9.RELEASE , 对应模块 并没有依赖,导致没有办法用
    • 所以这点难点,让我不知道该怎么办了。
  2. dubbo 的 xml 配置里边有 dubbo:basePackages 扫描包,annotation 有 @EnableDubbo#scanBasePackages ,所以如果要这个东西如何配置进去,还是一个问题 …

其实我比较倾向于 用 AutoConfigurationPackages#get(BeanFactory) 返回 List<String> 来做统一的抽象,这样子,更加统一一点,毕竟要做开源统一

可以讨论一下是否可以添加对应的依赖,然后,这个方案是否可以尝试着做一下的必要。😄 @EvenLjj E @seeflood

至于说用组合注解的方式的话,和 Spring 绑定的太深了。

1reaction
Darian1996commented, Dec 24, 2021

@Darian1996 你的方案应该就是倾向于 @Kunple-w 上面所讲到的方案二,扫描注解已经在com.alipay.sofa.runtime.spring.ServiceBeanFactoryPostProcessor 中做了,只需要将其注册到 Spring 容器中应该就是可以了。这个需求可能还要关注的一点,不影响原来的@SofaService的使用,保持兼容性可能会成为影响这个方案设计的一个重要因素。

@EvenLjj

扫描注解没有是前提

@Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        Arrays.stream(beanFactory.getBeanDefinitionNames())
            .collect(Collectors.toMap(Function.identity(), beanFactory::getBeanDefinition))
            .forEach((key, value) -> transformSofaBeanDefinition(key, value, beanFactory));
    }

这里是通过 @Service 注册到 Spring 容器中,然后 每一个 Bean 都去进行看一下是否发布 Sofa服务

 private void transformSofaBeanDefinition(String beanId, BeanDefinition beanDefinition,
                                             ConfigurableListableBeanFactory beanFactory) {
        if (BeanDefinitionUtil.isFromConfigurationSource(beanDefinition)) {
            generateSofaServiceDefinitionOnMethod(beanId, (AnnotatedBeanDefinition) beanDefinition,
                beanFactory);
        } else {
            Class<?> beanClassType = BeanDefinitionUtil.resolveBeanClassType(beanDefinition);
            if (beanClassType == null) {
                SofaLogger.warn("Bean class type cant be resolved from bean of {}", beanId);
                return;
            }
            generateSofaServiceDefinitionOnClass(beanId, beanClassType, beanDefinition, beanFactory);
        }
    }

所以,是没有缺失了先扫描 @SofaService 的过程 ~~~

BeanFactoryPostProcessor#postProcessBeanFactory 的这个,是注册到容器才会触发的。所以,扫描注解没有 … …

旧方法

以前的方法 @Service 注册到 容器 , 从容器中拿出来,遍历,发布 SOFA 服务。

新方式

扫描 @SofaService 注册容器, <font color=red>这一步才是难点。</font>

直到这个,才能到我的提出的需要扫描 包的方案,继续往下讨论。

所以,前提是去掉 @Service 的自动注册到 Spring 容器,增加 @SofaService 的自动注册到 Spring 容器

Read more comments on GitHub >

github_iconTop Results From Across the Web

Quick start guide for SOFABoot project - SOFAStack
Create a new Spring Boot application (In case of SOFABoot project, import to SOFABoot as described in SOFABoot Documentation - Dependency Management.
Read more >
SOFABoot: Open Source Java Development Framework Based on ...
To address the above issues while maintaining the advantages of Spring Boot, ... Please refer to SOFAStack Documentation for SOFABoot quick start guide....
Read more >
Spring Boot Java quick start :: Documentation - OptaPlanner
A student dislikes sequential lessons on the same subject. Mathematically speaking, school timetabling is an NP-hard problem. This means it is difficult to ......
Read more >
README.md · SOFAStack/sofa-boot - Gitee.com
Please refer to SOFAStack Documentation for SOFABoot quick start guide. Demos. Some SOFABoot demo projects to get your hands dirty: Class Isolation ·...
Read more >
Apache ShenYu document
As showing picture below,shenyu-admin will issue a configuration change ... This article introduces how to quick start the Apache ShenYu ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found