标签:alt ade port registry switch 设置 test overlay ops
本文说明了如何定制化JHipster-Registry,增加消息总线功能。
实现的效果就是修改配置中心的文件后,通过消息队列主动推送给微服务而无需重启微服务,实现配置内容热加载。
编号 | IP | 主机名 |
---|---|---|
B | 10.70.24.135 | node02 |
C | 10.70.24.136 | node03 |
D | 10.70.24.85 | node04 |
E | 10.70.24.95 | node05 |
组件名 | 说明 | 版本 |
---|---|---|
Jhipster Registry | 基于eureka和spring cloud config的基础服务。 | v4.0.0 |
Spring Cloud Bus | 消息总线组件,通过maven依赖引入。 | 1.3.1 |
RabbitMQ | 消息中间件,已部署在预研环境中。 | 3 |
myClient | 一个客户端服务,用于测试消息总线效果。 | 1.0 |
GitLab配置库 | JHipster-Registry引用的配置中心。所有服务的配置文件所在地。 |
Github上下载jhipster-jhipster源码。
https://github.com/jhipster/jhipster-registry/releases
保存到本地Gitlab源码库
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
application.yml
management:
security:
enabled: false
context-path: /management # 消息总线的url,这个前缀要加在 /bus/refresh 前面
application-prod.conf
spring:
rabbitmq:
host: rmqha_proxy
port: 56729
username: guest
password: guest
publisher-confirms: true # 生产者消息确认
addresses: rmqha_proxy # 客户端访问server地址, 作为发送者,配置和host一致
在安装了jdk,maven, yarn,nodejs的centos7服务器上执行命令
yarn && yarn install #前端资源配置生成
mvn -Pprod clean package dockerfile:build #项目构建并打包成docker镜像,版本号为3.0
docker service create --name base-registry --replicas 1 --network mycloud_base-overlay --constraint node.labels.mycloud.node.type==worker --constraint node.labels.mycloud.node.env==dev --env SPRING_PROFILES_ACTIVE=prod,git --env JHIPSTER_SECURITY_AUTHENTICATION_JWT_SECRET=gdd904a2dffb3eaf28a318b49e0ab986cc51012k --publish 8761:8761 dtr.devops.mycloud.com/common/jhipster-registry:3.0
可通过如下url访问
http://<IP>:8761
通过jhipster生成一个微服务,名为my-client。
保存到gitLab库git@gitlab.yourcompany.com:root-cloud/mycloud-monitor.git
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
application-prod.yml
eureka:
instance:
prefer-ip-address: true
client:
service-url: # 指向eureka服务器
defaultZone: http://admin:${jhipster.registry.password}@base-registry:8761/eureka/
spring:
rabbitmq:
host: rmqha_proxy
port: 56729
username: guest
password: guest
publisher-confirms: true # 生产者消息确认
addresses: rmqha_proxy # 客户端访问server地址, 作为发送者,配置和host一致
bootstrap-prod.yml
spring:
cloud:
config:
fail-fast: true
retry:
initial-interval: 1000
max-interval: 2000
max-attempts: 100
uri: http://admin:${jhipster.registry.password}@base-registry:8761/config # 指向配置服务器
# name of the config server‘s property source (file.yml) that we want to use
name: mq-client
profile: rd # 注意这里是rd分支,springcloud-config.git上的rd分支
label: master # toggle to switch to a different version of the configuration as stored in git
jhipster:
registry:
password: admin
MyClientApp.java
@(JHipster)RefreshScope # 加上这个注解
public class MyClientApp {
......
}
application-prod.yml
application:
domain: http://yourcompany.com/2017 # 增加一个配置项
ApplicationProperties
@ConfigurationProperties(prefix = "application", ignoreUnknownFields = false)
public class ApplicationProperties {
private String domain = "http://default";
public String getDomain() {
return domain;
}
public void setDomain(String domain) {
this.domain = domain;
}
}
TestResource.java
@RestController
@RequestMapping("/management")
public class TestResource {
private final ApplicationProperties appProperties;
@GetMapping("/mq/property")
@Timed
public ResponseEntity<String> getproperty() {
return ResponseUtil.wrapOrNotFound(Optional.ofNullable(appProperties.getDomain()));
}
}
在安装了jdk,maven, yarn,nodejs的centos7服务器上执行命令
$ mvn -Pprod clean package dockerfile:build #项目构建并打包成docker image
docker service create --name mq-client-mysql --replicas 1 --network name=mycloud_base-overlay,alias=mq-client-mysql --constraint node.labels.mycloud.node.type==worker --constraint node.labels.mycloud.node.env==data --env MYSQL_ROOT_PASSWORD=my-secret-pw --publish 32781:3306 dtr.devops.mycloud.com/common/mysql:5
docker service create --name my-client-app --replicas 1 --network mycloud_base-overlay --constraint node.labels.mycloud.node.type==worker --constraint node.labels.mycloud.node.env==dev --env SPRING_PROFILES_ACTIVE=prod,swagger --publish 18081:8081 dtr.devops.mycloud.com/yourcompany/my-client:1.0
可通过如下url访问
http://<IP>:18081
http://<IP>:18081/management/mq/property
http://yourcompany.com/2017 # 返回值
application:
domain: http://yourcompany.com/2018 # 把2017改成了2018
http://<IP>:18081/management/mq/property
http://yourcompany.com/2017 # 返回值没有改变
$ curl -X POST http://<IP>:8761/management/bus/refresh
http://<IP>:<port>/management/mq/property
http://yourcompany.com/2018 # 返回值已经更新为2018
JHipster技术栈定制 - JHipster Registry消息总线配置
标签:alt ade port registry switch 设置 test overlay ops
原文地址:https://www.cnblogs.com/yorkwu/p/10145203.html