Java之 Spring Cloud 微服务的 SpringCloud Config 配置中心(第四个阶段)【二】【SpringBoot项目实现商品服务器端调用】

《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!

username: root

password: root

jpa:

database: MySQL

show-sql: true

open-in-view: true

#配置Eureka

eureka:

client:

service-url:

defaultZone: http://localhost:9000/eureka/

instance:

prefer-ip-address: true #使用ip地址注册

instance-id: s p r i n g . c l o u d . c l i e n t . i p − a d d r e s s : {spring.cloud.client.ip-address}: spring.cloud.client.ipaddress:{server.port} #向组成中心注册服务ID

lease-renewal-interval-in-seconds: 5 #向注册中心中注册服务id

lease-expiration-duration-in-seconds: 10 #续约到期的时间

name: itbluebox

(2)ProductController

在这里插入图片描述

@RestController

@RequestMapping(“/product”)

public class ProductController {

@Autowired

private ProductService productService;

@Value(“${server.port}”)

private String port;

@Value(“${spring.cloud.client.ip-address}”)

private String ip;

@Value(value = “${name}”)

private String name;

@RequestMapping(value = “/{id}”, method = RequestMethod.GET)

public Product findById(@PathVariable Long id) {

Product product = productService.findByID(id);

product.setProductName(“访问的服务地址:”+ip+“:”+port);

return product;

}

@RequestMapping(value = “/test”)

public String test(){

return name;

}

}

(3)运行测试

在这里插入图片描述

(2)浏览器打开gitee.com,

1)注册用户 ,注册后登陆码云管理控制台

在这里插入图片描述

2)创建仓库

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

创建成功

在这里插入图片描述

3)抽取配置文件

文件命名规则:

  • {application}-{profile}.yml

  • {application}-{profile}.properties

  • application为应用名称 profile指的开发环境(用于区分开发环境,测试环境、生产环境等)

在这里插入图片描述

将上述文件移动到电脑桌面并,对文件重新命名修改配置文件product-pro.yml

在这里插入图片描述

server:

port: 9002 #端口

spring:

application:

name: service-product #服务名称

datasource:

driver-class-name: com.mysql.jdbc.Driver

url: jdbc:mysql://localhost:3306/springcloud?useUnicode=true&characterEncoding=utf8

username: root

password: root

jpa:

database: MySQL

show-sql: true

open-in-view: true

#配置Eureka

eureka:

client:

service-url:

defaultZone: http://localhost:9000/eureka/

instance:

prefer-ip-address: true #使用ip地址注册

instance-id: s p r i n g . c l o u d . c l i e n t . i p − a d d r e s s : {spring.cloud.client.ip-address}: spring.cloud.client.ipaddress:{server.port} #向组成中心注册服务ID

lease-renewal-interval-in-seconds: 5 #向注册中心中注册服务id

lease-expiration-duration-in-seconds: 10 #续约到期的时间

name: itbluebox-pro

将上述文件移动到电脑桌面并,创建新的配置文件product-dev.yml

在这里插入图片描述

server:

port: 9001 #端口

spring:

application:

name: service-product #服务名称

datasource:

driver-class-name: com.mysql.jdbc.Driver

url: jdbc:mysql://localhost:3306/springcloud?useUnicode=true&characterEncoding=utf8

username: root

password: root

jpa:

database: MySQL

show-sql: true

open-in-view: true

#配置Eureka

eureka:

client:

service-url:

defaultZone: http://localhost:9000/eureka/

instance:

prefer-ip-address: true #使用ip地址注册

instance-id: s p r i n g . c l o u d . c l i e n t . i p − a d d r e s s : {spring.cloud.client.ip-address}: spring.cloud.client.ipaddress:{server.port} #向组成中心注册服务ID

lease-renewal-interval-in-seconds: 5 #向注册中心中注册服务id

lease-expiration-duration-in-seconds: 10 #续约到期的时间

name: itbluebox-dev

4)将上述配置文件上传到gitee

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

2、 搭建服务端程序


(1)创建工程

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

(2)引入依赖

在这里插入图片描述

org.springframework.cloud

spring-cloud-config-server

org.springframework.cloud

spring-cloud-starter-config

(3)编写启动类

在这里插入图片描述

在这里插入图片描述

@SpringBootApplication

@EnableConfigServer

public class ConfigServerApplication {

public static void main(String[] args) {

SpringApplication.run(ConfigServerApplication.class,args);

}

}

  • @EnableConfigServer : 通过此注解开启注册中心服务端功能

(4)设置配置文件

在这之前复制一下Git请求的服务器的地址

在这里插入图片描述

在这里插入图片描述

server:

port: 10001 #服务端口

spring:

application:

name: config-server #指定服务名

cloud:

config:

server:

git:

uri: https://gitee.com/itbluebox/config-repostory.git

(5)启动服务进行测试

在这里插入图片描述

运行成功

在这里插入图片描述

访问测试:http://localhost:10001/product-dev.yml

在这里插入图片描述

3、修改客户端程序


(1)修改product_service

1)引入依赖

在这里插入图片描述

org.springframework.cloud

spring-cloud-starter-config

