Spring Cloud快速入门

代码地址:

https://gitee.com/gloryxu/spring-cloud-test

EureKa:服务注册中心

添加依赖

     <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

开启Eureka Server

@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication { public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
} }

配置

#设置tomcat服务端口号
server.port=
# 本地调试环境下关闭自我保护机制
eureka.server.enable-self-preservation=false
# 清理间隔时间,单位为毫秒
eureka.server.eviction-interval-timer-in-ms=
#设置服务名称
spring.application.name=eureka-service eureka.instance.hostname=localhost
#注册中心不需要注册自己
eureka.client.register-with-eureka=false
#注册中心不需要去发现服务
eureka.client.fetch-registry=false
#设置服务注册中心的URL
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka

启动成功

2.创建一个服务提供者

添加依赖

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
@EnableDiscoveryClient // 声明这是一个Eureka Client
@SpringBootApplication
public class Server1Application { public static void main(String[] args) {
SpringApplication.run(Server1Application.class, args);
} }

添加配置

server.port=
#设置服务名
spring.application.name=hello-service
#设置服务注册中心的URL,本服务要向该服务注册中心注册自己
eureka.client.serviceUrl.defaultZone=http://localhost:8101/eureka

添加Controller

@RestController
public class HelloController { Logger logger = LoggerFactory.getLogger(HelloController.class); @RequestMapping("/hello")
public String hello() {
return "hello";
}
}

启动注册成功

3.创建一个消费者

添加依赖

    <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
LoadBalanced 方式可实现负载均衡
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class ConsumerApplication { @Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
} public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
} }

添加配置

server.port=

spring.application.name=hello-consumer
eureka.client.serviceUrl.defaultZone=http://localhost:8101/eureka/

声明Feign方式 ,value为注册的服务名

@FeignClient(value = "hello-service")
public interface HelloService {
@RequestMapping(value = "/hello")
String hello();
}

以下以两种方式调用服务提供者,一种是以Rest方式,另一种以Feign方式

@RestController
public class ConsumerController { Logger logger = LoggerFactory.getLogger(ConsumerController.class); @Autowired
private RestTemplate restTemplate;
@Autowired
HelloService helloService; @GetMapping("/getserver")
public String getserver() {
String xx=restTemplate.getForObject("http://hello-service/hello", String.class);
return "consumer finish result:"+xx;
} @GetMapping("/gettest")
public String gettest(){
return helloService.hello();
}
}

启动

4.创建Zuul路由

添加依赖

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>

加入注解,以便注册到注册中心

@EnableZuulProxy
@SpringBootApplication
public class ZuulApplication { public static void main(String[] args) {
SpringApplication.run(ZuulApplication.class, args);
} }

配置路由,以下是以服务的方式调用

