长春 网站建设,桂林市内必去的地方,潍坊集团网站建设,百度网址安全中心怎么关闭目录 
概述 
环境说明 
步骤 
创建远端git仓库 
准备配置文件 
配置中心--服务端 
配置中心--客户端 
配置中心的高可用 
配置中心--服务端 
配置中心--客户端 
消息总线刷新配置 
配置中心--服务端 
配置中心--客户端 概述 
因为微服务架构有很多个服务#xff0c;手动一个一…目录 
概述 
环境说明 
步骤 
创建远端git仓库 
准备配置文件 
配置中心--服务端 
配置中心--客户端 
配置中心的高可用 
配置中心--服务端 
配置中心--客户端 
消息总线刷新配置 
配置中心--服务端 
配置中心--客户端 概述 
因为微服务架构有很多个服务手动一个一个管理各个服务配置很麻烦不同的环境开发、测试、生产往往需要不同的配置文件运行期间也需要动态调整配置修改配置后微服务需要自动更新配置所以需要一个统一管理应用配置的组件。 
配置中心是一种统一管理各种应用配置的基础服务组件。本文讲解的配置中心组件为Spring Cloud Config。 本文的操作可在 微服务调用链路追踪 的基础上进行 或者 在 Spring Cloud Stream实践 的基础上进行。 环境说明 
jdk1.8 
maven3.6.3 
mysql8 
spring cloud2021.0.8 
spring boot2.7.12 
idea2022 
rabbitmq3.12.4 步骤 
创建远端git仓库 
创建远端git仓库管理配置文件使用gitee创建仓库名称为config-repo创建时点击创建README文件方便直接用浏览器上传文件 可使用远端仓库管理微服务公共的配置而非公共的配置可放在各自微服务的application.yml中。 
公共的配置例如数据库配置、Eureka配置等 
非公共的配置例如端口号、应用名称等 准备配置文件 
配置中心的配置文件命名规则为: 
{application}-{profile}.yml
{application}-{profile}.propertiesapplication为应用名称 profile指的开发环境用于区分开发环境、测试环境、生产环境等 
复制product-service的application.yml重命名为application-pro.yml抽取公共配置例如数据库配置、zipkin、sleuth、rabbitmq、eureka等同时添加一个name属性用于区别不同的环境配置公共的配置放在远端git仓库。 application-pro.yml spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/shop1?useUnicodetruecharacterEncodingutf-8serverTimezoneGMT%2B8username: rootpassword: 123jpa:database: MySQLshow-sql: trueopen-in-view: truegenerate-ddl: true #自动创建表zipkin:sender:type: rabbit #数据的传输方式以rabbit的方式向server端发送数据sleuth:sampler:probability: 1 #采样比日志的采样比例默认0.1测试环境设置为100%收集日志rabbitmq:host: localhostport: 5672username: guestpassword: guestlistener: #这里配置重试策略direct:retry:enabled: truesimple:retry:enabled: trueeureka:client:service-url:defaultZone: http://localhost:9000/eureka/instance:prefer-ip-address: trueinstance-id: ${spring.cloud.client.ip-address}:${server.port}logging:level:root: infoorg.springframework.web.servlet.DispatcherServlet: DEBUGorg.springframework.cloud.sleuth: DEBUGname: product-pro 复制application-pro.yml得到application-dev.yml application-dev.yml 配置内容如下区别在于name的值为product-dev 
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/shop1?useUnicodetruecharacterEncodingutf-8serverTimezoneGMT%2B8username: rootpassword: 123jpa:database: MySQLshow-sql: trueopen-in-view: truegenerate-ddl: true #自动创建表zipkin:sender:type: rabbit #数据的传输方式以rabbit的方式向server端发送数据sleuth:sampler:probability: 1 #采样比日志的采样比例默认0.1测试环境设置为100%收集日志rabbitmq:host: localhostport: 5672username: guestpassword: guestlistener: #这里配置重试策略direct:retry:enabled: truesimple:retry:enabled: trueeureka:client:service-url:defaultZone: http://localhost:9000/eureka/instance:prefer-ip-address: trueinstance-id: ${spring.cloud.client.ip-address}:${server.port}logging:level:root: infoorg.springframework.web.servlet.DispatcherServlet: DEBUGorg.springframework.cloud.sleuth: DEBUGname: product-dev 
通过浏览器界面手动上传application-pro.yml和application-dev.yml到git仓库 点击上传文件区域选中需要上传的文件application-pro.yml和application-dev.yml 填写提交信息点击提交 上传成功后如下图 配置中心--服务端 
创建子工程config_server 引入依赖 dependenciesdependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-config-server/artifactId/dependency/dependencies 启动类 
package org.example.config;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;SpringBootApplication
EnableConfigServer //开启配置中心服务端功能
public class ConfigServerApplication {public static void main(String[] args) {SpringApplication.run(ConfigServerApplication.class, args);}
}配置application.yml 
server:port: 11111
spring:application:name: config-servercloud:config:server:git:uri: https://gitee.com/xxx/config-repo.git 
uri的值是刚才创建的远端git仓库的地址注意修改为自己的仓库地址。 
启动config_server服务 浏览器访问 http://localhost:11111/application-dev.yml 访问不到数据 
原因是gitee仓库是私有的需要把仓库公开或者配置gitee的登录信息。 
解决方法1将git仓库开源 解决方法2修改配置加上gitee的用户名和密码 
cloud:config:server:git:uri: https://gitee.com/xxx/config-repo.gitusername: xxxpassword: xxx 
两种方法选择其中一个。 解决后再次访问 http://localhost:11111/application-dev.yml 成功看到数据如下  配置中心--客户端 
配置中心客户端就是各个微服务 
这里创建一个微服务config_client_1 
config_client_1项目结构如下 添加依赖 dependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion8.0.33/version/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-jpa/artifactId/dependency!--引入EurekaClient--dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-netflix-eureka-client/artifactId/dependencydependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-config/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-actuator/artifactId/dependency!--消息总线bus--dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-bus/artifactId/dependencydependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-stream-binder-rabbit/artifactId/dependencydependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-bootstrap/artifactId/dependency/dependencies 
刷新依赖 非公共的配置application.yml 
server:port: 9101
spring:application:name: config-client1bootstrap.yml 
spring:cloud:config:name: application #应用名称对应git配置文件的前半部分例如application-dev.yml这里就写applicationprofile: dev #开发环境label: master #git中的分支uri: http://localhost:11111 #config_server的请求地址 ConfigClientApplication.java 
package org.example.config.client;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;SpringBootApplication
EntityScan(org.example.config.client.entity.Product)
public class ConfigClientApplication {public static void main(String[] args) {SpringApplication.run(ConfigClientApplication.class, args);}
} Product.java 
package org.example.config.client.entity;import lombok.Data;import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.math.BigDecimal;/*** 商品实体类*/
Data
Entity
Table(nametb_product)
public class Product {Idprivate Long id;private String productName;private Integer status;private BigDecimal price;private String productDesc;private String caption;private Integer inventory;
} ProductController.java 
package org.example.config.client.controller;import org.example.config.client.entity.Product;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;RestController
RequestMapping(/config-client/product)
public class ProductController {Value(${name})private String name;RequestMapping(/get)public Product getProduct(){Product product  new Product();product.setProductName(name);return product;}
} 启动eureka、config_server、config_client_1服务 注意因为远端git仓库的配置文件有eureka配置所以拉取到配置后需要启动eureka服务 浏览器访问 http://localhost:9101/config-client/product/get 响应 {id:null,productName:product-dev,status:null,price:null,productDesc:null,caption:null,inventory:null} pruductName为product-dev与git的application-dev.yml的name属性一致 将config_client_1的bootstrap.yml的profile改为pro 重启config_client_1服务 
再次访问 http://localhost:9101/config-client/product/get 响应 {id:null,productName:product-pro,status:null,price:null,productDesc:null,caption:null,inventory:null} pruductName为product-pro与git的application-pro.yml的name属性一致 修改git的配置 
查看application-pro.yml文件点击编辑 把product-pro改为product-pro1  点击提交 浏览器访问 http://localhost:9101/config-client/product/get 发现数据未更新还是product-pro 解决方式 
方式一重启config_client_1服务 
发现重启可以拿到最新数据但是在生产环境中重启服务有时候不太方便。 方式二手动刷新 修改config_client_1服务 
pom.xml引入actuator依赖之前已引入跳过 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-actuator/artifactId/dependency 在需要的更新数据的类上添加RefreshScope开启动态刷新 修改bootstrap.yml 
#开启动态刷新的端点
management:endpoints:web:exposure:include: refresh 重启config_client_1服务 访问 http://localhost:9101/config-client/product/get productName是product-pro1 将git配置文件的name值修改为product-pro2 发起post请求刷新数据 
curl -X POST http://localhost:9101/actuator/refresh 
可使用postman或cmd命令行发起请求这里使用cmd命令行发起post请求 再次访问 http://localhost:9101/config-client/product/get 响应最新的数据product-pro2 手动刷新操作总结 引入actuator依赖  需要的更新数据的类上添加RefreshScope开启动态刷新  bootstrap.yml开启动态刷新的端点  修改git配置  发起post请求手动刷新数据  手动刷新能解决在不重启应用情况下能做到动态刷新配置。但当服务很多时或需要更新的数据接口很多的情况下就需要发起很多次Post请求手动刷新显得很麻烦。可以使用消息总线技术解决该问题。 配置中心的高可用 
一台配置中心服务容易出现单点故障问题将配置中心集群化构成高可用的配置中心可以解决配置中心单点故障问题。 
将多个配置中心注册到eureka提供统一的服务名称客户端通过服务名称调用配置中心。 配置中心--服务端 
修改config_server服务 
添加依赖 dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-netflix-eureka-client/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-actuator/artifactId/dependency 
刷新依赖 application.yml添加eureka相关配置 
eureka:client:service-url:defaultZone: http://localhost:9000/eurekainstance:prefer-ip-address: trueinstance-id: ${spring.cloud.client.ip-address}:${server.port} 
修改config_server配置文件添加default-label: master配置 
cloud:config:server:git:uri: https://gitee.com/xxx/config-repo.gitdefault-label: master 
注意修改uri为自己git远端仓库地址。  启动eureka、config_server服务 查看Eureka Web UI看到了一台配置中心服务 http://localhost:9000/ 通过复制配置方式得到另外一台配置中心 
修改端口号复制运行配置再启动一台配置中心服务 刷新查看Eureka Web UI看到CONFIG-SERVER配置中心服务有两个实例。 配置中心--客户端 
config_client_1服务 
修改bootstrap.yml添加eureka配置修改通过服务名称config-server拿到配置 spring:cloud:config:name: application #应用名称对应git配置文件的前半部分例如application-dev.yml这里就写applicationprofile: pro #开发环境label: master #git中的分支#uri: http://localhost:11111 #config_server的请求地址discovery:enabled: true #开启服务发现service-id: config-server #配置服务的服务名称
#开启动态刷新的端点
management:endpoints:web:exposure:include: refresheureka:client:service-url:defaultZone: http://localhost:9000/eurekainstance:prefer-ip-address: trueinstance-id: ${spring.cloud.client.ip-address}:${server.port} 
重启config_client_1服务 访问 http://localhost:9101/config-client/product/get 响应 {id:null,productName:product-pro2,status:null,price:null,productDesc:null,caption:null,inventory:null} 能正常拿到数据说明配置正常 高可用测试 
测试关闭其中一个服务是否还可以正常提供服务 再次访问 访问依然正常说明实现了配置中心的高可用。 总结 服务端添加服务注册依赖及配置注册到eureka中开启两个服务。  客户端添加eureka配置使用服务名称获取配置  测试断开一个服务不影响访问  消息总线刷新配置 
此前通过发起Post请求手动刷新配置服务接口很多时就需要发起很多次Post请求显得很麻烦。 
通过消息总线刷新配置流程如下 配置中心--服务端 
config_server服务 
添加依赖 !--消息总线依赖--dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-bus-amqp/artifactId/dependencydependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-stream-rabbit/artifactId/dependency 
刷新依赖 配置application.yml 
配置Spring Cloud Bus的RabbitMQ连接信息启用Spring Cloud Busspring.cloud.bus.enabledtrue默认已启动同时暴露动态刷新的端点。 
server:port: 11112
spring:application:name: config-servercloud:config:server:git:uri: https://gitee.com/liang_123456/config-repo.gitdefault-label: master
#    bus:
#      enabled: truerabbitmq:host: 127.0.0.1port: 5672username: guestpassword: guest#开启动态刷新的端点
management:endpoints:bus-refresh:enabled: trueweb:exposure:include: bus-refresheureka:client:service-url:defaultZone: http://localhost:9000/eurekainstance:prefer-ip-address: trueinstance-id: ${spring.cloud.client.ip-address}:${server.port}注意开启动态刷新端点的management要顶格写 配置中心--客户端 
spring-client_1服务 
和config_server一样添加依赖如果已经存在就不用再添加 !--消息总线依赖--dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-bus-amqp/artifactId/dependencydependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-stream-rabbit/artifactId/dependency 
刷新依赖 配置rabbitmq信息因为是配置中心所以在git远端仓库的配置文件配置即可查看远端git配置文件已经配置如下 重启eureka、config_server和spring-client_1服务 访问 http://localhost:9101/config-client/product/get 响应 {id:null,productName:product-pro2,status:null,price:null,productDesc:null,caption:null,inventory:null} 修改git远端配置 访问 http://localhost:9101/config-client/product/get 响应 {id:null,productName:product-pro2,status:null,price:null,productDesc:null,caption:null,inventory:null} 响应product-pro2说明还不是最新配置 发起Post请求 刷新服务端配置 curl -X POST http://localhost:11112/actuator/busrefresh 注意新版本发起post请求路径要改为busrefresh而不是bus-refresh 访问 http://localhost:9101/config-client/product/get 响应 {id:null,productName:product-pro3,status:null,price:null,productDesc:null,caption:null,inventory:null} 响应的数据也是product-pro3说明使用消息总线方式刷新配置成功。  完成enjoy it!