1.新建注册中心,这里使用eureka 这个注册中心,服务和config server都注册到这上面
server: port: 8888 spring: application: name: eureka eureka: instance: hostname: 127.0.0.1 #服务注册中心IP地址 client: registerWithEureka: false #是否向服务注册中心注册自己 fetchRegistry: false #是否检索服务 serviceUrl: #服务注册中心的配置内容,指定服务注册中心的位置 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
2.使用githup,在上面新建两个配置文件
Type Name Latest commit message Commit time config-dev.properties Create config-dev.properties yesterday config-pro.properties Create config-pro.properties yesterday
3.新建config server 并注册到eureka
server: port: 8080 tomcat: uri-encoding: UTF-8 spring: application: name: config cloud: config: server: git: uri: https://github.com/zhangninghai/config.git http: encoding: force: true charset: UTF-8 enabled: true eureka: client: serviceUrl: #注册中心的注册地址 defaultZone: http://127.0.0.1:8888/eureka/
如中文乱码 :https://www.cnblogs.com/huanzi-qch/p/10149547.html
4.新建config client 这个客户端 服务提供方和消费方都属于config server的客户端,
eureka: client: serviceUrl: #注册中心的注册地址 defaultZone: http://127.0.0.1:8888/eureka/ server: port: 8083 #服务端口号 spring: application: name: service-consumer #服务名称--调用的时候根据名称来调用该服务的方法 cloud: config: discovery: enabled: true service-id: config #注册在 eureka上的服务名 profile: pro label: master name: config #githup 上的名字
5.手动刷新配置文件 config client pom.xml加上
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> <version>2.1.7.RELEASE</version> </dependency>bootstrap.yml 加上
management: endpoints: web: exposure: include: refresh类加上注解
@RequestScope发送post生效