spring-cloud配置eureka客户端
spring-cloud配置eureka客户端
eureka用来发现其他程序
需要提前配置eureka服务端,具体看 https://www.cnblogs.com/ye-hcj/p/10292944.html
依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Application
package test.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
配置
spring:
application:
name: eureka-client-clustered
---
eureka:
instance:
appname: eureka-client-clustered
client:
fetchRegistry: true
serviceUrl:
defaultZone: http://eureka:9001/eureka
server:
port: 8000
---
# java -jar -Dspring.profiles.active=c1 test-0.0.1-SNAPSHOT.jar
# java -jar -Dspring.profiles.active=c2 test-0.0.1-SNAPSHOT.jar
# java -jar -Dspring.profiles.active=c3 test-0.0.1-SNAPSHOT.jar
spring:
profiles: c1
eureka:
instance:
appname: eureka-client-clustered
client:
fetchRegistry: true
serviceUrl:
defaultZone: http://eureka:9001/eureka
server:
port: 8001
---
spring:
profiles: c2
eureka:
instance:
appname: eureka-client-clustered
client:
fetchRegistry: true
serviceUrl:
defaultZone: http://eureka:9001/eureka
server:
port: 8002
---
spring:
profiles: c3
eureka:
instance:
appname: eureka-client-clustered
client:
fetchRegistry: true
serviceUrl:
defaultZone: http://eureka:9001/eureka
server:
port: 8003
Controller
package test.test.controller;
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.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Autowired
private DiscoveryClient discoveryClient;
@ResponseBody
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home() {
return "<a href='showAllServiceIds'>Show All Service Ids</a>";
}
@ResponseBody
@RequestMapping(value = "/showAllServiceIds", method = RequestMethod.GET)
public String showAllServiceIds() {
List<String> serviceIds = this.discoveryClient.getServices();
if (serviceIds == null || serviceIds.isEmpty()) {
return "No services found!";
}
String html = "<h3>Service Ids:</h3>";
for (String serviceId : serviceIds) {
html += "<br><a href='showService?serviceId=" + serviceId + "'>" + serviceId + "</a>";
}
return html;
}
@ResponseBody
@RequestMapping(value = "/showService", method = RequestMethod.GET)
public String showFirstService(@RequestParam(defaultValue = "") String serviceId) {
List<ServiceInstance> instances = this.discoveryClient.getInstances(serviceId);
if (instances == null || instances.isEmpty()) {
return "No instances for service: " + serviceId;
}
String html = "<h2>Instances for Service Id: " + serviceId + "</h2>";
for (ServiceInstance serviceInstance : instances) {
html += "<h3>Instance: " + serviceInstance.getUri() + "</h3>";
html += "Host: " + serviceInstance.getHost() + "<br>";
html += "Port: " + serviceInstance.getPort() + "<br>";
}
return html;
}
@ResponseBody
@RequestMapping(value = "/loadBalancer", method = RequestMethod.GET)
public String showLoadBalancerData() {
// 这个api是方便后面演示负载均衡的,感兴趣的可以在运行成功本例后继续阅读 https://www.cnblogs.com/ye-hcj/p/10293287.html
return "我是通过负载均衡后返回的消息";
}
}
运行
运行 mvn install 将项目打包成jar
分别执行
java -jar -Dspring.profiles.active=c1 test-0.0.1-SNAPSHOT.jar
java -jar -Dspring.profiles.active=c2 test-0.0.1-SNAPSHOT.jar
java -jar -Dspring.profiles.active=c3 test-0.0.1-SNAPSHOT.jar
然后访问,即可看到其他同时注册的程序
http://localhost:8001
http://localhost:8002
http://localhost:8003
spring-cloud配置eureka客户端的更多相关文章
- Spring Cloud配置中心客户端读取配置
微服务连接配置中心来实现外部配置的读取. 引入依赖 <dependencies> <dependency> <groupId>org.springframework ...
- Spring Cloud与Eureka
Spring Cloud与Eureka 一.使用SpringCloud注册中心Eureka 1.1 Eureka和Zookeeper对比 1.1.1 Zookeeper保证CP 1.1.2 Eurek ...
- Spring Cloud配置中心(Config)
Spring Cloud配置中心(Config) Spring Cloud是现在流行的分布式服务框架,它提供了很多有用的组件.比如:配置中心.Eureka服务发现. 消息总线.熔断机制等. 配置中心在 ...
- spring cloud 配置纲要Properties
名称 默认 描述 encrypt.fail-on-error true 标记说,如果存在加密或解密错误,进程将失败. encrypt.key 对称密钥.作为一个更强大的替代方案,考虑使用密钥库. ...
- spring cloud之eureka简介
最近线上的接口出了一些问题,有一些可能不是代码的问题,但是由于是测试和其他方面的同事爆出来的,所以感觉对接口的监控应该提上日程. 经过搜索发现,spring cloud的eureka就是专门做这方面工 ...
- Spring cloud系列教程第十篇- Spring cloud整合Eureka总结篇
Spring cloud系列教程第十篇- Spring cloud整合Eureka总结篇 本文主要内容: 1:spring cloud整合Eureka总结 本文是由凯哥(凯哥Java:kagejava ...
- Spring Boot,Spring Cloud,Eureka,Actuator,Spring Boot Admin,Stream,Hystrix
Spring Boot,Spring Cloud,Eureka,Actuator,Spring Boot Admin,Stream,Hystrix 一.Spring Cloud 之 Eureka. 1 ...
- Spring Cloud 配置服务
Spring Cloud 配置服务 1. 配置服务简介 产生背景: 传统开发中,我们通常是将系统的业务无关配置(数据库,缓存服务器)在properties中配置,在这个文件中不会经常改变,但随着系统规 ...
- springcloud(五):Spring Cloud 配置中心的基本用法
Spring Cloud 配置中心的基本用法 1. 概述 本文介绍了Spring Cloud的配置中心,介绍配置中心的如何配置服务端及配置参数,也介绍客户端如何和配置中心交互和配置参数说明. 配置中心 ...
随机推荐
- JDeveloper 10g Certified on Windows 10 for EBS 12.1
JDeveloper 10g is now certified for Windows 10 desktops for Oracle E-Business Suite 12.1. See: Reco ...
- WCF基础:绑定(一)
WCF中的终结点(ServiceEndpoint)包含有三要素:地址(EndpointAddress),绑定(Binding),契约描述(ContractDescription)三要素:其中绑定的在整 ...
- [eShopOnContainers 学习系列] - 03 - 在远程 Ubuntu 16.04 上配置开发环境
直接把 md 粘出来了,博客园的富文本编辑器换成 markdown,没啥效果呀 ,先凑合吧.实在不行换地方 # 在远程 Ubuntu 16.04 上配置开发环境 ## 零.因 为什么要用这么麻烦的 ...
- LeetCode OJ:Permutations(排列)
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- LeetCode OJ:Three Sum(三数之和)
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- 20165210 Java第五次实验报告
20165210 实验五 网络编程与安全 网络编程与安全一: 实验要求: 两人一组结对编程: 参考http://www.cnblogs.com/rocedu/p/6766748.html#SECDSA ...
- New Concept English three (43)
30 54 Insurance companies are normally willing to insure anything. Insuring public or private proper ...
- AS3中ASCII码和字符互转函数
AS3中ASCII码和字符互转函数 字符转成ASCII码: 格式:字符串变量.charCodeAt(字符位置); var str:String = “A”; trace(str.charCodeAt( ...
- 边缘检测︱基于 HED网络TensorFlow 和 OpenCV 实现图片边缘检测
本文摘录自<手机端运行卷积神经网络的一次实践 – 基于 TensorFlow 和 OpenCV 实现文档检测功能> 只截取感兴趣 的片段. . 一.边缘检测 1.传统边缘检测 Google ...
- React Native探索(五)使用fetch进行网络请求
相关文章 React Native探索系列 前言 React Native可以使用多种方式来请求网络,比如fetch.XMLHttpRequest以及基于它们封装的框架,fetch可以说是替代XMLH ...