这一章节主要讲如何搭建eureka-client项目.

在我们生成的Demo项目上右键点击New->Module->spring Initializr, 然后next, 填写Group和Artifact等信息,

这里Artifact填写eurekaclient, 再次next,

这里选择的Web下的web和Cloud Discovery下的Eureka Discovery.

最后在Module Name中填写eureka-client.

生成的pom.xml文件如下:

<?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.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xum</groupId>
<artifactId>eureka-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-client</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>
<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</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>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
</project>

然后在EurekaClientApplication上加@EnableEurekaClient注解:

package com.xum.eurekaclient;

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

然后新建application.yml文件, 内容如下:

eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/ 这里的defaultZone是上一章节的对应eureka-server
server:
port: 8762
spring:
application:
name: eureka-client

然后写一个controller去获取数据, 这里是获取git上的数据, 关于config-server的项目下一章节说

package com.xum.eurekaclient.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
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.RestController; import java.lang.reflect.Method;
import java.util.List; @RefreshScope //这里加上RefreshScope注解,这样才能获取到git上的内容
@RestController
@RequestMapping(value = "/testone")
public class TestOneController { private static final Logger LOG = LoggerFactory.getLogger(TestOneController.class); @Autowired
private DiscoveryClient discoveryClient; @Value("${server.port}")
String port; /* from git config */
@Value("${democonfigclient.message}")
String message; /* from git config */
@Value("${foo}")
String foo; @RequestMapping(value = "/test")
public String test(@RequestParam(value = "name", required = false, defaultValue = "testOneClient")String name) {
/*List<ServiceInstance> list = discoveryClient.getInstances("eureka-client");
LOG.info("discoveryClient.getServices().size() = " + discoveryClient.getServices().size());
for( String s : discoveryClient.getServices()){
List<ServiceInstance> serviceInstances = discoveryClient.getInstances(s);
for(ServiceInstance si : serviceInstances){
LOG.info("services:" + s + ":getHost()=" + si.getHost());
LOG.info("services:" + s + ":getPort()=" + si.getPort());
LOG.info("services:" + s + ":getServiceId()=" + si.getServiceId());
LOG.info("services:" + s + ":getUri()=" + si.getUri());
}
}*/
String info = "Hi " + name + ", this is EurekaClient, port is " + port;
return info;
} @RequestMapping(value = "/config", method = RequestMethod.GET)
public String config() {
String info = "port:" + port + ",message:" + message + ",foo:" + foo;
return info;
}
}

最后通过Run DashBoard运行项目,

首先运行上一章节的eureka-server项目, 然后运行eureka-client的项目.

在浏览器上输入http://localhost:8761,显示如下: (我这里同时运行了config-server项目, 这个项目下一章节讲)

先在浏览器中输入http://localhost:8762/testone/test, 显示如下

port是eureka-client的端口

然后在浏览器中输入http://localhost:8762/testone/config, 显示如下

port是eureka-client的端口, message和foo是从git仓库中获取的, 是通过config-server项目获取的.

下一章节讲config-server项目的搭建.

SpringCloud的学习记录(2)的更多相关文章

  1. SpringCloud的学习记录(1)

    最近一段时间重新学习一边SpringCloud(有半年不用了),这里简单记录一下. 我用的是IntelliJ IDEA开发工具, SpringBoot的版本是2.1.3.RELEASE. 1. 构建M ...

  2. SPRINGCLOUD 开发学习记录

    一个简单的微服务系统:服务注册和发现,服务消费,负载均衡,断路器,智能路由,配置管理 服务注册中心: eureka是一个高可用组件,没有后端缓存,每一个实例注册后向注册中心发送心跳,默认情况下,eru ...

  3. SpringCloud的学习记录(8)

    这一章节讲zipkin-server. 在我们生成的Demo项目上右键点击New->Module->spring Initializr, 然后next, 填写Group和Artifact等 ...

  4. SpringCloud的学习记录(7)

    这一章节讲zuul的使用. 在我们生成的Demo项目上右键点击New->Module->spring Initializr, 然后next, 填写Group和Artifact等信息, 这里 ...

  5. SpringCloud的学习记录(6)

    这一章节讲fegin的使用. 在我们生成的Demo项目上右键点击New->Module->spring Initializr, 然后next, 填写Group和Artifact等信息, 这 ...

  6. SpringCloud的学习记录(5)

    这一章节讲如何使用ribbon和hystrix. 在我们生成的Demo项目上右键点击New->Module->spring Initializr, 然后next, 填写Group和Arti ...

  7. SpringCloud的学习记录(3)

    这一章节讲搭建config-server的项目. 在我们生成的Demo项目上右键点击New->Module->spring Initializr, 然后next, 填写Group和Arti ...

  8. SpringCloud的学习记录(4)

    本篇基于上一篇写的, 在git上更改配置后, eureka-client如何更新. 我们只需要在配置文件中配置 spring-cloud-starter-bus-amqp; 这就是说我们需要装rabb ...

  9. SpringCloud基础教程学习记录

    这个学习记录是学习自翟永超前辈的SpringCloud的基础教程. 自己写这个教程的目的主要是在于,想要更凝练总结一些其中的一些实用点,顺便做个汇总,这样自己在复习查看的时候更加方便,也能顺着自己的思 ...

随机推荐

  1. kuangbin专题十六 KMP&&扩展KMP POJ3080 Blue Jeans

    The Genographic Project is a research partnership between IBM and The National Geographic Society th ...

  2. POJ 2421 Constructing Roads(最小生成树)

    Description There are N villages, which are numbered from 1 to N, and you should build some roads su ...

  3. P3796 【模板】AC自动机

    传送门 AC自动机的模板 简单的理解就是字典树上的KMP 注意数组不要开太大 不然每次memset耗时太多 有一个小优化 每次走 fail 边找匹配时只有一些会更新答案 那么就可以把没用的fail边压 ...

  4. 原 tomcat的server.xml配置文件中三个端口的作用

    以Tomcat7.0为例, 在安装目录下. conf/server.xml 中可以配置三个端口号, 如果使用多个tomcat 是需要配置这三个. 该Connector 用于监听请求. protocol ...

  5. 学习ssm

    1.安装配置maven (1)在http://maven.apache.org/download.cgi下载apach-maven-3.5.4-bin.zip (2)将apach-maven-3.5. ...

  6. 转 oracle cursor 游标

    转自:http://blog.csdn.net/liyong199012/article/details/8948952 游标的概念:     游标是SQL的一个内存工作区,由系统或用户以变量的形式定 ...

  7. 关系型数据库---MySQL---对中文字段排序

    1.对中文进行排序时会发生错误,原因是使用的字符集不是中文的字符集: 解决:CONVERT函数 SELECT a.id,a.`name`,a.ch_name FROM `user` a ORDER B ...

  8. inventor安装失败怎样卸载安装inventor 2018?

    AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...

  9. git使用笔记-基础篇

    git使用手册:https://git-scm.com/book/zh/v1/ 一.分支 1.查看所有本地分支 git branch 2.查看所有本地分支和远程分支 git branch -a 3.查 ...

  10. .NET标准化题目

    1. 下面对FxCop的描述中,错误的是:(D) A. FxCop是一个静态代码分析工具. B. 可以定制自己的规则加入FxCop引擎. C. FxCop主要是对.NET中托管代码的assembly进 ...