Config整合Eureka
我们现在搞个实例来演示下,eureka整合config以及服务器提供者整合config,这样大伙可以举一反一,方便理解;
首先是eureka整合config
我们先搞个配置文件到git;
eureka_config.yml
spring: profiles: active: - dev --- server: port: 2004 context-path: / spring: profiles: dev eureka: instance: hostname: localhost client: register-with-eureka: false #false 由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己。 fetch-registry: false #false 由于注册中心的职责就是维护服务实例,它并不需要去检索服务,所以也设置为false service-url: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #设置与Eureka注册中心交互的地址,查询服务和注册服务用到 #设置与Eureka注册中心交互的地址,查询服务和注册服务用到 --- server: port: 2004 context-path: / spring: profiles: test eureka: instance: hostname: localhost client: register-with-eureka: false #false 由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己。 fetch-registry: false #false 由于注册中心的职责就是维护服务实例,它并不需要去检索服务,所以也设置为false service-url: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #设置与Eureka注册中心交互的地址,查询服务和注册服务用到 #设置与Eureka注册中心交互的地址,查询服务和注册服务用到
新建module microservice-eureka-server-config-2004
pom.xml:
<dependencies> <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> <!-- 修改后立即生效,热部署 --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies>
bootstrap.yml
spring: application: name: microservice-eureka-server-config cloud: config: name: eureka_config uri: http://configserver.java1234.com:4001 # 配置configserver地址 profile: dev # 级别 label: master # 分支 git中 默认master
application.yml
spring: application: name: microservice-eureka-server-config
启动类:
package com.java1234; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class EurekaApplication_2004 { public static void main(String[] args) { SpringApplication.run(EurekaApplication_2004.class, args); } }
我们启动 microservice-config-server-4001
再启动 microservice-eureka-server-config-2004
浏览器:http://eureka2001.java1234.com:2004/
说明成功读取远程Git配置,然后eureka启动OK;
然后我们就是把服务提供者和config整合,把服务提供者注册到eureka;
我们搞个配置provider_config.yml,push到远程GIT;
spring: profiles: active: - dev --- server: port: 1004 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/db_springcloud username: root password: 123456 jpa: hibernate: ddl-auto: update show-sql: true eureka: instance: hostname: localhost #eureka客户端主机实例名称 appname: microservice-student #客户端服务名 instance-id: microservice-student:1004 #客户端实例名称 prefer-ip-address: true #显示IP client: service-url: defaultZone: http://localhost:2004/eureka #把服务注册到eureka注册中心 info: groupId: $project.groupId$ artifactId: $project.artifactId$ version: $project.version$ 负责人: 张三 联系电话: 110 --- server: port: 1004 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/db_springcloud2 username: root password: 123456 jpa: hibernate: ddl-auto: update show-sql: true eureka: instance: hostname: localhost #eureka客户端主机实例名称 appname: microservice-student #客户端服务名 instance-id: microservice-student:1004 #客户端实例名称 prefer-ip-address: true #显示IP client: service-url: defaultZone: http://localhost:2004/eureka #把服务注册到eureka注册中心 info: groupId: $project.groupId$ artifactId: $project.artifactId$ version: $project.version$ 负责人: 张三 联系电话: 110
新建module:microservice-student-provider-config-1004
pom.xml:
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>com.java1234.springcloud</groupId> <artifactId>microservice-common</artifactId> <version>${project.version}</version> </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-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </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</artifactId> </dependency> <!-- 修改后立即生效,热部署 --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies>
bootstrap.yml:
spring: application: name: microservice-student-provider-config cloud: config: name: provider_config uri: http://configserver.java1234.com:4001 # 配置configserver地址 profile: dev # 级别 label: master # 分支 git中 默认master
application.yml
spring: application: name: microservice-student-provider-config
其他类文件从 原先的服务提供者里直接复制一份即可,这里不贴了;
启动下这个项目;
说明成功注册到服务注册中心了;
下一篇:Config配置搜索路径