上一篇博客介绍了SpringCloud Config的简介、Config Server及Client的使用,在微服务开发中,是需要将config整合进其他模块里的,本篇博客就来介绍下eureka整合config以及服务器提供者整合config,需要用到git上的两个配置文件:
Config整合注册中心
先搞个配置文件(eureka_config.yml)上传到git仓库去,文件内容如下:
spring:
profiles:
active:
- dev
---
server:
port: 2004
context-path: /
spring:
profiles: dev
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
---
server:
port: 2005
context-path: /
spring:
profiles: test
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
新建microservice-eureka-server-config-2004项目用来测试,在pom.xml里加上相关依赖:
<!--config配置中心相关依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
然后在项目的resources目录下新建bootstrap.yml文件,文件内容如下:
spring:
application:
name: microservice-eureka-server-config
cloud:
config:
name: eureka_config
#配置config server地址
uri: http://configserver.test.com:4001
#级别
profile: test
#git中的分支(默认master)
label: master
项目的application.yml配置如下:
spring:
application:
name: microservice-eureka-server-config
在项目的启动类加上@EnableEurekaServer注解:
然后先启动microservice-config-server-4001项目,再启动microservice-eureka-server-config-2004项目,根据具体配置情况在浏览器的地址栏输入http://localhost:2004或http://localhost:2005进行测试,当配置为“dev”时,测试结果如下:
当配置为“test”时,测试结果如下:
测试结果如上说明成功读取远程git配置,eureka启动也OK
Config整合生产者
然后把服务提供者和config进行整合,并把服务提供者注册到eureka去;先搞个配置文件(provider_config.yml)上传到git仓库去,文件内容如下:
spring:
profiles:
active: dev
---
server:
port: 1007
context-path: /
#数据源配置
spring:
profiles: dev
application:
name: microservice-student
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/demosite1?useUnicode=true&characterEncoding=utf8
username: root
password: root
jpa:
hibernate:
ddl-auto: update
show-sql: true
eureka:
instance:
hostname: localhost
appname: microservice-student
instance-id: microservice-student:1007
prefer-ip-address: true
client:
service-url:
defaultZone: http://localhost:2004/eureka
info:
groupId: com.ue.microservice
artifactId: microservice-student-provider-config-1007
version: 1.0-SNAPSHOT
contacts: Tom
phone: 123456
---
server:
port: 1008
context-path: /
#数据源配置
spring:
profiles: test
application:
name: microservice-student
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/demosite1?useUnicode=true&characterEncoding=utf8
username: root
password: root
jpa:
hibernate:
ddl-auto: update
show-sql: true
eureka:
instance:
hostname: localhost
appname: microservice-student
instance-id: microservice-student:1008
prefer-ip-address: true
client:
service-url:
defaultZone: http://localhost:2004/eureka
info:
groupId: com.ue.microservice
artifactId: microservice-student-provider-config-1008
version: 1.0-SNAPSHOT
contacts: Tom
phone: 123456
新建microservice-student-provider-config项目,其完整的pom.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.ue</groupId>
<artifactId>microservice</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>microservice-student-provider-config</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</dependency>
<!--修改后立即生效,热部署-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!--添加注册中心Eureka相关配置-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!--actuator监控引入-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>com.ue</groupId>
<artifactId>microservice-common</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
然后在项目的resources目录下新建bootstrap.yml文件,文件内容如下:
spring:
application:
name: microservice-student-provider-config
cloud:
config:
name: provider_config
#配置config server地址
uri: http://configserver.test.com:4001
#级别
profile: test
#git中的分支(默认master)
label: master
项目的application.yml配置如下:
spring:
application:
name: microservice-student-provider-config
项目的启动类代码如下:
package com.ue;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@SpringBootApplication
@EntityScan("com.ue.entity")
@EnableJpaRepositories("com.ue.repository")
@EnableEurekaClient
public class MicroserviceStudentProviderConfigApplication {
public static void main(String[] args) {
SpringApplication.run(MicroserviceStudentProviderConfigApplication.class, args);
}
}
然后先启动microservice-config-server-4001项目,再启动microservice-eureka-server-config-2004项目,最后再启动microservice-student-provider-config项目,然后访问2004注册中心查看生产者的注册情况:
以上结果说明生产者成功注册到注册中心了
总结
将springcloud config配置中心的用途与单体项目的配置切换进行对比,得到如下看法: