我们现在搞个实例来演示下,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: 2005 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:
<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>
bootstrap.yml
spring: application: name: microservice-eureka-server-config cloud: config: name: eureka_config uri: http://configserver.javaxl.com:4001 # 配置configserver地址 profile: dev # 级别 label: master # 分支 git中 默认master
application.yml
spring: application: name: microservice-eureka-server-config
启动类:
package com.javaxl.microserviceeurekaserverconfig2004; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class MicroserviceEurekaServerConfig2004Application { public static void main(String[] args) { SpringApplication.run(MicroserviceEurekaServerConfig2004Application.class, args); } }
我们启动 microservice-config-server-4001
再启动 microservice-eureka-server-config-2004
测试连接
http://eureka2001.javaxl.com:2004/
说明成功读取远程Git配置,然后eureka启动OK;
然后我们就是把服务提供者和config整合,把服务提供者注册到eureka;
我们搞个配置provider_config.yml,push到远程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/mybatis_ssm?useUnicode=true&characterEncoding=utf8 username: mybatis_ssm password: xiaoli jpa: hibernate: ddl-auto: update show-sql: true eureka: instance: hostname: localhost #eureka客户端主机实例名称 appname: microservice-student #客户端服务名 instance-id: microservice-student:1007 #客户端实例名称 prefer-ip-address: true #显示IP client: service-url: defaultZone: http://localhost:2004/eureka #把服务注册到eureka注册中心 info: groupId: com.javaxl.testSpringcloud artifactId: microservice-student-provider-config-1007 version: 1.0-SNAPSHOT userName: http://47.100.191.44 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/mybatis_ssm?useUnicode=true&characterEncoding=utf8 username: mybatis_ssm password: xiaoli jpa: hibernate: ddl-auto: update show-sql: true eureka: instance: hostname: localhost #eureka客户端主机实例名称 appname: microservice-student #客户端服务名 instance-id: microservice-student:1008 #客户端实例名称 prefer-ip-address: true #显示IP client: service-url: defaultZone: http://localhost:2004/eureka #把服务注册到eureka注册中心 info: groupId: com.javaxl.testSpringcloud artifactId: microservice-student-provider-config-1008 version: 1.0-SNAPSHOT userName: http://47.100.191.44 phone: 123456
新建module: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.javaxl</groupId> <artifactId>testSpringcloud</artifactId> <version>1.0-SNAPSHOT</version> </parent> <artifactId>microservice-student-provider-config</artifactId> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>com.javaxl</groupId> <artifactId>microservice-common</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-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> <dependency> <groupId>com.javaxl</groupId> <artifactId>microservice-common</artifactId> <version>1.0-SNAPSHOT</version> <scope>compile</scope> </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> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
bootstrap.yml:
spring: application: name: microservice-student-provider-config cloud: config: name: provider_config uri: http://configserver.javaxl.com:4001 # 配置configserver地址 profile: dev # 级别 label: master # 分支 git中 默认master
application.yml
spring: application: name: microservice-student-provider-config
其他类文件从 原先的服务提供者里直接复制一份即可,这里不贴了;
启动下这个项目;
说明成功注册到服务注册中心了;
over......
备案号:湘ICP备19000029号
Copyright © 2018-2019 javaxl晓码阁 版权所有