In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article "how SpringCloud builds netflix- Eureka micro-service cluster", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can gain something after reading this article. Let's take a look at this article "SpringCloud's method of building netflix- Eureka micro-service cluster".
New project
Open the URL: https://start.spring.io/
Select the components to be introduced, and then download them.
Change the project structure
For the convenience of testing, you need to change the project structure to a multi-module project.
The steps are as follows:
(1) create a new sub-module register-center/provider/consumer in turn, and delete the redundant src, target and other folders in the parent module
(2) modify the pom file of the parent module: only the configuration section is retained, and all the configuration sections are commented out, because dependencies can be added in the child module as needed.
(3) modify the dependency configuration in pom of register-center
Org.springframework.cloud spring-cloud-starter-netflix-eureka-server
(4) modify the dependency configuration in pom of provider and consumer
Org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-netflix-eureka-client create the corresponding test class and configuration file 1 register-center module
Startup class
Package com.hdwang.springcloudtest.registercenter;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer @ SpringBootApplication@EnableEurekaServerpublic class Application {/ * when running peer-to-peer mode (cluster mode), start different registry node instances * for example:-Dspring.profiles.active=peer2 * * @ param args * / public static void main (String [] args) {SpringApplication.run (Application.class, args);}}
Yml configuration
Spring: application: # Application name name: register-center freemarker: template-loader-path: classpath:/templates/ prefer-file-system-access: false # active configuration, which can be modified by adding parameters at run time-Dspring.profiles.active=peer2 profiles: active: peer1# # Eureka standalone mode configuration There is only one registry node # server:# port: 8090#eureka:# instance:# hostname: localhost# client:# # which serves only as a registry and neither provides nor subscribes to services # registerWithEureka: false# fetchRegistry: false# # registry address # serviceUrl:# defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/# Eureka peer-to-peer mode Ensure the high availability of the registry Registered instance information is synchronized from point to point eureka: client: serviceUrl: defaultZone: http://peer1.com:8091/eureka/,http://peer2.com:8092/eureka/, Http://peer3.com:8093/eureka/---# different configuration for each registry node spring: profiles: peer1server: port: 8091eureka: instance: # you can configure the hostname: peer1.com---spring: profiles: peer2server: port: 8092eureka: instance: hostname: peer2.com---spring: profiles: peer3server: port: 8093eureka: instance: hostname: peer3.com2 provider module in the native hosts
Startup class
Package com.hdwang.springcloudtest.provider;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class Application {/ * add VM parameter:-Dserver.port=8082 can start multiple provider instances * * @ param args * / public static void main (String [] args) {SpringApplication.run (Application.class, args);}}
Service registration class
Package com.hdwang.springcloudtest.provider.restservice;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * registered service * / @ RestControllerpublic class RestService {/ * log * / private static final Logger LOG = LoggerFactory.getLogger (RestService.class) @ RequestMapping ("/ sayHello") public String sayHello (String name) {LOG.info ("sayHello was called"); return "hello," + name;}}
Yml configuration
Spring: application: # Application name and eureka service name name: provider freemarker: template-loader-path: classpath:/templates/ prefer-file-system-access: falseserver: # Runtime Add parameter-Dserver.port=8082 runs the new provider instance port: 8081eureka: client: # registry address serviceUrl: # registry standalone mode # defaultZone: http://localhost:8090/eureka/ # registry peer-to-peer mode defaultZone: http://peer1.com:8091/eureka/,http://peer2.com:8092/eureka/,http://peer3.com:8093/eureka/3 consumer configuration
Startup class
Package com.hdwang.springcloudtest.consumer;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class Application {public static void main (String [] args) {SpringApplication.run (Application.class, args);}}
Service invocation test class
Package com.hdwang.springcloudtest.consumer.controller;import com.netflix.appinfo.InstanceInfo;import com.netflix.discovery.EurekaClient;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.http.HttpEntity;import org.springframework.http.HttpHeaders;import org.springframework.http.MediaType;import org.springframework.http.ResponseEntity;import org.springframework.util.LinkedMultiValueMap;import org.springframework.util.MultiValueMap;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController Import org.springframework.web.client.RestTemplate;import java.util.*;/** * Test * / @ RestControllerpublic class TestController {/ * use the service name for load balancing, not the address directly used * / private static final String REST_URL_PREFIX = "http://provider"; @ Autowired private EurekaClient discoveryClient; @ Autowired private RestTemplate restTemplate" GetMapping ("/ testGet") public String testGet () {ResponseEntity res = restTemplate.getForEntity (REST_URL_PREFIX + "/ sayHello?name= {1}", String.class, getName ()); return res.getBody ();} @ GetMapping ("/ testPost") public String testPost () {MultiValueMap params = new LinkedMultiValueMap (); params.add ("name", getName ()); / / HttpHeaders headers = new HttpHeaders () / / headers.setContentType (MediaType.APPLICATION_FORM_URLENCODED); HttpEntity
< MultiValueMap>Request = new HttpEntity (params, null); ResponseEntity res = restTemplate.postForEntity (REST_URL_PREFIX + "/ sayHello", request, String.class); return res.getBody ();} private String getName () {List greetings = Arrays.asList ("Bob", "Alice", "Jack"); Random rand = new Random (); int randomNum = rand.nextInt (greetings.size ()); return greetings.get (randomNum) }}
RestTemplate is responsible for balanced configuration classes.
Package com.hdwang.springcloudtest.consumer.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 RestTemplateConfig {/ * build RestTemplate instance object with load balancing function * * @ return RestTemplate instance object * / @ Bean @ LoadBalanced RestTemplate restTemplate () {return new RestTemplate ();}}
Yml configuration
Spring: application: # App name Also Eureka service name name: cosumerserver: port: 8088eureka: client: # registry address serviceUrl: # registry standalone mode # defaultZone: http://localhost:8090/eureka/ # registry peer-to-peer mode defaultZone: http://peer1.com:8091/eureka/,http://peer2.com:8092/eureka/,http://peer3.com:8093/eureka/ run test 1 native hosts configuration
127.0.0.1 peer1.com
127.0.0.1 peer2.com
127.0.0.1 peer3.com
2 Editing the running configuration
Three registry nodes running configuration
Running configuration of two service providers
3 run the program
(1) activate the registration centre
Start RegisterCenter1- > RegisterCenter2- > RegisterCenter3 in turn. After starting successfully, you can visit http://localhost:8091/ or http://localhost:8092/ or http://localhost:8093/ to check whether the startup is successful.
(2) start the service provider provider
Start Provider1- > Provider2 in turn, and casually visit the home page of a registry address to view the status, as shown below
(3) start consumer cosumer
(4) Test in the browser
(5) you can see the output result in the console of Provider1/Provider2
2021-04-07 15 c.h.s.provider.restservice.RestService 26 sayHello was called 56.043 INFO 8796-[nio-8081-exec-1]
2021-04-07 15 c.h.s.provider.restservice.RestService 26 INFO 58.860 8796-[nio-8081-exec-2] c.h.s.provider.restservice.RestService: sayHello was called
2021-04-07 15 c.h.s.provider.restservice.RestService 26 sayHello was called 59.535 INFO 8796-[nio-8081-exec-3]
2021-04-07 15 c.h.s.provider.restservice.RestService 26 sayHello was called 59.925 INFO 8796-[nio-8081-exec-4]
2021-04-07 15 c.h.s.provider.restservice.RestService 27 c.h.s.provider.restservice.RestService 00.266 INFO 8796-[nio-8081-exec-5] c.h.s.provider.restservice.RestService: sayHello was called
2021-04-07 15 c.h.s.provider.restservice.RestService 2714 00.663 INFO 8796-[nio-8081-exec-6] c.h.s.provider.restservice.RestService: sayHello was called
2021-04-07 15 c.h.s.provider.restservice.RestService 2714 00.938 INFO 8796-[nio-8081-exec-7] c.h.s.provider.restservice.RestService: sayHello was called
2021-04-07 15 c.h.s.provider.restservice.RestService 26 Zhou 58.602 INFO 17828-[nio-8082-exec-1] c.h.s.provider.restservice.RestService: sayHello was called
2021-04-07 15 c.h.s.provider.restservice.RestService 26 sayHello was called 59.194 INFO 17828-[nio-8082-exec-2]
2021-04-07 15 c.h.s.provider.restservice.RestService 26 sayHello was called 59.737 INFO 17828-[nio-8082-exec-3]
2021-04-07 15 c.h.s.provider.restservice.RestService 27 00.109 INFO 17828-[nio-8082-exec-4] c.h.s.provider.restservice.RestService: sayHello was called
2021-04-07 15 c.h.s.provider.restservice.RestService 2714 00.414 INFO 17828-[nio-8082-exec-5] c.h.s.provider.restservice.RestService: sayHello was called
2021-04-07 15 c.h.s.provider.restservice.RestService 2714 00.815 INFO 17828-[nio-8082-exec-6] c.h.s.provider.restservice.RestService: sayHello was called
The above is the content of this article on "SpringCloud's method of building netflix- eureka micro-service cluster". I believe you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more related knowledge, please 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.
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.