Spring Cloud入门程序——注册服务提供者
1、创建Spring Starter project
2、引入依赖

点击finish

3、创建启动类
package com.hello; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; /***
*
* @EnableDiscoveryClient
* 让服务使用eureka服务器
* 实现服务注册和发现
*
*/
@EnableDiscoveryClient
@SpringBootApplication
public class Springmvc012HelloServiceApplication { public static void main(String[] args) {
SpringApplication.run(Springmvc012HelloServiceApplication.class, args);
}
}
4、创建controller
package com.hello; import java.util.List; 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.RestController; @RestController
public class HelloController { @Autowired
DiscoveryClient discoveryClient; @RequestMapping("/hello")
public String hello() {
List<ServiceInstance> list = discoveryClient.getInstances("STORES"); System.out.println("discoveryClient.getServices().size() = " + discoveryClient.getServices().size()); for( String s : discoveryClient.getServices()){
System.out.println("services " + s);
List<ServiceInstance> serviceInstances = discoveryClient.getInstances(s);
for(ServiceInstance si : serviceInstances){
System.out.println(" services:" + s + ":getHost()=" + si.getHost());
System.out.println(" services:" + s + ":getPort()=" + si.getPort());
System.out.println(" services:" + s + ":getServiceId()=" + si.getServiceId());
System.out.println(" services:" + s + ":getUri()=" + si.getUri());
System.out.println(" services:" + s + ":getMetadata()=" + si.getMetadata());
} } System.out.println(list.size());
if (list != null && list.size() > 0 ) {
System.out.println( list.get(0).getUri() );
} return "hello,this is hello-service"; } }
5、配置项目的 application.properties
server.port=
#设置服务名
spring.application.name=hello-service
#设置服务注册中心的URL,本服务要向该服务注册中心注册自己
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka
6、浏览器访问
(1)访问服务提供者项目 的请求 /hello:

控制台打印:

(2)访问服务注册中心:
之前:

之后,再次访问localhost:1111,发现有服务注册到注册中心了

7、总结:
服务治理可以说是微服务架构中最为核心和基础的模块,它主要用来实现各个微服务实例的自动化注册和发现。
Spring Cloud Eureka是Spring Cloud Netflix 微服务套件的一部分,主要负责完成微服务架构中的服务治理功能。
本文通过简单的小例子来分享下如何通过Eureka进行服务治理:
- 搭建服务注册中心
- 注册服务提供者(本文)
- 服务发现和消费
实践终极目标:手把手,以上实例实现了基本的服务治理:
- 通过spring-cloud-starter-eureka-server和@EnableEurekaServer实现服务注册中心
- 通过spring-cloud-starter-eureka和@EnableDiscoveryClient使用并注册到服务注册中心
- 通过spring-cloud-starter-eureka和@EnableDiscoveryClient使用注册中心并发现服务,通过spring-cloud-starter-ribbon来实现负载均衡消费服务
Donate捐赠
如果我的文章帮助了你,可以赞赏我 1 元给我支持,让我继续写出更好的内容)

