本文主要是介绍Spring Cloud(Finchley.RCI) (二) Spring Cloud服务注册与发现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在这里, 我们需要的组件是Spring Cloud Netflix的Eureka, Eureka是一个服务注册与发现模块
创建服务注册中心
pom文件
<?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>com.funtl</groupId><artifactId>hello-spring-cloud-dependencies</artifactId><version>1.0.0-SNAPSHOT</version><relativePath>../hello-spring-cloud-dependencies/pom.xml</relativePath></parent><artifactId>hello-spring-cloud-eureka</artifactId><packaging>jar</packaging><name>hello-spring-cloud-eureka</name><url>http://www.funtl.com</url><inceptionYear>2018-Now</inceptionYear><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-cloud-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><mainClass>com.funtl.hello.spring.cloud.eureka.EurekaApplication</mainClass></configuration></plugin></plugins></build>
</project>
Application
启动一个服务注册中心, 只需要一个注解@EnableEurekaServer
package com.funtl.hello.spring.cloud.eureka;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {public static void main(String[] args) {SpringApplication.run(EurekaApplication.class, args);}
}
application.yml
Eureka是一个高可用的组件, 它没有后端缓存, 每一个实例注册之后需要向注册中心发送心跳(因此可以在内存中完成), 在默认情况下Eureka Server也是一个Eureka Client必须要指定一个Server.
spring:application:name: hello-spring-cloud-eurekaserver:port: 5761eureka:instance:hostname: localhostclient:register-with-eureka: falsefetch-registry: falseserviceUrl:defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
这篇关于Spring Cloud(Finchley.RCI) (二) Spring Cloud服务注册与发现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!