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. 最近公共祖先(LCT)

    来一发\(LCT\)求\(LCA\) \(LCT\)在时间上不占据优势,码量似乎还比树剖,倍增,\(Tarjan\)大一点 但是却是一道\(LCT\)的练手题 对于每一个询问,我们只需要把其中一个点( ...

  2. 阿里云ECS服务器部署Node.js项目全过程详解

    本文详细介绍如何部署NodeJS项目到阿里云ECS上,以及本人在部署过程中所遇到的问题.坑点和解决办法,可以说是全网最全最详细的教程了.同时讲解了如何申请阿里云免费SSL证书,以及一台ECS服务器配置 ...

  3. css解决内联元素间的空白间隔

    在内联元素的父级元素上设置font-size: 0px;即可.例如: <div class="wrap"> <ul> <li class=" ...

  4. (双指针) leetcode 485. Max Consecutive Ones

    Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...

  5. nginx日志格式定义和nginx.conf配置模板说明

    在http的功能里添加log_format模块,内容如下: log_format main escape=json '{ "@timestamp": "$time_iso ...

  6. JDK环境部署

    JDK环境部署 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 说起JDK想必大家并不陌生,作为运维的小伙伴,谁不层接触过Java程序员呢?而且在搭建服务上时也必须得接触他,比如to ...

  7. bzoj 4244 括号序列dp

    将各种情况绕环等看作括号序列,括号内的区域上下都需要累加答案,左右也是 f[i][j] 代表 前i个车站已经处理完的有j个左括号的最小权值 我们可以发现,更新的来源来自于 i-1, 和 i 将上 描述 ...

  8. java(11)带参数的方法

    一.java中的包(package) 1.1 包,对应到磁盘中的文件夹 1.2 新建一个class,默认保存在缺省包中 1.3 声明包的关键字:package package语句,置顶位置 1.4 导 ...

  9. Arduino-常用指令

    pinMode(LEDPin,OUTPUT);        //设置引脚模式 参数1      引脚 参数2      OUTPUT  输出:INPUT   输入 用在setup()函数里 digi ...

  10. vue项目的mode:history模式

    最近做的Vue + Vue-Router + Webpack +minitUI项目碰到的问题,在此记录一下,Vue-router 中有hash模式和history模式,vue的路由默认是hash模式, ...