2)删除application.yml

springboot的应用配置文件,需要通过Config-server获取,这里不再需要。

在这里插入图片描述

spring:

cloud:

config:

name: product #应用名称需要对应Git中配置文件的名称的前半部分

profile: dev #开发环境

label: master #git当中的分支

uri: http://localhost:10001 #config-server的请求地址

3)运行测试

在这里插入图片描述

访问:http://localhost:9001/product/1

在这里插入图片描述

访问:http://localhost:9001/product/test

在这里插入图片描述

4)修改配置文件然后在运行测试

将dev修改为pro

在这里插入图片描述

spring:

cloud:

config:

name: product #应用名称需要对应Git中配置文件的名称的前半部分

profile: pro #开发环境

label: master #git当中的分支

uri: http://localhost:10001 #config-server的请求地址

重新启动运行

在这里插入图片描述

请求的端口改变了

在这里插入图片描述

访问:http://localhost:9002/product/1

在这里插入图片描述

4、手动刷新


(1)问题解析

我们已经在客户端取到了配置中心的值,

修改Gitee上的值

在这里插入图片描述

在这里插入图片描述

修改内容

在这里插入图片描述

在这里插入图片描述

访问:http://localhost:9002/product/test

在这里插入图片描述

但当我们修改Gitee上面的值时,

服务端(Config Server)能实时获取最新的值,但客户端(Config Client)读的是缓存,无法实时获取最新值。

SpringCloud已经为我们解决了这个问题,那就是客户端使用post去触发refresh,获取最新数据,需要依赖spring-boot-starter-actuator

(2)引入依赖

在这里插入图片描述

org.springframework.boot

spring-boot-starter-actuator

(3)在ProductController类加上@RefreshScope

在这里插入图片描述

@RestController

@RequestMapping(“/product”)

@RefreshScope //代表的是开启动态刷新

public class ProductController {

@Autowired

private ProductService productService;

@Value(“${server.port}”)

private String port;

@Value(“${spring.cloud.client.ip-address}”)

private String ip;

@Value(value = “${name}”)

private String name;

@RequestMapping(value = “/{id}”, method = RequestMethod.GET)

public Product findById(@PathVariable Long id) {

Product product = productService.findByID(id);

product.setProductName(“访问的服务地址:”+ip+“:”+port);

return product;

}

@RequestMapping(value = “/test”)

public String test(){

return name;

}

}

(4)配置文件bootstrap.yml中开发端点

在这里插入图片描述

开启动态刷新的请求路径的端点

management:

endpoints:

web:

exposure:

include: refresh

(5)运行测试

在这里插入图片描述

访问:http://localhost:9002/product/test

在这里插入图片描述

其值itbluebox-product改变因为重新启动了

我们再次修改Gitee对应的配置文件的值

在这里插入图片描述

提交并保存

再次访问:http://localhost:9002/product/test

在这里插入图片描述

(6)手动刷新发起POST请求,这里使用的工具是Insomnia

访问:http://localhost:9002/actuator/refresh

在这里插入图片描述

再次访问:http://localhost:9002/product/test

在这里插入图片描述

三、配置中心的高可用

=====================================================================

在之前的代码中,客户端都是直接调用配置中心的server端来获取配置文件信息。

这样就存在了一个问题,客户端和服务端的耦合性太高,如果server端要做集群,客户端只能通过原始的方式来路由,server端改变IP地址的时候,客户端也需要修改配置,不符合springcloud服务治理的理念。

springcloud提供了这样的解决方案,我们只需要将server端当做一个服务注册到eureka中,client端去eureka中去获取配置中心server端的服务既可。

1、 config_server改造


(1)添加依赖

在这里插入图片描述

org.springframework.cloud

spring-cloud-starter-netflix-eureka-client

org.springframework.boot

spring-boot-starter-actuator

org.springframework.cloud

spring-cloud-bus

org.springframework.cloud

spring-cloud-stream-binder-rabbit

(2)配置文件

在这里插入图片描述

#配置Eureka

eureka:

client:

service-url:

defaultZone: http://localhost:9000/eureka/

instance:

prefer-ip-address: true #使用ip地址注册

instance-id: s p r i n g . c l o u d . c l i e n t . i p − a d d r e s s : {spring.cloud.client.ip-address}: spring.cloud.client.ipaddress:{server.port} #向组成中心注册服务ID

lease-renewal-interval-in-seconds: 5 #向注册中心中注册服务id

lease-expiration-duration-in-seconds: 10 #续约到期的时间

这样server端的改造就完成了。

(3)运行测试

先启动eureka注册中心,在启动server端,

在浏览器中访问:

在这里插入图片描述

复制一份ConfigServerApplication

在这里插入图片描述

在这里插入图片描述

修改一下配置文件的端口号

在这里插入图片描述

在这里插入图片描述

访问Eurekahttp://localhost:9000/

在这里插入图片描述

2、 对微服务进行改造


修改配置文件

在这里插入图片描述

spring:

cloud:

config:

name: product #应用名称需要对应Git中配置文件的名称的前半部分

profile: pro #开发环境

label: master #git当中的分支

#uri: http://localhost:10001 #config-server的请求地址

