一 Eureka的基本架构
Spring Cloud 封装了 Netflix 公司开发的 Eureka 模块来实现服务注册和发现(请对比Zookeeper)。
Eureka 采用了 C-S 的设计架构。Eureka Server 作为服务注册功能的服务器,它是服务注册中心。
二 Eureka的三大角色
-
Eureka Server 提供服务注册和发现
-
Service Provider服务提供方将自身服务注册到Eureka,从而使服务消费方能够找到
-
Service Consumer服务消费方从Eureka获取注册服务列表,从而能够消费服务
三 构建步骤
-
创建eureka-server项目
-
pom.xml
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <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> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
-
yml
server: port: 8761 eureka: instance: hostname: localhost client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
-
启动类
@SpringBootApplication @EnableEurekaServer public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
-
启动结果
-
-
创建producer-service
-
pom.xml
1 <dependencies> 2 <dependency> 3 <groupId>org.springframework.boot</groupId> 4 <artifactId>spring-boot-starter-actuator</artifactId> 5 </dependency> 6 <dependency> 7 <groupId>org.springframework.boot</groupId> 8 <artifactId>spring-boot-starter-web</artifactId> 9 </dependency> 10 <dependency> 11 <groupId>org.springframework.cloud</groupId> 12 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> 13 </dependency> 14 15 <dependency> 16 <groupId>org.springframework.boot</groupId> 17 <artifactId>spring-boot-starter-test</artifactId> 18 <scope>test</scope> 19 </dependency> 20 </dependencies> 21 22 <dependencyManagement> 23 <dependencies> 24 <dependency> 25 <groupId>org.springframework.cloud</groupId> 26 <artifactId>spring-cloud-dependencies</artifactId> 27 <version>${spring-cloud.version}</version> 28 <type>pom</type> 29 <scope>import</scope> 30 </dependency> 31 </dependencies> 32 </dependencyManagement> 33 <!-- 微服务info内容详细信息 --> 34 <build> 35 <finalName>microservicecloud</finalName> 36 <resources> 37 <resource> 38 <directory>src/main/resources</directory> 39 <filtering>true</filtering> 40 </resource> 41 </resources> 42 <plugins> 43 <plugin> 44 <groupId>org.apache.maven.plugins</groupId> 45 <artifactId>maven-resources-plugin</artifactId> 46 <configuration> 47 <delimiters> 48 <delimit>$</delimit> 49 </delimiters> 50 </configuration> 51 </plugin> 52 </plugins> 53 </build>
-
yml
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ instance: instance-id: product-service8080 #主机名称:服务名称修改 prefer-ip-address: true #访问路径可以显示IP地址 server: port: 8080 spring: application: name: product-service #微服务info内容详细信息 info: app.name: product-servic company.name: www.topcheer.com
-
启动类
@SpringBootApplication public class ProducerServiceApplication { public static void main(String[] args) { SpringApplication.run(ProducerServiceApplication.class, args); } }
-
结果:
-