springcloud+eureka简单入门案例
springcloud+eureka简单入门案例
一、服务提供者
直接提供服务,入门案例没有特别要设置的地方,注意下端口,由于要启动多个服务,可能会冲突
配置文件(src/main/resources/application.yml)
server:
port: 8000
二、服务消费者
服务消费者的依赖在这个单独的demo中其实可有可无,亲测不添加,也可以实现demo服务提供能
三、服务消费者启动类里注入RestTemplate,用于调用远程服务
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
public class MovieApplication {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(MovieApplication.class, args);
}
}
四、服务消费者Controller
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.xujie.pojo.User;
@RestController
public class UserController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/getUser")
public User getUser() {
return this.restTemplate.getForObject("http://localhost:8000/getUser", User.class);
}
}
此时可以通过访问消费者,间接调用服务提供者的服务,
五、创建服务注册中心,这里选用Eureka
5.1在springboot基础环境上添加依赖
<!-- springcloud版本声明 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 引入eureka依赖 -->
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
5.2启动类的编码
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer //声明这是一个Eureka服务器
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
5.3配置文件(src/main/resources/application.yml)
server:
port: 8761 #声明端口号
eureka:
client:
register-with-eureka: false #默认是true,将自己注册到eureka上
fetch-registry: false #是否从eureka上获取信息,由于本案例是单机,无需从别的eureka上获取注册信息
service-url:
defaultZone: http://localhost:8761/eureka #设置与eureka交互的地址,查询服务和注册服务都需要依赖这个地址,默认是:http://localhost:8761/eureka
六、将服务提供者注册到服务注册中心
6.1改造服务提供者
6.1.1添加依赖,便于把服务注册到注册中心Eureka中去:
<!-- springcloud版本声明 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- eureka的依赖 -->
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
6.1.2修改配置文件,添加下列配置
spring:
application:
name: provider #注册到Eureka Server上的应用名称
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka #注册的位置
instance:
prefer-ip-address: true #将自己的ip注册到EuekaServer上
6.1.3修改启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient //也可以用EnableDiscoveryClient代替,前者兼容性更大,后者仅能兼容Eureka
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
此时可以正常启动并将服务注册到了eureka中
启动Eureka和服务提供者访问:http://localhost:8761,界面如下:

七、Eureka的高可用
在这里demo之前,修改本地hosts文件,为了区分本地的两个eureka节点,分别通过:http://peer1:8761和http://peer2:8762访问
7.1将刚才的eureka项目复制一份,修改两个的配置文件如下:
eureka1的配置:
server:
port: 8761 #声明端口号
eureka:
instance:
hostname: peer1
appname: peer1
client:
#register-with-eureka: false #默认是true,将自己注册到eureka上,这里设置eureka的高可用,所以需要将自己注册到eureka上
#fetch-registry: false #是否从eureka上获取信息,由于本案例是单机,无需从别的eureka上获取注册信息,这里设置eureka的高可用,所以需要在eureka上获取服务
service-url:
defaultZone: http://peer2:8762/eureka #设置与eureka交互的地址,查询服务和注册服务都需要依赖这个地址,默认是:http://localhost:8761/eureka
eureka2的配置:
server:
port: 8762 #声明端口号
eureka:
instance:
hostname: peer2
appname: peer2
client:
#register-with-eureka: false #默认是true,将自己注册到eureka上,这里设置eureka的高可用,所以需要将自己注册到eureka上
#fetch-registry: false #是否从eureka上获取信息,由于本案例是单机,无需从别的eureka上获取注册信息,这里设置eureka的高可用,所以需要在eureka上获取服务
service-url:
defaultZone: http://peer1:8761/eureka #设置与eureka交互的地址,查询服务和注册服务都需要依赖这个地址,默认是:http://localhost:8761/eureka
此时启动两个服务,界面如下
这是peer1:

下面这个是peer2:

