eureka 介绍
eureka提供基于rest的服务,在集群中主要用于服务管理。eureka提供了基于java语言的客户端组件,客户端组件实现了负载均衡的功能,为业务组件的集群部署创造了条件。使用该框架,可以将业务组件注册到eureka容器中,这些业务组件可进行集群部署,eureka主要维护这些服务的列表并自动检查它们的状态。
程序结构
创建eureka server
maven依赖
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<dependencymanagement>
<dependencies>
<dependency>
<groupid>org.springframework.cloud</groupid>
<artifactid>spring-cloud-dependencies</artifactid>
<version>dalston.sr1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencymanagement>
<dependencies>
<dependency>
<groupid>org.springframework.cloud</groupid>
<artifactid>spring-cloud-starter-eureka-server</artifactid>
</dependency>
</dependencies>
|
更改spring boot 启动端口 在application.yml
|
1
2
|
server:
port: 8761
|
开启eureka服务注解 @enableeurekaserver
|
1
2
3
4
5
6
7
8
|
@enableeurekaserver
@springbootapplication
public class ekserverapplication {
public static void main(string[] args) {
new springapplicationbuilder(ekserverapplication.class).run(args);
}
}
|
启动springboot
|
1
2
3
4
|
[thread-11] o.s.c.n.e.server.eurekaserverbootstrap: initialized server context
[main] s.b.c.e.t.tomcatembeddedservletcontainer: tomcat started on port(s): 8761 (http)
[main] .s.c.n.e.s.eurekaautoserviceregistration: updating port to 8761
[main] c.b.firstekserver.ekserverapplication: started ekserverapplication in 8.594 seconds (jvm running for 9.523)
|
启动期间会出现一个无法连接到服务器的异常 这个是由于eureka在启动的时候会把自己当作一个客户端去服务器抓取注册信息
com.netflix.discovery.shared.transport.transportexception: cannot execute request on any known server
增加如下配置启动时便不会再出现该异常
|
1
2
3
4
|
eureka:
client:
registerwitheureka: false
fetchregistry: false
|
registerwitheureka 声明是否将自己的信息注册到eureka服务器,默认值为true。
fetchregistry 声明是否到eureka服务器中抓取注册信息,默认值为true。
在浏览器中访问 http://localhost:8761 查看eureka控制台 输入图片说明
创建服务提供者
依赖
|
1
2
3
4
5
6
7
8
9
10
11
12
|
<dependency>
<groupid>org.springframework.cloud</groupid>
<artifactid>spring-cloud-starter-config</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-ribbon</artifactid>
</dependency>
|
在 application.yml 中配置端口、eureka实例名称和eureka服务地址
|
1
2
3
4
5
6
7
8
9
10
11
|
server:
port: 8080
spring:
application:
name: ek-provider
eureka:
instance:
hostname: localhost
client:
serviceurl:
defaultzone: http://localhost:8761/eureka/
|
创建一个 rest 服务
|
1
2
3
4
5
6
7
8
|
@restcontroller
public class hellocontroller {
@requestmapping("/hello")
public string hello(httpservletrequest request) {
return "hello:" + request.getrequesturl();
}
}
|
开启eureka客户端注解 @enableeurekaserver
|
1
2
3
4
5
6
7
8
|
@enableeurekaclient
@springbootapplication
public class ekproviderapplication {
public static void main(string[] args) {
new springapplicationbuilder(ekproviderapplication.class).run(args);
}
}
|
启动之后在 eureka 控制台可以看到服务提供者已经在 eureka 中注册
创建服务调用者
依赖
|
1
2
3
4
5
6
7
8
9
10
11
12
|
<dependency>
<groupid>org.springframework.cloud</groupid>
<artifactid>spring-cloud-starter-config</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-ribbon</artifactid>
</dependency>
|
在 application.yml 中配置端口、eureka实例名称和eureka服务地址
|
1
2
3
4
5
6
7
8
9
10
11
|
server:
port: 9000
spring:
application:
name: ek-invoke
eureka:
instance:
hostname: localhost
client:
serviceurl:
defaultzone: http://localhost:8761/eureka/
|
编写一个 rest 服务 调用服务提供者的 “/hello”
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@restcontroller
@configuration
public class invokecontroller {
@bean
@loadbalanced
public resttemplate getresttemplate() {
return new resttemplate();
}
@requestmapping("/invoke")
public string invoke() {
resttemplate resttemplate = getresttemplate();
return resttemplate.getforobject("http://ek-provider/hello", string.class);
}
}
|
在传统模式中,我们通常会用apache中的httpclient来调用 rest 服务,在这里我们使用 spring 提供调用 rest 服务的组件 resttemplate。 resttemplate 本身并不具备调用分布式服务的能力,但是resttemplate的bean被@loadbalanced注解修饰后,这个resttemplate实例就具有访问分布式服务的能力,这得益于 spring 为其提供的各种拦截器
开启eureka客户端注解 @enableeurekaserver
|
1
2
3
4
5
6
7
8
|
@enableeurekaclient
@springbootapplication
public class ekinvokeapplication {
public static void main(string[] args) {
new springapplicationbuilder(ekinvokeapplication.class).run(args);
}
}
|
启动之后在 eureka 控制台可以看到服务调用者已经在 eureka 中注册
之后在浏览器访问服务调用者的 “invoke” 接口 返回如下
总结
eureka 服务器 通过心跳链接来维护最新的注册信息,这些注册信息都保存在内存中。
eureka 服务提供者 主要进行:
- 向 eureka 服务器注册服务
- 发送心跳到 eureka服务器,使 eureka 服务器注册信息保持最新
- 向服务器获取最新的注册列表,通常 eureka 客户端既是服务提供者也是服务调用者,但其主要职责为服务提供。
eureka 服务调用者 主要进行:
- 向 eureka 服务器注册服务
- 发送心跳到 eureka服务器,使 eureka 服务器注册信息保持最新
- 向服务器获取最新的注册列表,通常 eureka 客户端既是服务提供者也是服务调用者,但其主要职责为发现与调用。
源码地址:https://github.com/xc564864894/springcloud/tree/master/eureka(%e4%b8%80)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:https://my.oschina.net/u/3444403/blog/1627531
相关文章
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
2025-05-25 83
-
2025-05-25 66
-
2025-05-26 79
-
关于安装linux redhat后无法使用yum命令安装gcc-c++问题的解决过程
2025-05-27 54 -
2025-05-29 88





