SpringCloud(2)------将服务注册到Eureka
程序员文章站
2022-06-13 15:32:47
...
一、Eureka Client
Eureka client:不管是服务注册者还是服务消费者都算客户端
Eureka Server:提供服务注册和发现、注册中心
服务提供者将服务注册到注册中心以后才能被服务消费者发现
1.pom.xml依赖
服务将以客户端eureka client的形式注册到eureka server中。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
下边为完整的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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>client</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
</dependencies>
<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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
</project>
2.application.yml
###服务别名
spring:
application:
name: microservice-h3
eureka:
client:
service-url:
###当前服务注册到eureka
defaultZone: http://localhost:8100/eureka
#检索服务
fetch-registry: true
#将服务注册到eureka
register-with-eureka: true
#项目端口号
server:
port: 8001
3.编写接口
4.开启EurekaClient,添加@EnableEurekaClient注解
5.启动服务,将服务注册到注册中心(启动服务前需要启动注册中心eureka),如何大点eureka请查看SpringCloud(1)------搭建Eureka
1)打开eureka web页面,可以查看到服务已经注册到注册中心,此时便可让服务消费者消费。
~~完成
推荐阅读
-
SpringCloud之服务注册与发现Spring Cloud Eureka实例代码
-
SpringCloud-微服务的注册与发现Eureka
-
SpringCloud微服务实战:一、Eureka注册中心服务端
-
SpringCloud(二):服务的注册与发现(Eureka)
-
SpringCloud:1章 Eureka服务注册与发现
-
SpringCloud-服务注册与实现-Eureka创建服务注册中心(附源码下载)
-
springcloud之服务注册与发现(zookeeper注册中心)-Finchley.SR2版
-
每天学点SpringCloud(二):服务注册与发现Eureka
-
转:SpringCloud服务注册中心比较:Consul vs Zookeeper vs Etcd vs Eureka
-
SpringCloud - (八)Eureka高可用服务注册中心