spring cloud(学习笔记) Enreka服务治理
服务治理是微服务架构最为核心和基础的模块,主要用来实现各个微服务实例的自动化注册和发现。
记录一下服务注册中心的搭建以及高可用注册中心的实现
1.首先创建两个基础 的spring boot工程,spring boot创建工程的网站:http://start.spring.io/,创建界面如下

2.解压工程,用Maven的形式导入工程(File->new->project from Existing Soures,选择解压工程导入)
3.两个工程中都添加依赖(eureka)
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
4.在注册中心工程配置文件中添加服务
server.port=9000 spring.application.name=eureka-server
spring.profiles.active=dev eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.server.enable-self-preservation=false
不知道这些参数的自己百度
5.在注册中心的入口类中加入注解@EnableEurekaServer
package com.example.enrekaserver; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer
@SpringBootApplication
public class EnrekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EnrekaServerApplication.class, args);
}
}
6.启动注册中心,在浏览器中访问,界面如下

7.在另一个工程中,加入注解 @EnableDiscoveryClient
package com.example.demoOne; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient
@SpringBootApplication
public class DemoOneApplication {
public static void main(String[] args) {
SpringApplication.run(DemoOneApplication.class, args);
}
}
8.配置文件中加入配置,指定服务注册中心
spring.application.name=demoOne-service
spring.profiles.active=dev
eureka.client.service-url.defaultZone=http://localhost:9000/eureka/
9.我们可以在需要注册是工程里添加一个类作为测试,并打印日志
package com.example.demoOne.didispace; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import java.util.logging.Logger; @RestController
public class HelloController {
private final Logger logger=Logger.getLogger(String.valueOf(getClass())); @Autowired
private DiscoveryClient client; @RequestMapping(value = "/hello",method = RequestMethod.POST)
public String index(){
ServiceInstance instance = (ServiceInstance) client.getServices();
logger.info("/hello,host:"+instance.getHost()+",service_id:"+instance.getServiceId());
return "hello world";
}
}
10.启动工程,我们可以在服务注册中心看到我们注册的服务

