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入门程序——注册服务提供者的更多相关文章

  1. Spring Cloud入门程序

    本文手把手教你,做出第一个Spring Cloud程序,Eureka的简单入门使用 1.创建Spring Starter Project工程 点击next,添加项目名 2.引入Spring Cloud ...

  2. Spring Cloud 入门教程(一): 服务注册

    1.  什么是Spring Cloud? Spring提供了一系列工具,可以帮助开发人员迅速搭建分布式系统中的公共组件(比如:配置管理,服务发现,断路器,智能路由,微代理,控制总线,一次性令牌,全局锁 ...

  3. spring cloud 入门系列:总结

    从我第一次接触Spring Cloud到现在已经有3个多月了,当时是在博客园里面注册了账号,并且看到很多文章都在谈论微服务,因此我就去了解了下,最终决定开始学习Spring Cloud.我在一款阅读A ...

  4. spring cloud 入门系列四:使用Hystrix 实现断路器进行服务容错保护

    在微服务中,我们将系统拆分为很多个服务单元,各单元之间通过服务注册和订阅消费的方式进行相互依赖.但是如果有一些服务出现问题了会怎么样? 比如说有三个服务(ABC),A调用B,B调用C.由于网络延迟或C ...

  5. Spring Cloud 入门教程(二): 配置管理

    使用Config Server,您可以在所有环境中管理应用程序的外部属性.客户端和服务器上的概念映射与Spring Environment和PropertySource抽象相同,因此它们与Spring ...

  6. Spring Cloud入门教程(二):客户端负载均衡(Ribbon)

    对于大型应用系统负载均衡(LB:Load Balancing)是首要被解决一个问题.在微服务之前LB方案主要是集中式负载均衡方案,在服务消费者和服务提供者之间又一个独立的LB,LB通常是专门的硬件,如 ...

  7. Spring Cloud 入门系列(一)

    前言 Spring Could作为目前最流行基于Java开发的构建微服务的完整框架.发现目前相关系列教程太少,本文是基于官网教程做的一套翻译. 何为Spring Cloud? Spring Cloud ...

  8. Spring Cloud 服务端注册与客户端调用

    Spring Cloud 服务端注册与客户端调用 上一篇中,我们已经把Spring Cloud的服务注册中心Eureka搭建起来了,这一章,我们讲解如何将服务注册到Eureka,以及客户端如何调用服务 ...

  9. Spring Cloud 入门教程 - 搭建配置中心服务

    简介 Spring Cloud 提供了一个部署微服务的平台,包括了微服务中常见的组件:配置中心服务, API网关,断路器,服务注册与发现,分布式追溯,OAuth2,消费者驱动合约等.我们不必先知道每个 ...

随机推荐

  1. 使用between and 作为查询条件

  2. 网络工程18级《C++程序设计II》实践作业1

    A.类的应用1 Time Limit: 1000 MS Memory Limit: 32768 K Total Submit: 162 (133 users) Total Accepted: 136 ...

  3. 读经典——《CLR via C#》(Jeffrey Richter著) 笔记_.Net Framework 部署目标

    1.解决Windows性能不稳定: 2.降低Windows程序安装的复杂性: 3.解决Windows程序不安全性: 4.解决应用程序状态在硬盘上分散: 5.允许用户灵活地控制哪些东西能够安装,哪些东西 ...

  4. BFS + 状态搜索

    题目 题意 给一个100x100的迷宫,'.'表示路面,'S'表示起点,'T'表示终点:'#'表示毒气区,进入毒气区必须要消耗一个氧气:'B'表示氧气区,每次进入自动获得一个氧气,可反复进入从而获得多 ...

  5. Linux下如何使用Wireshark进行抓包

    1. 安装wireshark Ubuntu 14.04.3 缺省安装后, 不包含Wireshark抓包软件,因此首先需要手工进行Wireshark的安装:     apt-get update apt ...

  6. 工作中常用的linux命令(持续更新)

    一.top 实时动态地查看系统的整体运行情况1.在top命令后 > < 切换排序方式,根据cpu排名或者内存排名查看 2.top -p 进程pid 查看某一进程的整体运行情况 二.解压缩 ...

  7. Java学习笔记day03_引用数据类型

    1.引用数据类型 步骤: 1. 导包   2. 创建引用类型变量 类型 变量名 = new 类型名();   3. 使用数据类型的功能 变量名.功能名(); 如Scanner类: import jav ...

  8. my04_Mysql复制数据一致性校验

    1. 搭建一套双节点的Mysql主从复制数据库 2. 主库初始化测试数据 drop table if exists test; ),test_id int NOT NULL AUTO_INCREMEN ...

  9. js学习笔记 -- await/ async

    await 暂停async function函数,等待Promise处理完成,若Promise 状态为fulfilled,其回调resolve的参数作为await的值,Promise 状态为rejec ...

  10. Mybatis学习笔记7 - select查询的相关属性使用

    1.当接口的返回类型是集合List时,resultType要写集合中元素的类型 示例如下: 接口定义: package com.mybatis.dao; import com.mybatis.bean ...