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

本文介绍了如何使用SpringBoot和SpringCloud构建配置中心服务,包括如何配置@EnableConfigServer,设置Git仓库的配置文件,以及如何通过Eureka实现服务发现和配置中心的高可用,同时涉及了消息总线SpringCloudBus的应用。
摘要由CSDN通过智能技术生成

(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:

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 #续约到期的时间

(3)微服务客户端引入依赖

在这里插入图片描述

org.springframework.cloud

spring-cloud-bus

org.springframework.cloud

spring-cloud-stream-binder-rabbit

(4)微服务客户端配置

1)bootstrap.yml 当中删除请求路径的端点的配置

在这里插入图片描述

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

#配置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

2)需要在码云对应的配置文件中添加rabbitmq的配置信息

修改product-pro.yml

在这里插入图片描述

rabbitmq:

host: 127.0.0.1

port: 5672

username: guest

password: guest

提交并保存

(5)运行测试

将之前的ConfigServerApplication1 :10002/关闭

在这里插入图片描述

重新启动对应的eureka-server , config-server , product-service。

配置信息刷新后,只需要向配置中心发送对应的请求,即可刷新每个客户端的配置。

在这里插入图片描述

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

在这里插入图片描述

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

在这里插入图片描述

这个时候修改Gitee当中product-pro.yml 的内容

在这里插入图片描述

提交保存后再次访问http://localhost:9002/product/test

在这里插入图片描述

我们发现并没有改变

http://localhost:10001/actuator/bus-refresh,发送POST请求

在这里插入图片描述

然后启动RabbitMQ

在这里插入图片描述

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

在这里插入图片描述

| SpringCloud学习目录点击跳转对应的文章 | |

| — | — |

| Java之 Spring Cloud 微服务搭建(第一个阶段)【一】【SpringBoot项目实现商品服务器端是调用】 | |

| Java之 Spring Cloud 微服务 Eureka (第一个阶段)【二】【SpringBoot项目实现商品服务器端是调用】 | |

| Java之 Spring Cloud 微服务搭建Ribbon(第一个阶段)【三】【SpringBoot项目实现商品服务器端是调用】 | |

| Java之 Spring Cloud 微服务搭建 Consul(第一个阶段)【四】【SpringBoot项目实现商品服务器端是调用】 | |

| Java之 Spring Cloud 微服务搭建 Feign组件(第二个阶段)【一】【SpringBoot项目实现商品服务器端是调用】 | |

| Java之 Spring Cloud 微服务搭建 Hystrix (第二个阶段)【二】【SpringBoot项目实现商品服务器端是调用】 | |

| Java之 Spring Cloud 微服务搭建Sentinel (第二个阶段)【三】【SpringBoot项目实现商品服务器端是调用】 | |

| Java之 Spring Cloud 微服务搭建网关 nginx,Zuul(第三个阶段)【一】【SpringBoot项目实现商品服务器端是调用】 | |

| Java之 Spring Cloud 微服务搭建网关SpringCloud Gateway微服务网关GateWay(第三个阶段)【二】【SpringBoot项目实现商品服务器端是调用】 | |
| Java之 Spring Cloud 微服务的链路追踪 Sleuth 和 Zipkin(第三个阶段)【三】【SpringBoot项目实现商品服务器端是调用】 | |
| Java之 Spring Cloud 微服务的 Spring Cloud Stream(第四个阶段)【一】【SpringBoot项目实现商品服务器端调用】 | |

最后

|

| Java之 Spring Cloud 微服务 Eureka (第一个阶段)【二】【SpringBoot项目实现商品服务器端是调用】 | |

| Java之 Spring Cloud 微服务搭建Ribbon(第一个阶段)【三】【SpringBoot项目实现商品服务器端是调用】 | |

| Java之 Spring Cloud 微服务搭建 Consul(第一个阶段)【四】【SpringBoot项目实现商品服务器端是调用】 | |

| Java之 Spring Cloud 微服务搭建 Feign组件(第二个阶段)【一】【SpringBoot项目实现商品服务器端是调用】 | |

| Java之 Spring Cloud 微服务搭建 Hystrix (第二个阶段)【二】【SpringBoot项目实现商品服务器端是调用】 | |

| Java之 Spring Cloud 微服务搭建Sentinel (第二个阶段)【三】【SpringBoot项目实现商品服务器端是调用】 | |

| Java之 Spring Cloud 微服务搭建网关 nginx,Zuul(第三个阶段)【一】【SpringBoot项目实现商品服务器端是调用】 | |

| Java之 Spring Cloud 微服务搭建网关SpringCloud Gateway微服务网关GateWay(第三个阶段)【二】【SpringBoot项目实现商品服务器端是调用】 | |
| Java之 Spring Cloud 微服务的链路追踪 Sleuth 和 Zipkin(第三个阶段)【三】【SpringBoot项目实现商品服务器端是调用】 | |
| Java之 Spring Cloud 微服务的 Spring Cloud Stream(第四个阶段)【一】【SpringBoot项目实现商品服务器端调用】 | |

最后

[外链图片转存中…(img-LHRqQLMY-1714337844580)]

[外链图片转存中…(img-8RFXbrrG-1714337844581)]

[外链图片转存中…(img-KnthcrSR-1714337844581)]

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

  • 13
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值