1. 新建sping boot eureka server

新建立spring starter  project

修改pom.xml文件

在parent后追加

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

追加

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>

应用

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication
@EnableEurekaServer
public class SpringBootEurekaServerApplication { public static void main(String[] args) {
SpringApplication.run(SpringBootEurekaServerApplication.class, args);
}
}

属性文件

server.port=8761

spring.application.name=Spring-Boot-Admin-Web
eureka.instance.hostname=localhost eureka.client.registerWithEureka=true
eureka.client.fetchRegistry=false
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

2. 建立spring boot eureka client

pom.xml

   	<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>

应用

package com.example;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @SpringBootApplication
@RestController
@EnableEurekaClient
public class SpringBootEurekaClientApplication { @Autowired
private DiscoveryClient discoveryClient; @RequestMapping("/")
String home() {
return "Hello World!";
} @RequestMapping("/registered")
public String getRegistered(){
List<ServiceInstance> list = discoveryClient.getInstances("STORES");
System.out.println(discoveryClient.getLocalServiceInstance());
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 null;
} public static void main(String[] args) {
SpringApplication.run(SpringBootEurekaClientApplication.class, args);
}
}

配置文件

spring.application.name=cloud-simple-service
server.port=8011 eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

建立eureka服务和客户端(客户端获取已经注册服务)的更多相关文章

  1. dotnet 通过 WMI 获取系统启动的服务

    本文告诉大家如何通过 WMI 获取系统启动的服务 通过 Win32_Service 可以获取系统启动的服务 获取的时候只需要拿Caption和State就可以 var mc = "Win32 ...

  2. 解析 .Net Core 注入 (1) 注册服务

    在学习 Asp.Net Core 的过程中,注入可以说是无处不在,对于 .Net Core 来说,它是独立的一个程序集,没有复杂的依赖项和配置文件,所以对于学习 Asp.Net Core 源码的朋友来 ...

  3. 解析 .Net Core 注入——注册服务

    在学习 Asp.Net Core 的过程中,注入可以说是无处不在,对于 .Net Core 来说,它是独立的一个程序集,没有复杂的依赖项和配置文件,所以对于学习 Asp.Net Core 源码的朋友来 ...

  4. .Net Core 注入学习——注册服务

    解析 .Net Core 注入——注册服务发表于:2017-10-23 10:47 作者:行走即歌 来源:51Testing软件测试网采编字体:大 中 小 | 上一篇 | 下一篇 |我要投稿 | 推荐 ...

  5. SpringCloud02 Eureka知识点、Eureka服务端和客户端的创建、Eureka服务端集群、Eureka客户端向集群的Eureka服务端注册

    1 Eureka知识点 按照功能划分: Eureka由Eureka服务端和Eureka客户端组成 按照角色划分: Eureka由Eureka Server.Service Provider.Servi ...

  6. 【Eureka】服务端和客户端

    [Eureka]服务端和客户端 转载:https://www.cnblogs.com/yangchongxing/p/10778357.html Eureka服务端 1.添加依赖 <?xml v ...

  7. 阶段5 3.微服务项目【学成在线】_day09 课程预览 Eureka Feign_05-Feign远程调用-客户端负载均衡介绍

    2 Feign远程调用 在前后端分离架构中,服务层被拆分成了很多的微服务,服务与服务之间难免发生交互,比如:课程发布需要调用 CMS服务生成课程静态化页面,本节研究微服务远程调用所使用的技术. 下图是 ...

  8. java版gRPC实战之六:客户端动态获取服务端地址

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  9. eureka服务端和客户端的简单搭建

    本篇博客简单记录一下,eureka 服务端和 客户端的简单搭建. 目标: 1.完成单机 eureka server 和 eureka client 的搭建. 2.完成eureka server 的添加 ...

随机推荐

  1. Super Jumping! Jumping! Jumping!

    Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumping!” is very popular in HDU. May ...

  2. android studio 项目迁移编码问题

    关于编码问题.首先,eclipse上一般我们文件默认都设置成UTF-.对于迁移到Android Studio显示不存在乱码问题. 部分同学可能会遇到一个问题:代码中的中文(包括注释的中文),在编译时跳 ...

  3. link和@import的区别、及各自的应用

    面试的过程中遇到的问题,当时自己回答的感觉自己心里还是很满意的,但是回来百度查看后才知道自己回答的有多么的糟糕: 下面我这这个知识点做一些总结的书面说明,为了少走点弯路,多涨点见识吧. 首先我们要了解 ...

  4. 使用requireJS,backboneJS,和underscoreJS完成自定义模板封装

    使用requireJS,backboneJS,和underscoreJS完成自定义模板封装 原来的代码 当我们进行一个列表的数据填充的时候,是这样做的: //获取美食列表 function getFo ...

  5. SQLServer 游标 (A)

    游标 游标分为客户端游标和服务器端游标.Sql通过游标可以对一个结果集进行逐行处理.对于使用服务器端游标的过程有:声明.打开.读取.关闭.释放. 1 声明游标 1.1 SQL-92标准的声明 Decl ...

  6. https基础流程

    背景: https基于SSL,目的是保护http通信的过程,防止中间人篡改信息,或假冒服务端的问题.   要解决的问题: 1. 客户端如何证明是与正确的服务端进行通信 2. 客户端如何确认收到服务端的 ...

  7. 遇到的java面试题

    1.struts2与struts1的区别 2.声明式事务是什么,怎么实现? 3.ajax两种请求方式 4.java中string str=new string("ss")创建了个几 ...

  8. JavaScript打印正倒直线

    做了一个作业,用JavaScript打印正倒直线,突然觉得自己还是逻辑有待加强训练啊 document.write("<h3>打印倒正金字塔直线</h3>" ...

  9. php 笔试题

    1.用PHP打印出前一天的时间格式是2006-5-10 22:21:21 解:echo date(‘Y-m-d H:i:s’, strtotime(‘-1 day’)); 原因: format 字符说 ...

  10. Java 自定义客户端与服务器

    一:浏览器如何请求数据基本原理 基本原理: 当浏览器输入地址向服务器请求数据时,实际上浏览器会在内部建立一个Socket对象,把http请求报文转变成byet[]字节,然后调用Socket的方法把这些 ...