Spring Cloud入门程序
本文手把手教你,做出第一个Spring Cloud程序,Eureka的简单入门使用
1、创建Spring Starter Project工程
点击next,添加项目名
2、引入Spring Cloud 的 Eureka
点击next
点击 finish
3、配置项目的 application.properties
#设置tomcat服务端口号
server.port=
#设置服务名称
spring.application.name=eureka-service eureka.instance.hostname=localhost
#注册中心不需要注册自己
eureka.client.register-with-eureka=false
#注册中心不需要去发现服务
eureka.client.fetch-registry=false
#设置服务注册中心的URL
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka
注意:
在默认设置下,Eureka服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为。
禁止方式如下:在application.properties配置文件中增加以下内容
eureka.client.register-with-eureka=
false
eureka.client.fetch-registry=
false
或者com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
4、创建启动类
用Spring Boot创建一个服务类Springmvc011Application ,需要一个注解@EnableEurekaServer加在springboot工程的启动类上
package com.hello; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; /**
*
* @EnableEurekaServer
* 用来指定该项目为Eureka的服务注册中心
*/
@EnableEurekaServer
@SpringBootApplication
public class Springmvc011Application { public static void main(String[] args) {
SpringApplication.run(Springmvc011Application.class, args);
}
}
5、启动服务并访问,我们会看到这样的画面:
6、总结:
服务治理可以说是微服务架构中最为核心和基础的模块,它主要用来实现各个微服务实例的自动化注册和发现。
Spring Cloud Eureka是Spring Cloud Netflix 微服务套件的一部分,主要负责完成微服务架构中的服务治理功能。
本文通过简单的小例子来分享下如何通过Eureka进行服务治理:
- 搭建服务注册中心(本文)
- 注册服务提供者
- 服务发现和消费
Donate捐赠
如果我的文章帮助了你,可以赞赏我 1 元给我支持,让我继续写出更好的内容)
(微信) (支付宝)
微信/支付宝 扫一扫
Spring Cloud入门程序的更多相关文章
- Spring Cloud入门程序——注册服务提供者
1.创建Spring Starter project 2.引入依赖 点击finish 3.创建启动类 package com.hello; import org.springframework.boo ...
- Spring Cloud 入门教程(十):和RabbitMQ的整合 -- 消息总线Spring Cloud Netflix Bus
在本教程第三讲Spring Cloud 入门教程(三): 配置自动刷新中,通过POST方式向客户端发送/refresh请求, 可以让客户端获取到配置的最新变化.但试想一下, 在分布式系统中,如果存在很 ...
- Spring Cloud 入门教程(二): 配置管理
使用Config Server,您可以在所有环境中管理应用程序的外部属性.客户端和服务器上的概念映射与Spring Environment和PropertySource抽象相同,因此它们与Spring ...
- spring cloud 入门系列:总结
从我第一次接触Spring Cloud到现在已经有3个多月了,当时是在博客园里面注册了账号,并且看到很多文章都在谈论微服务,因此我就去了解了下,最终决定开始学习Spring Cloud.我在一款阅读A ...
- Spring Cloud 入门系列(一)
前言 Spring Could作为目前最流行基于Java开发的构建微服务的完整框架.发现目前相关系列教程太少,本文是基于官网教程做的一套翻译. 何为Spring Cloud? Spring Cloud ...
- spring cloud 入门系列四:使用Hystrix 实现断路器进行服务容错保护
在微服务中,我们将系统拆分为很多个服务单元,各单元之间通过服务注册和订阅消费的方式进行相互依赖.但是如果有一些服务出现问题了会怎么样? 比如说有三个服务(ABC),A调用B,B调用C.由于网络延迟或C ...
- Spring Cloud 入门教程 - 搭建配置中心服务
简介 Spring Cloud 提供了一个部署微服务的平台,包括了微服务中常见的组件:配置中心服务, API网关,断路器,服务注册与发现,分布式追溯,OAuth2,消费者驱动合约等.我们不必先知道每个 ...
- Spring Cloud 入门教程(七): 熔断机制 -- 断路器
对断路器模式不太清楚的话,可以参看另一篇博文:断路器(Curcuit Breaker)模式,下面直接介绍Spring Cloud的断路器如何使用. SpringCloud Netflix实现了断路器库 ...
- Spring Cloud 入门教程(六): 用声明式REST客户端Feign调用远端HTTP服务
首先简单解释一下什么是声明式实现? 要做一件事, 需要知道三个要素,where, what, how.即在哪里( where)用什么办法(how)做什么(what).什么时候做(when)我们纳入ho ...
随机推荐
- redis之常用Set和ZSet命令
redis五种数据类型:String,Hash,List,Set,ZSet.五种数据类型各有优点,各司其职.最后两种数据类型Set和ZSet基本上是一种,ZSet就是给Set加了个排序而已(相当于加了 ...
- [Groovy]Parse properties file in Groovy
def props = new Properties() new File("foo.properties").withInputStream { s -> props.lo ...
- Hibernate常见报错
1.A different object with the same identifier value was already associated with the session(使用Hibern ...
- aspectj 与 spring 自定义注解
** * ErrorCode: * * @author yangzhenlong * @since 2016/7/21 */ @Target({ElementType.METHOD}) @Retent ...
- P4245 【模板】任意模数NTT
Luogu4245 只要做三次的NTT,快的飞起 普通NTT,做9次 #include<cstdio> #include<cstring> #include<iostre ...
- Java实现范围内随机数
JDK1.7及以上 int randomNum = ThreadLocalRandom.current().nextInt(min, max + 1); // 包含max,所以要加1 JDK1.7以前 ...
- 【Effective Java】阅读
Java写了很多年,很惭愧,直到最近才读了这本经典之作<Effective Java>,按自己的理解总结下,有些可能还不够深刻 一.Creating and Destroying Obje ...
- man bash 关于shell的应有尽有 语法、快捷键...
文件加载顺序 for if case ... 语法 往前移动一个单词 alt f https://github.com/hokein/Wiki/wiki/Bash-Shell%E5%B8%B8%E7% ...
- 技巧:Python中print打印信息的同时打印文件、行号
import sys def Log(msg): print('Print Message: '+msg+' ,File: "'+__file__+'", Line '+str(s ...
- Ancoda 下的python多版本共存
Ancoda 下的python多版本共存 virtualenv python 多版本共存 Pip, Virtualenv 和Fabric 被称为python 的三大神器,Pip 是包管理工具,Virt ...