Coder Social home page Coder Social logo

change-on-fly's Introduction

Change Spring on the fly

  • config-service: spring-cloud server
  • custom-service: client of config-service
  • custom-service-self-config: spring-cloud client without server

config-service

spring-cloud server with the application properties that can be used by clients.

Properties / YAML

At service application.yml, define the properties/yml location that will be externalized.

# file:// OR git dir
spring.cloud.config.server.git.uri: file:///tmp/externalized-properties

Dependencies

// springBootVersion = 1.5.3.RELEASE
// springCloudVersion = Dalston.SR1
dependencies {
	compile 'org.springframework.boot:spring-boot-starter'
	compile 'org.springframework.cloud:spring-cloud-config-server'
}

custom-service

Client of config-service using the externalized properties/yml and refresh on the fly.

Appling changes on the fly

@RefreshScope
@RestController
class CustomController {

    // cloud.foo must be configured at externalized properties
    // that config-service is looking.
    // Example:
    // /tmp/externalized-properties/application.properties
    // cloud.foo=change_me
    @Value("${cloud.foo}")

    private String value;

    @RequestMapping
    public String sayValue() {
        return value;
    }
}

Dependencies

dependencies {
	compile 'org.springframework.boot:spring-boot-starter-web'
	compile 'org.springframework.boot:spring-boot-starter-actuator'
	compile 'org.springframework.cloud:spring-cloud-config-client'
}

Considering both services is running, let's fun:

  • GET
➜  ~ curl -v http://localhost:8080/
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET / HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.50.1
> Accept: */*
> 
< HTTP/1.1 200 
< X-Application-Context: application
< Content-Type: text/plain;charset=UTF-8
< Content-Length: 9
< Date: Wed, 07 Jun 2017 13:52:27 GMT
< 
* Connection #0 to host localhost left intact
change_me
  • Change the external config
➜  ~ echo cloud.foo=changed! > /tmp/externalized-properties/application.properties
  • Refresh scope
➜  ~ curl -X POST http://localhost:8080/refresh                                
["cloud.foo"]
  • GET
➜  ~ curl -v http://localhost:8080/                                               
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET / HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.50.1
> Accept: */*
> 
< HTTP/1.1 200 
< X-Application-Context: application
< Content-Type: text/plain;charset=UTF-8
< Content-Length: 8
< Date: Wed, 07 Jun 2017 13:55:58 GMT
< 
* Connection #0 to host localhost left intact
changed!

custom-service-self-config

Refresh the application properties itself.

Dependencies

dependencies {
	compile 'org.springframework.boot:spring-boot-starter-web'
	compile 'org.springframework.boot:spring-boot-starter-actuator'
	compile 'org.springframework.cloud:spring-cloud-config-client'
}

Once the application is running, let's fun:

  • GET
➜  ~ curl -v http://localhost:8080/            
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET / HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.50.1
> Accept: */*
> 
< HTTP/1.1 200 
< X-Application-Context: application:8080
< Content-Type: text/plain;charset=UTF-8
< Content-Length: 6
< Date: Wed, 07 Jun 2017 14:19:36 GMT
< 
* Connection #0 to host localhost left intact
try me
  • Change the application yml
➜ sed -ri 's/^(\s*)(foo\s*:\s*try me\s*$)/\1foo: tried/' application.yml
  • Refresh scope
➜  ~ curl -X POST http://localhost:8080/refresh
["cloud.foo"]
  • GET
➜  ~ curl -v http://localhost:8080/            
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET / HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.50.1
> Accept: */*
> 
< HTTP/1.1 200 
< X-Application-Context: application:8080
< Content-Type: text/plain;charset=UTF-8
< Content-Length: 5
< Date: Wed, 07 Jun 2017 14:30:33 GMT
< 
* Connection #0 to host localhost left intact
tried

change-on-fly's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.