ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Spring Cloud 自学笔记(1): Eureka 服务注册中心的搭建和服务注册的简单demo

2019-09-16 17:37:56  阅读:148  来源: 互联网

标签:spring demo springframework eureka 服务端 注册 Spring cloud


先声明一个坑,Spring Cloud版本与Spring Boot版本一定要匹配,不然会导致启动项application.java中无法导入注释@EnableEurekaServer
Spring Cloud版本与Spring Boot版本的对应关系见博客https://blog.csdn.net/russle/article/details/80865288

本文所使用的Spring Boot版本为2.1.8.RELEASE 对应的Spring Cloud版本为Finchley.SR2

首先是服务注册中心的搭建eureka-server,创建名为eureka-server的Spring Boot项目(名字可自定义):
1.在pom.xml文件中添加eureka服务端所需的依赖:
Spring Boot版本的依赖:

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.1.8.RELEASE</version>
	<relativePath/> <!-- lookup parent from repository -->
</parent>

Spring Cloud的依赖:

<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.SR2</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
	</dependencies>
</dependencyManagement>

2.启动项Appliction.java中添加eureka服务端的注释:@EnableEurekaServer
3.在配置文件application.properties中添加配置,yml格式的配置文件自行转换:

server.port=1111

eureka.instance.hostname=serviceCenter
#注册中心默认是会向自己注册的,所以要禁止本身注册
eureka.client.register-with-eureka=false
#注册中心的职责就是维护服务实例,不需要检索服务,所以禁止检索服务
eureka.client.fetch-registry=false
#服务中心地址
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

完成以上配置,就可启动项目,并访问localhost:1111进入eureka注册中心页面进行查看。
在这里插入图片描述
完成服务端的配置后进行客户端的配置,并在服务端中进行注册。搭建客户端,创建名为eureka-client-01的Spring Boot项目(名字可自定义):
1.在pom.xml文件中添加eureka客户端所需的依赖:
其实与服务端基本一样,区别在于一个依赖的是spring-cloud-starter-netflix-eureka-server,另一个依赖的是spring-cloud-starter-netflix-eureka-client。
Spring Boot版本的依赖:

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.1.8.RELEASE</version>
	<relativePath/> <!-- lookup parent from repository -->
</parent>

Spring Cloud的依赖:

<dependencies>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>//区别在这
	</dependency>
</dependencies>
<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-dependencies</artifactId>
			<version>Finchley.SR2</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
	</dependencies>
</dependencyManagement>

2.启动项Appliction.java中添加eureka服务端的注释:@EnableDiscoveryClient或者@EnableEurekaClient,两者的区别在于前者面向于其他注册中心,后者面向Eureka注册中心。
3.在配置文件application.properties中添加配置,yml格式的配置文件自行转换:

server.port=1001

spring.application.name=eureka-client-01

#指定eureka注册中心地址
eureka.client.service-url.defaultZone=http://localhost:1111/eureka/

4.作为实验,写一个简单的hellocontroller

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String hello(){
        return "Hello World!";
    }
}

先启动服务端,再启动客户端。当客户端启动完成后,控制台会输出以下代码,代表注册成果:

在这里插入图片描述
服务端的页面即可看到成功注册的服务端:
在这里插入图片描述

标签:spring,demo,springframework,eureka,服务端,注册,Spring,cloud
来源: https://blog.csdn.net/qq_38425719/article/details/100894018

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有