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

How to use springCloud

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

Share

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

This article mainly introduces "the use of springCloud". In the daily operation, I believe that many people have doubts about the use of springCloud. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about the use of springCloud. Next, please follow the editor to study!

SpringCloud: how microservices are implemented.

SpringCloud reduces the requirements for large-scale system construction in terms of technical architecture, enabling us to build an efficient, distributed and fault-tolerant platform at a very low cost (technology or hardware). However, SpringCloud is not without shortcomings, and small and independent projects are not suitable for use.

SpringCloud is an ordered collection of frameworks. It makes use of the development convenience of SpringBoot to skillfully simplify the development of distributed system infrastructure, such as service discovery registration, configuration center, message bus, load balancing, fuses, data monitoring, etc., all of which can be started and deployed with one button using SpringBoot development style. Spring does not repeat manufacturing wheels, it just combines the service frameworks developed by various companies that are more mature and can stand the actual test, and re-encapsulates the complex configuration and implementation principles through SpringBoot style, and finally leaves a set of distributed system development kits for developers that are easy to understand, easy to deploy and easy to maintain.

Microservices are service order members who test independent deployment, horizontal expansion and independent access.

Components:

Eureka: registry

Zuul: service Gateway

Ribbon: load balancing

Feign: service invocation

Hystrix: fuse

Starter code

Premise: the parent project is springboot, and the version of the jar package is managed.

Eureka: registry that implements the registration and discovery functions of services

1. Import coordinates

Org.springframework.cloud spring-cloud-starter-netflix-eureka-server org.glassfish.jaxb jaxb-runtime 2.3.2

2. Create a startup class

@ SpringBootApplication@EnableEurekaServerpublic class EurekaServer {public static void main (String [] args) {SpringApplication.run (EurekaServer.class);}}

3. Add a configuration file

Server: port: 8084spring: application: name: eureka-servereureka: client: service-url: defaultZone: http://127.0.0.1:8084/eureka

Ribbon: load balancing

Hystrix: fuse

Feign: service invocation

Zuul: service Gateway

Sample code:

1. Import coordinates

Org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.cloud spring-cloud-starter-netflix-ribbon org.springframework.cloud spring-cloud-starter-netflix-hystrix org.springframework.cloud spring-cloud-starter-openfeign

2. Write startup classes

@ EnableCircuitBreaker@SpringBootApplication@EnableDiscoveryClient@EnableFeignClients/*@SpringCloudApplication*/public class ConsumerApplication {@ Bean @ LoadBalanced public RestTemplate restTemplate () {return new RestTemplate ();} public static void main (String [] args) {SpringApplication.run (ConsumerApplication.class);}}

3. Implementation code

@ RestController@RequestMapping ("consumer") public class ConsumerController {@ Autowired private RestTemplate restTemplate; @ Autowired private LoadBalancerClient client; @ GetMapping ("{id}") @ HystrixCommand (fallbackMethod = "queryByIdFallback") public Account queryById (@ PathVariable ("id") Integer id) {/ / List instances = discoveryClient.getInstances ("user_service"); / / ServiceInstance instanceInfo = instances.get (0); ServiceInstance instance = client.choose ("user_service") String url = "http://"+instance.getHost()+":"+instance.getPort()+"/account/"+id; System.out.println (url); Account account = restTemplate.getForObject (url,Account.class); return account;} public Account queryByIdFallback (Integer id) {return null;}} at this point, the study of" how to use springCloud "is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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