Spring-Cloud组件之eureka
eureka是什么?
eureka是Netflix的子模块之一,也是一个核心的模块。是一个基于REST的服务。
eureka里有2个组件,一个是EurekaServer(一个独立的项目) 这个是用于定位服务以实现中间层服务器的负载平衡和故障转移,另一个便是EurekaClient(我们的微服务) 它是用于与Server交互的,可以使得交互变得非常简单:只需要通过服务标识符即可拿到服务。
Eureka 采用了 C-S 的设计架构。Eureka Server 作为服务注册功能的服务器,它是服务注册中心。
而系统中的其他微服务,使用 Eureka 的客户端连接到 Eureka Server并维持心跳连接。这样系统的维护人员就可以通过 Eureka Server 来监控系统中各个微服务是否正常运行。SpringCloud 的一些其他模块(比如Zuul)就可以通过 Eureka Server 来发现系统中的其他微服务,并执行相关的逻辑
角色关系图
废话不多说,还是来撸代码
eureka客户端 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">
<parent>
<artifactId>springcloud</artifactId>
<groupId>com.cloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>eureka3000</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
application.yml
server:
port: 3000
eureka:
server:
enable-self-preservation: false #关闭自我保护机制
eviction-interval-timer-in-ms: 4000 #设置清理无效节点时间间隔 (单位毫秒,默认是 60*1000)
instance:
hostname: localhost
lease-expiration-duration-in-seconds: 3 #服务失效时间,Eureka多长时间没收到服务的renew操作,就剔除该服务,默认90秒
client:
# register-with-eureka: false #不把自己作为一个客户端注册到自己身上
# fetch-registry: false #不需要从服务端获取注册信息(因为在这里自己就是服务端,而且已经禁止自己注册了)
service-url:
defaultZone: http://localhost:${server.port}/eureka/ # eureka服务器的地址(/eureka/ 这个是固定值)
spring:
application:
name: eureka3000
启动类
@SpringBootApplication
@EnableEurekaServer
public class Eureka3000SpringApplication {
public static void main(String[] args) {
SpringApplication.run(Eureka3000SpringApplication.class);
}
}
好了,客户端这样就可以了,接下来弄一下服务端吧,先写一个user的服务端
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">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>user</artifactId>
<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>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
application.yml
server:
port: 6001
eureka:
client:
service-url:
defaultZone: http://localhost:3000/eureka/ #Eureka服务端提供的注册地址
instance:
instance-id: user #此实例注册到Eureka服务端的唯一的实例ID
prefer-ip-address: true #是否显示IP
lease-renewal-interval-in-seconds: 10 #Eureka 客户端需要多长时间发送心跳给Eyreka服务器,表明他还活着 ,默认为30 秒 (与下面配置的单位都是秒)
lease-expiration-duration-in-seconds: 30 #Eureka 服务器在接收到实例的最后一次发出的心跳后,需要等待多久才可以将此实例删除,默认为90秒
spring:
application:
name: server-user #此实例注册到Eureka服务端的name
启动类
@SpringBootApplication
@EnableEurekaClient
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class);
}
}
其实这样就可以启动两个项目了,但是呢 为了我们能够看到效果,我们再继续写一点东西!
编写一个AppConfig类
@Configuration
@ComponentScan("com")
public class AppConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
UserController.java
@RestController
public class UserController {
private final static String POWER_URL="http://SERVER-POWER";
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/getUser")
public Object getUser(){
Map<String,Object> map = new HashMap<>();
map.put("key","user");
return map;
}
@RequestMapping("/getPower")
public Object getPower(){
return restTemplate.getForObject(POWER_URL+"/getPower.do",Object.class);
}
}
user服务端到此结束啦。我们再写一个power服务端吧:其他跟user没啥区别,就是在端口改一下,然后controller如下:
UserController.java
@SpringBootApplication
@EnableEurekaClient
public class PowerApplication {
public static void main(String[] args) {
SpringApplication.run(PowerApplication.class);
}
}
好啦 接下来我们启动一下~
启动成功后 浏览器访问:http://localhost:3000/
user 和 power已经注册进来了!
浏览器请求:http://localhost:6001/getUser
浏览器请求:http://localhost:9999/getPower.do
浏览器请求:http://localhost:6001/getPower
好了,这个算是完成了吧。但是我们再真正项目的时候可不止部署一个EurakaServer的,一般都会部署很多个EurakeServer,接下来我们稍微改一下吧。
先去改一下我们本地环境的一个文件。C:\Windows\System32\drivers\etc\hosts
在最后面加上这个。
192.168.9.202 1124abc.com
127.0.0.1 eureka3000.com
127.0.0.1 eureka3001.com
127.0.0.1 eureka3002.com
拷贝EurekaServer项目两份,稍微改一下配置文件,这里不过多解释。
就是eureka3001.com 的 在service-url: 这里就需要填写另外两台。不需要填写自己的,因为这个是相互注册,把自己的填上意义不大。
server:
port: 3001
eureka:
server:
enable-self-preservation: false #关闭自我保护机制
eviction-interval-timer-in-ms: 4000 #设置清理间隔 (单位毫秒,默认是 60*1000)
instance:
hostname: eureka3001.com
client:
# register-with-eureka: false #不把自己作为一个客户端注册到自己身上
# fetch-registry: false #不需要从服务端获取注册信息(因为在这里自己就是服务端,而且已经禁止自己注册了)
service-url:
defaultZone: http://eureka3000.com:3000/eureka,http://eureka3002.com:3002/eureka
我们再来改一下user和power的配置文件
sserver:
port: 6001
eureka:
client:
service-url:
defaultZone: http://eureka3000.com:3000/eureka,http://eureka3001.com:3001/eureka,http://eureka3002.com:3002/eureka #Eureka服务端提供的注册地址
instance:
instance-id: user #此实例注册到Eureka服务端的唯一的实例ID
prefer-ip-address: true #是否显示IP
lease-renewal-interval-in-seconds: 10 #Eureka 客户端需要多长时间发送心跳给Eyreka服务器,表明他还活着 ,默认为30 秒 (与下面配置的单位都是秒)
lease-expiration-duration-in-seconds: 30 #Eureka 服务器在接收到实例的最后一次发出的心跳后,需要等待多久才可以将此实例删除,默认为90秒
spring:
application:
name: server-user #此实例注册到Eureka服务端的name
这里defaultZone 填了三个eurekaserver的地址,其实填一个其他两台服务器也能收到注册信息的,但还是需要把所有都填上,防止你填写的那台服务器挂了后影响这个服务注册了。
然后我们就可以启动了。
这个就算是Eureka的高可用吧。
各位一定要注意版本啊,可以去官网查看版本兼容的。
好啦 这是我的第一篇博客,以前都是以word文档或者有道云笔记去做记录的。以后我一定会坚持写博客的,如果各位可爱的小宝贝们发现问题了记得留言说一下哦。毕竟我也是一个小菜鸡,还在这条大道上慢慢的摸索前进。