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. 整合feign过程中出现问题:

    一:编译器报错 这个地方是由于没有被spring管理,编译器报错,可以选择加上@comoponent这个注解 也可以选择不加,启动程序是不会报错的. 二 报错为空: org.springframewo ...

  2. Set去掉重复的元素

    String[] uids= request.getParameterValues("dxus");获取页面传过来的id //--------------------------- ...

  3. POJ1051 P,MTHBGWB

    题目来源:http://poj.org/problem?id=1051 题目大意: Morse密码里每个字母用长度不定的点和线来表示,一条信息中字母的编码之间用空隙隔开.下表为Morse密码的编码表: ...

  4. Nginx多域名负载均衡配置

    Nginx负载均衡设置 环境: 负载均衡:192.168.188.128:80 Web1:192.168.188.128:81 Web2:192.168.188.129:80 正式环境中,需要解析域名 ...

  5. 【学习笔记】jQuery的基础学习

    [学习笔记]jQuery的基础学习 新建 模板 小书匠  什么是jQuery对象? jQuery 对象就是通过jQuery包装DOM对象后产生的对象.jQuery 对象是 jQuery 独有的. 如果 ...

  6. C#工具类之字符串扩展类

    /// <summary> /// 字典串帮忙类 /// </summary> public static class StringHelper { /// <summa ...

  7. php5 编译安装

    #!/bin/bash######################################## File Name: php.sh# Version: V1.0# Author: sun yu ...

  8. Nginx配置SSL报错 nginx: [emerg] unknown directive "ssl"

    Nginx配置SSL报错 nginx: [emerg] unknown directive "ssl"     出现如图所示错误,处理办法如下 去nginx解压目录下执行 ./co ...

  9. 3.数据校验和SpringEL

    1.数据验证 数据验证不应该被限定在web层去处理,他应该在任何需要做数据验证的地方做验证: 基于以上考虑,Spring设计了一个既方便又可以在所有层使用的Validator接口 Spring提供了V ...

  10. my16_sql_thread执行慢导致主从延迟高的一个情景

    现象:从库延迟高,查看slave status发现sql_thread执行语句的速度比主库慢,这样的延迟会一直高下去,下面是排查的一些过程1. 检查了从库的配置,磁盘的写入速度的确没有主库高2. io ...