Introduction
In a distributed system, due to the number of services, in order to facilitate unified management service profile, updated in real time, so they need a distributed configuration center.
github address: https: //github.com/erlieStar/spring-cloud-learning
Set up the configuration repository
Configuring placed in https://github.com/erlieStar/spring-cloud-learning/ warehouse conf-repo directory
Put a javashitang service of three environmental profile
Jvshitng-Devkyml
from: javashitang-git-dev
Jvshitng-Testkyml
from: javashitang-git-test
Jvshitng-Prodkyml
from: javashitang-git-prod
Setting up the Configuration Center
在https://github.com/erlieStar/spring-cloud-learning
示例项目:config-service(spring-cloud-config)
consumer-config-service(spring-cloud-config)
config-service
1. Project configuration is as follows
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
application.yaml
server:
port: 8080
spring:
application:
name: config-service
cloud:
config:
server:
git:
uri: https://github.com/erlieStar/spring-cloud-learning # 配置github地址
search-paths: config-repo # 仓库的相对地址
username: # github仓库的账号,公有仓库可以不用配置
password: # github仓库的密码,公有仓库可以不用配置
label: master # 配置仓库的分支
2. Start class add comments @EnableConfigServer
@EnableConfigServer
@SpringBootApplication
public class ConfigService {
public static void main(String[] args) {
SpringApplication.run(ConfigService.class);
}
}
consumer-config-service
1. Project configuration is as follows
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
bootstrap.yaml
server:
port: 8090
spring:
application:
name: javashitang # 要获取的配置的应用名
cloud:
config:
uri: http://localhost:8080 # 配置中心config-server的地址
profile: dev # 对应配置文件中的{profile}部分
label: master # 对应配置文件中的{label}部分,git即分支名
启动config-service(spring-cloud-config)
consumer-config-service(spring-cloud-config)
Repository configuration files will be converted into Web interfaces, access can refer to the following rules:
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
The above URL mapped {application} - {profile} .yml corresponding profile, wherein the corresponding {label} Git on different branches, the default is master. In javashitang-dev.yml as an example, its application is javashitang, profile is dev.
Visit http: // localhost: 8080 / javashitang / dev
results are as follows, configured to provide normal service center
{
"name": "javashitang",
"profiles": [
"dev"
],
"label": null,
"version": "2040b0fe167c8641f1c0d8f5e8844e4f35f80896",
"state": null,
"propertySources": [
{
"name": "https://github.com/erlieStar/spring-cloud-learning/config-repo/javashitang-dev.yaml",
"source": {
"from": "javashitang-git-dev"
}
}
]
}
Visit http: // localhost: 8090 / from
the following results
javashitang-git-dev
Dynamic refresh configuration
We content javashitang-dev.yaml changed as follows, and submit to github
from: javashitang-git-dev-update
Again go to http: // localhost: 8090 / from
the following results
javashitang-git-dev
You can not get to see the latest configuration, how to get the client to get the latest configuration? Only the need for reform on the basis of consumer-config-service (spring-cloud-config) on.
1. Add dependence
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2. The need to give top class load variables plus @RefreshScope notes, access / actuator / refresh the client will be updated when following these variable values.
@RefreshScope
@RestController
@SpringBootApplication
public class ConsumerConfigService {
public static void main(String[] args) {
SpringApplication.run(ConsumerConfigService.class);
}
@Value("${from}")
private String from;
@RequestMapping("from")
public String from() {
return this.from;
}
}
Increase follows 3.bootstrap.yaml
management:
endpoints:
web:
exposure:
include: refresh
4. Use the postman calls with post http way: // localhost: 8090 / actuator / refresh
Again go to http: // localhost: 8090 / from
the following results
javashitang-git-dev-update
We can see that the client has got the latest value. However, each manual refresh client too much, as long as there is no way to submit code to automatically call clients to update it, Github is a way of Webhook
Webhook
Webhook when an event occurs, the notification information to the recipient by sending an HTTP POST request. Webhook you to monitor events on the Github.com, the most common way to push events. If you set up a monitoring events Webhook push, so whenever you have any commits to the project, this Webhook will be triggered, then Github will send a HTTP POST request to the address you configured.
In this way, you will be able to automate some repetitive work this way, for example, you can automatically trigger a number of continuous integration (CI) operation with Webhook tools, such as Travis CI; or is deployed you go through Webhook online server. The figure is above Webhook Github configuration.
- Payload URL: after triggering the callback URL
- Content type: data format, two general use json
- Secret: used to encrypt the body of the POST string. Using HMAC
- events: a list of event triggers.
events event type | description |
---|---|
push | Triggered when push warehouse. Default Event |
create | Fires when there is a branch or tag is created |
delete | Fires when there is a branch or tag is deleted |
So that we can take advantage of the hook mechanism to trigger the update client, but when more and more clients when, elegant hook mechanism is not enough, the other in increments of client needs change hook it is unrealistic. In fact, Spring Cloud gives us a better solution --Spring Cloud Bus. We will continue to follow learn how to implement a message bus in the manner of notification of configuration change information, to automate the update on the cluster by Spring Cloud Bus.
To build a highly available configuration center
The above example can be seen in the presence of a single point config-service, then we can consider to make a micro-distribution center service, which clustered.
示例项目:config-cluster(spring-cloud-config)
consumer-config-cluster(spring-cloud-config)
config-cluster
1. Project configuration is as follows
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
application.yaml
server:
port: 9080
spring:
application:
name: config-service
cloud:
config:
server:
git:
uri: https://github.com/erlieStar/spring-cloud-learning # 配置github地址
search-paths: config-repo # 仓库的相对地址
username: # github仓库的账号,公有仓库可以不用配置
password: # github仓库的密码,公有仓库可以不用配置
label: master # 配置仓库的分支
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
Plus @EnableEurekaClient and @EnableConfigServer comment on 2. Start class
@EnableEurekaClient
@EnableConfigServer
@SpringBootApplication
public class ConfigCluster {
public static void main(String[] args) {
SpringApplication.run(ConfigCluster.class);
}
}
consumer-config-cluster
1. Project configuration is as follows
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
bootstrap.yaml
server:
port: 9090
spring:
application:
name: javashitang # 要获取的配置的应用名
cloud:
config:
# uri: http://localhost:8080 # 配置中心config-server的地址
profile: dev # 对应配置文件中的{profile}部分
label: master # 对应配置文件中的{label}部分
discovery:
enabled: true
service-id: config-cluster
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
management:
endpoints:
web:
exposure:
include: refresh
Plus @EnableEurekaClient and @RefreshScope comment on 2. Start class
@RefreshScope
@RestController
@EnableEurekaClient
@SpringBootApplication
public class ConsumerConfigCluster {
public static void main(String[] args) {
SpringApplication.run(ConsumerConfigCluster.class);
}
@Value("${from}")
private String from;
@RequestMapping("from")
public String from() {
return this.from;
}
}
启动eureka-service(spring-cloud-eureka)
config-cluster(spring-cloud-config)
consumer-config-cluster(spring-cloud-config)
Visit http: // localhost: 9080 / javashitang / dev
results are as follows, configured to provide normal service center
{
"name": "javashitang",
"profiles": [
"dev"
],
"label": null,
"version": "0760c0643488f25bfba1cf95b5af51006e0f312a",
"state": null,
"propertySources": [
{
"name": "https://github.com/erlieStar/spring-cloud-learning/config-repo/javashitang-dev.yaml",
"source": {
"from": "javashitang-git-dev-update"
}
}
]
}
Visit http: // localhost: 9090 / from
the following results
javashitang-git-dev-update