作者:尚福惠珠綺裕 | 来源:互联网 | 2023-06-07 01:39
代码信息本篇文章涉及代码版本组件版本SpringBoot2.0.8.RELEASESpringCloudFinchley.SR1本篇文章涉及应用应用说明base-eureka服务发
代码信息
本篇文章涉及代码版本
组件 | 版本 |
---|
Spring Boot | 2.0.8.RELEASE |
Spring Cloud | Finchley.SR1 |
本篇文章涉及应用
应用 | 说明 |
---|
base-eureka | 服务发现 |
base-config-service | 配置中心服务 |
base-config-client | 配置中心客户端 |
服务的配置
在微服务中为了实现逻辑的拆分通常业务功能会被拆分成多个不同的应用进行独立维护,而当系统中的应用变得非常多的时候,需要维护的配置也会变得更多,为了方便微服务的管理,我们可以使用spring cloud的config组件。
为了实现并且测试配置中心是否完成并且可用我们需要两个应用config-server配置服务和config-client调用服务的客户端。
配置中心服务端
构建maven依赖
其他配置可以参照client应用,需要额外加入的依赖
<dependency><groupId>org.springframework.cloudgroupId><artifactId>spring-cloud-config-serverartifactId>dependency>
参数配置application.yml
spring:application:name: base-config-servicecloud:config:server:git:uri: https://gitee.com/daifylearn/cloud-configsearch-paths: /config,/base-config-client
server:port: 8700eureka:client:service-url: defaultZone: http://localhost:8000/eureka/
logging:file: ${spring.application.name}.log
代码编写
主类需要配置&#64;EnableConfigServer注解
&#64;SpringBootApplication
&#64;EnableEurekaClient
&#64;EnableConfigServer
public class ConfigServiceApplication {public static void main(String[] args) {SpringApplication.run(ConfigServiceApplication.class, args);}
}
配置中心客户端
构建maven依赖
其他配置可以参照client应用&#xff0c;需要额外加入的依赖&#xff0c;注意和服务端不同
<dependency><groupId>org.springframework.cloudgroupId><artifactId>spring-cloud-config-clientartifactId>dependency>
参数配置bootstrap.yml
注意此时为了保证配置第一时间加载进容器中&#xff0c;需要使用bootstrap来进行配置&#xff0c;虽然application是spring boot项目的配置参数文件&#xff0c;但是在application调用之前还有一个比更早加载的配置文件bootstrap
spring:application:name: base-config-clientcloud:config:profile: testlabel: masteruri: http://localhost:8700
参数配置application.yml
理论上说因为配置中心可以做任何配置&#xff0c;所以实际上客户端可以完全舍弃application配置
server:port: 8710eureka:client:service-url: defaultZone: http://localhost:8000/eureka/
logging:file: ${spring.application.name}.log
代码编写
主类需要任何其他配置
&#64;SpringBootApplication
&#64;EnableEurekaClient
public class ConfigClientApplication {public static void main(String[] args) {SpringApplication.run(ConfigClientApplication.class, args);}
}
测试用的代码
我在配置文件中设置了一个config.name的参数根据这个此参数判断是否获取配置
&#64;RestController
&#64;RequestMapping("config")
public class ConfigController {&#64;Value("${config.name}")private String profile;&#64;GetMapping("/name")public String getProfile() {return this.profile;}
}
配置中心
我的配置中心目录是这样的
配置目录的定位
spring config使用{name}-{profiles}类似这样的文件定位方式。所以上面客户端名字为base-config-client而激活的文件为test&#xff0c;所以最终会定位到base-config-client-test文件。
测试
首先启动服务端项目然后我们尝试通过路径访问http://localhost:8700/base-config-client/test
浏览器返回内容
<Environment><name>base-config-clientname><profiles><profiles>testprofiles>profiles><label/><version>f4429f2d4ceb4c1012f229b7839598c7425b16d8version><state/><propertySources><propertySources><name>https://gitee.com/daifylearn/cloud-config/base-config-client/base-config-client-test.ymlname><source><config.name>test1config.name>source>propertySources>propertySources>
Environment>
这个时候可以看到服务端已经拉取到配置中心的配置了。
然后启动客户端项目然后请求http://localhost:8710/config/name
然后看客户端是否拉取到服务端的数据。
返回内容。
这个时候一个简单的config配置中心就已经完成&#xff0c;并且测试可用了。
本篇文章并未贴出所有代码&#xff0c;涉及的源码下载地址&#xff1a;https://gitee.com/daifylearn/cloud-learn
ps.上述的所有项目都是可以成功运行的。但是在后期为了实现每个应用端口尽量不冲突会有些许调整&#xff0c;而后续某次作死调整结构和名称可能会导致部分项目无法运行o(╯□╰)o&#xff0c;如果发现请留言我进行修改。