欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

2.2注册中心:Eureka

程序员文章站 2023-04-05 16:06:33
pom.xml配置

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelversion>4.0.0</modelversion>

<groupid>com.demo</groupid>
<artifactid>eurekaserver</artifactid>
<version>0.0.1-snapshot</version>
<packaging>jar</packaging>

<name>eurekaserver</name>
<description>demo project for spring boot</description>

<parent>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-parent</artifactid>
<version>1.5.2.release</version>
<relativepath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceencoding>utf-8</project.build.sourceencoding>
<project.reporting.outputencoding>utf-8</project.reporting.outputencoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<!--eureka server -->
<dependency>
<groupid>org.springframework.cloud</groupid>
<artifactid>spring-cloud-starter-eureka-server</artifactid>
</dependency>

<!-- spring boot test-->
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-test</artifactid>
<scope>test</scope>
</dependency>
</dependencies>

<dependencymanagement>
<dependencies>
<dependency>
<groupid>org.springframework.cloud</groupid>
<artifactid>spring-cloud-dependencies</artifactid>
<version>dalston.rc1</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>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>


</project>

启动一个服务注册中心,只需要一个注解@enableeurekaserver,这个注解需要在springboot工程的启动application类上加:


@enableeurekaserver
@springbootapplication
public class eurekaserverapplication {

public static void main(string[] args) {
springapplication.run(eurekaserverapplication.class, args);
}
}

 

eureka是一个高可用的组件,它没有后端缓存,每一个实例注册之后需要向注册中心发送心跳(因此可以在内存中完成),在默认情况下erureka server也是一个eureka client ,必须要指定一个 server。eureka server的配置文件application.yml:

server:
port: 8765

eureka:
instance:
hostname: localhost
client:
registerwitheureka: false
fetchregistry: false
serviceurl:
defaultzone: http://${eureka.instance.hostname}:${server.port}/eureka/

通过eureka.client.registerwitheureka:false和fetchregistry:false来表明自己是一个eureka server.

eureka server 是有界面的,启动工程,打开浏览器访问:
 

因为没有注册服务当然不可能有服务被发现了,所以(no application available 没有服务被发现)

第二步创建一个服务提供者 (eureka client)

当client向server注册时,它会提供一些元数据,例如主机和端口,url,主页等。eureka server 从每个client实例接收心跳消息。 如果心跳超时,则通常将该实例从注册server中删除。

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelversion>4.0.0</modelversion>

<groupid>com.forezp</groupid>
<artifactid>service-hi</artifactid>
<version>0.0.1-snapshot</version>
<packaging>jar</packaging>

<name>service-hi</name>
<description>demo project for spring boot</description>

<parent>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-parent</artifactid>
<version>1.5.2.release</version>
<relativepath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceencoding>utf-8</project.build.sourceencoding>
<project.reporting.outputencoding>utf-8</project.reporting.outputencoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupid>org.springframework.cloud</groupid>
<artifactid>spring-cloud-starter-eureka</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>
</dependencies>

<dependencymanagement>
<dependencies>
<dependency>
<groupid>org.springframework.cloud</groupid>
<artifactid>spring-cloud-dependencies</artifactid>
<version>dalston.rc1</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>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>


</project>

通过注解@enableeurekaclient 表明自己是一个eurekaclient.

如:

@value("${server.port}")

string port;

@requestmapping("/hi")

public string home(@requestparam string name) {

return "hi "+name+",i am from port:" +port;

}

仅仅@enableeurekaclient是不够的,还需要在配置文件中注明自己的服务注册中心的地址,application.yml配置文件如下:

eureka:
client:
serviceurl:
defaultzone: http://localhost:8761/eureka/
server:
port: 8762
spring:
application:
name: service-hi

需要指明spring.application.name,这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个name 。