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

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,可以看到信息改变了。

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Cloud Config Server注册到Eureka注册中心的步骤如下: 1. 首先需要在pom.xml文件中添加Eureka Client的依赖,如下所示: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>2.2.1.RELEASE</version> </dependency> ``` 2. 在应用程序的配置文件中添加以下配置: ```yaml eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ ``` 其中,`defaultZone`指定了Eureka Server的地址。 3. 在Spring Boot应用程序类上添加@EnableEurekaClient注解,以将Config Server注册到Eureka Server上: ```java @EnableConfigServer @EnableDiscoveryClient @SpringBootApplication public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } } ``` 4. 运行Config Server应用程序,查看Eureka Server的管理页面(http://localhost:8761)上是否已经注册成功。 5. 客户端应用程序可以通过以下方式从Config Server获取配置: ```yaml spring: application: name: client-service cloud: config: uri: http://localhost:8888 fail-fast: true retry: maxAttempts: 20 multiplier: 1.5 maxInterval: 5000 label: master ``` 其中,`cloud.config.uri`指定了Config Server的地址,`spring.application.name`指定了客户端应用程序的名称,`label`指定了Git仓库的分支名称。 6. 运行客户端应用程序,即可从Config Server获取配置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值