赞
踩
https://spring.io/projects/spring-cloud-netflix
https://github.com/Netflix/eureka
https://github.com/spring-cloud-samples/eureka
1.实际上Netflix开源了好多组件,Eureka只是其中的一个,大家可以查看GitHub上面的相关开源项目,
1.在微服务架构系统之中,我们经常提三个角色:
1.1.注册中心 (Register):服务发布者将服务发布到注册中心,服务消费者从注册中心获取可以进行访问的服务列表;
1.2.服务提供者(Provider):需要发布服务的应用
1.3.服务消费者(Consumer):需要访问服务的应用
1.首先,我们可以看到上面注册中心使用了集群部署:us-east-1c,us-east-1d,us-east-1e(美国东部1c,1d,1e注册中心)
2.集群中的机器,数据会进行同步复制更新(replicate),保证注册中心数据最终一致性;
1.服务发布者,即下游相关的平台或者是提供服务的应用,对于自己的应用服务(Application Service),做下面的操作
1.1.服务注册:将自己的服务接口方法以及服务发布的地址等,注册到Eureka Server注册中心中;
1.2.服务更新:对已经发布到注册中心的服务通知进行更新操作;
1.3.服务删除:通过相关操作(如停止应用操作)通知注册中心,将应用服务从注册中心删除(移除);
2.服务发布者通过Eureka这个注册中心,每30秒发送一次心跳更新注册中心的数据;
如果服务消费者一段时间之内不能更新这个服务发布者的服务信息,那么90s之内,这个服务将会被注册中心移除;
1.服务消费者(Application Client),在应用启动的时候,根据自己订阅的服务,会去注册中心(Eureka Server)拉取所有的服务清单列表
会缓存到应用本地缓存,
2.
这里的版本引入主要是下面这个
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
当然pom里还有一些其他的内容,我们不做一一讲解
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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.gaoxinfu.demo.spring.cloud</groupId> <artifactId>demo-spring-cloud-netflix-eureka-server</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>demo-spring-cloud-netflix-eureka-server</name> <description>Eureka Server demo project</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.BUILD-SNAPSHOT</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <start-class>eurekademo.EurekaApplication</start-class> <java.version>1.8</java.version> <docker.image.prefix>springcloud</docker.image.prefix> </properties> <build> <plugins> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.2.3</version> <configuration> <baseImage>openjdk:8-jre-alpine</baseImage> <imageName>${docker.image.prefix}/${project.artifactId}</imageName> <exposes>8761</exposes> <entryPoint>["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/${project.build.finalName}.jar"]</entryPoint> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <!-- defined in spring-cloud-starter-parent pom (as documentation hint), but needs to be repeated here --> <configuration> <requiresUnpack> <dependency> <groupId>com.netflix.eureka</groupId> <artifactId>eureka-core</artifactId> </dependency> <dependency> <groupId>com.netflix.eureka</groupId> <artifactId>eureka-client</artifactId> </dependency> </requiresUnpack> </configuration> </plugin> <plugin> <groupId>pl.project13.maven</groupId> <artifactId>git-commit-id-plugin</artifactId> <configuration> <failOnNoGitDirectory>false</failOnNoGitDirectory> </configuration> </plugin> <plugin> <!--skip deploy (this is just a test module) --> <artifactId>maven-deploy-plugin</artifactId> <configuration> <skip>true</skip> </configuration> </plugin> </plugins> </build> <repositories> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/libs-snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/libs-milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> <repository> <id>spring-releases</id> <name>Spring Releases</name> <url>https://repo.spring.io/libs-release</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/libs-snapshot-local</url> <snapshots> <enabled>true</enabled> </snapshots> </pluginRepository> <pluginRepository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/libs-milestone-local</url> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> </project>
server:
port: 8761
eureka:
client:
registerWithEureka: true
fetchRegistry: true
server:
waitTimeInMsWhenSyncEmpty: 0
server.port:定义Eureka Server 端口
eureka.client.registerWithEureka :表示是否将自己注册到Eureka Server,默认为true。由于当前这个应用就是Eureka Server,故而设为false。
eureka.client.fetchRegistry :表示是否从Eureka Server获取注册信息,默认为true。因为这是一个单点的Eureka Server,不需要同步其他的Eureka Server节点的数据,故而设为false。
spring:
application:
name: demo-spring-cloud-netflix-eureka-server
cloud:
config:
uri: ${CONFIG_SERVER_URL:http://localhost:8888}
package com.gaoxinfu.demo.spring.cloud.netflix.eureka.server; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; /** * @Description: * @Author: gaoxinfu * @Date: 2020-09-11 11:18 */ @EnableAutoConfiguration @EnableEurekaServer public class DemoSpringCloudNetflixEurekaServerApp { public static void main(String[] args) { SpringApplication.run(DemoSpringCloudNetflixEurekaServerApp.class,args); } }
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
spring.application.name=demo-srping-cloud-netflix-eureka-provider
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
server.port=8762
package com.gaoxinfu.demo.spring.cloud.netflix.eureka.provider; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; /** * @Description: * @Author: gaoxinfu * @Date: 2020-09-13 12:04 */ @SpringBootApplication @EnableEurekaClient public class DemoSpringCloudNetflixEurekaProviderApp { public static void main(String[] args) { SpringApplication.run(DemoSpringCloudNetflixEurekaProviderApp.class,args); } }
2020-09-22 14:01:12.352 INFO 19151 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@3d74bf60: startup date [Tue Sep 22 14:01:12 CST 2020]; root of context hierarchy 2020-09-22 14:01:12.702 INFO 19151 --- [ main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring 2020-09-22 14:01:12.782 INFO 19151 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'configurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$e525a3bd] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.0.2.RELEASE) 2020-09-22 14:01:13.319 INFO 19151 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:8888 2020-09-22 14:01:13.478 INFO 19151 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Connect Timeout Exception on Url - http://localhost:8888. Will be trying the next url if available 2020-09-22 14:01:13.479 WARN 19151 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Could not locate PropertySource: I/O error on GET request for "http://localhost:8888/demo-srping-cloud-netflix-eureka-provider/default": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused) 2020-09-22 14:01:13.482 INFO 19151 --- [ main] .DemoSpringCloudNetflixEurekaProviderApp : No active profile set, falling back to default profiles: default 2020-09-22 14:01:13.496 INFO 19151 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@791d1f8b: startup date [Tue Sep 22 14:01:13 CST 2020]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@3d74bf60 2020-09-22 14:01:14.060 INFO 19151 --- [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=692091bb-c487-3110-9c25-53bfc11f7aa0 2020-09-22 14:01:14.077 INFO 19151 --- [ main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring 2020-09-22 14:01:14.126 INFO 19151 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$e525a3bd] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2020-09-22 14:01:14.154 WARN 19151 --- [ main] c.n.c.sources.URLConfigurationSource : No URLs will be polled as dynamic configuration sources. 2020-09-22 14:01:14.154 INFO 19151 --- [ main] c.n.c.sources.URLConfigurationSource : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath. 2020-09-22 14:01:14.157 WARN 19151 --- [ main] c.n.c.sources.URLConfigurationSource : No URLs will be polled as dynamic configuration sources. 2020-09-22 14:01:14.157 INFO 19151 --- [ main] c.n.c.sources.URLConfigurationSource : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath. 2020-09-22 14:01:14.557 INFO 19151 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 2020-09-22 14:01:14.564 INFO 19151 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Bean with name 'configurationPropertiesRebinder' has been autodetected for JMX exposure 2020-09-22 14:01:14.564 INFO 19151 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Bean with name 'environmentManager' has been autodetected for JMX exposure 2020-09-22 14:01:14.565 INFO 19151 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Bean with name 'refreshScope' has been autodetected for JMX exposure 2020-09-22 14:01:14.567 INFO 19151 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Located managed bean 'environmentManager': registering with JMX server as MBean [org.springframework.cloud.context.environment:name=environmentManager,type=EnvironmentManager] 2020-09-22 14:01:14.575 INFO 19151 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Located managed bean 'refreshScope': registering with JMX server as MBean [org.springframework.cloud.context.scope.refresh:name=refreshScope,type=RefreshScope] 2020-09-22 14:01:14.581 INFO 19151 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Located managed bean 'configurationPropertiesRebinder': registering with JMX server as MBean [org.springframework.cloud.context.properties:name=configurationPropertiesRebinder,context=791d1f8b,type=ConfigurationPropertiesRebinder] 2020-09-22 14:01:14.587 INFO 19151 --- [ main] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase 0 2020-09-22 14:01:14.596 INFO 19151 --- [ main] o.s.c.n.eureka.InstanceInfoFactory : Setting initial instance status as: STARTING 2020-09-22 14:01:14.620 INFO 19151 --- [ main] com.netflix.discovery.DiscoveryClient : Initializing Eureka in region us-east-1 2020-09-22 14:01:14.764 INFO 19151 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using JSON encoding codec LegacyJacksonJson 2020-09-22 14:01:14.764 INFO 19151 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using JSON decoding codec LegacyJacksonJson 2020-09-22 14:01:14.872 INFO 19151 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using XML encoding codec XStreamXml 2020-09-22 14:01:14.872 INFO 19151 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using XML decoding codec XStreamXml 2020-09-22 14:01:14.990 INFO 19151 --- [ main] c.n.d.s.r.aws.ConfigClusterResolver : Resolving eureka endpoints via configuration 2020-09-22 14:01:15.004 INFO 19151 --- [ main] com.netflix.discovery.DiscoveryClient : Disable delta property : false 2020-09-22 14:01:15.004 INFO 19151 --- [ main] com.netflix.discovery.DiscoveryClient : Single vip registry refresh property : null 2020-09-22 14:01:15.004 INFO 19151 --- [ main] com.netflix.discovery.DiscoveryClient : Force full registry fetch : false 2020-09-22 14:01:15.004 INFO 19151 --- [ main] com.netflix.discovery.DiscoveryClient : Application is null : false 2020-09-22 14:01:15.004 INFO 19151 --- [ main] com.netflix.discovery.DiscoveryClient : Registered Applications size is zero : true 2020-09-22 14:01:15.004 INFO 19151 --- [ main] com.netflix.discovery.DiscoveryClient : Application version is -1: true 2020-09-22 14:01:15.004 INFO 19151 --- [ main] com.netflix.discovery.DiscoveryClient : Getting all instance registry info from the eureka server 2020-09-22 14:01:15.103 INFO 19151 --- [ main] com.netflix.discovery.DiscoveryClient : The response status is 200 2020-09-22 14:01:15.106 INFO 19151 --- [ main] com.netflix.discovery.DiscoveryClient : Starting heartbeat executor: renew interval is: 30 2020-09-22 14:01:15.107 INFO 19151 --- [ main] c.n.discovery.InstanceInfoReplicator : InstanceInfoReplicator onDemand update allowed rate per min is 4 2020-09-22 14:01:15.110 INFO 19151 --- [ main] com.netflix.discovery.DiscoveryClient : Discovery Client initialized at timestamp 1600754475109 with initial instances count: 1 2020-09-22 14:01:15.113 INFO 19151 --- [ main] o.s.c.n.e.s.EurekaServiceRegistry : Registering application DEMO-SRPING-CLOUD-NETFLIX-EUREKA-PROVIDER with eureka with status UP 2020-09-22 14:01:15.113 INFO 19151 --- [ main] com.netflix.discovery.DiscoveryClient : Saw local status change event StatusChangeEvent [timestamp=1600754475113, current=UP, previous=STARTING] 2020-09-22 14:01:15.129 INFO 19151 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_DEMO-SRPING-CLOUD-NETFLIX-EUREKA-PROVIDER/10.56.39.27:demo-srping-cloud-netflix-eureka-provider:8762: registering service... 2020-09-22 14:01:15.142 INFO 19151 --- [ main] .DemoSpringCloudNetflixEurekaProviderApp : Started DemoSpringCloudNetflixEurekaProviderApp in 3.389 seconds (JVM running for 4.149) 2020-09-22 14:01:15.144 INFO 19151 --- [ Thread-11] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@791d1f8b: startup date [Tue Sep 22 14:01:13 CST 2020]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@3d74bf60 2020-09-22 14:01:15.144 INFO 19151 --- [ Thread-11] o.s.c.n.e.s.EurekaServiceRegistry : Unregistering application DEMO-SRPING-CLOUD-NETFLIX-EUREKA-PROVIDER with eureka with status DOWN 2020-09-22 14:01:15.145 WARN 19151 --- [ Thread-11] com.netflix.discovery.DiscoveryClient : Saw local status change event StatusChangeEvent [timestamp=1600754475145, current=DOWN, previous=UP] 2020-09-22 14:01:15.146 INFO 19151 --- [ Thread-11] o.s.c.support.DefaultLifecycleProcessor : Stopping beans in phase 0 2020-09-22 14:01:15.147 INFO 19151 --- [ Thread-11] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown 2020-09-22 14:01:15.147 INFO 19151 --- [ Thread-11] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans 2020-09-22 14:01:15.148 INFO 19151 --- [ Thread-11] com.netflix.discovery.DiscoveryClient : Shutting down DiscoveryClient ... 2020-09-22 14:01:15.156 INFO 19151 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_DEMO-SRPING-CLOUD-NETFLIX-EUREKA-PROVIDER/10.56.39.27:demo-srping-cloud-netflix-eureka-provider:8762 - registration status: 204 2020-09-22 14:01:15.157 INFO 19151 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_DEMO-SRPING-CLOUD-NETFLIX-EUREKA-PROVIDER/10.56.39.27:demo-srping-cloud-netflix-eureka-provider:8762: registering service... 2020-09-22 14:01:15.162 INFO 19151 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_DEMO-SRPING-CLOUD-NETFLIX-EUREKA-PROVIDER/10.56.39.27:demo-srping-cloud-netflix-eureka-provider:8762 - registration status: 204 2020-09-22 14:01:15.163 INFO 19151 --- [ Thread-11] com.netflix.discovery.DiscoveryClient : Unregistering ... 2020-09-22 14:01:15.169 INFO 19151 --- [ Thread-11] com.netflix.discovery.DiscoveryClient : DiscoveryClient_DEMO-SRPING-CLOUD-NETFLIX-EUREKA-PROVIDER/10.56.39.27:demo-srping-cloud-netflix-eureka-provider:8762 - deregister status: 200 2020-09-22 14:01:15.176 INFO 19151 --- [ Thread-11] com.netflix.discovery.DiscoveryClient : Completed shut down of DiscoveryClient Process finished with exit code 0
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
package com.gaoxinfu.demo.spring.cloud.netflix.eureka.provider.controller; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; /** * @Description: * @Author: gaoxinfu * @Date: 2020-09-22 11:41 */ @EnableEurekaClient @RestController public class HelloController { @ResponseBody @RequestMapping("/hello") public String hello(String name) { return name + ", Welcome to Eureka Client Test Demo!"; } }
http://localhost:8762/hello?name=gaoxinfu
从上面可以看出来,服务端的服务是没有问题的;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。