In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Spring Cloud how to integrate Eureka, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
Server side
The first step is to add dependence
Org.springframework.cloud spring-cloud-starter-eureka-server
The second step is to add comments
/ / annotate @ EnableEurekaServer@SpringBootApplicationpublic class EurekaApplication {public static void main (String [] args) {SpringApplication.run (EurekaApplication.class, args);} on the startup class
Step 3: write the configuration
For more information, please see EurekaServerConfigBean for spring: application: name: eureka#. Note that unlike the jar package of Client and Instance in client, Server is the jar package of server. The configuration of # eureka can be seen in EurekaXXXConfigBean. Eureka: datacenter: cloud # modify the System Status Data center environment of the Eureka monitoring page: test # modify the System Status Environment instance: hostname: localhost prefer-ip-address: true leaseRenewalIntervalInSeconds: 5 # heartbeat interval of the Eureka monitoring page, 5 seconds leaseExpirationDurationInSeconds: 10 # No heartbeat elimination time 10 seconds instance-id: ${spring.application.name}: ${spring.cloud.client.ip-address}: ${spring.application.instance_id:$ {server.port}} # SpringCloud 2.0 has been changed to ${spring.cloud.client.ip-address}, so modify client: healthcheck: enabled: true # by default, eureka server is also eureka client, used to register with each other to form highly available eureka services. # when a single point is configured as true, eureka server will report an error Cannot execute request on any known server registerWithEureka: whether false # registers with the eureka service. The default is true, and it is already eureka server and single point eureka. Therefore, if the network is unstable between false fetchRegistry: false # eureka, the client generally caches the registration list, so the eureka service can not be cached. I think it is more able to ensure the consistency between eureka. ServiceUrl: after # registerWithEureka is turned off, there is no need to configure defaultZone. If it is turned on, an error is reported even if it is configured as native. # that is, there is no need to configure defaultZone as localhost at any time. At this point, the configuration of John is better, and the configuration of Yongchao and Zhou Li, including friends, is a bit redundant. # but Zhou Li is right, the default configuration of this attribute is http://localhost:8761/eureka, that is, when you do not have user name and password security authentication, the client can not be configured when debugging locally. # but for server, this default has no effect. For client, it only works when debugging. # but it's strange that since port 8761 is default, why should the default port of eureka server be 8080 instead of 8761? DefaultZone: host name of http://${eureka.instance.hostname}:${server.port}/eureka/ # application # defaultZone: http://${security.user.name}:${security.user.password}@localhost:${server.port}/eureka # this configuration should be deleted. Server: # self-protection mechanism, default true. When turned on, services with a heartbeat failure of less than 85% (renewalPercentThreshold) within 15 minutes are not excluded. # after closing, the home page prompts: RENEWALS ARE LESSER THAN THE THRESHOLD. THE SELF PRESERVATION MODE IS TURNED OFF. # THIS MAY NOT PROTECT INSTANCE EXPIRY IN CASE OF NETWORK/OTHER PROBLEMS. EnableSelfPreservation: true # can be turned off by fasle when debugging locally. However, the production suggestion is turned on, which can prevent the service from being mistakenly removed due to network instability and other reasons. RenewalPercentThreshold: 0.85 # default 85% # the initial time to wait before the server receives the request. By default, wait for 5min (John Carnell) waitTimeInMsWhenSyncEmpty: 5 # John says it is best to annotate this configuration during development. Service registration requires 3 heartbeats of 10s, that is, 30s, to be displayed in eureka. But why do I show it here right away? # the time when eureka server refreshes readCacheMap. Note that client reads readCacheMap, which determines how long the cache of readWriteCacheMap will be updated to readCacheMap. The default is 30 seconds. Eclipse indicates that 0 should be an error by default. ResponseCacheUpdateIntervalMs = 30 * 1000 in the source code. Response-cache-update-interval-ms: 3000 # many experts' blogs on the Internet are miswritten as responseCacheUpdateInvervalMs, please note. This is configured for 3 seconds. # eureka server cache readWriteCacheMap expiration time, this cache will expire only after this time, and will not be updated before expiration. # re-read the registration service information from registry after expiration. Registry is a ConcurrentHashMap. # since evict is enabled, it is not necessary to change this configuration. By default, active failure is enabled in 180 s responseCacheAutoExpirationInSeconds: 180 #, and each active failure detection interval is 3 s. Source code evictionIntervalTimerInMs = 60 * 1000. Default is one minute. # it should be noted that this configuration will print INFO logs, increase the number of info logs, and change it from once every 60 seconds to once every 3 seconds. EvictionIntervalTimerInMs: 3000 # be careful not to write as EvictionIntervalTimerInMs,yml case sensitive.
If multiple instances are highly available, modify the following configurations
Whether eureka: client: registerWithEureka: true # registers to the hostname client of the eureka service serviceUrl: defaultZone: http://peer2:1112/eureka/,http://peer3:1112/eureka/ # application
The first step is to add dependence
Org.springframework.cloud spring-cloud-starter-netflix-eureka-client
The second step is to add comments
/ / @ EnableEurekaClient@EnableDiscoveryClient@SpringBootApplicationpublic class ClientApplication {public static void main (String [] args) {SpringApplication.run (ClientApplication.class, args);}}
Step 3: write the configuration
Eureka: instance:# ip-address: # specifies whether the ip address # registers to the Eureka Server as IP, if false, it is not IP but the server name # but I set up the false,eureka home page to show that 192.168.100.16:client-microservice:8010 preferIpAddress: true # registers IP to Eureka Server, and if not configured is the hostname of the machine. Default false. Should always be set to true. If it is based on the deployment of containers such as Docker, the container will generate a random hostname, which does not exist in DNS and cannot be resolved-John Carnell # instance name. In the SpringCloud system, when a service entity registers with eureka, the registration name defaults to "IP name: application name: application port name" ${spring.application.name}: ${spring.cloud.client.ip-address}: ${spring.application.instance_id:$ {random.int}} # if the service name, ip, and port are all the same Eureka shows only one service instance-id: ${spring.cloud.client.hostname}: ${spring.application.name}: ${spring.cloud.client.ip-address}: ${spring.application.instance_id:$ {random.int [1re9]}}-@ project.version@ # two important attributes of service renewal leaseRenewalIntervalInSeconds: 30 # service renewal interval. By default, every 30 seconds, the client sends a heartbeat to the server. See DiscoveryClient.initScheduledTasks leaseExpirationDurationInSeconds: 90 # Service failure time. If the client's heartbeat is not received by the server for 90 seconds by default, the client service instance is excluded. # Endpoint configuration. If the monitoring endpoint is configured with context-path,actuator, the prefix will be added. At this point, eureka also needs to add the metadata of # status-page-url-path: ${server.servlet.context-path} / actuator/info # health-check-url-path: ${server.servlet.context-path} / actuator/health # Eureka metadata-map: zc-data: Current services are goods services # will not affect the metadata understood by the client zone: ABD # Eureka It can affect the location of client # appname: AAAAA # pit filling Swagger: configuration and spring.application.name conflict client: # eureka service. If the configuration is incorrect, please see: Cannot execute request on any known server # for more information: com.netflix.discovery.endpoint.EndpointUtils service-url: defaultZone: http://localhost:8761/eureka/ # whether the host name of the application enables the eureka client. Default true enabled: true # when debugging locally, if you don't want to start eureka, you can configure false instead of commenting out @ EnableDiscoveryClient. Thanks to Yongchao, I know this attribute from his book. # registerWithEureka (John, Zhou Li) and register-with-eureka (Zhai Yongchao) are supported, and the STS of eclipse uses the latter by default. # basically all configurations can use horizontal bars or humps. With the mouse over it, eclipse can display detailed comments and default values (if any). RegisterWithEureka: true # defaults to true, so it can also be omitted. FetchRegistry: true # default true, which is not configured here. # update time of cache manifest. Default is 30 seconds. See EurekaClientConfigBean, where DefaultEurekaClientConfig can be ignored (the former spring implementation, the latter Netflix implementation) registry-fetch-interval-seconds: 30 # if you want eureka server to be implemented in client as soon as possible after removing the service, I think this time can be shortened. Zhou Li said in Camden SR4 (corresponding to eureka-client.jar1.2.6) that there was this attribute, but I couldn't find it in SR6 (corresponding to 1.2.4) and SR4. # also looked for Brixton SR7 (corresponding to 1.1.7, in fact, not only eureka-client, but the entire spring-cloud-netflix is this version), but also none. # this is because the attribute IDE really cannot be prompted, but it is written correctly. The function is to modify the health examination mode of eureka (heartbeat) and change it to actuator. For more information, please see HealthCheckHandler HealthIndicator. Zhou Li's writing is not too detailed, but can be found in this blog: https://blog.csdn.net/xiao_jun_0820/article/details/77991963 # if you configure healthcheck, you need to introduce actuator. Healthcheck: enabled: true # I recommend configuring it to true. There is a problem with the heartbeat mechanism, such as when the client's database connection is not available due to problems, the heartbeat mechanism can not be reflected, but the health of actuator can.
Finally, you can print out the relevant content of the service instance in the log through the DiscoveryClient object.
@ Slf4j@RestControllerpublic class TestController {@ Autowired private DiscoveryClient discoveryClient; @ GetMapping ("/ getDiscoveryClient") public List getDiscoveryClient () {return discoveryClient.getInstances ("server-1"); / / get the client instance service} @ GetMapping ("/ getServices") public List getServices () {return discoveryClient.getServices ();}} is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.