21.0、springcloud-Config-远程配置实战测试
我们在实战开发中会把环境的配置放在远程端,这样每次修改配置的时候就不用去碰代码,
实现配置与代码的分离
那我们先来把 eureka 的配置放到远程仓库去试试看
第一步:在本地仓库创建一个 config-eureka.yaml 文件,如下:
spring:
profiles:
active: dev
---
server:
port: 7001
#spring配置
spring:
profiles: dev
application:
name: springcloud-config-eureka
#Eureka
eureka:
instance:
hostname: localhost #Eureka服务端的实例名称
client:
register-with-eureka: false #表示是否向Eureka注册中心注册自己
fetch-registry: false #表示如果为false,则表示自己为注册中心
service-url: #他是与eureka的交互页面,说白了就是监控页面
defaultZone: http://localhost:7002/eureka/,http://localhost:7003/eureka/
---
server:
port: 7001
#spring配置
spring:
profiles: dev
application:
name: springcloud-config-eureka
#Eureka
eureka:
instance:
hostname: localhost #Eureka服务端的实例名称
client:
register-with-eureka: false #表示是否向Eureka注册中心注册自己
fetch-registry: false #表示如果为false,则表示自己为注册中心
service-url: #他是与eureka的交互页面,说白了就是监控页面
defaultZone: http://localhost:7002/eureka/,http://localhost:7003/eureka/
再把 provider_8001 服务端的配置也丢到远程
spring
profiles:
active: dev
---
server:
port: 8001
# mybatis配置
mybatis:
type-aliases-package: com.hkl.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: org.gjt.mm.mysql.Driver
url: jdbc:mysql://localhost:3306/db01?useUnicode=true&characterEncoding=utf-8
username: root
password: root
test-while-idle: true
validation-query: SELECT 1
#Eureka的配置,服务注册到哪里
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka/,http://localhost:7002/eureka/,http://localhost:7003/eureka/
instance:
instance-id: springcloud-provider-dept8001 #修改eureka上的默认描述信息
#info配置
info:
app.name: hkl-springcloud
company.name: blog.hklspringcloud.com
---
server:
port: 8001
# mybatis配置
mybatis:
type-aliases-package: com.hkl.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: org.gjt.mm.mysql.Driver
url: jdbc:mysql://localhost:3306/db02?useUnicode=true&characterEncoding=utf-8
username: root
password: root
test-while-idle: true
validation-query: SELECT 1
#Eureka的配置,服务注册到哪里
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka/,http://localhost:7002/eureka/,http://localhost:7003/eureka/
instance:
instance-id: springcloud-provider-dept8001 #修改eureka上的默认描述信息
#info配置
info:
app.name: hkl-springcloud
company.name: blog.hklspringcloud.com
把这两个yaml文件都push到远程仓库中去即可
创建一个新的 model , springcloud-config-eureka-7001
第一步:在pom.xml中导入相关依赖:
<!--config-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
第二步:创建一个主启动类 EurekaServer_7001.java :
package com.hkl.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer //@EnableEurekaServer 服务端的启动类,可以接收别人注册进来
public class EurekaServer_7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaServer_7001.class,args);
}
}
第三步:配置 bootstarp.yaml 文件:【3344服务器搭建在前面的19.0文章里有说】
spring:
cloud:
config:
name: config-eureka #从远处读取的文件名称
label: master #从远程的那个分支读取
profile: dev #读取多文档配置文件中的哪一个环境
uri: http://localhost:3344 #从本地的哪一个中转站去访问到远程
配置application.yaml文件:【这里就配置一下项目的名称,让我们知道这是什么项目就行】
spring:
application:
name: springcloud-config-eureka-7001
好了到这里就配置完了,来测试一下
先启动3344服务器
然后去用3344端口访问一下远程的eureka配置文件看看能不能访问到码云远程仓库的配置文件
成功访问之后再启动springcloud-config-eureka-7001
然后去访问 localhost:7001 ,如果访问成功,就说明咱们的这个服务器已经成功的读取了远程的文件【因为我们在本地并没有给这个服务器配置端口号,而是在远程的配置文件里配置了7001端口】
思想理解:
就是说这些服务器干的事情都没有改变,module里除了配置文件改动了,其他的代码都原封不动,就只是读取配置的地方从本地读取变成了远程仓库读取。
291

被折叠的 条评论
为什么被折叠?



