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,消费者驱动合约等.我们不必先知道每个 ...
随机推荐
- vs已停止工作的解决方案
解决办法 第一步: 开始-->所有程序-->Microsoft Visual Studio 2013(2015)-->VisualStudio Tools-->VS2013(2 ...
- redis修改密码和更改端口
Liunx下redis修改密码和更改端口 redis一个实例就是一个节点,每个节点分配一个端口号,每个节点对应一个redis.conf配置文件. redis默认配置的端口号是6379,假设现在要多配置 ...
- IE浏览器提示对象不支持“append”属性或方法
如下代码在IE浏览器中无法执行,提示对象不支持“append”属性或方法 var tImg = document.createElement("img"); tImg.setAtt ...
- Codeforces-Salem and Sticks(枚举+思维)
Salem gave you nn sticks with integer positive lengths a1,a2,-,ana1,a2,-,an. For every stick, you ca ...
- P4824 [USACO15FEB]Censoring (Silver) 审查(银)
传送门 一个串的匹配肯定考虑KMP 那就暴力KMP 记录一下到每个字符时匹配的位置 找到一个符合的串就标记然后暴力回跳 感觉好像太暴力了... #include<iostream> #in ...
- Codeforces Round #482 (Div. 2) B、Treasure Hunt(模拟+贪心)979B
题目 大致题意 n表示要进行n次操作,接着给出三个字符串,表示三个人初始拥有的串.每次操作要替换字符串中的字母,询问最后在游戏中曾出现过的相同的子串谁最多. 思路 (1) 讨论最多的子串,肯定是全部 ...
- 一个基于QT简单登录对话框
1. 登录对话框式样 2. QLoginDialog.h #ifndef DIALOG_H #define DIALOG_H #include <QtGui/QDialog> #inclu ...
- Linux安装Sqlmap等工具
简单记录一下安装过程,都是小白教程,省的哪天又忘了要去百度. 1.下载sqlmap 源码进行安装 wget https://github.com/sqlmapproject/sqlmap/tarbal ...
- Python数据操作
列表操作 保存matrix或者保存ndarray 数据类型转换 读取CSV某列 numpy数组写入到csv pandas to_csv 最左边 多一列 的问题 DataFrame对象操作
- Java Jdk 环境配置和测试
测试jdk:1.6 安装完jdk1.6以后,在我的电脑中找到环境变量配置 在系统变量的Path 中输入 ;E:\Program Files (x86)\Java\jdk1.6.0_01\bin(路径根 ...