Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

SpringCloud distributed microservice cloud architecture part 7: highly available distributed configuration Center (Config)

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

The previous article described how a service reads files from a configuration center, and how a configuration center reads configuration files from a remote git, when there are many service instances

All the files are read from the configuration center. At this time, you can consider turning the configuration center into a micro-service and clustering it to achieve high availability. The architecture figure is as follows:

I. preparatory work

Continue to use the project in the previous article, understand the springcloud architecture can add request: 353 624 7259 to create an eureka-server project

Used as a service registry.

The start of introducing Eureka into its pom.xml file depends on spring-cloud-starter-netflix- eureka-server, as follows:

4.0.0 com.forezp config-server 0.0.1-SNAPSHOT jar config-server Demo project for Spring Boot com.forezp sc-f-chapter7 0.0.1-SNAPSHOT org.springframework.cloud Spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-config-server Org.springframework.boot spring-boot-maven-plugin

On the configuration file application.yml, specify the service port as 8889, plus the basic configuration as the service registry, the code is as follows:

Server: port: 8889eureka: instance: hostname: localhost client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

Entry class:

@ EnableEurekaServer@SpringBootApplicationpublic class EurekaServerApplication {public static void main (String [] args) {SpringApplication.run (EurekaServerApplication.class, args);}}

Second, transform config-server

The starting point of adding EurekaClient to its pom.xml file depends on spring-cloud-starter-netflix-eureka-client, as follows:

Org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-config-server

Configuration file application.yml, which specifies that the service registration address is http://localhost:8889/eureka/. Other configurations are the same as in the previous article. The complete configuration is as follows:

Spring.application.name=config-serverserver.port=8888spring.cloud.config.server.git.uri= https://github.com/forezp/SpringcloudConfig/spring.cloud.config.server.git.searchPaths=respospring.cloud.config.label=masterspring.cloud.config.server.git.username= your usernamespring.cloud.config.server.git.password= your passwordeureka.client.serviceUrl.defaultZone= http://localhost:8889/eureka/

Finally, you need to add the @ EnableEureka annotation to the startup class Application of the program.

Third, transform config-client

To register it to the service registry, as an Eureka client, you need a pom file plus a start-up dependency spring-cloud-starter-netflix-eureka-client, as follows:

Org.springframework.cloud spring-cloud-starter-config org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-netflix-eureka-client

The configuration file bootstrap.properties, note that it is bootstrap. Plus the registered address of the service is http://localhost:8889/eureka/

Spring.application.name=config-clientspring.cloud.config.label=masterspring.cloud.config.profile=dev#spring.cloud.config.uri= http://localhost:8888/eureka.client.serviceUrl.defaultZone=http://localhost:8889/eureka/spring.cloud.config.discovery.enabled=truespring.cloud.config.discovery.serviceId=config-serverserver.port=8881

Spring.cloud.config.discovery.enabled reads files from the configuration center.

The servieId of spring.cloud.config.discovery.serviceId configuration center, that is, the service name.

At this time, it is found that when reading the configuration file, the ip address is no longer written, but the service name. At this time, if the configuration service is deployed in multiple copies, it will be highly available through load balancing.

Start eureka-servr,config-server,config-client in turn

Visit the website: http://localhost:8889/

Visit http://localhost:8881/hi and the browser displays:

Foo version 3

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report