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:WindowsSystem32driversetchosts文件中进行映射
127.0.0.1 server1 127.0.0.1 server2 127.0.0.1 server3
启动后