Spring Cloud实践之服务注册与发现Eureka
一、简述:
服务提供者producer与服务消费者consumer都注册到eureka server,然后服务consumer在其应用内直接调用producer的服务名来调用服务,而不是像之前一样调用producer的IP:port的url来调用他的rest API。(实际的过程是,consumer向eureka获取了producer的地址,然后再调用producer)
二、配置使用
1、eureka server:
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
private static final Logger LOGGER = LoggerFactory.getLogger(EurekaServerApplication.class);
public static void main(String[] args) {
LOGGER.info("服务注册与发现服务器启动...");
SpringApplication.run(EurekaServerApplication.class, args);
}
}
application.properties
# server
server.port=8761
# spring
spring.application.name=eureka-server
# eureka
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
#security
security.basic.enabled=true
security.user.name=liny
security.user.password=123456
build.gradle
/*
* This build file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java Library project to get you started.
* For more details take a look at the Java Libraries chapter in the Gradle
* user guide available at https://docs.gradle.org/4.3/userguide/java_library_plugin.html
*/
buildscript {
ext {
springBootVersion = '1.4.2.RELEASE'
}
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
// Apply the java-library plugin to add support for Java Library
apply plugin: 'java-library'
apply plugin: 'java'
apply plugin: 'org.springframework.boot' version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
// In this section you declare where to find the dependencies of your project
repositories {
// Use jcenter for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
mavenCentral()
maven { url "http://dl.bintray.com/oembedler/maven" }
maven { url "https://repo.spring.io/libs-release" }
maven { url "http://10.100.122.249:8881/nexus/content/groups/public/" }
} dependencyManagement {
imports {
//mavenBom 'org.springframework.cloud:spring-cloud-netflix:1.2.0.M1'
mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Camden.SR2'
}
} dependencies {
// Use JUnit test framework
testImplementation 'junit:junit:4.12'
compile("org.springframework.cloud:spring-cloud-starter-parent:Camden.SR2")
//开启安全验证
compile("org.springframework.boot:spring-boot-starter-security")
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-actuator")
compile("org.springframework.boot:spring-boot-starter-test")
compile("org.springframework.cloud:spring-cloud-starter-eureka-server")
}
2、服务提供者
主程序上加@EnableEurekaClient注解,表示把自己注册到eureka
application.properties里添加如下配置:
# eureka
eureka.client.serviceUrl.defaultZone=http://liny:123456@localhost:8761/eureka/
#spring
spring.application.name=core
butild.gradle
与eureka server不同的是添加的依赖是org.springframework.cloud:spring-cloud-starter-eureka,而eureka server是org.springframework.cloud:spring-cloud-starter-eureka-server
buildscript {
ext {
springBootVersion = '1.4.2.RELEASE'
}
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
maven { url "http://dl.bintray.com/oembedler/maven" }
maven { url "https://repo.spring.io/libs-release" }
maven { url "http://10.100.122.249:8881/nexus/content/groups/public/" }
}
dependencyManagement {
imports {
mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Camden.SR2'
}
}
dependencies {
compile("org.springframework.cloud:spring-cloud-starter-config")
compile("org.springframework.cloud:spring-cloud-starter-eureka")
compile group: 'org.springframework.boot',name:'spring-boot-starter-web',version:'1.4.4.RELEASE'
compile group: 'org.springframework.boot',name:'spring-boot-starter-test',version:'1.4.4.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '1.4.4.RELEASE'
compile group: 'mysql', name: 'mysql-connector-java', version:'5.1.18'
compile group: 'org.apache.tomcat', name: 'tomcat-jdbc', version: '8.0.32'
compile group: 'cglib', name: 'cglib-nodep', version:'2.2'
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.4'
compile group: 'commons-io', name: 'commons-io', version: '2.4'
compile group: 'com.alibaba', name: 'fastjson', version: '1.2.23'
compile group: 'commons-codec', name: 'commons-codec', version: '1.10'
compile group: 'org.apache.httpcomponents', name: 'fluent-hc', version:'4.3.1'
compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.6.1'
compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.6.1'
compile'org.projectlombok:lombok:1.16.18'
compile group: 'com.qiniu', name: 'qiniu-java-sdk', version: '7.2.1'
testCompile group: 'junit', name: 'junit', version: '4.11'
}
bootRepackage {
mainClass = 'com.hzydbs.MainApplication'
}
服务消费者:
主程序
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
同样的,服务消费者也需要注册到eureka,同时@EnableFeignClients意思是将使用Feign作为http rest API接口调用工具。feign允许用类似rpc的方式调用远程接口在本地的映射,像调用本地方法那样调用远程接口。但这里其本质上是http协议。
application.properties 只需要配置eureka的地址,而不再需要配置服务提供者的地址了
# eureka
eureka.client.serviceUrl.defaultZone=http://liny:123456@localhost:8761/eureka/
现在假设服务消费者gateway需要调用服务提供者core上这样一个REST接口方法:
@RequestMapping(value = "getOne", method = RequestMethod.GET)
public ItemResponse getOne(@RequestParam(required = true) long itemId)
我们只需在服务消费者心中一个interface
@FeignClient(name= "core")
public interface CoreRemote {
@RequestMapping(value = "/item/getOne", method = RequestMethod.GET)
public ItemResponse getOne(@RequestParam(value = "itemId") long itemId);
}
@FeignClient(name= "core")意思是映射服务名为core的应用的方法,getOne与远程core上的同名方法保持入参和返回一致。
然后在消费者的程序中就可以像调用本地方法一样调用远程的方法了
@Autowired
private CoreRemote coreRemote;
....
ItemResponse itemResponse = coreRemote.getOne(itemId);
消费者这里之前遇到了一个问题:feign/Feign$Builder
Caused by: java.lang.NoClassDefFoundError: feign/Feign$Builder
网上了解了一下之后大致是spring boot与spring cloud版本之间兼容性的问题
所以这里消费者的build.gradle文件略微有些调整:
buildscript {
ext {
springBootVersion = '1.4.2.RELEASE'
}
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
maven { url "http://dl.bintray.com/oembedler/maven" }
maven { url "https://repo.spring.io/libs-release" }
}
allprojects {
apply plugin: 'org.springframework.boot'
repositories {
mavenCentral()
maven { url "http://dl.bintray.com/oembedler/maven" }
maven { url "https://repo.spring.io/libs-milestone/" }
maven { url "https://repo.spring.io/libs-release" }
jcenter()
}
dependencyManagement {
imports {
mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Camden.SR2'
}
}
}
dependencies {
compile("org.springframework.cloud:spring-cloud-starter-eureka")
compile("org.springframework.cloud:spring-cloud-starter-feign")
}
Spring Cloud实践之服务注册与发现Eureka的更多相关文章
- spring cloud 学习之 服务注册和发现(Eureka)
一:服务注册和发现(Eureka) 1:采用Eureka作为服务注册和发现组件 2:Eureka 项目中 主要在启动类加上 注解@EnableEurekaServer @SpringBootAppli ...
- Spring Cloud Alibaba | Nacos服务注册与发现
目录 Spring Cloud Alibaba | Nacos服务注册与发现 1. 服务提供者 1.1 pom.xml项目依赖 1.2 配置文件application.yml 1.3 启动类Produ ...
- Spring Cloud Consul 实现服务注册和发现
Spring Cloud 是一个基于 Spring Boot 实现的云应用开发工具,它为基于 JVM 的云应用开发中涉及的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全局锁.决策竞选.分布 ...
- Spring Cloud Consul使用——服务注册与发现(注册中心)
整理自该文章 一.Consul 服务端接下来我们开发 Consul 的服务端,创建一个 spring-cloud-consul-producer 项目 1.添加依赖包 <dependencies ...
- Spring Cloud Alibaba Nacos 服务注册与发现功能实现!
Nacos 是 Spring Cloud Alibaba 中一个重要的组成部分,它提供了两个重要的功能:服务注册与发现和统一的配置中心功能. 服务注册与发现功能解决了微服务集群中,调用者和服务提供者连 ...
- Spring Cloud(一):服务注册与发现
Spring Cloud是什么 Spring Cloud是一系列框架的有序集合.它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册.配置中心.消息总线.负载均 ...
- Spring Cloud Alibaba 的服务注册与发现
Spring Cloud Alibaba 服务发现例子 一.需求 1.提供者完成的功能 2.消费者完成的功能 3.可以附加的额外配置 二.实现步骤 1.总的依赖引入 2.服务提供者和发现者,引入服务发 ...
- Spring Cloud(二):服务注册与发现 Eureka【Finchley 版】
Spring Cloud(二):服务注册与发现 Eureka[Finchley 版] 发表于 2018-04-15 | 更新于 2018-05-07 | 上一篇主要介绍了相关理论,这一篇开始我们 ...
- 小D课堂 - 新版本微服务springcloud+Docker教程_3-05 服务注册和发现Eureka Server搭建实战
笔记 5.服务注册和发现Eureka Server搭建实战 简介:使用IDEA搭建Eureka服务中心Server端并启动,项目基本骨架介绍 官方文档:http://clou ...
随机推荐
- Armadillo installation
1.dependencies sudo apt-get install libopenblas-devsudo apt-get install liblapack-devsudo apt-get in ...
- 熟悉JSON
JSON是什么 JSON ( JavaScript Object Notation) ,是一种数据交互格式. 为什么有这个技术 Json之前,大家都用 XML 传递数据.XML 是一种纯文本格式,所以 ...
- MySQL LOCK TABLES 与UNLOCK TABLES
http://blog.csdn.net/zyz511919766/article/details/16342003 1语法 LOCK TABLES tbl_name[[AS] alias] lock ...
- spring+springMVC+mybatis+maven+mysql环境搭建(一)
环境搭建是最基础的,但是发现平时很多时候大家都是ctrl c+ctrl v,这样对于很多细节完全不清楚,来,一起深入了解下 一.准备工作 首先得准备好maven.mysql啥的,这些略... 并且my ...
- boost--ref
1.ref简介 reference_wrapper包含在ref库中,它是引用包装器类型,即其内部包装了引用. 成员函数get().get_pointer()分别可以获得被包装的引用和其指针.使用需要包 ...
- java json注解
(1)初级我们从几个简单的使用场景开始:重命名属性,忽略属性,以及修改属性所使用的类型.注意:下面的例子仅仅显示了成员属性(field properties),注解同样也可以用在成员方法(getter ...
- Linux查看登录到服务的用户,查看用户的操作已经剔掉干坏事的用户的命令
在工作中,我们有时候会经常的切换用户,有时候会忘记切换到哪个用户了,我们就需要知道当前登录的用户时谁,可以使用: whoami 查看当前登录到系统中的用户有哪些: who 列表中显示,第一列是用户名, ...
- BZOJ 1029 [JSOI2007]建筑抢修 (贪心 + 优先队列)
1029: [JSOI2007]建筑抢修 Time Limit: 4 Sec Memory Limit: 162 MBSubmit: 5452 Solved: 2422[Submit][Statu ...
- shell 脚本学习之read
Read的一些选项 Read可以带有-a, -d, -e, -n, -p, -r, -t, 和 -s八个选项. -a :将内容读入到数值中 echo -n 'please enter:'read -a ...
- 如何使用git提交代码
如何使用Git管理代码 Git 是开发人员用来向代码库(msstash)中提交代码或者下载远端代码库中代码的工具. 如何使用git向代码库中提交我们修改后的代码呢? 1.如果是第一次使用git,那么需 ...