Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client spring integration #543

Merged
merged 2 commits into from Feb 15, 2017

Conversation

Projects
None yet
5 participants
@nobodyiam
Copy link
Collaborator

commented Feb 15, 2017

Apollo Client Integration With Spring.

Features

The following features are now provided:

1. Spring Property Placeholder Support

1.1 XML Placeholder

    <bean class="com.ctrip.framework.apollo.spring.TestXmlBean">
        <property name="timeout" value="${timeout:100}"/>
        <property name="batch" value="${batch:200}"/>
    </bean>
  public class TestXmlBean {
    private int timeout;
    private int batch;

    public void setTimeout(int timeout) {
      this.timeout = timeout;
    }

    public int getTimeout() {
      return timeout;
    }

    public int getBatch() {
      return batch;
    }

    public void setBatch(int batch) {
      this.batch = batch;
    }
  }

1.2 @value Placeholder

  public class TestJavaConfigBean {
    @Value("${timeout:100}")
    private int timeout;
    private int batch;

    @Value("${batch:200}")
    public void setBatch(int batch) {
      this.batch = batch;
    }

    public int getTimeout() {
      return timeout;
    }

    public int getBatch() {
      return batch;
    }
  }

2. Spring Annotation Support

  public class TestApolloAnnotationBean {
    @ApolloConfig
    private Config config; //inject config for namespace application
    @ApolloConfig("application")
    private Config anotherConfig; //inject config for namespace application
    @ApolloConfig("FX.apollo")
    private Config yetAnotherConfig; //inject config for namespace FX.apollo

    //config change listener for namespace application
    @ApolloConfigChangeListener
    private void someOnChange(ConfigChangeEvent changeEvent) {
      //do something
    }

    //config change listener for namespace application
    @ApolloConfigChangeListener("application")
    private void anotherOnChange(ConfigChangeEvent changeEvent) {
      //do something
    }

    //config change listener for namespaces application and FX.apollo
    @ApolloConfigChangeListener({"application", "FX.apollo"})
    private void yetAnotherOnChange(ConfigChangeEvent changeEvent) {
      //do something
    }
  }

Configuration

Apollo supports both the traditional XML based configuration and moden Java based configuration.

1. XML Based Configuration Example

<?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:apollo="http://www.ctrip.com/schema/apollo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.ctrip.com/schema/apollo http://www.ctrip.com/schema/apollo.xsd">
    <!-- Inject namespaces application and FX.apollo to Spring property sources  -->
    <apollo:config namespaces="application,FX.apollo"/>

    <bean class="com.ctrip.framework.apollo.spring.TestXmlBean">
        <property name="timeout" value="${timeout:100}"/>
        <property name="batch" value="${batch:200}"/>
    </bean>
    <bean class="com.ctrip.framework.apollo.spring.TestApolloAnnotationBean" />
</beans>

2. Java Based Configuration Example

  @Configuration
  @EnableApolloConfig({"application", "FX.apollo"})
  public class AppConfig {
    @Bean
    public TestJavaConfigBean javaConfigBean() {
      return new TestJavaConfigBean();
    }
    @Bean
    public TestApolloAnnotationBean apolloAnnotationBean() {
      return new TestApolloAnnotationBean();
    }
 }
@coveralls

This comment has been minimized.

Copy link

commented Feb 15, 2017

Coverage Status

Coverage increased (+0.5%) to 50.146% when pulling 61bb693 on nobodyiam:client-spring-integration into 87129ad on ctripcorp:master.

@codecov-io

This comment has been minimized.

Copy link

commented Feb 15, 2017

Codecov Report

Merging #543 into master will increase coverage by 0.51%.
The diff coverage is 94.33%.

@@             Coverage Diff              @@
##             master     #543      +/-   ##
============================================
+ Coverage     46.32%   46.83%   +0.51%     
- Complexity     1306     1340      +34     
============================================
  Files           312      319       +7     
  Lines          8469     8575     +106     
  Branches        814      824      +10     
============================================
+ Hits           3923     4016      +93     
- Misses         4263     4276      +13     
  Partials        283      283
Impacted Files Coverage Δ Complexity Δ
...o/spring/annotation/ApolloAnnotationProcessor.java 100% <100%> (ø) 11 <11> (?)
...ork/apollo/spring/config/ConfigPropertySource.java 100% <100%> (ø) 2 <2> (?)
...apollo/spring/config/PropertySourcesProcessor.java 100% <100%> (ø) 10 <10> (?)
.../spring/config/ConfigPropertySourcesProcessor.java 100% <100%> (ø) 2 <2> (?)
...pollo/spring/annotation/ApolloConfigRegistrar.java 100% <100%> (ø) 2 <2> (?)
...ework/apollo/spring/util/BeanRegistrationUtil.java 72.72% <72.72%> (ø) 3 <3> (?)
...amework/apollo/spring/config/NamespaceHandler.java 85% <85%> (ø) 3 <3> (?)
...mework/apollo/portal/component/PortalSettings.java 55.73% <ø> (-8.2%) 5% <ø> (ø)
...work/apollo/biz/message/DatabaseMessageSender.java 44.68% <ø> (-6.39%) 6% <ø> (ø)
... and 7 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 87129ad...61bb693. Read the comment docs.