#通过注册中心去获取config-server配置

discovery:

enabled: true #开启服务发现

service-id: config-server

开启动态刷新的请求路径的端点

management:

endpoints:

web:

exposure:

include: refresh

#配置Eureka

eureka:

client:

service-url:

defaultZone: http://localhost:9000/eureka/

instance:

prefer-ip-address: true #使用ip地址注册

instance-id: s p r i n g . c l o u d . c l i e n t . i p − a d d r e s s : {spring.cloud.client.ip-address}: spring.cloud.client.ipaddress:{server.port} #向组成中心注册服务ID

重新启动

在这里插入图片描述

访问测试:http://localhost:9002/product/1

在这里插入图片描述

四、消息总线bus

====================================================================

1、消息总线bus概念介绍


在微服务架构中,

通常会使用轻量级的消息代理来构建一个共用的消息主题来连接各个微服务实例,

它广播的消息会被所有在注册中心的微服务实例监听和消费,也称消息总线。

SpringCloud中也有对应的解决方案,

SpringCloud Bus 将分布式的节点用轻量的消息代理连接起来,

可以很容易搭建消息总线,

配合SpringCloud config 实现微服务应用配置信息的动态更新。

在这里插入图片描述

根据此图我们可以看出利用Spring Cloud Bus做配置更新的步骤:

  • 提交代码触发post请求给bus/refresh

  • server端接收到请求并发送给Spring Cloud Bus

  • Spring Cloud bus接到消息并通知给其它客户端

  • 其它客户端接收到通知,请求Server端获取最新配置

  • 全部客户端均获取到最新的配置

2、消息总线整合配置中心


(1)服务端引入依赖

刚刚在上述已经引入了对应消息总线的依赖,在这里是做介绍

在这里插入图片描述

(2)服务端添加配置

在这里插入图片描述

server:

port: 10001 #服务端口

spring:

application:

name: config-server #指定服务名

cloud:

config:

server:

git:

uri: https://gitee.com/itbluebox/config-repostory.git

rabbitmq:

host: 127.0.0.1

port: 5672

username: guest

password: guest

management:

endpoints:

web:

exposure:

include: bus-refresh

#配置Eureka

eureka:

client:

service-url:

defaultZone: http://localhost:9000/eureka/

instance:

总结

我个人认为,如果你想靠着背面试题来获得心仪的offer,用癞蛤蟆想吃天鹅肉形容完全不过分。想必大家能感受到面试越来越难,想找到心仪的工作也是越来越难,高薪工作羡慕不来,却又对自己目前的薪资不太满意,工作几年甚至连一个应届生的薪资都比不上,终究是错付了,错付了自己没有去提升技术。

这些面试题分享给大家的目的,其实是希望大家通过大厂面试题分析自己的技术栈,给自己梳理一个更加明确的学习方向,当你准备好去面试大厂,你心里有底,大概知道面试官会问多广,多深,避免面试的时候一问三不知。

大家可以把Java基础,JVM,并发编程,MySQL,Redis,Spring,Spring cloud等等做一个知识总结以及延伸,再去进行操作,不然光记是学不会的,这里我也提供一些脑图分享给大家:

希望你看完这篇文章后,不要犹豫,抓紧学习,复习知识,准备在明年的金三银四拿到心仪的offer,加油,打工人!
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!
aXRibHVlYm94,size_20,color_FFFFFF,t_70,g_se,x_16)

server:

port: 10001 #服务端口

spring:

application:

name: config-server #指定服务名

cloud:

config:

server:

git:

uri: https://gitee.com/itbluebox/config-repostory.git

rabbitmq:

host: 127.0.0.1

port: 5672

username: guest

password: guest

management:

endpoints:

web:

exposure:

include: bus-refresh

#配置Eureka

eureka:

client:

service-url:

defaultZone: http://localhost:9000/eureka/

instance:

总结

我个人认为,如果你想靠着背面试题来获得心仪的offer,用癞蛤蟆想吃天鹅肉形容完全不过分。想必大家能感受到面试越来越难,想找到心仪的工作也是越来越难,高薪工作羡慕不来,却又对自己目前的薪资不太满意,工作几年甚至连一个应届生的薪资都比不上,终究是错付了,错付了自己没有去提升技术。

这些面试题分享给大家的目的,其实是希望大家通过大厂面试题分析自己的技术栈,给自己梳理一个更加明确的学习方向,当你准备好去面试大厂,你心里有底,大概知道面试官会问多广,多深,避免面试的时候一问三不知。

大家可以把Java基础,JVM,并发编程,MySQL,Redis,Spring,Spring cloud等等做一个知识总结以及延伸,再去进行操作,不然光记是学不会的,这里我也提供一些脑图分享给大家:

[外链图片转存中…(img-a9TVhKuM-1714678042107)]

[外链图片转存中…(img-W6Hm9hYk-1714678042107)]

[外链图片转存中…(img-jetfFtFy-1714678042108)]

希望你看完这篇文章后,不要犹豫,抓紧学习,复习知识,准备在明年的金三银四拿到心仪的offer,加油,打工人!
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!

  • 16
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值