Eureka Server的使用

1. eureka组件

Eureka Server 和 Eureka Client

调用过程:

1. 服务提供者启动时向服务注册中心注册自己的服务。

2. 服务消费者启动时向服务注册中心订阅自己所需要的服务。

3. 注册中心将服务提供者地址提供给消费者。

4. 服务消费者通过地址进行消费。

2. 注册中心搭建

pom.xml

 

 1   <parent>
 2         <groupId>org.springframework.boot</groupId>
 3         <artifactId>spring-boot-starter-parent</artifactId>
 4         <version>2.0.2.RELEASE</version>
 5   </parent>
 6 
 7     <properties>
 8         <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
 9     </properties>
10 
11     <dependencies>
12         <dependency>
13             <groupId>org.projectlombok</groupId>
14             <artifactId>lombok</artifactId>
15             <version>1.16.18</version>
16         </dependency>
17         <dependency>
18             <groupId>org.springframework.cloud</groupId>
19             <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
20         </dependency>
21         <dependency>
22             <groupId>org.springframework.boot</groupId>
23             <artifactId>spring-boot-starter-test</artifactId>
24             <scope>test</scope>
25         </dependency>
26     </dependencies>
27 
28     <dependencyManagement>
29         <dependencies>
30             <dependency>
31                 <groupId>org.springframework.cloud</groupId>
32                 <artifactId>spring-cloud-dependencies</artifactId>
33                 <version>${spring-cloud.version}</version>
34                 <type>pom</type>
35                 <scope>import</scope>
36             </dependency>
37         </dependencies>
38     </dependencyManagement>
39 
40     <repositories>
41         <repository>
42             <id>spring-milestones</id>
43             <name>Spring Milestones</name>
44             <url>https://repo.spring.io/milestone</url>
45             <snapshots>
46                 <enabled>false</enabled>
47             </snapshots>
48         </repository>
49     </repositories>    

 

启动类

 

1 @EnableEurekaServer
2 @SpringBootApplication
3 public class EurekaApplication {
5     public static void main(String[] args) {
6         SpringApplication.run(EurekaApplication.class, args);
7     }
8 }

 

application.yml

 

spring:
  application:
    name: ad-eureka #服务名

server:
  port: 8000

eureka:
  instance:
    hostname: localhost
  client:
    fetch-registry: false #是否从eureka获取信息
    register-with-eureka: false #是否注册到eureka(自身是不需要注册到自己的)
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

 

application.yml(关于两两注册)

 

spring:
  application:
    name: ad-eureka
  profiles: server1
server:
  port: 8000
eureka:
  instance:
    hostname: server1
    prefer-ip-address: false
  client:
    service-url:
      defaultZone: http://server2:8001/eureka/,http://server3:8002/eureka/

---
spring:
  application:
    name: ad-eureka
  profiles: server2
server:
  port: 8001
eureka:
  instance:
    hostname: server2
    prefer-ip-address: false
  client:
    service-url:
      defaultZone: http://server1:8000/eureka/,http://server3:8002/eureka/

---
spring:
  application:
    name: ad-eureka
  profiles: server3
server:
  port: 8002
eureka:
  instance:
    hostname: server3
    prefer-ip-address: false
  client:
    service-url:
      defaultZone: http://server1:8000/eureka/,http://server2:8001/eureka/

 

当然,此时还得在C:\Windows\System32\drivers\etc\hosts文件中进行映射

127.0.0.1    server1
127.0.0.1    server2
127.0.0.1    server3

启动后

 

转载于:https://www.cnblogs.com/rabbitli/p/11140162.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值