Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Spring clound implements eureka Service Discovery component

Shulou Source: shulou.com Published: 2022-06-02 09:28:24 11月17日 Update

This article mainly explains the "spring clound implementation of eureka service discovery components", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "spring clound implementation of eureka service discovery components" bar!

Registry eureka server

Configuration

Server: port: 8087eureka: instance: hostname: eureka-server # hostname of eureka instance client: register-with-eureka: false # do not register yourself on eureka fetch-registry: false # do not obtain service registration information from eureka service-url: defaultZone: http://localhost:8007/eureka/package com.eureka;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication Import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;// enables eureka function @ EnableEurekaServer@SpringBootApplicationpublic class EurekaServerApplication {public static void main (String [] args) {SpringApplication.run (EurekaServerApplication.class, args);}}

Service provider eureka discovery

Server: port: 8003spring: application: name: provider-ticketeureka: instance: prefer-ip-address: true # use the ip address of the service client: service-url: defaultZone: http://localhost:8761/eureka/ when registering the service

Service consumer eureka discovery

Configuration information

Spring: application: name: consumer-userserver: port: 8200eureka: instance: prefer-ip-address: true # use the ip address of the service client: service-url: defaultZone: http://localhost:8761/eureka/package com.eureka;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.loadbalancer.LoadBalanced;import org.springframework.cloud.netflix.eureka.EnableEurekaClient when registering the service Import org.springframework.context.annotation.Bean;import org.springframework.web.client.RestTemplate;@EnableEurekaClient / / enable service listening @ SpringBootApplicationpublic class ConsummerApplication {public static void main (String [] args) {SpringApplication.run (ConsummerApplication.class, args) } / * register the Template of a request Rest request * only the class that joins the LoadBalanced can use the registered domain name to access * @ LoadBalanced join the request to take effect http://PROVIDER-TICKET/hello * otherwise it will not take effect * reason: * as to why the annotation must be modified, I'll talk about it here. With the addition of the loadBalance comment, this comment has three things to deal with. * * the first thing is to select a corresponding service instance from the load balancer, and some people will ask why it is selected from the load balancer. The obvious reason is that all service name instances are placed in the serverlist in the load balancer. * * the second thing is to request content from the instance selected for the first thing. * * the third thing is to change the service name to the actual ip address * @ return * / @ LoadBalanced @ Bean public RestTemplate restTemplate () {return new RestTemplate ();}}

Fulfill the request

@ RestControllerpublic class UserController {@ Autowired RestTemplate template; @ Autowired private LoadBalancerClient client; @ GetMapping ("/ gethello") public String hello () {ServiceInstance instance = client.choose ("provider-ticket"); System.out.println (instance.getHost ()); System.out.println (instance.getPort ()); template.getForObject ("http://PROVIDER-TICKET/hello",String.class); return" e213123 ") }} Thank you for reading, the above is the content of "spring clound implements eureka service discovery components". After the study of this article, I believe you have a deeper understanding of the problem of spring clound implementing eureka service discovery components, and the specific usage still needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Services components things instances equalization content address comments equalizer learning information reason that is time configuration obvious host function only name Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB NVidia OPPO Reno Shulou Information Xiaomi