@lepdou lepdou merged commit 2e125a6 into ctripcorp:master Feb 15, 2017

4 checks passed

codecov/patch 94.33% of diff hit (target 46.32%)
Details
codecov/project 46.83% (+0.51%) compared to 87129ad
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
coverage/coveralls Coverage increased (+0.5%) to 50.146%
Details

@nobodyiam nobodyiam deleted the nobodyiam:client-spring-integration branch Feb 16, 2017

@yobuke

This comment has been minimized.

Copy link

commented Mar 8, 2019

May I ask you a question?
I have a request:
When encapsulating the Apollo client SDK, it is necessary to determine whether the value corresponding to the key start with fields ‘>>>’ and end with fields '<<<' (encrypted markers) at getconfig time.
If there is an encryption token, the decrypted value is returned by calling the internal decryption SDK after load.
How to achieve this, which classes need to be rewritten.

可以问一个问题吗?
我有一个需求:
封装Apollo client SDK的时候需要在getconfig的时候判断key对应的value是否包含“>>>***<<<”字段(加密标记)
如果有加密标记,在load后通过调用内部解密SDK返回解密后的value。
这个如何实现,需要重写哪些class。

@yobuke

This comment has been minimized.

Copy link

commented Mar 8, 2019

defaultConfig.getProperty(someKey, new Function<String, String>() {
            @Override
            public String apply(String s) {
                DecryptRequest request = new DecryptRequest();
                request.setEncryptedText(s);
                return HttpEncryptionWrapper.decrypt(request);
            }
        }, "defaultValue");

I am ready implement by AbstractConfig.getProperty(String key, Function<String, T> function, T defaultValue);
but do not know how to implement with @value Placeholder/XML Placeholder/Spring Annotation.
that devs just import xxx-company-apollo-client.jar and nothing need to modify.

@nobodyiam

This comment has been minimized.

Copy link
Collaborator Author

commented Mar 8, 2019

Currently there is no hook to support this feature, I think apollo-client should provide such capability in the future.

@yobuke

This comment has been minimized.

Copy link

commented Mar 11, 2019

Currently there is no hook to support this feature, I think apollo-client should provide such capability in the future.

I hook it with overwrite some classes.

DefaultInjector -> XXXInjector
DefaultConfigManager -> XXXConfigManager
DefaultConfigFactoryManager -> XXXConfigFactoryManager
DefaultConfigRegistry -> XXXConfigRegistry
DefaultConfigFactory -> XXXConfigFactory
DefaultConfig -> XXXConfig

overwrite file:
path:{rootMenu}/META-INF/services/com.ctrip.framework.apollo.internals.Injector
content(namespace of XXXInjector):com.{xxxcompany}.{xxxproductname}.{xxxapolloclient}.{XXXInjector}

Is that the right way to overwrite Default behavior

@yobuke

This comment has been minimized.

Copy link

commented Mar 12, 2019

How to overwrite apollo default behavior and add decode function(define customer apollo client)?

  • Solution 1:
    step 1. overwrite class
{XXX}Injector implements Injector
{XXX}ConfigFactory implements ConfigFactory
{XXX}Config extends AbstractConfig implements RepositoryChangeListener

step 2. overwrite file:
path:{rootMenu}/META-INF/services/com.ctrip.framework.apollo.internals.Injector
content(namespace of XXXInjector):com.{xxxcompany}.{xxxproductname}.{xxxapolloclient}.{XXXInjector}

PS:app server nothing have to modified.

  • Solution 2:
    step 1. define class
{XXX}PropertySourcesPlaceholderConfigurer extends PropertySourcesPlaceholderConfigurer

step 2. define class

{XXX}ApolloConfigRegistrar implements ImportBeanDefinitionRegistrar

step 3. define customer annotation:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import({XXX}ApolloConfigRegistrar.class)
public @interface Enable{XXX}ApolloConfig {
    String[] value() default {ConfigConsts.NAMESPACE_APPLICATION};
    int order() default Ordered.HIGHEST_PRECEDENCE;
}

step 4(PS). use @enable{XXX}ApolloConfig instead of @EnableApolloConfig in app server.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.