java怎么配置中心服务化和高可用
发布时间:2023-04-19 14:17:59 所属栏目:教程 来源:
导读:客户端和服务端的耦合性太高,如果server端要做集群,客户端只能通过原始的方式来路由,server端改变IP地址的时候,客户端也需要修改配置,不符合springcloud服务治理的理念。springcloud提供了这样的解决方案,我们
|
客户端和服务端的耦合性太高,如果server端要做集群,客户端只能通过原始的方式来路由,server端改变IP地址的时候,客户端也需要修改配置,不符合springcloud服务治理的理念。springcloud提供了这样的解决方案,我们只需要将server端当做一个服务注册到eureka中,client端去eureka中去获取配置中心server端的服务既可。 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: 8001 spring: application: name: spring-cloud-config-server cloud: config: server: git: uri: # 配置git仓库的地址 search-paths: config-repo # git仓库地址下的相对地址,可以配置多个,用,分割。 username: username # git仓库的账号 password: password # git仓库的密码 eureka: client: serviceUrl: defaultZone:/ ## 注册中心eurka地址 增加了eureka注册中心的配置 3、启动类 启动类添加@EnablediscoveryClient激活对配置中心的支持 @EnablediscoveryClient @Enableconfigserver @SpringBootApplication public class configserverApplication { public static void main(String[] args) { SpringApplication.run(configserverApplication.class, args); } } 这样server端的改造就完成了。先启动eureka注册中心,在启动server端,在浏览器中访问。 java怎么配置中心服务化和高可用 按照上篇的测试步骤对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-client server.port=8002 spring.cloud.config.name=neo-config spring.cloud.config.profile=dev spring.cloud.config.label=master spring.cloud.config.discovery.enabled=true spring.cloud.config.discovery.serviceId=spring-cloud-config-server 主要是去掉了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 @SpringBootApplication public class ConfigClientApplication { public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); } } 启动client端,在浏览器中访问: 就会看到server端和client端都已经注册了到注册中心了。 java怎么配置中心服务化和高可用 高可用 为了模拟生产集群环境,我们改动server端的端口为8003,再启动一个server端来做服务的负载,提供高可用的server端支持。 (编辑:汽车网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
