Spring Cloud(Dalston.SR5)--Eureka 服务提供者
要使微服务应用向注册中心发布自己,首先需要在 pom.xml 配置文件中增加对 spring-boot-starter-eureka 的依赖,然后在主类中增加 @EnableDiscoveryClient 注解来启动服务注册(必须在项目中实现了RESTful 服务)。
- 创建项目
创建名称为 service-provider 的 Spring Cloud 项目,修改 POM.xml 中增加以下依赖项:
<?xmlversion="1.0"encoding="UTF-8"?>
<projectxmlns="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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.lixue.webservice</groupId>
<artifactId>service-provider</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/><!--lookupparentfromrepository-->
</parent>
<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>Dalston.SR5</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-eureka</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>
</project>
- 创建 REST 服务
创建 HelloWorldController 类,用来提供 REST 服务器,代码如下:
package org.lixue.webservices.services;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
impor torg.springframework.web.bind.annotation.RestController;
@RestController
publicclassHelloWorldController{
@RequestMapping(method=RequestMethod.GET,name="speak")
publicStringspeak(@RequestParam(value="body",required=false)Stringbody){
if(body==null||body==""){
return"helloworld";
}
return"speak"+body;
}
}
- 启动类启用Eureka客户端
在启动类中,增加 @EnableDiscoveryClient 注解,声明这是一个 Eureka 客户端,代码如下:
package org.lixue.webservices.services;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
publicclassServiceProviderApplication{
publicstaticvoidmain(String[]args){
SpringApplication.run(ServiceProviderApplication.class,args);
}
}
- 基本配置
在 src/main/resources 目录中,创建 application.yml 文件,该文件为 Spring Cloud 的配置文件,增加基本配置:
#配置应用名称
spring:
application:
name:helloworld-provider
#服务端口
server:
#设置eureka服务注册中心的地址,如果多个以逗号分割
eureka:
client:
service-url:
#defaultZone表示默认的区域的eureka服务地址,多个使用逗号分割
defaultZone:http://localhost:9000/eureka/
- 启动项目
运行项目,访问地址 http://localhost:9000 即可打开 Eureka 注册中心的服务控制台,在 instance currently registered with Eureka 中可以看到我们的服务提供者
Spring Cloud(Dalston.SR5)--Eureka 服务提供者的更多相关文章
- Spring Cloud(Dalston.SR5)--Eureka 服务消费
服务被注册.发布到 Eureka 服务器后,需要有程序去发现他,并且进行调用,称为服务消费,一个服务可能会部署多个实例,调用过程可能涉及负载均衡.服务器查找等问题,这些问题 Netflix 项目已经帮 ...
- Spring Cloud(Dalston.SR5)--Eureka 注册中心搭建
基于 Netflix Eureka 做了二次封装,主要负责完成微服务架构中的服务治理功能,服务治理可以说是微服务架构中最为核心和基础的模块,他主要用来实现各个微服务实例的自动化注册与发现 服务注册:在 ...
- Spring Cloud(Dalston.SR5)--Eureka 服务实例健康检查
默认情况下,Eureka 客户端每隔 30 秒会发送一次心跳给服务器端,告知正常存活,但是,实际环境中有可能出现这种情况,客户端表面上可以正常发送心跳,但实际上服务是不可用的,例如,一个需要访问数据的 ...
- Spring Cloud(Dalston.SR5)--Eureka 常用配置
配置参数 默认值 说明 服务注册中心配置 Bean类:org.springframework.cloud.netflix.eureka.server.EurekaServerConfigBean eu ...
- Spring Cloud(Dalston.SR5)--Eureka 注册中心高可用-服务提供和消费
由于 Eureka 注册中心只是在内存中保存服务注册实例,并且没有将服务注册实例进行同步,因此我们需要对服务提供和消费进行调整,需要指定服务提供和消费的注册.服务发现的具体Eureka 注册中心配置, ...
- Spring Cloud(Dalston.SR5)--Eureka 注册中心高可用搭建
高可用集群 在微服务架构这样的分布式环境中,我们需要充分考虑发生故障的情况,所以在生产环境中必须对各个组件进行高可用部署,对与微服务和服务注册中心都需要高可用部署,Eureka 高可用实际上就是将自己 ...
- Spring Cloud(Dalston.SR5)--Zuul 网关-微服务集群
通过 url 映射的方式来实现 zuul 的转发有局限性,比如每增加一个服务就需要配置一条内容,另外后端的服务如果是动态来提供,就不能采用这种方案来配置了.实际上在实现微服务架构时,服务名与服务实例地 ...
- Spring Cloud(Dalston.SR5)--Feign 与 Hystrix 断路器整合
创建项目 要使 Feign 与 Hystrix 进行整合,我们需要增加 Feign 和 Hystrix 的依赖,修改 POM.xml 中增加以下依赖项如下: <?xmlversion=" ...
- Spring Cloud(Dalston.SR5)--Hystrix 断路器
Spring Cloud 对 Hystrix 进行了封装,使用 Hystrix 是通过 @HystrixCommand 注解来使用的,被 @HystrixCommand 注解标注的方法,会使用 Asp ...
随机推荐
- L2-002. 链表去重(数组模拟)
L2-002. 链表去重 因为数值比较小,所以直接用数组来模拟 #include<cstdio> #include<cstring> #include<iostream& ...
- POJ 1287 Networking(最小生成树裸题有重边)
Description You are assigned to design network connections between certain points in a wide area. Yo ...
- lftp的安装
lftp的安装 安装依赖的包 yum -y install make readline-devel gnutls* 解压上传的包 tar -jxf lftp-4.0.1.tar.bz2 cd到解压 ...
- CentOS中yum安装ffmpeg
1.升级系统 sudo yum install epel-release -y sudo yum update -y sudo shutdown -r now 2.安装Nux Dextop Yum 源 ...
- 服务器安装wordpress,搭建自己的博客平台
自己构造网站的话,建立一个简单的网页还可以(比如,yongjieshi.com),对于建立复杂的博客就需要借助第三方的工具,常见的有wordpress,在阿里云上安装wordpress,我主要参考了这 ...
- PowerDesigner15 增加Domain域
第一步: 第二步: 点击此按钮,在弹出框中对Domain域打钩即可
- ACM-ICPC 2018 沈阳赛区网络预赛-K:Supreme Number
Supreme Number A prime number (or a prime) is a natural number greater than 11 that cannot be formed ...
- Atcode ABC105-C:Base -2 Number
C - Base -2 Number Time Limit: 2 sec / Memory Limit: 1024 MB Score : 300300 points Problem Statement ...
- mvc core2.1 Identity.EntityFramework Core 注册 (二)
Startup.cs-> Configure app.UseAuthentication(); //启动验证 Controllers->AccountController.cs 新建 us ...
- dc-vastinspector
https://developers.google.com/interactive-media-ads/docs/sdks/html5/vastinspector hosts: https://gis ...