博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Cloud 2.x完整入门Demo样例(Greenwich版本)
阅读量:2390 次
发布时间:2019-05-10

本文共 9302 字,大约阅读时间需要 31 分钟。

2019年6月,当前最新版Greenwich.SR1发布。

参考官方文档

1、新建Maven项目

我们新建一个普通的Maven项目,项目名称cloud,对应的pom.xml文件说明如下。

该pom文件作为父级pom文件,起到依赖版本控制的作用,其他Module模块均继承该pom。

4.0.0
com.cntaiping.tpa
cloud
1.0-SNAPSHOT
pom
cloud
Demo project for Spring Boot
UTF-8
UTF-8
1.8
Greenwich.SR1
org.springframework.boot
spring-boot-starter-parent
2.1.5.RELEASE
register-server
producer
consumer
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin

2、新建Module

我们需要在cloud项目中新建3个Module模块,服务注册中心(register-server)、服务生产者(producer)和服务消费者(consumer)

新建Module模块的基本方法:

选择Maven项目cloud,右键单击–>New–>Module–>选择spring initialir
也就是新建一个SpringBoot模块。
在这里插入图片描述

2.1 register-server Module

新建服务注册中心模块(register-server),也是一个SpingBoot模块。

(1) pom.xml

修改对应的pom文件,内容如下,继承父级pom文件。

4.0.0
com.cntaiping.tpa.cloud
register-server
0.0.1-SNAPSHOT
jar
register-server
Demo project for Spring Boot
com.cntaiping.tpa
cloud
1.0-SNAPSHOT
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server

(2) application.properties

#注册中心服务IDspring.application.name=register-server#端口号server.port=8800# eureka.client.registerWithEureka :表示是否将自己注册到Eureka Server,默认为true。# 由于当前这个应用就是Eureka Server,故而设为falseeureka.client.register-with-eureka=false# eureka.client.fetchRegistry :表示是否从Eureka Server获取注册信息,默认为true。因为这是一个单点的Eureka Server,# 不需要同步其他的Eureka Server节点的数据,故而设为false。eureka.client.fetch-registry=false# eureka.client.serviceUrl.defaultZone :设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。默认是eureka.client.serviceUrl.defaultZone=http://localhost:8800/eureka/

需要指明spring.application.name参数,这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个name。

(3) Application类

需要添加一个注解@EnableEurekaServer

package com.cntaiping.tpa.cloud.registerserver;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@EnableEurekaServer@SpringBootApplicationpublic class RegisterServerApplication {    public static void main(String[] args) {        SpringApplication.run(RegisterServerApplication.class, args);    }}

(4) 运行效果

在这里插入图片描述

No application available

表示没有服务被发现, 因为现在还没有服务提供者进行注册服务。

2.2 producer Module

新建服务生产者模块(producer)。服务的提供方可以看做是服务注册中心的客户端,所以需要引入spring-cloud-starter-netflix-eureka-client。

(1) pom.xml

修改对应的pom文件,内容如下。

4.0.0
com.cntaiping.tpa
producer
0.0.1-SNAPSHOT
jar
producer
Demo project for Spring Boot
com.cntaiping.tpa
cloud
1.0-SNAPSHOT
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-web

(2) application.properties

#服务名称spring.application.name=producer#端口号server.port=8700#在注册中心中进行注册eureka.client.serviceUrl.defaultZone=http://localhost:8800/eureka/#启动服务发现的功能,开启了才能调用其它服务spring.cloud.config.discovery.enabled=true#发现的服务的名字--对应注测中心的服务名字spring.cloud.config.discovery.serviceId=register-server

(3) Application类

需要添加一个@EnableEurekaClient注解 表明自己是一个eurekaclient

package com.cntaiping.tpa.producer;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication@EnableEurekaClientpublic class ProducerApplication {    public static void main(String[] args) {        SpringApplication.run(ProducerApplication.class, args);    }}

服务生产者提供的具体服务如下,这里只是一个简单例子,返回一个字符串。

package com.cntaiping.tpa.producer.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class MessageController {    @Value("${server.port}")    String port;    @GetMapping("/get")    public String getMessage(@RequestParam("name")String name){        return "Hi " + name + " ,I am from port:" + port;    }}

(4) (多端口)多实例运行

当服务提供方任务繁重时,可以多起几个该服务的实例,通过负载均衡来解决。

这里我们通过多端口来启动服务生产者producer模块的多个实例。

第一步:在IntelliJ IDEA右上角点击XXXXApplication右边的下三角,弹出下拉框,点击“Edit Configuration…”

在这里插入图片描述
第二步:复制服务生产者模块(producer)的实例
通过复制按钮复制服务生产者模块(producer)的实例,这样就有两个实例了。为了区别,修改了实例名称Name,分别加上了启动端口号做后缀。

