这一章节主要讲如何搭建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项目的搭建.

最新文章

  1. swift-运算符
  2. phpexcel读取excel的xls xlsx csv格式
  3. django url路径与模板中样式相对路径的问题
  4. SQL查询语句 常用示例
  5. EmguCV 一些结构
  6. maven实战_01_搭建maven开发环境
  7. iOS 沙盒目录结构介绍
  8. MySQL计划任务(事件调度器)(Event Scheduler)
  9. 纯CSS写九宫格样式,高宽自适应正方形
  10. Struts2-2.了解struts.xml&gt;package&gt;action&gt;result的name属性
  11. 11. 配置ZooKeeper ensemble
  12. mysql 数据类型别名参考
  13. Hibernate中的对象有三种状态
  14. 一次Spring Bean初始化顺序问题排查记录
  15. 【LeetCode每天一题】Plus One(加一)
  16. orcal - 添加用户、授权
  17. netty入坑第一步:了解netty和编写简单的Echo服务器和客户端
  18. ffmpeg安装
  19. crm作业知识点集合[二]
  20. 带网上开户表单jQuery焦点图

热门文章

  1. 数据结构14:队列(Queue),“先进先出”的数据结构
  2. 谁能赢呢? BZOJ 2463
  3. Long类型比较不能直接用等于
  4. django终端打印Sql语句
  5. Go语言构建json和解析json实例
  6. environment与@ConfigurationProperties的关系 加载过程分析
  7. yii1的后台分页和列表
  8. 用navigator.geolocation.getCurrentPosition在IOS10以上的系统无法定位
  9. mysql 死锁解决办法
  10. 使用eclipse IDE遇到的问题