SpringCloud(四)将服务注册至Eureka服务中心
文章内索引
[显示]
eureka服务的客户端可以基于正常的springboot服务,只需要引入eureka-discovery。
Quick Start
可以通过 http://start.spring.io/ 快速创建项目,通过关键字Eureka Discovery关键字,快速创建基于eurekaServer的客户端(对外提供的服务)。
pom文件
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.hanson</groupId> <artifactId>Eureka-ProductService</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>Eureka-ProductService</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.10.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Edgware.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</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-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> </project> |
1 2 3 4 5 |
#spring.application.name,这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个name 。 spring.application.name=hanson-service server.port=8001 #与Eureka-Server中的${eureka.client.serviceUrl.defaultZone}一致。 eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/ |
代码
在Restful服务中添加@EnableDiscoveryClient注册,将服务注册至Eureka服务中心。
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
package com.hanson.eureka; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController @EnableDiscoveryClient public class ProductServiceApplication { @Autowired private DiscoveryClient discoveryClient; @Value("${server.port}") private String port; @RequestMapping(value = { "" }, method = RequestMethod.GET) String get() { String ret = String.format("你好,这是你要的XX商品,由%s为您提供服务。", port); return ret; } @RequestMapping("/registered") public String getRegistered(){ List<ServiceInstance> list = discoveryClient.getInstances("STORES"); System.out.println(discoveryClient.getLocalServiceInstance()); System.out.println("discoveryClient.getServices().size() = " + discoveryClient.getServices().size()); for( String s : discoveryClient.getServices()){ System.out.println("services " + s); List<ServiceInstance> serviceInstances = discoveryClient.getInstances(s); for(ServiceInstance si : serviceInstances){ System.out.println(" services:" + s + ":getHost()=" + si.getHost()); System.out.println(" services:" + s + ":getPort()=" + si.getPort()); System.out.println(" services:" + s + ":getServiceId()=" + si.getServiceId()); System.out.println(" services:" + s + ":getUri()=" + si.getUri()); System.out.println(" services:" + s + ":getMetadata()=" + si.getMetadata()); } } System.out.println(list.size()); if (list != null && list.size() > 0 ) { System.out.println( list.get(0).getUri()); } return null; } public static void main(String[] args) { SpringApplication.run(ProductServiceApplication.class, args); } } |
启动服务后,再次访问 http://localhost:8761/,会发现服务已经注册至注册中心。此处的我分别使用了8001、8002、8003启动了3个服务。
出现红色警告信息
EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY’RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.
原因:自我保护机制。Eureka Server在运行期间,会统计心跳失败的比例(Rennews last min)是否低于阈值(Rennews threshold),如果出现低于的情况(在单机调试的时候很容易满足,实际在生产环境上通常是由于网络不稳定导致),Eureka Server会将当前的实例注册信息保护起来,同时提示这个警告。
参考这篇文章
解决:
将统计心跳失败比例的阈值由默认的85%设置为49%,因为当单独启动一个Eureka服务器时,阈值将为0。
eureka.server.renewalPercentThreshold=0.49
©版权声明:本文为【翰林小院】(huhanlin.com)原创文章,转载时请注明出处!
发表评论