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客户端的更多相关文章

  1. Spring Cloud配置中心客户端读取配置

    微服务连接配置中心来实现外部配置的读取. 引入依赖 <dependencies> <dependency> <groupId>org.springframework ...

  2. Spring Cloud与Eureka

    Spring Cloud与Eureka 一.使用SpringCloud注册中心Eureka 1.1 Eureka和Zookeeper对比 1.1.1 Zookeeper保证CP 1.1.2 Eurek ...

  3. Spring Cloud配置中心(Config)

    Spring Cloud配置中心(Config) Spring Cloud是现在流行的分布式服务框架,它提供了很多有用的组件.比如:配置中心.Eureka服务发现. 消息总线.熔断机制等. 配置中心在 ...

  4. spring cloud 配置纲要Properties

    名称 默认 描述 encrypt.fail-on-error true 标记说,如果存在加密或解密错误,进程将失败. encrypt.key   对称密钥.作为一个更强大的替代方案,考虑使用密钥库. ...

  5. spring cloud之eureka简介

    最近线上的接口出了一些问题,有一些可能不是代码的问题,但是由于是测试和其他方面的同事爆出来的,所以感觉对接口的监控应该提上日程. 经过搜索发现,spring cloud的eureka就是专门做这方面工 ...

  6. Spring cloud系列教程第十篇- Spring cloud整合Eureka总结篇

    Spring cloud系列教程第十篇- Spring cloud整合Eureka总结篇 本文主要内容: 1:spring cloud整合Eureka总结 本文是由凯哥(凯哥Java:kagejava ...

  7. 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 ...

  8. Spring Cloud 配置服务

    Spring Cloud 配置服务 1. 配置服务简介 产生背景: 传统开发中,我们通常是将系统的业务无关配置(数据库,缓存服务器)在properties中配置,在这个文件中不会经常改变,但随着系统规 ...

  9. springcloud(五):Spring Cloud 配置中心的基本用法

    Spring Cloud 配置中心的基本用法 1. 概述 本文介绍了Spring Cloud的配置中心,介绍配置中心的如何配置服务端及配置参数,也介绍客户端如何和配置中心交互和配置参数说明. 配置中心 ...

随机推荐

  1. 不能在Python Console中运行pytest

    在Python Console中运行pytest发现报错了 这是为什么?因为Python Console已经是进入python之后的环境,就像在python自带的IDLE中运行pytest pytes ...

  2. HBase Cassandra比较

    转自:http://itindex.net/detail/22338-cassandra-hbase-%E8%AE%BE%E8%AE%A1     Cassandra HBase 一致性 Quorum ...

  3. 【sparkSQL】DataFrame的常用操作

    scala> import org.apache.spark.sql.SparkSession import org.apache.spark.sql.SparkSession scala> ...

  4. hibernate13--缓存

    <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN&q ...

  5. eclipse配置及常用快捷键

    1. eclipse查看一个方法被谁引用(调用)的快捷键四种方式 1.(首推)双击选中该方法,Ctrl+Alt+H  如果你想知道一个类的方法到底被那些其他的类调用,那么请选中这个方法名,然后按“Ct ...

  6. EditPlus保存时不生成bak文件(转)

    如何设置EditPlus保存时不生成bak文件 EditPlus是一个强大的编辑工具,不单单是编辑文字强大,很多的刚开始学习编程语言的初学者会选择它,例如html,js,php,java.小编刚开始学 ...

  7. .Net 中的IL中间语言基本语法

    一.前言 IL是什么? Intermediate Language (IL)微软中间语言 C#代码编译过程? C#源代码通过LC转为IL代码,IL主要包含一些元数据和中间语言指令: JIT编译器把IL ...

  8. ndoutils_mq项目: 发送Nagios的性能、报警、配置文件到RabbitMQ

    本人目前开发的一个项目,改造ndoutils,主要是它的ndomod. 将性能.报警.配置文件使用JSON格式发送到RabbitMQ. 由于NEB(Nagios Event Broker)使用C开发, ...

  9. ORACLE PL/SQL编程之触发器

    8.1 触发器类型 8.1.1 DML触发器 8.1.2 替代触发器 8.1.3 系统触发器 8.2 创建触发器 8.2.1 触发器触发次序 8.2.2 创建DML触发器 8.2.3 创建替代(INS ...

  10. Java JDK安装和配置(Windows)

    安装和配置JDK JDK中自带了JRE,不需要单独下载, 打开JDK安装, 选择安装目录,下一步,装完JDK,会问是否安装JRE,选下一步, 最后还会问是否安装Java FX, 装完后就全部完成了JD ...