springcolud-eureka
1 介绍
1、Spring-Cloud Euraka介绍
Spring-Cloud Euraka是Spring Cloud集合中一个组件,它是对Euraka的集成,用于服务注册和发现。Eureka是Netflix中的一个开源框架。它和 zookeeper、Consul一样,都是用于服务注册管理的,同样,Spring-Cloud 还集成了Zookeeper和Consul。
在项目中使用Spring Cloud Euraka的原因是它可以利用Spring Cloud Netfilix中其他的组件,如zull等,因为Euraka是属于Netfilix的。
2、Euraka介绍
Eureka由多个instance(服务实例)组成,这些服务实例可以分为两种:Eureka Server和Eureka Client。为了便于理解,我们将Eureka client再分为Service Provider和Service Consumer。
- Eureka Server 提供服务注册和发现
- Service Provider 服务提供方,将自身服务注册到Eureka,从而使服务消费方能够找到
- Service Consumer服务消费方,从Eureka获取注册服务列表,从而能够消费服务
3.项目中的实际应用 - 1.新建一个Springcloud工程:工程名:eureka, jdk8,springcloud的版本:Edgware.RELEASE
-
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.chinasoft</groupId> <artifactId>eureka</artifactId> <version>0.0.1-SNAPSHOT</version> <name>eureka</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Edgware.RELEASE</spring-cloud.version> </properties> <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>Edgware.RELEASE</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> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
2.application.yml
-
server: port: 6001 spring: application: name: eureka #应用名称 eureka: instance: appname: eureka-server #服务名 hostname: localhost #应用实例主机名 prefer-ip-address: true #客户端在注册的时候使用自己的IP而不是主机名 client: register-with-eureka: false #是否将自己注册到eureka fetch-registry: false #服务发现,是否从Eureka中获取注册信息 service-url: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ server: # 自我保护 enable-self-preservation: false # 主动检查服务时间 eviction-interval-timer-in-ms: 5000 # 服务过期时间,超过这个时间剔除服务 lease-expiration-duration-in-seconds: 10 # 服务刷新时间 主动心跳 lease-renewal-interval-in-seconds: 5 # 禁用readOnlyCacheMap useReadOnlyResponseCache: false3.EurekaApplication.java
1 //开启eureka服务端 2 @EnableEurekaServer 3 @SpringBootApplication 4 public class EurekaApplication { 5 public static void main(String[] args) { 6 SpringApplication.run(EurekaApplication.class, args); 7 } 8 }

3.2创建一个服务消费者的工程:eureka_consumer,
3.2.1 pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.chinasoft</groupId> <artifactId>eureka_consumer</artifactId> <version>0.0.1-SNAPSHOT</version> <name>eureka_consumer</name> <description>spring-cloud eureka consumer</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Edgware.RELEASE</spring-cloud.version> </properties> <dependencies> <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-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Edgware.RELEASE</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> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
2.application.yml
server: port: 7001 spring: application: name: eureka_consumer ##此方法是把工程中服务注册到端口为6001,工程为eureka中去; eureka: client: register-with-eureka: true fetch-registry: true service-url: defaultZone: http://localhost:6001/eureka/ instance: prefer-ip-address: true ip-address: ${IP_ADDRESS:127.0.0.1} instance-id: ${spring.application.name}:${server.port} #eureka服务中心的名字 ucenter:7001
3.EurekaConsumerApplication.java
@SpringBootApplication //servlet扫描器 @ServletComponentScan //开启eureka客户端,启动的时候会被eureka发现并注册到服务中心 @EnableDiscoveryClient public class EurekaConsumerApplication { public static void main(String[] args) { SpringApplication.run(EurekaConsumerApplication.class, args); } }
4.TestController.java
@RestController public class TestController { @GetMapping("/getuserinfo/{id}") public String get(@PathVariable("id") Long id) { return "用户信息:" + id; } }
效果展示:


浙公网安备 33010602011771号