Spring Cloud -- 配置Eureka注册中心,注册服务
2019-05-27 11:52
585 查看
1.配置Eureka注册中心:
1) 首先在工程中创建一个模块(Module),用于创建Eureka注册中心,这里命名为eureka-server:
2) 然后,引入依赖(可以借助spring初始化器引入cloud service):
<!-- 指定springcloud版本 --> <properties> <java.version>1.8</java.version> <spring-cloud.version>Edgware.SR5</spring-cloud.version> </properties>
<!-- 服务提供者 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
<!-- 配置使用springcloud --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
3) 配置Eureka注册中心:
# 注册中心的端口号 server: port: 8761 # 注册中心的ip eureka: instance: hostname: localhost client: # 检索服务选项,当设置为True(默认值)时,会进行服务检索,注册中心不负责检索服务 register-with-eureka: false # 服务注册中心也会将自己作为客户端来尝试注册自己,为true(默认)时自动生效 fetch-registry: false service-url: # 重点!配置注册中心的地址 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
4)开启注册中心并运行:
package com.id0304.eurekaserver; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer //创建注册中心 @SpringBootApplication public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
5) 进入注册中心:
2.注册服务提供者:
1) 引入依赖:
<!-- 服务提供者--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <!-- Eureka客户端配置,注册提供者要加 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
当然别忘了配置springcloud:
<properties> <java.version>1.8</java.version> <spring-cloud.version>Edgware.SR5</spring-cloud.version> </properties>
<!-- 配置使用springcloud --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
2) 配置服务提供者:
spring: application: name: dictionary # 配置提供者的名称 server: port: 8081 # 这里配置服务提供者的端口 eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ # 注意,这里的地址是注册中心的地址
3) 注册并启动服务提供者
package com.id0304.dictionary; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient //注册该服务进Eureka,成为服务提供者 public class DictionaryApplication { public static void main(String[] args) { SpringApplication.run(DictionaryApplication.class, args); } }
4) 查看注册中心:
注册中心就会多出一个服务
附上Eureka服务端,Eureka生产者客户端配置文件和依赖:
Eureka服务端:
<!-- spring boot begin --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.1.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>2.1.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- spring boot end --> <!-- spring cloud begin--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.SR1</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix-eureka-server</artifactId> <version>2.1.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> <version>2.1.1.RELEASE</version> </dependency> <!-- spring cloud end-->
spring: application: name: aweather-eureka # 注册中心的端口号 server: port: 8761 # 注册中心的ip eureka: instance: hostname: localhost client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
@SpringBootApplication @EnableEurekaServer
Eureka生产者客户端:
<!-- spring boot begin --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.1.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>2.1.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- spring boot end --> <!-- spring cloud begin--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.SR1</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix-eureka-server</artifactId> <version>2.1.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix-eureka-client</artifactId> <version>2.1.1.RELEASE</version> </dependency> <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-config-server</artifactId> <version>2.1.1.RELEASE</version> </dependency> <!-- spring cloud end-->
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/
@SpringBootApplication @EnableEurekaClient
相关文章推荐
- spring cloud 服务注册中心eureka高可用集群搭建
- 基于springboot 2.0.1.RELEASE 的Spring Cloud Eureka服务连不上注册中心
- 【Spring Cloud】Eureka服务注册中心搭建
- springcloud之eureka服务注册中心
- 【微服务架构】SpringCloud之Eureka(注册中心集群篇)(三)
- 学习笔记系列之SpringCloud | 第一章 服务注册中心(Eureka)
- Spring Cloud Eureka 服务关闭但是未从注册中心删除 自我保护机制
- spring cloud快速入门教程(二)服务注册中心Eureka
- 【Spring Cloud】Eureka服务注册中心搭建
- spring cloud配置注册中心显示服务的ip地址和端口
- (四)SpringBoot+SpringCloud —— Eureka注册中心的机制与配置
- Spring Cloud Eureka 服务注册与发现中心(一)
- 【Spring Cloud】Eureka服务注册中心搭建
- SpringCloud之高可用的服务注册中心(Eureka) |第八章 -yellowcong
- Spring Cloud Eureka 入门 (一)服务注册中心详解 「Spring Cloud Eureka 入门系列」 Spring Cloud Eureka 入门 (一)服务注册中心详解 Spr
- SpringCloud的服务注册中心(三) - 进一步了解 Eureka
- Spring Cloud(九)高可用的分布式配置中心 Spring Cloud Config 集成 Eureka 服务
- 【Spring Cloud】Eureka服务注册中心搭建
- 【微服务架构】SpringCloud之Eureka(注册中心集群篇)(三)
- springCloud-eureka注册中心集群+服务集群demo