spring.application.name=eureka-zuul
server.port=
zuul.routes.hello-service.path=/hello-service/**
zuul.routes.hello-service.serviceId=hello-service
eureka.client.serviceUrl.defaultZone=http://localhost:8101/eureka/

启动 注册成功,调用成功

Spring Cloud 快速入门的更多相关文章

  1. spring boot入门教程——Spring Boot快速入门指南

    Spring Boot已成为当今最流行的微服务开发框架,本文是如何使用Spring Boot快速开始Web微服务开发的指南,我们将使创建一个可运行的包含内嵌Web容器(默认使用的是Tomcat)的可运 ...

  2. Spring Boot 快速入门

    Spring Boot 快速入门 http://blog.csdn.net/xiaoyu411502/article/details/47864969 今天给大家介绍一下Spring Boot MVC ...

  3. Spring Boot快速入门(二):http请求

    原文地址:https://lierabbit.cn/articles/4 一.准备 postman:一个接口测试工具 创建一个新工程 选择web 不会的请看Spring Boot快速入门(一):Hel ...

  4. Spring Cloud Gateway入门

    1.什么是Spring Cloud GatewaySpring Cloud Gateway是Spring官方基于Spring 5.0,Spring Boot 2.0和Project Reactor等技 ...

  5. Spring Cloud Alibaba入门实战之nacos(一)

    Spring Cloud Alibaba入门实战之nacos(一) 前情介绍 ​ Spring Cloud Alibaba 是阿里巴巴提供的新一代的微服务解决方案,相信会有越来越多采用微服务架构的公司 ...

  6. Spring Cloud 从入门到精通

    Spring Cloud 是一套完整的微服务解决方案,基于 Spring Boot 框架,准确的说,它不是一个框架,而是一个大的容器,它将市面上较好的微服务框架集成进来,从而简化了开发者的代码量. 本 ...

  7. 主流微服务一站式解决方案Spring Cloud Alibaba入门看这篇就足够了

    学习路线 **本人博客网站 **IT小神 www.itxiaoshen.com 生态概述 架构演进 什么是微服务 https://martinfowler.com/microservices/ Mic ...

  8. Spring Cloud Ribbon入门

    一.简介 Spring Cloud Ribbon是一个基于Http和TCP的客户端负载均衡工具,它是基于Netflix Ribbon实现的.它不像服务注册中心.配置中心.API网关那样独立部署,但是它 ...

  9. Spring Boot 快速入门 史上最简单

    1.Spring Boot 概述 Spring Boot 是所有基于 Spring 开发的项目的起点.Spring Boot 的设计是为了让你尽可能快的跑起来 Spring 应用程序并且尽可能减少你的 ...

随机推荐

  1. 不用代码就能实现get与post

    这些天在测试各种API,每次都敲代码实现,就显得有点浪费时间了 为了节约时间,提高效率,我想着收集一些可以只用 -命令行- 或者是 -浏览器- 就能够实现的技巧 在这里,我简单测试三种工具来实现 ge ...

  2. (转)ReentrantLock实现原理及源码分析

    背景:ReetrantLock底层是基于AQS实现的(CAS+CHL),有公平和非公平两种区别. 这种底层机制,很有必要通过跟踪源码来进行分析. 参考 ReentrantLock实现原理及源码分析 源 ...

  3. 对称加密之AES加密详解

    最近有人问我AES对称加密是啥,我回答了个大概,发现自己不能清晰的讲出来,特此记录,以供学习 一.对称加密 对称加密是最快速.最简单的一种加密方式,加密(encryption)与解密(decrypti ...

  4. 原生js实现平滑滚动

    在以前的项目中有用到,在此整理一下: html部分 <span id="gotop">回到顶部</span> JS部分 // 使用requestAnimat ...

  5. Go package(1) time 用法

    golang使用的版本: go version go1.10.3 一:功能介绍 time的一些功能,比如时区,像linux中的定时器,时间计算等 格式化时间 时区(Location) 时间计算 Tic ...

  6. [Android] Android Build 时报错: java.io.IOException: Could not parse XML from android/accounts/annotations.xml

    Android构建时报错: app:lintVitalRelease[Fatal Error] :3:214: 与元素类型 “item” 相关联的 “name” 属性值不能包含 ‘<’ 字符. ...

  7. shell 批量远程主机执行命令

    [yunwei@Y24-209 ~]$cat ls.sh #!/bin/bash ip55=`cat ip1` for i in $ip55;do ping -c 1 $i if [ $? -eq 0 ...

  8. 微信小程序常见的坑

    wxml的标签跟html里面的一些标签是一样的,比如view标签相当于div标签,text标签相当于span标签. 在微信小程序中,表单元素都是原生组件,微信小程序中原生组件层级最高,所以在用inpu ...

  9. Selenium-ActionChainsApi--鼠标连贯操作

    ActionChains UI自动化测试过程中,经常遇到那种,需要鼠标悬浮后,要操作的元素才会出现的这种场景,那么我们就要模拟鼠标悬浮到某一个位置,做一系列的连贯操作,Selenium给我们提供了Ac ...

  10. asp.net webapi 获取报文体的问题

    用这种方法: var data=await Request.Content.ReadAsStringAsync(); 一般都无法获取到内容.原因是内部的流对象已经到了最后面.要获取到里面的需要把流的位 ...