实例配置:将默认的Single instance only(单实例)的钩去掉,VM options中添加“-Dserver.port=8700”,表示将从8700端口启动一个实例。另一个实例端口修改为8701即可。

在这里插入图片描述

(5) 运行效果

分别运行服务生产者模块(producer)的两个实例,效果如下。

在这里插入图片描述
在这里插入图片描述
再次刷新注册中心页面,可以发现服务生产者的两个实例(8700和8701)
在这里插入图片描述

2.3 consumer Module

同样新建一个服务消费者模块,名称是consumer。

(1) pom.xml

修改pom文件内容如下。

4.0.0
com.cntaiping.tpa
consumer
0.0.1-SNAPSHOT
jar
consumer
Demo project for Spring Boot
com.cntaiping.tpa
cloud
1.0-SNAPSHOT
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-ribbon

Spring cloud有两种服务调用方式,一种是ribbon+restTemplate,另一种是feign。这里我们采用ribbon+restTemplate方式,pom文件中增加了spring-cloud-starter-netflix-ribbon依赖。

(2) application.yml

这里配置文件采用yml格式,服务消费者模块的端口号是 8600。

eureka:  client:    serviceUrl:      defaultZone: http://localhost:8800/eureka/server:  port: 8600spring:  application:    name: consumer

(3) Application类

相同点:@EnableDiscoveryClient和@EnableEurekaClient都是能够让注册中心能够发现,扫描到改服务。

不同点:@EnableEurekaClient只适用于Eureka作为注册中心,@EnableDiscoveryClient 可以是其他注册中心。

从Spring Cloud Edgware开始,@EnableDiscoveryClient 或@EnableEurekaClient 可省略。只需加上相关依赖,并进行相应配置,即可将微服务注册到服务发现组件上。

package com.cntaiping.tpa.consumer;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.client.loadbalancer.LoadBalanced;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.context.annotation.Bean;import org.springframework.web.client.RestTemplate;@SpringBootApplication@EnableEurekaClient@EnableDiscoveryClientpublic class ConsumerApplication {    public static void main(String[] args) {        SpringApplication.run(ConsumerApplication.class, args);    }    @LoadBalanced //使用负载均衡机制    @Bean    public RestTemplate restTemplate(){        return new RestTemplate();    }}

服务消费类如下,通过Spring DI注入的restTemplate来消费producer的“/get”接口

package com.cntaiping.tpa.consumer.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate;@RestControllerpublic class MessageController {    @Autowired    RestTemplate restTemplate;    @GetMapping("/show")    public String showMessage(@RequestParam String name){        return restTemplate.getForObject("http://producer/get?name="+name, String.class);    }}

(4) 运行效果

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

3、整体架构

在这里插入图片描述

  • 一个服务注册中心,register-server,端口号8800
  • 服务生产者producer跑了两个实例,端口分别为8700,8701,分别向服务注册中心注册
  • 服务消费者consumer,端口为8600,向服务注册中心注册
  • 当consumer通过restTemplate调用producer的get接口时,因为用ribbon进行了负载均衡,会轮流的调用producer:8700和8701 两个端口的get接口。

转载地址:http://egvab.baihongyu.com/

你可能感兴趣的文章
新手网站建设指南(2)
查看>>
HTML特殊字符显示(常用到的特殊符号,箭头相关,数学相关,标点,符号相关等)...
查看>>
40岁的程序员找不到工作,原来码农真的是碗青春饭
查看>>
2018年前端性能优化总结,这也是我做程序员的第五个年头了
查看>>
前端进阶(三)从0到1学AJAX,这篇就够了!
查看>>
强大的CSS:实现平行四边形布局效果
查看>>
强大的CSS:var变量的局部作用域(继承)特性
查看>>
强大的CSS: 使用“变量种子计数器”扩展动画更多可能性
查看>>
强大的CSS:focus-visible伪类真的太6了!
查看>>
强大的CSS:3种姿势实现26个英文字母的案例
查看>>
强大的CSS:placeholder-shown伪类实现Material Design占位符交互效果
查看>>
强大的CSS:图形绘制合集,方便你我!
查看>>
强大的CSS:scroll-snap滚动事件停止及元素位置检测
查看>>
程序员30岁前,月薪达不到30K,该何去何从?
查看>>
Web前端很难学?html、css t、JavaScrip知识架构图分享
查看>>
常见的前端开发:Javascript 面试题及回答策略
查看>>
web前端开发学习推荐这5本书
查看>>
Windows资源管理器相关信息获取
查看>>
windows资源管理器及ie监听
查看>>
No module named 'Crypto'
查看>>