【spring cloud学习】Eureka Server注册中心
    		       		warning:
    		            这篇文章距离上次修改已过421天,其中的内容可能已经有所变动。
    		        
        		                
                在Spring Cloud中,我们可以使用Eureka作为服务注册和发现的组件。以下是如何创建一个Eureka Server的简单步骤和示例代码:
- 创建一个新的Spring Boot项目,并添加Eureka Server依赖。
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
</dependencies>- 在主应用类上添加@EnableEurekaServer注解来启用Eureka Server。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
 
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}- 在application.properties或application.yml配置文件中配置Eureka Server。
# application.properties
spring.application.name=eureka-server
server.port=8761
 
eureka.instance.hostname=localhost
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/- 启动Eureka Server,访问http://localhost:8761,你将看到Eureka的管理页面。
以上步骤创建了一个基本的Eureka Server,它可以运行并且可以使用来注册和发现服务。
评论已关闭