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. Flex布局【弹性布局】学习

    先让我们看看在原来的学习中遇到的问题 之前在软件工程的大作业中,自己从零开始学习如何开发一个网站,从页面,到后台,当然数据库是大二的必修课 在学习如何编写一个静态页面的时候,完全是自学,自己摸索,所以 ...

  2. dsu on tree 树上启发式合并 学习笔记

    近几天跟着dreagonm大佬学习了\(dsu\ on\ tree\),来总结一下: \(dsu\ on\ tree\),也就是树上启发式合并,是用来处理一类离线的树上询问问题(比如子树内的颜色种数) ...

  3. 如何隐藏overflow: scroll的滚动条

    css3有一个直接调用的css,保证隐藏滚动条的同时还可以继续通过滚轮向下翻 ::-webkit-scrollbar { /*隐藏滚轮*/ display: none; } 但是仅限于支持css3的浏 ...

  4. django 1.开发接口环境搭建

    首先需要的环境: pycharm Python 3.6.0 django 2.1.3        安装命令: pip3 install django   查看版本号和安装的路径: pip show ...

  5. 博弈论中的Nim博弈

    瞎扯 \(orzorz\) \(cdx\) 聚聚给我们讲了博弈论.我要没学上了,祝各位新年快乐.现在让我讲课我都不知道讲什么,我会的东西大家都会,太菜了太菜了. 马上就要回去上文化课了,今明还是收下尾 ...

  6. 《Java》第九周学习总结

    下载mysql 选择mysql的管理软件 idea可以直接连接 然后用库运行程序,但是没有截图,,因为想在navicat上试试,可惜速度太慢了 打开idea又很慢,所以明天再更新

  7. ES6.3.2 副本失败处理

    ES6.3.2 副本失败处理 副本的失败处理对理解ES的数据副本模型很有帮助.在ES6.3.2 index操作源码流程的总结中提到:ES的写操作会先写主分片,然后主分片再将操作同步到副本分片.本文给出 ...

  8. Vue插槽的深入理解和应用

    一开始接触vue时并不知道插槽是什么,后来看了很多文章也是一知半解.然后自己手动敲了一下,在项目中实际应用一下,实在太好用了.后来做小程序后发现也能使用slot,不单单在vue中使用.我就是这么目光短 ...

  9. httpclient用getStatusCode

      TP 定义的状态代码的值(.net HttpWebResponse.HttpStatusCode 成员名称 说明 Continue 等效于 HTTP 状态 100.Continue 指示客户端可能 ...

  10. 查询sql 索引

    SELECT indexname = a.name , tablename = c. name , indexcolumns = d .name , a .indidFROM sysindexes a ...