一、简述:

服务提供者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的更多相关文章

  1. spring cloud 学习之 服务注册和发现(Eureka)

    一:服务注册和发现(Eureka) 1:采用Eureka作为服务注册和发现组件 2:Eureka 项目中 主要在启动类加上 注解@EnableEurekaServer @SpringBootAppli ...

  2. Spring Cloud Alibaba | Nacos服务注册与发现

    目录 Spring Cloud Alibaba | Nacos服务注册与发现 1. 服务提供者 1.1 pom.xml项目依赖 1.2 配置文件application.yml 1.3 启动类Produ ...

  3. Spring Cloud Consul 实现服务注册和发现

    Spring Cloud 是一个基于 Spring Boot 实现的云应用开发工具,它为基于 JVM 的云应用开发中涉及的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全局锁.决策竞选.分布 ...

  4. Spring Cloud Consul使用——服务注册与发现(注册中心)

    整理自该文章 一.Consul 服务端接下来我们开发 Consul 的服务端,创建一个 spring-cloud-consul-producer 项目 1.添加依赖包 <dependencies ...

  5. Spring Cloud Alibaba Nacos 服务注册与发现功能实现!

    Nacos 是 Spring Cloud Alibaba 中一个重要的组成部分,它提供了两个重要的功能:服务注册与发现和统一的配置中心功能. 服务注册与发现功能解决了微服务集群中,调用者和服务提供者连 ...

  6. Spring Cloud(一):服务注册与发现

    Spring Cloud是什么 Spring Cloud是一系列框架的有序集合.它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册.配置中心.消息总线.负载均 ...

  7. Spring Cloud Alibaba 的服务注册与发现

    Spring Cloud Alibaba 服务发现例子 一.需求 1.提供者完成的功能 2.消费者完成的功能 3.可以附加的额外配置 二.实现步骤 1.总的依赖引入 2.服务提供者和发现者,引入服务发 ...

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

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

  9. 小D课堂 - 新版本微服务springcloud+Docker教程_3-05 服务注册和发现Eureka Server搭建实战

    笔记 5.服务注册和发现Eureka Server搭建实战     简介:使用IDEA搭建Eureka服务中心Server端并启动,项目基本骨架介绍          官方文档:http://clou ...

随机推荐

  1. python之Flask框架

    一.简单的Flask框架 1)flask简介 Flask 是一个 web 框架.也就是说 Flask 为你提供工具,库和技术来允许你构建一个 web 应用程序. 这个 wdb 应用程序可以使一些 we ...

  2. windows mysql绿色版配置

    MySQL绿色版安装 1.下载地址 https://dev.mysql.com/downloads/mysql/ 2.配置my.ini 文件 解压下载文件到指定目录.如: my.ini文件内容: [m ...

  3. 2018.11.01 NOIP训练 图论(线段树+倍增+dfs序)

    传送门 一道挺妙的题. 对于询问点(u,v),如右图所示,我们可以发现存在一个点m在u->v的路径中,m子树的点到u是最近的,m子树外到v是最近的.其中dis(u,m)=(dis(u,v)-1) ...

  4. AttributeError: type object 'testClass' has no attribute 'testMothod'

    点击"Unittest for test_post_API.testClass"按钮,点击”Edit configuration...“,弹出对话框Run/Debug config ...

  5. vue中的路由嵌套

    1定义组件 const Index = { template:` <div>首页</div> ` } const Order = { template:` <div> ...

  6. HDU 2147 kiki's game (奇偶博弈)

    题意:给定一个 n * m 的格子,从右上角(1, m) 开始每个玩家只能从向下,向左,或者向左下走,谁不能走,谁输. 析:自己做出来,看了网上的几个博客,好像都没说为什么是只有全奇的情况才会输,个人 ...

  7. python中的分号

    很多编程语言是以分号作为一行代码的的结束标志,但是在Python中不是这样的,而是靠缩进来识别程序结构. Python中一行代码以分号结束,并不是必须的,准确来说是不被推荐的,因为加上分号就是画蛇添足 ...

  8. 天使投资、A轮、B轮、C轮

    一般是这样划分的. A轮融资:公司产品有了成熟模样,开始正常运作一段时间并有完整详细的商业及盈利模式,在行业内拥有一定地位和口碑.公司可能依旧处于亏损状态.资金来源一般是专业的风险投资机构(VC).投 ...

  9. HDMI中checksum计算法

    在AVI传输过程中有三个字节没有被传输.这是在HDMI1.4B中找到的前三个字节的数据. >> hex2dec('82') ans = 130 下图中的数据中在HDMI中接收到的一串数据, ...

  10. 如何悄悄地提升MySQL用户权限

    温馨提示: 一次成功的非法提权,需要的必备条件是:1.对mysql权限表的查.改权限: 2.一次不经意的数据库服务器重启: 此次测试版本:5.6.25 准备邪恶用户: grant update on ...