本文主要讲解eureka
的简单使用,由于eureka
的停更所以目前的学习只作为基础的了解。其中eureka
的配置主要涉及pom(依赖导入)、application.yaml(配置)、代码注解这三个方面,本文主要着重在这三个方面!
Eureka服务端
服务端pom的引入内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <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-web</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
|
服务端的application.yaml配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| server: port: 7002
eureka: instance: hostname: eureka7002.com client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://eureka7001.com:7001/eureka/ server: enable-self-preservation: false eviction-interval-timer-in-ms: 2000
|
服务端的启动代码
1 2 3 4 5 6 7 8
| @SpringBootApplication @EnableEurekaServer public class EurekaMain7002 { public static void main(String[] args){ SpringApplication.run(EurekaMain7002.class,args); } }
|
Eureka客户端-服务注册
客户端pom的引入内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</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-actuator</artifactId> </dependency>
|
客户端的application.yaml配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| server: port: 8001 spring: application: name: cloud-payment-service eureka: client: register-with-eureka: true fetch-registry: true service-url: defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka instance: instance-id: payment8001 prefer-ip-address: true lease-renewal-interval-in-seconds: 1 lease-expiration-duration-in-seconds: 2
|
客户端的启动代码与注册代码
1 2 3 4 5 6 7 8 9 10
| @SpringBootApplication @EnableEurekaClient public class PaymentMain8001 {
public static void main(String[] args){ SpringApplication.run(PaymentMain8001.class,args); } }
|
后续所有的Controller中的调用接口都会被注册到服务中
注:根据本人在实际使用中发现注册端去除掉@EnableEurekaClient
任然注册到eureka中。
Eureka客户端-服务调用
客户端pom的引入内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</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-actuator</artifactId> </dependency>
|
客户端的application.yaml配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| server: port: 80 spring: application: name: cloud-order-service
eureka: client: register-with-eureka: true fetch-registry: true service-url: defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
|
客户端调用的代码
此处的代码调用没采用ribbon
,只是简单的采用了RestTemplate
1 2 3 4 5 6 7 8
| public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";
private RestTemplate restTemplate = new RestTemplate();
@PostMapping(value = "/consumer/payment/create") public CommonResult<Payment> create(@RequestBody Payment payment) { return restTemplate.postForObject(PAYMENT_URL + "/payment/create", payment, CommonResult.class); }
|
Eureka的一些特殊配置技巧
配置注册服务中的服务名字
1 2 3 4 5 6 7 8 9 10 11
| spring: application: name: cloud-payment-service eureka: instance: instance-id: payment8001 prefer-ip-address: true
|
配置注册服务中服务端的模式
1 2 3 4 5
| eureka: server: enable-self-preservation: false eviction-interval-timer-in-ms: 2000
|
其中模式涉及到CAP原理,如果设置为false为CP,如果设置为true为AP。CAP为C-数据一致性;A-服务可用性;P-服务对网络分区故障的容错性。
结语
上诉所有配置与引入只是为了对eureka有一个认识,后续将加上ribbon
的使用,但是不会对其内部原理继续深入。
根据上诉内容eureka的yaml配置内容实质只有三个部分client、server与instance。
其中instance属于全局概念,client与server都可以被作为instance,因而无论在client还是server中都可以配置instance。
client与server作为一对互斥的概念。当作为server的时候,client的一些注册属性必须消除;当作为client的时候,server不能对其有任何配置。