Spring Cloud Learning (5)-config server

Source: Internet
Author: User
Tags webhook

Distributed environment under the Unified configuration framework, there are many, such as Baidu's disconf, Ali's Diamand. See the solution for Spring Cloud today:

As you can see from the architecture, there is a big difference between the disconf and the like, the main differences are:

    • Configurations are stored in different ways
      • Disconf is to save the configuration information in MySQL, zookeeper, and Spring Cloud Config is to save the configuration on the GIT/SVN (that is, the configuration is managed as source code)
    • Configuration is managed in different ways
      • Spring Cloud Config does not have a unified management interface like disconf, since the configuration is considered as git and other sources to see, git management interface, is the configuration of the management interface
    • Different notification mechanisms for configuration changes
      • After configuration changes in disconf, reliance on ZK's event watcher to notify the app, and Spring Cloud Config, which relies on git to trigger the Webhook callback after each push, eventually triggers Spring cloud bus (message bus), The associated application is then notified by the message bus.

In addition, Spring Cloud Config server itself is a microservices, like other microservices, can also be registered to the Eureka server, let other users from the registry to find, simply from the problem/scenario solved, disconf and spring Cloud Config server is highly coincident, it is difficult to say which is good, that poor, but the design philosophy is different.

But there is a point, from the notification mechanism of configuration changes, if there are 100 application nodes, all rely on the unified configuration, if you modify the configuration, just want to let a few nodes "grayscale" Update configuration, Spring Cloud Config server easier to do, This is more flexible than disconf (explained in more detail later).

Steps to use:

One, create a configuration item on GIT/SVN (to save the configuration file)

Take https://github.com/yjmyzz/spring-cloud-config-repository this as an example, the above put a few configuration files (recommended in the new YML format, the Chinese support better code).

The contents of the application.yml are as follows:

Demo:  title: "Default title"

Several other files application_xxx.yml, inside the xxx, representing the different profile.

Second, create config-server micro-service

2.1 Adding dependencies

dependencies {    compile ' org.springframework.cloud:spring-cloud-starter-eureka '    compile ' Org.springframework.cloud:spring-cloud-config-server '    compile ' org.springframework.boot: Spring-boot-starter-actuator '}

The key is a 2nd dependency

2.2 Application.yml

Spring:  application:    name:config-server  Profiles:      active:server1  Cloud:    config:      Server:        git:          uri:https://github.com/yjmyzz/spring-cloud-config-repository#          Username: *****#          Password: *****eureka:  instance:    prefer-ip-address:true    Instance-id: ${ Spring.application.name}:${server.port}  Client:    service-url:      defaultzone:http://yjmyzz:[email Protected]:8100/eureka,http://yjmyzz:[email protected]:8200/eurekamanagement:  security:    Enabled:false

Notice the Cloud.config.server in the above section, where the GIT configuration item is configured. In addition: The Config-server service itself also requires ha, so this example has 2 instances, corresponding to Server1, server2 two profiles, with different ports, run 2 instances in this machine to simulate high availability.

Application-server1.yml

Server:  port:8004

Application-server2.yml

Server:  port:8005

2.3 Main entry class

Package Com.cnblogs.yjmyzz.spring.cloud.study.config;import Org.springframework.boot.springapplication;import Org.springframework.boot.autoconfigure.springbootapplication;import Org.springframework.cloud.config.server.enableconfigserver;import org.springframework.cloud.netflix.eureka.enableeurekaclient;/** * Created by yangjunming on 2017/7/5. */@SpringBootApplication @enableeurekaclient@enableconfigserverpublic class Configserver {public    static void Main (string[] args) {        springapplication.run (configserver.class, args);}    }

The key is to @enableconfigserver this annotation.

2.4 , run and look.

You can see that 2 config-server have been registered on the Eureka, and then take a separate tour: HTTP://LOCALHOST:8004/APPLICATION-DEV.YML

has already exported the contents of application-dev.yml on Git.

Third, the use of Config-server

3.1 Add a dependency in the previous Service-provider

Compile ' org.springframework.cloud:spring-cloud-starter-config '

3.2 Creating a simple configuration class

