700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > (八)JAVA springcloud ssm b2b2c多用户商城系统源码:配置中心服务化和高可用

(八)JAVA springcloud ssm b2b2c多用户商城系统源码:配置中心服务化和高可用

时间:2023-03-27 13:50:03

相关推荐

(八)JAVA springcloud ssm b2b2c多用户商城系统源码:配置中心服务化和高可用

server端改造

1、添加依赖

<dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency></dependencies>复制代码

需要多引入spring-cloud-starter-eureka包,来添加对eureka的支持。

2、配置文件

server:server:port: 8001spring:application:name: spring-cloud-config-servercloud:config:server:git:uri: /ityouknow/spring-cloud-starter/# 配置git仓库的地址search-paths: config-repo # git仓库地址下的相对地址,可以配置多个,用,分割。username: username# git仓库的账号password: password# git仓库的密码eureka:client:serviceUrl:defaultZone: http://localhost:8000/eureka/ ## 注册中心eurka地址复制代码

增加了eureka注册中心的配置

3、启动类

启动类添加@EnableDiscoveryClient激活对配置中心的支持

@EnableDiscoveryClient@EnableConfigServer@SpringBootApplicationpublic class ConfigServerApplication {public static void main(String[] args) {SpringApplication.run(ConfigServerApplication.class, args);}}复制代码

这样server端的改造就完成了。先启动eureka注册中心,在启动server端,在浏览器中访问:http://localhost:8000/就会看到server端已经注册了到注册中心了。

按照上篇的测试步骤对server端进行测试服务正常。

客户端改造

1、添加依赖

<dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>复制代码

需要多引入spring-cloud-starter-eureka包,来添加对eureka的支持。

2、配置文件

spring.application.name=spring-cloud-config-clientserver.port=8002spring.cloud.config.name=neo-configspring.cloud.config.profile=devspring.cloud.config.label=masterspring.cloud.config.discovery.enabled=truespring.cloud.config.discovery.serviceId=spring-cloud-config-servereureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/复制代码

主要是去掉了spring.cloud.config.uri直接指向server端地址的配置,增加了最后的三个配置:

spring.cloud.config.discovery.enabled:开启Config服务发现支持spring.cloud.config.discovery.serviceId:指定server端的name,也就是server端spring.application.name的值eureka.client.serviceUrl.defaultZone:指向配置中心的地址

这三个配置文件都需要放到bootstrap.properties的配置中

3、启动类

启动类添加@EnableDiscoveryClient激活对配置中心的支持

@EnableDiscoveryClient@SpringBootApplicationpublic class ConfigClientApplication {public static void main(String[] args) {SpringApplication.run(ConfigClientApplication.class, args);}}复制代码

启动client端,在浏览器中访问:http://localhost:8000/就会看到server端和client端都已经注册了到注册中心了。

高可用

为了模拟生产集群环境,我们改动server端的端口为8003,再启动一个server端来做服务的负载,提供高可用的server端支持。

如上图就可发现会有两个server端同时提供配置中心的服务,防止某一台down掉之后影响整个系统的使用。

我们先单独测试服务端,分别访问:http://localhost:8001/neo-config/devhttp://localhost:8003/neo-config/dev返回信息:

{"name": "neo-config", "profiles": ["dev"], "label": null, "version": null, "state": null, "propertySources": [{"name": "/ityouknow/spring-cloud-starter/config-repo/neo-config-dev.properties", "source": {"neo.hello": "hello im dev"}}]}复制代码

说明两个server端都正常读取到了配置信息。

整体架构如下:

完整项目的源码来源

Spring Cloud大型企业分布式微服务云构建的B2B2C电子商务平台源码请加企鹅求求:一零三八七七四六二六

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。