(微信) (支付宝)
微信/支付宝 扫一扫
Spring Cloud入门程序——注册服务提供者的更多相关文章
- Spring Cloud入门程序
本文手把手教你,做出第一个Spring Cloud程序,Eureka的简单入门使用 1.创建Spring Starter Project工程 点击next,添加项目名 2.引入Spring Cloud ...
- Spring Cloud 入门教程(一): 服务注册
1. 什么是Spring Cloud? Spring提供了一系列工具,可以帮助开发人员迅速搭建分布式系统中的公共组件(比如:配置管理,服务发现,断路器,智能路由,微代理,控制总线,一次性令牌,全局锁 ...
- spring cloud 入门系列:总结
从我第一次接触Spring Cloud到现在已经有3个多月了,当时是在博客园里面注册了账号,并且看到很多文章都在谈论微服务,因此我就去了解了下,最终决定开始学习Spring Cloud.我在一款阅读A ...
- spring cloud 入门系列四:使用Hystrix 实现断路器进行服务容错保护
在微服务中,我们将系统拆分为很多个服务单元,各单元之间通过服务注册和订阅消费的方式进行相互依赖.但是如果有一些服务出现问题了会怎么样? 比如说有三个服务(ABC),A调用B,B调用C.由于网络延迟或C ...
- Spring Cloud 入门教程(二): 配置管理
使用Config Server,您可以在所有环境中管理应用程序的外部属性.客户端和服务器上的概念映射与Spring Environment和PropertySource抽象相同,因此它们与Spring ...
- Spring Cloud入门教程(二):客户端负载均衡(Ribbon)
对于大型应用系统负载均衡(LB:Load Balancing)是首要被解决一个问题.在微服务之前LB方案主要是集中式负载均衡方案,在服务消费者和服务提供者之间又一个独立的LB,LB通常是专门的硬件,如 ...
- Spring Cloud 入门系列(一)
前言 Spring Could作为目前最流行基于Java开发的构建微服务的完整框架.发现目前相关系列教程太少,本文是基于官网教程做的一套翻译. 何为Spring Cloud? Spring Cloud ...
- Spring Cloud 服务端注册与客户端调用
Spring Cloud 服务端注册与客户端调用 上一篇中,我们已经把Spring Cloud的服务注册中心Eureka搭建起来了,这一章,我们讲解如何将服务注册到Eureka,以及客户端如何调用服务 ...
- Spring Cloud 入门教程 - 搭建配置中心服务
简介 Spring Cloud 提供了一个部署微服务的平台,包括了微服务中常见的组件:配置中心服务, API网关,断路器,服务注册与发现,分布式追溯,OAuth2,消费者驱动合约等.我们不必先知道每个 ...
随机推荐
- 将优狐智能插座接入 Domoticz
前言 前几天在某淘宝优惠中看到一个 WiFi 智能插座卖 29 块包邮,心想要是里面是 ESP8266 模块说不定可以刷上固件玩玩,就买了俩回来,记下折腾过程. 拆解 WiFi 智能插座的淘宝介绍页 ...
- doors dxl 遍历object 查找
Module m = current; //m = edit(“xxx”) Object o for o in m do { string sht = o.”shtName” Buffer bf=cr ...
- mysql 配置utf8 编码,支持 emoji 方法!!!
utf8_general_ci 已经 过时了...请以后用mysql 考虑使用 utf8mb4, utf8mb4_unicode_ci!!! 兼容性更好. mysql的utf8编码的一个字符最多3个字 ...
- Flask&&人工智能AI -- 8 HTML5+ 初识,HBuilder,夜神模拟器,Webview
昨日内容回顾 1.增删改查: 增: db.collections.insert({a:1}) // 官方不推荐了 db.collections.insertMany([{a:1},{b:1}]) in ...
- 02Data
1.数据从何而来 2.数据对象和属性类型 数据集合的类型 结构数据的重要特征 数据对象 属性 属性类型 数据属性的类型 离散 vs.连续属性 3.数据的(基本)统计描述 分布度量 代数度量 整体度量 ...
- 配置中心:Nacos, Apollo, Consul, Etcd
Nacos, Apollo, Consul, Etcd 服务.应用不同粒度的配置更丰富的路由规则集中式管理的动态参数规则
- hbase数据库操作
.实验内容与完成情况:(实验具体步骤和实验截图说明) (一)编程实现以下指定功能,并用 Hadoop 提供的 HBase Shell 命令完成相同任务: () 列出 HBase 所有的表的相关信息,例 ...
- Dev Express Report 学习总结(二)关于如何使用Grouping分组
对于所有的报表工具来说,基本上所有Grouping功能的都很相似.正如前面说到的,Group处于Page Header和Page Footer之间,同时又将Detail包括与其中. 下面还是通过一个例 ...
- Robot Framework自动化测试(一)
=============所需要环境========== Python: https://www.python.org/ RF框架是基于python 的,所以一定要有python环境. Robot f ...
- MAC环境下idea:maven+Spring+Dubbo+Zookeeper简单工程搭建
: 一:安装软件:tomcatZookeeperDubbo+admin 二:工程: 总工程 API Pom.xml:不用引用任何东西 Provider Pom.xml:要denpend ...