Package Com.cnblogs.yjmyzz.spring.cloud.study.config;import Lombok. Data;import Org.springframework.boot.context.properties.configurationproperties;import org.springframework.stereotype.component;/** * Created by yangjunming on 2017/7/5. */@Component @data@configurationproperties (prefix = "Demo") public class Democonfig {    private String title;}

Then find a sample service that uses this configuration:

Package Com.cnblogs.yjmyzz.spring.cloud.study.service.impl;import Com.cnblogs.yjmyzz.spring.cloud.study.api.userservice;import Com.cnblogs.yjmyzz.spring.cloud.study.config.democonfig;import Com.cnblogs.yjmyzz.spring.cloud.study.dto.userdto;import org.springframework.beans.factory.annotation.Autowired , Import Org.springframework.stereotype.Service, @Service ("UserService") public class Userserviceimpl implements UserService {    @Autowired    democonfig config;    @Override public    userdto finduser (Integer userId) {        userdto user = new Userdto ();        User.setuserid (userId);        User.setusername ("Yang over the Bodhi tree (" + config.gettitle () + ")");        return user;    }}

3.3 Adding bootstrap.yml configuration files

Spring:  application:    name:application  Cloud:    config:      profile:dev      label:master      Discovery:        enabled:true        Service-id:config-servereureka:  instance:    prefer-ip-address:true  client:    service-url:      defaultzone:http://yjmyzz:[email protected]:8100/eureka,http://yjmyzz:[ Email Protected]:8200/eureka

Note Spring.cloud This section, which specifies the profile as Dev, reads the GIT configuration file branch to master, while allowing automatic discovery of the Config-server instance from the Eureka. Another spring.applicatin.name is the name of the configuration file (i.e.: APPLICATION_XXX.YML)

3.4 , run and look.

Description has been taken from Config-server to the configuration.

 

iv. Configuration Updates

Add @refreshscope on 4.1 Controller

@RestController @refreshscopepublic class Usercontroller {    @Autowired    private userservice userservice;    @GetMapping ("/user/{id}") Public    userdto Finduser (@PathVariable Integer id) {        return Userservice.finduser (ID );    }}

This annotation, according to the source of the argument: Beans annotated this-way can is refreshed at runtime and any of the components that is using them would get a new I Nstance on the next method call, fully initialized and injected with all dependencies. With this annotation, you can flush the bean directly at run time and get a completely new instance the next time the method is called.

4.2 Manual Refresh /refresh

You can try to change the contents of application-dev.yml in the Git configuration project, and then browse the contents of the HTTP://LOCALHOST:8001/USER/1 found.

Http://localhost:8001/refresh manual to this address, send a POST request (can be used postman or curl-d "Http://localhost:8001/refresh), you can see

Description Demo.title This configuration item is refreshed, and then browsing HTTP://LOCALHOST:8001/USER/1 can see the change

But this is obviously not a method, such as there are 10 service-provider composed of clusters, if you want 1 units manually refreshed, too low (in addition to do the configuration of gray-level update, you can refresh 1 of this scenario)

4.3 integrated spring cloud bus to batch Refresh

Spring Cloud Bus currently only supports RABBITMQ and Kafka, we take Kafka as an example, first in the Service-provider application.yml, add the following configuration

Then, in the dependencies, add:

Compile ' Org.springframework.cloud:spring-cloud-starter-bus-kafka '

Note: About the Kafka environment, there are a lot of information on the Internet, we can refer to.

After configuring these, the machine starts Kafka and then restarts Service-provider, it will have a /bus/refresh endpoint, namely: Http://xxx:port/bus/refresh, Whenever a POST request is initiated to the/bus/refresh of any machine in the cluster, all other nodes are flushed synchronously. Basically, the machine sends a message to the Kafka, and the other machines are on the message bus, and the message is heard and the configuration is refreshed.

The last question: even if there is a/bus/refresh, someone or a system trigger is needed. This is a good solution, GitHub or Gitlab generally have a webhook function that can be configured to trigger some address callbacks when code push.

This will trigger an automatic refresh whenever the configured code commits.

Sample source code: Https://github.com/yjmyzz/spring-cloud-demo

Spring Cloud Learning (5)-config server

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.