一、简述:

服务提供者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. 用php脚本比较MySQL两个数据库的结构差异

    define('DATABASE1', 'mysql://root:password@127.0.0.1/db1'); $dbi1 = new DbMysql; $dbi1->dbh = DAT ...

  2. linux代码笔记

    sudo passwd root更新root密码 软件包管理及shell命令_deb软件包管理一_笔记:dpkj -i 安装dpkj -r 移除dpkj -P 全部移除dpkj -L 列出安装清单dj ...

  3. keras框架的CNN手写数字识别MNIST

    参考:林大贵.TensorFlow+Keras深度学习人工智能实践应用[M].北京:清华大学出版社,2018. 首先在命令行中写入 activate tensorflow和jupyter notebo ...

  4. kbmmw 5.02发布

    5.02.00 May 27 2017 Important notes (changes that may break existing code) ========================= ...

  5. 28、shareSDK分享以及 QQ应用平台申请遇到的问题

    第一点:菜单列表没出来 未添加白名单 第二点: QQ平台申请,和安卓共用一个APP名字,出现的 问题 第三点

  6. fabric 安装

    fabric 是一个python的库,fabric可以通过ssh批量管理服务器. 第一步安装依赖包 安装fabric依赖及pip yum install -y python-pip gcc pytho ...

  7. Python开课复习-10/10

    1. 什么时匿名函数def 定义 的是有名函数:特点是可以通过名字重复调用 def func(): #func = 函数的内存地址 pass匿名函数就是没有名字的函数:特点是只能在定义时使用一次 2. ...

  8. 2018.12.15 poj3415 Common Substrings(后缀自动机)

    传送门 后缀自动机基础题. 给两个字符串,让你求长度不小于kkk的公共子串的数量. 这题可以用后缀自动机解决废话 考虑对其中一个字串建出后缀自动机,然后用另一个在上面跑,注意到如果一个状态有贡献的话, ...

  9. OSI七层模型和TCP/IP四层模型

    1)网络层负责点到点的传输(这里的“点”指主机或路由器),而传输层负责端到端的传输(这里的“端”指应用进程) 2)ARP协议介于数据链路层和网络层之间(IPv4专有,IPv6的地址映射功能在ICMPv ...

  10. s5-10 路由

    路由器转发分组的依据 路由表 路由表从何而来 直连路由.静态路由.动态路由 路由器收到一个分组之后-  打开分组L3,提取出目的IP地址  确定目标网络,查找路由表 按位"AND&quo ...