要使用Eureka实现服务发现,需要项目中包含Eureka的服务端发现组件以及客户端发现组件。

搭建Maven父工程

  创建一个Maven父工程xcservice-springcloud,并在工程的pom.xml中添加Spring Cloud的版本依赖等信息,如文件4-1所示。
  文件4-1 pom.xml

<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>
<groupId>com.xc</groupId>
<artifactId>xcservice-springcloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/>
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<!--Spring Boot的编译插件 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

搭建服务端工程

  在父工程xcservice-springcloud中,创建Maven子模块xcservice-eureka-server作为服务端工程,该模块是一个基础的Spring Boot工程,其主要文件代码的实现过程如下。
  (1)添加依赖。
  在pom.xml中添加Eureka Server的依赖,如文件4-2所示。
  文件4-2 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>com.xc</groupId>
<artifactId>xcservice-springcloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.xc</groupId>
<artifactId>xcservice-eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>xcservice-eureka-server</name>
<description>服务端工程</description> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

  (2)编写配置文件。
  在配置文件中增加端口号等配置信息,如文件4-3所示。
  文件4-3 application.yml

server:
port: 8761 eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
#server:
#enable-self-preservation: false #关闭保护机制,以确保注册中心可以将不可用的实例正确删除

  上述代码中,首先配置了端口号为8761,所有服务的实例都需要向此端口注册。接下来配置了实例名为localhost。由于本项目是一个注册中心,是不需要向自己注册和检索服务的,所以register-with-eureka和fetch-registry都需要设置为false。最后defaultZone中的地址是注册中心的地址。

  (3)修改服务端Java代码。
  在项目的引导类上添加注解@EnableEurekaServer,该注解用于声明标注类是一个EurekaServer,如文件4-4所示。
  文件4-4 EurekaApplication.java

package com.xc.xcserviceeurekaserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; /**
* http://localhost:8761/
*/
@SpringBootApplication
@EnableEurekaServer
public class XcserviceEurekaServerApplication { public static void main(String[] args) {
SpringApplication.run(XcserviceEurekaServerApplication.class, args);
} }

  (4)启动应用,查看信息。
  完成上述配置后,启动应用程序并在浏览器中访问地址http://localhost:8761/即可看到Eureka的信息面板。

  可以看出,Eureka Server的信息页面已经成功显示,但此时“Instances currently registered with Eureka”下的显示信息为“No instances available”,这表示该注册中心还没有注册任何可用的实例。

搭建客户端工程

  在父工程xcservice-springcloud中,创建Maven子模块xcservice-eureka-user作为客户端工程,该模块也是一个基础的Spring Boot工程,其主要文件代码的实现过程如下。
  (1)添加依赖。在pom.xml中添加Eureka依赖,如文件4-5所示。
  文件4-5 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>com.xc</groupId>
<artifactId>xcservice-springcloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.xc</groupId>
<artifactId>xcservice-eureka-user</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>xcservice-eureka-user</name>
<description>客户端工程</description> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

  (2)编写配置文件。在配置文件中添加Eureka服务实例的端口号、服务端地址等信息,如文件4-6所示。
  文件4-6 application.yml

server:
port:8000 # 指定该Eureka实例的端口号 eureka:
instance:
prefer-ip-address: true # 是否显示主机的IP
#instance-id: ${spring.cloud.client.ipAddress}:${server.port} #将Status中的显示内容也以“IP:端口号”的形式显示
client:
service-url:
defaultZone: http://localhost:8761/eureka/ # 指定Eureka服务端地址 spring:
application:
name: microservice-eureka-user # 指定应用名称

  (3)修改客户端Java代码。
  在项目的引导类上添加注解@EnableEurekaClient,该注解用于声明标注类是一个Eureka客户端组件,如文件4-7所示。
  文件4-7 Application.java

package com.xc.xcserviceeurekauser;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @SpringBootApplication
@EnableEurekaClient
@RestController
public class XcserviceEurekaUserApplication { @RequestMapping("/hello")
public String home() {
return "hello world!";
} public static void main(String[] args) {
SpringApplication.run(XcserviceEurekaUserApplication.class, args);
} }

  (4)启动应用,查看信息。
  完成上述配置后,分别启动服务器工程和客户端工程,在浏览器中访问地址http://local-host:8761/后,我们可以从Eureka的信息面板中看到注册的服务信息,如图4-5所示。
  

  从图4-5中可以看出,服务已经成功注册到了注册中心,注册后的服务就可以直接被其他服务调用了。 

 

