In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how to use Resttemplate and Ribbon to call Eureka to achieve load balancing". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Service Registration and Discovery Eureka
Can be used as service governance.
two。 First of all, let's build a father-son project.
The outermost layer is forezp.
Four sub-projects are established below it.
Eureka-server
Eureka-client
Eureka-client1
Eureka-ribbon-client
3.forezp engineering related
The 1.forezp pom file is as follows
4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.5.RELEASE com.example forezp 0.0.1-SNAPSHOT forezp Demo project for Spring Boot UTF-8 UTF-8 1.8 Dalston.SR1 org.springframework.cloud spring-cloud-dependencies $ {spring-cloud.version}} pom import euraka-client euraka-server eureka-ribbon-client euraka-client2 4.Eureka Service Center: eureka-server related
1.pom file
4.0.0 com.example forezp 0.0.1-SNAPSHOT com.example euraka-server 0.0.1-SNAPSHOT euraka-server Demo project for Spring Boot 1.8 Greenwich.SR1 org.springframework.cloud spring-cloud-starter-netflix-eureka-server org.springframework.boot spring -boot-starter-test test org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin
2.application.properties file
Server.port=8761 eureka.instance.hostname=localhost # prevent yourself from registering your own eureka.client.register-with-eureka=falseeureka.client.fetch-registry=false#eureka 's registered address eureka.client.service-url.default-zone= http://${eureka.instance.hostname}:${server.port}/eureka/}
3. You need to add @ EnableEurekaServer annotation to the startup class to turn on the registration service
Package com.example.eurakaserver; import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @ SpringBootApplication@EnableEurekaServerpublic class EurakaServerApplication {public static void main (String [] args) {SpringApplication.run (EurakaServerApplication.class, args);}} 5.eureka-client and eureka-client1 are used to provide services
In fact, there is little difference between these two sub-projects, only to verify the implementation of load balancing.
1.pom file
4.0.0 com.example forezp 0.0.1-SNAPSHOT com.example euraka-client2 0.0.1-SNAPSHOT euraka-client2 Demo project for Spring Boot 1.8 Greenwich.SR1 org.springframework.boot spring-boot-starter-web org .springframework.cloud spring-cloud-starter-netflix-eureka-client org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test org.springframework.cloud Spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin
two。 Create a new ApiController class
Package com.example.eurakaclient2; import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController; @ RestControllerpublic class ApiController {@ Value ("${server.port}") String port; @ GetMapping ("/ hi") public String home (@ RequestParam String name1) {return "hi" + name1+ "i am a port:" + port;}}
The apllication.properties configuration for 3.eureka-client is as follows
Eureka.client.service-url.defaultZone= http://localhost:8761/eureka/server.port=8762spring.application.name=eureka-client
The apllication.properties configuration for 4.eureka-client2 is as follows
Eureka.client.service-url.defaultZone= http://localhost:8761/eureka/server.port=8763spring.application.name=eureka-client
5. You need to add @ EnableEurekaClient to the startup class to enable the service provider.
Package com.example.eurakaclient2; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @ SpringBootApplication@EnableEurekaClientpublic class EurakaClient2Application {public static void main (String [] args) {SpringApplication.run (EurakaClient2Application.class, args);}} 6.eureka-ribbon-client project related
1.pom file
4.0.0 com.example forezp 0.0.1-SNAPSHOT com.example eureka-ribbon-client 0.0.1-SNAPSHOT eureka-ribbon-client Demo project for Spring Boot 1.8 Greenwich.SR1 org.springframework.boot spring-boot-starter-web org.springframework.cloud spring- Cloud-starter-netflix-ribbon org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test org.springframework.cloud Spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin
The 2.applicaiton.properties configuration file is as follows
Spring.application.name=eureka-ribbon-clientserver.port=8764eureka.client.service-url.defaultZone= http://localhost:8761/eureka/
3. Create a new RibbonConfig class, where the RestTemplate class is injected and the load balancer is enabled.
Package com.example.eurekaribbonclient; 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 RibbonConfig {@ Bean @ LoadBalanced RestTemplate restTemplate () {return new RestTemplate ();}}
4. Create a new service layer and call the restful API provided by eureka-client (eureka-client1), where eureka-client is the serviceId information registered on eureka-service by eureka-client (eureka-client1). If serviceId is not configured, spring.application.name is used by default.
Package com.example.eurekaribbonclient; import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.web.client.RestTemplate; @ Service public class RibbonService {@ Autowired RestTemplate restTemplate; public String hi (String name) {return restTemplate.getForObject ("http://eureka-client/hi?name1="+name,String.class);}})
5. Create a new controller layer and call the method provided for us by RibbonService
Package com.example.eurekaribbonclient; import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController; @ RestControllerpublic class RibbonController {@ Autowired RibbonService ribbonService; @ GetMapping ("/ hi") public String hi (@ RequestParam (required = false,defaultValue = "forezp") String name) {return ribbonService.hi (name) }}
6. You also need to add @ EnableEurekaClient annotations to the startup class.
Package com.example.eurekaribbonclient; import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @ SpringBootApplication@EnableEurekaClientpublic class EurekaRibbonClientApplication {public static void main (String [] args) {SpringApplication.run (EurekaRibbonClientApplication.class, args);}} 7. Verification
1. Start the project sequentially
two。 Browsers access http://localhost:8761/
You can see that EUREKA-CLIENT and EUREKA-RIBBON-CLIENT have already registered.
3. We visited http://localhost:8764/hi?name=forezp twice.
You can see that the pages are returned separately
Hi forezpi am a port:8762
Hi forezpi am a port:8763
This verifies that we have achieved load balancing.
This is the end of "how to use Resttemplate and Ribbon to call Eureka to achieve load balancing". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.