1. 新建springcloud-config-server-3344模块
2. 导入依赖并在启动类添加注解
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--config-server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
</dependencies>
@EnableConfigServer
3. application.yml连接到git
server:
port: 3344
spring:
application:
name: springcloud-config-server
# 连接远程仓库
cloud:
config:
server:
git:
uri: https://gitee.com/yuantu123/springcloud-config.git # https ,不是 git
# 通过 config-server可以连接到git,访问其中的资源以及配置~
4. 改造springcloud-eureka-7001
-
添加依赖
<!--config-starter--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> <version>2.1.1.RELEASE</version> </dependency>
-
bootstrap.yml获取git配置文件
# 系统级别的配置 spring: cloud: config: name: config-eureka # 需要从git上读取的资源名称,不要后缀 profile: dev label: master uri: http://localhost:3344 #调用springcloud-config-server-3344获取git
-
git上的文件config-eureka.yml
spring: profiles: active: dev --- spring: profiles: dev application: name: springcloud-config-eureka-7001 server: port: 7001 #Eureka配置 eureka: instance: hostname: eureka7001.com #Eureka服务端的实例名称 client: register-with-eureka: false # 表示是否向eureka注册中心注册自己 fetch-registry: false #fetch-registry如果为false,则表示自己为注册中心 service-url: #defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/ --- spring: profiles: test application: name: springcloud-config-eureka-7001 server: port: 7001 #Eureka配置 eureka: instance: hostname: eureka7001.com #Eureka服务端的实例名称 client: register-with-eureka: false # 表示是否向eureka注册中心注册自己 fetch-registry: false #fetch-registry如果为false,则表示自己为注册中心 service-url: #defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
5. 改造 springcloud-provider-dept-8001
-
添加依赖
<!--config-starter--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> <version>2.1.1.RELEASE</version> </dependency>
-
bootstrap.yml获取git配置文件
# 系统级别的配置 spring: cloud: config: name: config-dept # 需要从git上读取的资源名称,不要后缀 profile: dev label: master uri: http://localhost:3344
-
git上的文件config-dept.yml
spring: profiles: active: dev --- server: port: 8001 #mybatis配置 mybatis: type-aliases-package: com.jx.springcloud.pojo config-location: classpath:mybatis/mybatis-config.xml mapper-locations: classpath:mybatis/mapper/*.xml #spring的配置 spring: profiles: dev application: name: springcloud-config-dept datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/db01?useUnicode=true&characterEncoding=utf-8 username: root password: 123456 #Eureka的配置,服务注册到哪里 eureka: client: service-url: defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/ instance: instance-id: springcloud-provider-dept8001 # 修改eureka上的默认描述信息! prefer-ip-address: true # true,可以显示服务的IP地址 ~ #info配置 info: app.name: jx-springcloud company.name: jx.com --- server: port: 8001 #mybatis配置 mybatis: type-aliases-package: com.jx.springcloud.pojo config-location: classpath:mybatis/mybatis-config.xml mapper-locations: classpath:mybatis/mapper/*.xml #spring的配置 spring: profiles: test application: name: springcloud-config-dept datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/db02?useUnicode=true&characterEncoding=utf-8 username: root password: 123456 #Eureka的配置,服务注册到哪里 eureka: client: service-url: defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/ instance: instance-id: springcloud-provider-dept8001 # 修改eureka上的默认描述信息! prefer-ip-address: true # true,可以显示服务的IP地址 ~ #info配置 info: app.name: jx-springcloud company.name: jx.com