注册中心搭建成功。可以测试一下试试
欢迎大家关注公众号,不定时干货,只做有价值的输出
作者:Dawnzhang
出处:https://www.cnblogs.com/clwydjgs/
版权:本文版权归作者
转载:欢迎转载,但未经作者同意,必须保留此段声明;必须在文章中给出原文连接;否则必究法律责任
spring cloud(学习笔记) Enreka服务治理的更多相关文章
- Spring Cloud 学习笔记(一)——入门、特征、配置
[TOC] 0 放在前面 0.1 参考文档 http://cloud.spring.io/spring-cloud-static/Brixton.SR7/ https://springcloud.cc ...
- Spring Cloud学习笔记-005
服务消费者 之前已经搭建好了微服务中的核心组件——服务注册中心(包括单节点模式和高可用模式).也有了服务提供者,接下来搭建一个服务消费者,它主要完成两个目标,发现服务以及消费服务.其中,服务发现的任务 ...
- Spring Cloud学习笔记【一】Eureka服务注册与发现
Spring Cloud Eureka 是 Spring Cloud Netflix 微服务套件的一部分,基于 Netflix Eureka 做了二次封装,主要负责完成微服务架构中的服务治理功能,服务 ...
- Spring Cloud学习笔记之微服务架构
目录 什么是微服务 架构优点 架构的挑战 设计原则 什么是微服务 微服务构架方法是以开发一种小型服务的方式,来开发一个独立的应用系统的. 其中每个小型服务都运行在自己的进程中,并经常采 ...
- Spring Cloud学习笔记【八】服务网关 Zuul(过滤器)
在上篇文章中我们了解了 Spring Cloud Zuul 作为网关所具备的最基本功能:路由(Router),下面我们将关注 Spring Cloud Zuul 的另一核心功能:过滤器(Filter) ...
- Spring Cloud学习笔记【七】服务网关 Zuul(路由)
Spring Cloud Zuul 路由是微服务架构的不可或缺的一部分,提供动态路由.监控.弹性.安全等的边缘服务.Zuul 是 Netflix 出品的一个基于 JVM 路由和服务端的负载均衡器. 准 ...
- spring cloud学习笔记五 网关服务zuul
网关服务是指,客户端发送的请求不用直接访问特定的微服务接口,而且是经过网关服务的接口进行交互,网关服务再去到特定的微服务中进行调用. 网关服务的路由功能和Nginx的反向代理一样,所有的服务都先会 ...
- Spring Cloud学习笔记--Spring Boot初次搭建
1. Spring Boot简介 初次接触Spring的时候,我感觉这是一个很难接触的框架,因为其庞杂的配置文件,我最不喜欢的就是xml文件,这种文件的可读性很不好.所以很久以来我的Spring学习都 ...
- Spring Cloud系列之Eureka服务治理
写在前面 Spring Cloud Eureka是基于Netflix Eureka做的二次封装.主要包含两部分: 服务注册中心 eureka server 服务提供者 eureka client ps ...
- Spring Cloud学习笔记-002
搭建Spring Cloud注册中心:Eureka 服务注册:在服务治理框架中,通常都会构建一个注册中心,每个服务单元向注册中心登记自己提供的服务,将主机与端口号.版本号.通信协议等一些附加信息告诉注 ...
随机推荐
- luogu3731 新型城市化
题目链接 思路 这道题对于题意的转化很关键. 题目要求的是添上一条边,使得图中最大团的大小变大.给出的边是原图的补集,这就给我们了提示. 因为题目中说,原图中最多有两个团.所以给出的边一定形成了一个二 ...
- (转)每天一个linux命令(44):top命令
背景:在面试时候面试官问到关于linux服务器下内存优化的问题.自己之前可能接触过也没有深入总结过. top命令 每天一个linux命令(44):top命令
- adb导出安卓 把手机内存文件导入到电脑里 adb安装软件
记得先找对路劲adb shellls 最上面的ls: ./ 打头的没有权限.而下面的这些acct sdcard等 都有权限. 然后cd sdcardls 看下目录,发现gxm文件夹在sdcard下面. ...
- 4.django学习模板
##引用模板 步骤: 在应用目录下创建templates目录,在目录下创建html文件 在views.py返回render(渲染) 1.requests请求本身,2.模板文件,3.后台传递到前端的数据 ...
- C# 数据库批量插入数据之 —— SqlBulkCopy、表值参数
创建了一个用来测试的Student表: CREATE TABLE [dbo].[Student]( [ID] [int] PRIMARY KEY NOT NULL, ) NULL, ) NULL, [ ...
- Mac 上有哪些值得推荐的软件?冷门小众软件但实用
确实Mac上有很多小众.冷门,但却是十分实用.值得推荐的工具,小编针对用的比较多的软件,整理了一些,希望有帮助. The Unarchive:解压缩工具 macOS 对于压缩文件的处理不是很好.如果你 ...
- django_orm查询性能优化
查询操作和性能优化 1.基本操作 增 models.Tb1.objects.create(c1='xx', c2='oo') 增加一条数据,可以接受字典类型数据 **kwargs obj = mode ...
- mac上安装虚拟机
1.Mac | 怎么安装虚拟机 2.Mac | 怎么安装VMware Fusion虚拟机 资源下载链接: 1.win7旗舰版-64位.iso 2.VMFusion811.rar
- python调用shell命令之三慷慨法
preface: 忙于近期的任务,须要用到libsvm的一些命令.如在终端执行java svm_train train_file model_file. pythonsubset.py file tr ...
- As 400错
8:25 Gradle sync started 8:25 Gradle sync failed: Unable to tunnel through proxy. Proxy returns &quo ...
