1.工具及软件版本
  • JDK1.8
  • Spring Boot 1.4.3.RELEASE
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4..RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
  • Spring Cloud Camden SR4
<!--引入spring-cloud依赖-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
  • Maven3.3.9
  • 数据库:mysql5.7
  • 项目目录
2.编写EureKa Server
  • 项目:microservice-discovery-eureka
  • 在pom.xml中添加以下依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
  • 编写启动类,在启动类上添加@EnableEurekaServer
@SpringBootApplication
@EnableEurekaServer
public class MicroserviceDiscoveryEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(MicroserviceDiscoveryEurekaApplication.class, args);
}
}
  • 在配置文件application.yml中添加以下内容
server:
port:
eureka:
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://localhost:8761/eureka
3.将服务提供者注册到Eureka Server上
  • 项目:microservice-simple-provider-user
  • 在pom.xml添加添加以下依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
  • 在配置文件application.yml中添加以下配置
server:
port: spring:
application:
name: microservice-provider-user
datasource:
url: jdbc:mysql://127.0.0.1:3306/springcloud
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
jpa:
properties:
hibernate:
hbm2ddl:
auto: none
show-sql: true
devtools:
restart:
additional-exclude: non-restart/**
resources:
static-locations: classpath:/resources/static/,classpath:/static/ eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true logging:
level:
org:
springframework:
security: INFO
  • 在启动类上添加@EnableDiscoveryClient注解,声明这是一个Eureka Client
@SpringBootApplication
@EnableDiscoveryClient
public class MicroserviceSimpleProviderUserApplication {
public static void main(String[] args) {
SpringApplication.run(MicroserviceSimpleProviderUserApplication.class, args);
}
}
4.将服务消费者注册到Eureka Server上
  • 项目:microservice-simple-consumer-movie
  • 在pom.xml中添加以下依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
  • 在配置文件application.yml中添加以下配置
server:
port: user:
userServiceUrl: http://MICROSERVICE-PROVIDER-USER/ spring:
application:
name: microservice-consumer-movie eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
  • 在启动类上添加@EnableDiscoveryClient注解,声明这是一个Eureka Client
@SpringBootApplication
@EnableDiscoveryClient
public class ConsumerMovieApplication {
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(ConsumerMovieApplication.class, args);
}
}
  • 服务调用
@RestController
public class MovieController { @Autowired
private RestTemplate restTemplate; @Value("${user.userServiceUrl}")
private String userServiceUrl; @GetMapping("/user/{id}")
public User findById(@PathVariable Long id){
return this.restTemplate.getForObject(this.userServiceUrl + id,User.class);
}
}
 
注意:消费者调用提供者的服务,可用通过IP的形式也可以通过服务名称,如果通过服务名称获取需要引入ribbon。
 

Eureka服务注册与发现-提供消费服务模型的更多相关文章

  1. SpringCloud(3)---Eureka服务注册与发现

    Eureka服务注册与发现 一.Eureka概述 1.Eureka特点 (1) Eureka是一个基于REST的服务,用于定位服务,以实现云端中间层服务发现和故障转移. (2) Eureka 主管服务 ...

  2. 微服务框架SpringCloud(Dalston版)学习 (一):Eureka服务注册与发现

    eureka-server eureka服务端,提供服务的注册与发现,类似于zookeeper 新建spring-boot工程,pom依赖: <dependency> <groupI ...

  3. SpringCloud的入门学习之概念理解、Eureka服务注册与发现入门

    1.微服务与微服务架构.微服务概念如下所示: 答:微服务强调的是服务的大小,它关注的是某一个点,是具体解决某一个问题.提供落地对应服务的一个服务应用,狭意的看,可以看作Eclipse里面的一个个微服务 ...

  4. Eureka服务注册与发现

    一.服务注册 注册Eureka的服务非常的简单,只需要引入spring-cloud-starter-netflix-eureka-client的jar包即可. <dependency> & ...

  5. Spring Cloud之Eureka服务注册与发现

    解决什么问题 ➟阐述微服务以及服务注册发现的部分概念 ➟阐述Eureka服务注册与发现的部分原理及细节 为什么需要服务中心 过去,每个应用都是一个CPU,一个主机上的单一系统.然而今天,随着大数据和云 ...

  6. SpringCloud 进阶之Eureka(服务注册和发现)

    1. Eureka 服务注册与发现 Eureka 是一个基于REST的服务,用于服务的的注册与发现; Eureka采用C-S的设计架构,Eureka Server作为服务注册功能的服务器,它是服务注册 ...

  7. Spring Cloud学习(一):Eureka服务注册与发现

    1.Eureka是什么 Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的. Eureka ...

  8. java框架之SpringCloud(3)-Eureka服务注册与发现

    在上一章节完成了一个简单的微服务案例,下面就通过在这个案例的基础上集成 Eureka 来学习 Eureka. 介绍 概述 Eureka 是 Netflix 的一个子模块,也是核心模块之一.Eureka ...

  9. SpringCloud Eureka服务注册及发现——服务端/客户端/消费者搭建

    Eureka 是 Netflix 出品的用于实现服务注册和发现的工具. Spring Cloud 集成了 Eureka,并提供了开箱即用的支持.其中, Eureka 又可细分为 Eureka Serv ...

随机推荐

  1. Java学习笔记-----eclipse中建立Java项目并成功运行

    环境:WIN7 64位 +eclipse 2018 12version 具体方法:https://jingyan.baidu.com/album/9c69d48fefa53113c9024eb3.ht ...

  2. 关于&联系我

    本文已迁移至: Github博客:https://coco5666.github.io/blog/about Gitee博客:https://coco56.gitee.io/blog/about 博客 ...

  3. Nginx 别名访问

    #添加另一个域名,在浏览器中输入 etiantian.org,总是调到 www.etiantian.org server { listen            80; server_name   w ...

  4. Django的MTV模型

    MTV模型 Django框架的设计模式借鉴了MVC框架的思想,也是分成三部分,来降低各个部分之间的耦合性. MTV框架是Django的框架,三部分为: Model Template(模板) View ...

  5. String与C风格字符串转换

    String字符串转换为C风格字符串需要利用string类的成员函数c_str().而C风格字符串转换转换为string字符串可以直接利用运算符=.首先介绍c_str()函数原型: const val ...

  6. 2019.9.17更换ubuntu的镜像源 ubuntu安装lamp iis安装网站和ftp站

    更换ubuntu的镜像源 /etc/apt/sources.list cp  /etc/apt/sources.list  /etc/apt/sources.list.bak 备份这个文件 vim / ...

  7. Tomb Raider HihoCoder - 1829 (二进制枚举+暴力)(The 2018 ACM-ICPC Asia Beijing First Round Online Contest)

    Lara Croft, the fiercely independent daughter of a missing adventurer, must push herself beyond her ...

  8. 动态列表+动态样式(vue双向绑定)

    先上效果图 注:下面的几个值可以从其他地方获取,这边演示我是写死的 在上逻辑图 接着上代码template部分 <template> <div > <div> &l ...

  9. Spring 核心之IOC 容器

    核心概念: IOC(Inversion of Control)控制反转:所谓控制反转,就是把原先我们代码里面需要实现的对象创建.依赖的代码,反转给容器来帮忙实现. DI(Dependency Inje ...

  10. jsp三种注释方法

    HTML注释(输出注释):指在客户端查看源代码时能看见注释.例如, <!-- this is an html comment.it will show up int the response. ...