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

Configure highly available registry cluster and ribbon- load balancing in Spring Cloud

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article shows you the configuration of high-availability registry cluster and ribbon- load balancing in Spring Cloud, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Create a parent project

Step 1: parent project could_parent

Step 2: modify the pom.xml file, configure the spring boot version, spring cloud version, lock the cloud dependency, and determine the cloud private repository

Org.springframework.boot spring-boot-starter-parent 2.1.4.RELEASE UTF-8 1.8 Greenwich.RELEASE org.springframework.cloud spring-cloud-dependencies ${spring-cloud-release.version} pom Import spring-milestones Spring Milestones https://repo.spring.io/milestone false

Create a registry

Step 1: create a project eureka_demo

Step 2: modify pom.xml and configure web and eureka server dependencies

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

Step 3: create a yml file, configure the port number, service name, and registered address

# Port number server: port: 1008 service name spring: application: # Service name cannot be underlined, such as: eureka_demo will become a space in subsequent calls, resulting in not finding the service name: eurekaDemo3# registered address eureka: client: service-url: defaultZone: http://localhost:10086/eureka register-with-eureka: false # whether to register yourself To the registry fetch-registry: false # whether to pull the list of services from the registry

Step 4: create a startup class and add the annotation @ EnableEurekaServer to enable eureka server

Package com.czxy;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication@EnableEurekaServer / / Open eureka servicepublic class EurekaDemoApplication {public static void main (String [] args) {SpringApplication.run (EurekaDemoApplication.class,args);}}

Configure a highly available registry: cluster

Step 1: modify the registry (eureka_demo) core yml file, application.yml, and configure only the service name (shared content)

# Service name spring: application: name: eurekaDemo

Step 2: create application-10086.yml, configure port 10086 and registration path (10087)

# Port number server: port: 1008 registered address eureka: client: service-url: defaultZone: http://localhost:10087/eureka register-with-eureka: true # whether to register yourself with the registry. Default value: true (omitted) fetch-registry: true # whether to pull the list of services from the registry, default true (may be omitted)

Step 3: create application-10087.yml, configure port 10087 and registration path (10086)

# Port number server: port: 1008 registered address eureka: client: service-url: defaultZone: http://localhost:10086/eureka register-with-eureka: true # whether to register yourself with the registry fetch-registry: true # whether to pull the list of services from the registry

Step 4: modify the core yml file, activate the 10086 configuration, and start the program. At this time, the console throws an exception, 10087 has not been started, and the exception disappears automatically after 10087 starts.

# Service name spring: application: name: eurekaDemo profiles: active: 10086

Step 5: modify the core yml file, activate the 10087 configuration, and start the program (idea does not support multi-startup and needs to be set manually. Different versions have different settings)

# Service name spring: application: name: eurekaDemo profiles: active: 10087

All registries yml

Configure multiple booting in IDEA

Step 1: create a spring boot startup item

Step 2: configure 10087 to start the same as configuration 10086 (omitted here)

Step 3: start

Create a service provider eureka_service4 (cluster)

Pom file:

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

Services with the same name can be clustered automatically as long as multiple instances are provided and registered in eureka.

Step 1: create the application-8081.yml file and configure the port number 8081

Server: port: 8081

Step 2: create the application-8082.yml file and configure the port number 8082

Server: port: 8081

Step 3: configure startup items for two yml files (same as 10086 and 10087 above)

Step 4: start and test

Add controller to the provider eureka_service3 to facilitate testing data

Package com.czxy.controller;import org.springframework.http.ResponseEntity;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletRequest @ RestController@RequestMapping ("/ test") public class TestController {@ GetMapping public ResponseEntity test (HttpServletRequest request) {/ / port number return ResponseEntity.ok ("test data" + request.getServerPort ());}}

Create a service caller

Step 1: create a project eureka_client3

Step 2: core step 3, pom file, yml file, startup class

Pom file:

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

Yml file:

Server: port: 9090spring: application: name: eurekaClienteureka: client: service-url: defaultZone: http://localhost:10086/eureka

Startup class:

Package com.czxy;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication@EnableEurekaClientpublic class ClientApplication {public static void main (String [] args) {SpringApplication.run (ClientApplication.class,args);}}

Step 3: write the configuration class and configure the RestTemplate instance to make RestTemplate support the "service name" access mechanism. You need to add the annotation @ LoadBalanced

Package com.czxy.config;import org.springframework.cloud.client.loadbalancer.LoadBalanced;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.client.RestTemplate;@Configurationpublic class HttpConfig {@ Bean @ LoadBalanced / / enables RestTemplate to support load balancing, that is, it supports "service name" to access public RestTemplate restTemplate () {return new RestTemplate ();}}

Step 4: modify dao. When you use RestTemplate to make a remote call, you can use "service name" to make the call.

Package com.czxy.dao;import org.springframework.http.ResponseEntity;import org.springframework.stereotype.Component;import org.springframework.web.client.RestTemplate;import javax.annotation.Resource;@Componentpublic class DataDao {@ Resource private RestTemplate restTemplate; public ResponseEntity data () {/ / String url = "http://localhost:8081/test"; String url =" http://service3/test"; return restTemplate.getForEntity (url,String.class);}}

Test results:

The above is the configuration of highly available registry clusters and ribbon- load balancers in Spring Cloud. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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