特别详细的Spring Cloud 系列教程1:服务注册中心Eureka的启动

时间:2024-04-08 19:54:39

Eureka已经被Spring Cloud继承在其子项目spring-cloud-netflix中,搭建Eureka Server的方式还是非常简单的。只需要通过一个独立的maven工程即可搭建Eureka Server。 

我们引入spring cloud的依赖和eureka的依赖。

    <dependencyManagement>
        <!--		spring cloud版本-->
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.SR6</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <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>
        <!-- spring cloud Eureka Server 启动器 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

注意spring cloud和springboot的版本要对应,不然容易出现各种奇怪的错误。

不知道springboot和springcloud的版本对应关系的话,可以去spring官网查看 Spring Cloud

在application.yml里设置配置信息(如果你是application.properties,可以把application.properties修改为application.yml)

spring:
  application:
    name: eureka-server
server:
  #指定服务端口
  port: 8761
eureka:
  #指定主机名称
  instance:
    hostname: localhost
  #server一定程度上也是client,互为client,
  client:
    #由于自己就是服务器,不需要注册到自己
    register-with-eureka: false
    #由于自己就是服务器,不需要从服务器获取注册信息
    fetch-registry: false
    #服务地址
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

加入@EnableEurekaServer注解

@EnableEurekaServer
@SpringBootApplication
public class ServiceCenterApplication {

	public static void main(String[] args) {
		SpringApplication.run(ServiceCenterApplication.class, args);
	}

}

 启动应用,在浏览器输入http://localhost:8761/

在application这个区域可以看到我们暂时没有应用注册到我们的eureka服务注册中心

至此,eureka的服务端就搭建好了。接下来,另起一个项目,我们把它叫为producer。

在producer的pom文件里,我们加上以下依赖:

	<dependencies>
		<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>
		<!-- spring cloud Eureka Client -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>
	</dependencies>

唯一的区别就是将eureka server的依赖换成eureka client的依赖。 

这是producer的yml文件。

server:
  port: 8081
eureka:
  client:
    registerWithEureka: true #服务注册开关
    fetchRegistry: true #服务发现开关
    serviceUrl: #Eureka服务端进行交互的地址,多个中间用逗号分隔,EUREKA_SERVER是我们设置的属性名,冒号:代表如果找不到属性,则后面的内容为默认值
      defaultZone: ${EUREKA_SERVER:http://localhost:8761/eureka/}
  instance:
    hostname: producer-service #实例名称
    prefer-ip-address:  true  #将自己的ip地址注册到Eureka服务中
    ip-address: ${IP_ADDRESS:127.0.0.1}
    instance-id: ${spring.application.name:producer}:${server.port} #指定实例id

这是producer的代码,主要是加上了@EnableDiscoveryClient注解,表明这是一个eureka client端,它需要根据我们给它设置的配置信息,找到eureka server端的地址,向它发起注册。

@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class ProducerApplication {

	public static void main(String[] args) {
		SpringApplication.run(ProducerApplication.class, args);
	}

	@GetMapping("goods")
	public String generate(){
		return "goods";
	}
}

启动应用。刷新localhost:8761,可以看到信息改变了。