建立eureka服务和客户端(客户端获取已经注册服务)
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服务和客户端(客户端获取已经注册服务)的更多相关文章
- dotnet 通过 WMI 获取系统启动的服务
本文告诉大家如何通过 WMI 获取系统启动的服务 通过 Win32_Service 可以获取系统启动的服务 获取的时候只需要拿Caption和State就可以 var mc = "Win32 ...
- 解析 .Net Core 注入 (1) 注册服务
在学习 Asp.Net Core 的过程中,注入可以说是无处不在,对于 .Net Core 来说,它是独立的一个程序集,没有复杂的依赖项和配置文件,所以对于学习 Asp.Net Core 源码的朋友来 ...
- 解析 .Net Core 注入——注册服务
在学习 Asp.Net Core 的过程中,注入可以说是无处不在,对于 .Net Core 来说,它是独立的一个程序集,没有复杂的依赖项和配置文件,所以对于学习 Asp.Net Core 源码的朋友来 ...
- .Net Core 注入学习——注册服务
解析 .Net Core 注入——注册服务发表于:2017-10-23 10:47 作者:行走即歌 来源:51Testing软件测试网采编字体:大 中 小 | 上一篇 | 下一篇 |我要投稿 | 推荐 ...
- SpringCloud02 Eureka知识点、Eureka服务端和客户端的创建、Eureka服务端集群、Eureka客户端向集群的Eureka服务端注册
1 Eureka知识点 按照功能划分: Eureka由Eureka服务端和Eureka客户端组成 按照角色划分: Eureka由Eureka Server.Service Provider.Servi ...
- 【Eureka】服务端和客户端
[Eureka]服务端和客户端 转载:https://www.cnblogs.com/yangchongxing/p/10778357.html Eureka服务端 1.添加依赖 <?xml v ...
- 阶段5 3.微服务项目【学成在线】_day09 课程预览 Eureka Feign_05-Feign远程调用-客户端负载均衡介绍
2 Feign远程调用 在前后端分离架构中,服务层被拆分成了很多的微服务,服务与服务之间难免发生交互,比如:课程发布需要调用 CMS服务生成课程静态化页面,本节研究微服务远程调用所使用的技术. 下图是 ...
- java版gRPC实战之六:客户端动态获取服务端地址
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- eureka服务端和客户端的简单搭建
本篇博客简单记录一下,eureka 服务端和 客户端的简单搭建. 目标: 1.完成单机 eureka server 和 eureka client 的搭建. 2.完成eureka server 的添加 ...
随机推荐
- 18.有一个网页地址, 比如PHP开发资源网主页: http://www.phpres.com/index.html,如何得到它的内容?
方法1(对于PHP5及更高版本): $readcontents = fopen("http://www.phpres.com/index.html", "rb" ...
- 今天携程出事了:让我们来学习下http的响应码
就在今天,2015年5月28日,中国最大的旅游机票预订网站--携程网粗大事了.据传携程网的数据库被人物理删除了,而容灾备份的数据又无法正常使用,服务器全面遭受瘫痪.每小时给携程带来的损失约100万美元 ...
- 分享一个动态生成RDLC报表的类
在实际工作中,当需要进行大批量查询和生成报表的时候,可以使用我写的类. 特点: 无需报表设计器.无需为报表设置数据集 只需要传入查询结果就可以全自动生成报表,传入的对象为Dynamic(目前支持Dat ...
- C#在高性能计算领域为什么性能却如此不尽人意
C#的优雅,强大IDE的支持,.net下各语言的二进制兼容,自从第一眼看到C#就被其良好的设计吸引.一直希望将其应用于高性能计算领域,长时间努力却效果却不尽如人意. 对于小的测试代码用例而言,C#用2 ...
- Html5的表单
跨浏览器 https://github.com/westonruter/webforms2 https://github.com/Modernizr/Modernizr http://www.blog ...
- Hi, there!
Wish you a happy day~我们一起玩WEB前端!^ ^
- nodejs的初学
1.启服务器.先server.js,再命令行输入命令node server.js,打开浏览器输入http://127.0.0.1:2016可以看到有内容输出. server.js代码如下: var h ...
- JQuery常用方法一览
$(”p”).addClass(css中定义的样式类型); 给某个元素添加样式 $(”img”).attr({src:”test.jpg”,alt:”test Image”}); 给某个元素添加属性/ ...
- Ubuntu 16.04应用布署小记
本系列文章记录了升级Ubuntu 16.04的布署过程 回到目录 10. Dokuwiki 将虚拟目录配置文件链接到Apache2的可用的配置文件库中 sudo ln -s /opt/dokuwiki ...
- jquery_ui_api中文
http://files.cnblogs.com/files/xjyggd/JQuery_UI_API.rar