springcloud+eureka简单入门案例的更多相关文章
- MyBatis学习总结(一)简单入门案例
MyBatis学习总结(一)简单入门案例 主要内容:本文主要通过对数据库中的use表进行增删改查总结mybatis的环境搭建和基本入门使用 一.需要的jar包: 1.核心包 2.依赖包 3.jdbc数 ...
- drools的简单入门案例
一.背景 最近在学习规则引擎drools,此处简单记录一下drools的入门案例. 二.为什么要学习drools 假设我们存在如下场景: 在我们到商店购买衣服的时候,经常会发生这样的事情,购买1件不打 ...
- Lucene介绍及简单入门案例(集成ik分词器)
介绍 Lucene是apache软件基金会4 jakarta项目组的一个子项目,是一个开放源代码的全文检索引擎工具包,但它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了完整的查询引擎和 ...
- Dubbo+Zookeeper的简单入门案例
1.1 Dubbo简介 Apache Dubbo是一款高性能的Java RPC框架.其前身是阿里巴巴公司开源的一个高性能.轻量级的开源Java RPC框架,可以和Spring框架无缝集成. 什么是R ...
- javaweb 基于java Servlet登入 简单入门案例
项目流程 第一步:创建一个java webproject第二步:创建三个界面,1,login.jsp 2 success.jsp 3 fail.jsp第三步:更改新建界面编码格式,utf-8 默然编码 ...
- Spring Cloud Eureka简单入门
步骤: 1.创建父工程 2.创建EurekaServer工程 3.创建EurekaClient工程 父工程pom.xml <?xml version="1.0" encodi ...
- Spring Boot 简单入门案例
第一:打开idea 找到spring Initializr 第二:点击Next 在点击下一步 找到web之后勾选第一个spring web 就完成了 在写一个类 点击运行 结果如下:
- EF CodeFirst系列(1)---CodeFirst简单入门
1.什么是CodeFirst 从EF4.1开始,EF可以支持CodeFirst开发模式,这种开发模式特别适用于领域驱动设计(Domain Driven Design,大名鼎鼎的DDD).在CodeFi ...
- 1.Spring框架入门案例
一.简单入门案例 入门案例:IoC 1.项目创建与结构 2.接口与实现类 User.java接口 package com.jd.ioc; /** * @author weihu * @date 201 ...
随机推荐
- 树莓派配置 USB 无线网卡
树莓派配置 USB 无线网卡来上网的过程. 本人使用的USB无线网卡型号:EP-N8508GS(树莓派专用型号) 一.检查 USB 无线网卡是否已经正确识别 将无线 USB 网卡插入树莓派后启动树莓派 ...
- lintcode-57-三数之和
57-三数之和 给出一个有n个整数的数组S,在S中找到三个整数a, b, c,找到所有使得a + b + c = 0的三元组. 注意事项 在三元组(a, b, c),要求a <= b <= ...
- ASP.NET MVC如何使用输出缓存
通过这篇文章你将学习到在MVC中如何使用输出缓存,业务逻辑我就不多介绍了,主要是Outputcache的基本使用.至于数据缓存还是等我的下一篇文章吧,一步一步来不急的. 输出缓存的使用方法是在Co ...
- 找jar包的网址
http://search.maven.org/#search%7Cga%7C1%7Cmybatis http://mvnrepository.com/
- Spark探索经典数据集MovieLens
Spark探索经典数据集MovieLens 阅读目录 前言 环境 初步预览 探索用户数据 探索电影数据 探索评级数据 回到顶部 前言 MovieLens数据集包含多个用户对多部电影的评级数据,也包括电 ...
- 【转】Visio画用例模型图竟然没有include关系
转自:http://blog.csdn.net/shuixin536/article/details/8289746 由于电脑上没有安装Rose,因此决定用visio来画UML中的用例模型图,在绘制的 ...
- 【题解】HAOI2007分割矩阵
水题盛宴啦啦啦……做起来真的极其舒服,比某些毒瘤题好太多了…… 数据范围极小 --> 状压 / 搜索 / 高维度dp:观察要求的均方差,开始考虑是不是能够换一下式子.我们用\(a_{x}\)来表 ...
- JavaScript的大括号的语义
Javascript中大括号"{}"有四种语义作用: 语义1. 组织复合语句,这是最常见的: view source print? 1 if( condition ) { 2 ...
- [SDOI2011]消防/[NOIP2007] 树网的核
消防 题目描述 某个国家有n个城市,这n个城市中任意两个都连通且有唯一一条路径,每条连通两个城市的道路的长度为zi(zi<=1000). 这个国家的人对火焰有超越宇宙的热情,所以这个国家最兴旺的 ...
- Substrings Sort string 基本操作
You are given nn strings. Each string consists of lowercase English letters. Rearrange (reorder) the ...