Eureka是一个服务治理组件,它主要包括服务注册和服务发现,主要用来搭建服务注册中心。
Eureka 是一个基于 REST 的服务,用来定位服务,进行中间层服务器的负载均衡和故障转移;
Eureka是Netflix 公司开发的,Spring Cloud发现eureka很好使,因此将eureka封装到自己的模块中。
1、要使用eureka,首先要创建一个服务,eureka本身也是一个微服务
引入springcloud和eureka-server
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.4.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
这个地方有一个坑,我折腾了半天才启动成功。springboot和springcloud是有对应关系的
SpringCloud版本 |
SpringBoot版本 |
2021.0.1-SNAPSHOT |
Spring Boot >=2.6.4-SNAPSHOT and <2.7.0-M1 |
2021.0.0 |
Spring Boot >=2.6.1 and <2.6.4-SNAPSHOT |
2021.0.0-RC1 |
Spring Boot >=2.6.0-RC1 and <2.6.1 |
2021.0.0-M3 |
Spring Boot >=2.6.0-M3 and <2.6.0-RC1 |
2021.0.0-M1 |
Spring Boot >=2.6.0-M1 and <2.6.0-M3 |
2020.0.5 |
Spring Boot >=2.4.0.M1 and <2.6.0-M1 |
Hoxton.SR12 |
Spring Boot >=2.2.0.RELEASE and <2.4.0.M1 |
Hoxton.BUILD-SNAPSHOT |
Spring Boot >=2.2.0.BUILD-SNAPSHOT |
Hoxton.M2 |
Spring Boot >=2.2.0.M4 and <=2.2.0.M5 |
Greenwich.BUILD-SNAPSHO |
Spring Boot >=2.1.9.BUILD-SNAPSHOT and <2.2.0.M4 |
Greenwich.SR2 |
Spring Boot >=2.1.0.RELEASE and <2.1.9.BUILD-SNAPSHOT |
Greenwich.M1 |
Spring Boot >=2.1.0.M3 and <2.1.0.RELEASE |
Finchley.BUILD-SNAPSHOT |
Spring Boot >=2.0.999.BUILD-SNAPSHOT and <2.1.0.M3 |
Finchley.SR4 |
Spring Boot >=2.0.3.RELEASE and <2.0.999.BUILD-SNAPSHOT |
Finchley.RC2 |
Spring Boot >=2.0.2.RELEASE and <2.0.3.RELEASE |
Finchley.RC1 |
Spring Boot >=2.0.1.RELEASE and <2.0.2.RELEASE |
Finchley.M9 |
Spring Boot >=2.0.0.RELEASE and <=2.0.0.RELEASE |
Finchley.M7 |
Spring Boot >=2.0.0.RC2 and <=2.0.0.RC2 |
Finchley.M6 |
Spring Boot >=2.0.0.RC1 and <=2.0.0.RC1 |
Finchley.M5 |
Spring Boot >=2.0.0.M7 and <=2.0.0.M7 |
Finchley.M4 |
Spring Boot >=2.0.0.M6 and <=2.0.0.M6 |
Finchley.M3 |
Spring Boot >=2.0.0.M5 and <=2.0.0.M5 |
Finchley.M2 |
Spring Boot >=2.0.0.M3 and <2.0.0.M5 |
Edgware.SR5 |
1.5.20.RELEASE |
Edgware.SR5 |
1.5.16.RELEASE |
Edgware.RELEASE |
1.5.9.RELEASE |
Dalston.RC1 |
1.5.2.RELEASE |
为eureka服务增加注解说明
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
为eureka服务增加配置
server:
port: 9000
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
这里我使用9000端口作为eureka的服务端口
启动后可以看到eureka服务的展示页面,可以看到,这时候是没有服务注册进来的。
2、创建微服务注册到eureka服务
pom文件,简化版,其他springcloud和springboot的引用和eureka相同
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
服务启动类
@SpringBootApplication
@EnableEurekaClient
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
}
配置文件
server:
port: 1001
eureka:
instance:
prefer-ip-address: true
hostname: localhost
client:
service-url:
defaultZone: http://${eureka.instance.hostname}:9000/eureka/
spring:
application:
name: orderservice
这里我使用1001作为第一个微服务 端口号,下边使用1002创建另一个微服务,配置同理
启动后
可以看到,两个服务均已经成功注册到eureka服务。
3、创建另一个微服务去调用已经注册进去的服务,配置相同,这里只截图调用相关代码
@Autowired
RestTemplate restTemplate;
@RequestMapping("indexClient.do")
public @ResponseBody String indexClient() {
return restTemplate.getForEntity("http://orderservice/index.do", String.class).getBody();
}
使用的是RestTemplate,RestTemplate简化了发起 HTTP 请求以及处理响应的过程,并且支持 REST,类似于自己创建的http请求类。
这里有个小坑,如果clean服务后配置文件application.yml有可能会被从target中删除,这里需要手动复制到target中,不然会启动失败,而且配置的server端口不生效,谨记。
为了不至于学习资料丢失,代码已经上传至以下地址:
(3条消息) springboot整合eureka源代码,学习资料-Java文档类资源-CSDN文库