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

Spring clound implements eureka Service Discovery component

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

Share

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

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!

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