Spring Cloud 如何使用Eureka注册服务 4.2.2的更多相关文章

  1. Spring Cloud 系列之 Eureka 实现服务注册与发现

    如果你对 Spring Cloud 体系还不是很了解,可以先读一下 Spring Cloud 都有哪些模块 Eureka 是 Netflix 开源的服务注册发现组件,服务发现可以说是微服务架构的核心功 ...

  2. Spring Cloud(Dalston.SR5)--Eureka 注册中心高可用-服务提供和消费

    由于 Eureka 注册中心只是在内存中保存服务注册实例,并且没有将服务注册实例进行同步,因此我们需要对服务提供和消费进行调整,需要指定服务提供和消费的注册.服务发现的具体Eureka 注册中心配置, ...

  3. 【Spring Cloud笔记】Eureka注册中心增加权限认证

    在Spring Cloud通过Eureka实现服务注册与发现时,默认提供web管理界面,但是如果在生产环境暴露出来,会存在安全问题.为了解决这个问题,我们可以通过添加权限认证进行控制,具体步骤如下: ...

  4. Spring Cloud(Dalston.SR5)--Eureka 注册中心搭建

    基于 Netflix Eureka 做了二次封装,主要负责完成微服务架构中的服务治理功能,服务治理可以说是微服务架构中最为核心和基础的模块,他主要用来实现各个微服务实例的自动化注册与发现 服务注册:在 ...

  5. Spring Cloud(Dalston.SR5)--Eureka 注册中心高可用搭建

    高可用集群 在微服务架构这样的分布式环境中,我们需要充分考虑发生故障的情况,所以在生产环境中必须对各个组件进行高可用部署,对与微服务和服务注册中心都需要高可用部署,Eureka 高可用实际上就是将自己 ...

  6. SpringCloud学习笔记(5)----Spring Cloud Netflix之Eureka的服务认证和集群

    1. Eureka服务认证 1. 引入依赖 <dependency> <groupId>org.springframework.boot</groupId> < ...

  7. Spring Cloud(二):服务注册与发现 Eureka【Finchley 版】

    Spring Cloud(二):服务注册与发现 Eureka[Finchley 版]  发表于 2018-04-15 |  更新于 2018-05-07 |  上一篇主要介绍了相关理论,这一篇开始我们 ...

  8. spring cloud+.net core搭建微服务架构:服务注册(一)

    背景 公司去年开始使用dotnet core开发项目.公司的总体架构采用的是微服务,那时候由于对微服务的理解并不是太深,加上各种组件的不成熟,只是把项目的各个功能通过业务层面拆分,然后通过nginx代 ...

  9. Spring Cloud(三):服务提供与调用 Eureka【Finchley 版】

    Spring Cloud(三):服务提供与调用 Eureka[Finchley 版]  发表于 2018-04-15 |  更新于 2018-05-07 |  上一篇文章我们介绍了 Eureka 服务 ...

随机推荐

  1. Spring Boot 2.x实战之定时任务调度

    在后端开发中,有些场景是需要使用定时任务的,例如:定时同步一批数据.定时清理一些数据,在Spring Boot中提供了@Scheduled注解就提供了定时调度的功能,对于简单的.单机的调度方案是足够了 ...

  2. BZOJ 2406 矩阵(二分+有源汇上下界可行流)

    题意 题解 二分答案+可行流判断. 模板题. CODE #include <cstdio> #include <cstring> #include <algorithm& ...

  3. cube.js 学习(八)backend部署模式

    cube.js 从设计上就进行了系统上的分层,backend,frontend,backend 是cube.js 的核心 对于cube.js backend 的部署官方也提供了好多中方法 部署模型 s ...

  4. 《挑战30天C++入门极限》新手入门:C++中的函数重载

        新手入门:C++中的函数重载 函数重载是用来iostream>  using namespace std;  int test(int a,int b);  float test(flo ...

  5. Android Studio导入google training example gradle失败

    Error:Unable to tunnel through proxy. Proxy returns "HTTP/1.1 400 Bad Request 每次从github的Google ...

  6. 列出5个python标准库

    os:提供了不少与操作系统相关联的函数 sys:   通常用于命令行参数 re:   正则匹配 math: 数学运算 datetime:处理日期时间

  7. Spring MVC原理及配置

    Spring MVC原理及配置 1. Spring MVC概述 Spring MVC是Spring提供的一个强大而灵活的web框架.借助于注解,Spring MVC提供了几乎是POJO的开发模式,使得 ...

  8. git 的使用方法以及要注意的地方~

    1.假如你在一个分支,非master分支,例如avatar,在你修改之前一定要 get merge master,git pull,再开始写代码.如果改好了,也要先git merge master,g ...

  9. BAT文件语法和技巧(bat文件的编写及使用)

    比较有用的东西 首先,批处理文件是一个文本文件,这个文件的每一行都是一条DOS命令(大部分时候就好象我们在DOS提示符下执行的命令行一样),你可以使用DOS下的Edit或者Windows的记事本(no ...

  10. Requests库的主要方法:requests.request为requests.get和requests.post两个的汇总,只是需要传方法

    1. requests.request(method,url,**kwargs) method:请求方式,对应get/put/post等七种 :拟获取页面的url链接 :控制访问参数,共13个 met ...