您的位置:首页 > 其它

Eureka:服务治理(入门篇)

2019-03-22 14:12 411 查看

前言

近来微服务越来越火,出于探究的目的,初次接触了Spring Cloud(基于Spring Boot实现的微服务架构开发工具),Spring cloud体系中比较常用的组件=Eureka(服务注册中心)+Feign(生产者)+client(消费者/服务)+zuul(网关路由)+Hystrix(熔断降级)+kafka(统一日志服务器)

服务治理:Spring Cloud Eureka

服务治理是微服务架构中最为核心和基础的模块,主要用来实现各个微服务实例的自动化注册与发现

基础架构

服务治理的基础架构包括三个核心元素:

  • 服务注册中心:Eureka提供的服务端,提供服务的注册与发现的功能,也就是我们即将实现的eureka-service

  • 服务提供者:提供服务的工程,可以是Spring Boot应用,也可以是其他技术平台,遵守Eureka的通信机制,将自己提供的服务注册到注册中心,也就是我们即将实现的eureka-client应用

  • 服务消费者:消费者应用从服务注册中心获取服务列表,知晓如何调用其所需的服务,可以使用Rabbon或者Feign实现

Eureka服务端/服务注册中心

搭建服务注册中心,创建一个基础的Spring boot工程,命名为eureka-server,在pom文件引入必要的依赖:

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</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>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

通过@EnableEurekaServer 注解 启动一个服务注册中心提供给其他应用

/**
* 注册中心
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}

配置application.yml,代码如下:

server:
port: 8761

eureka:
instance:
prefer-ip-address: true # 允许使用ip地址定义注册中心地址,默认false
hostname: localhost
client:
registerWithEureka: false  # 代表不向注册中心注册自己
fetchRegistry: false   # 由于注册中心的职责就是维护服务实例,不必去检索服务
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

spring:
application:
name: eurka-server

启动项目,访问:http://localhost:8761/

Eureka Client/注册服务提供者

接下来让我们把提供服务的工程加入到服务注册中心,创建服务提供者工程,pom文件配置如下:

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

application.yml配置如下:

server:
port: 8762
spring:
application:
name: service-hi  # 为服务命名
eureka:
client:
serviceUrl:
defaultZone:  http://localhost:8761/eureka  # 指定服务注册中心地址

在主类中添加@EnableEurekaClient注解;( 注:使用@EnableEurekaClient的情景,就是在服务治理采用eureka作为注册中心的时候 )

@SpringBootApplication
@EnableEurekaClient
@RestController
public class EurekaClientApplication {

public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}

@Value("${server.port}")
String port;

@RequestMapping("/hi")
public String home(@RequestParam(value = "name", defaultValue = "lucas") String name) {
return "hi " + name + " ,i am from port:" + port;
}
}

本人偷懒了,demo代码都写在启动类上,然后分别启动服务注册中心工程和服务端工程
服务注册中心打印注册信息,表示服务注册成功

参考资料

https://www.geek-share.com/detail/2702646617.html
http://projects.spring.io/spring-cloud/spring-cloud.html#_spring_cloud_netflix ( 官方文档 )
电子书:Spring Cloud微服务实战
不得不说,方志朋先生的教程还是非常符合入门者学习的( 参考资料中第一条就是方志朋先生的spring cloud eureka教程 )

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: