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的配置中心,介绍配置中心的如何配置服务端及配置参数,也介绍客户端如何和配置中心交互和配置参数说明. 配置中心 ...
随机推荐
- python3.7安装模块MySQLdb报错error: Microsoft Visual C++ 14.0 is required.
error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools&quo ...
- Educational Codeforces Round 13
http://codeforces.com/contest/678 A:水题 #include<bits/stdc++.h> #define fi first #define se sec ...
- 实现QQ抽屉效果
代码: #coding: utf-8 from PyQt4.QtCore import * from PyQt4.QtGui import * import sys QTextCodec.setCod ...
- shell编程学习1
1.shell是操作系统的终端命令行 (1)shell可以理解为软件系统提供给用户操作的命令行界面,可以说它是人机交互的一种方式. (2)我们可以使用shell和操作系统.uboot等软件系统进 ...
- Mac下webpack安装
最近开始接触构建工具webpack,公司电脑是 windows,而我自己的呢是mac.本来以为在自己电脑安装很简单,但是出了点问题,所以写出来分享下. 这里用npm的方式安装,首先你要安装node.j ...
- react antd 动态表单
import React, { Component } from 'react'; import ReactDOM from 'react-dom'; import { Form, InputNumb ...
- 20165210 Java第四次实验报告
20165210 实验四 Android程序设计 实验步骤 第24章:初识Android 任务一:完成Hello World, 要求修改res目录中的内容,Hello World后要显示自己的学号 学 ...
- Android系统代码查询命令集合
Android系统代码查询命令集合 *#06# 显示MEID *#*#4636#*#* 显示版本,或更新相机韧体 *#*#7594#*#* 当长按关机按钮时,会出现一个切换手机部分设置及更改设定 WL ...
- Android常见面试笔试题目
Android常见面试笔试题目 1.在多线程编程这块,我们经常要使用Handler,Thread和Runnable这三个类,那么他们之间的关系你是否弄清楚了呢? 答:可以处理消息循环的线程,他是一个拥 ...
- Thrift之java实例
一.java实例 1.下载与安装thrift工具 http://thrift.apache.org/download/ .服务器代码 服务Test实现类 package com.zychen.thri ...