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 build spring cloud application

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the relevant knowledge of "how to build spring cloud applications". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Spring cloud micro-service architecture

2. Build spring cloud application step by step

Service client

Service: the application that provides the service, the port is 808x Client: the application port that invokes the service is 809x, the whole program is based on the client side to call the server program according to the user id to return the user information for example.

1. It is relatively simple to call two single spring boot applications in this section. Two common spring boot applications are created separately without using anything related to spring cloud. The service side provides a restful interface service, and the client side makes calls through RestTemplate. First of all, you only need to create two separate Spring boot applications, the server port is 8081, the client port is 8071, and then you can call it with RestTemplate on the client side. The server code is not posted. Is a common spring boot web project created with spring initializr, mainly talking about client-side calls.

Configure RestTemplate

@ Configurationpublic class ConfigurationBean {private SimpleClientHttpRequestFactory getHttpRequestFactory () {SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory (); factory.setConnectTimeout (10000); factory.setReadTimeout (10000); return factory

Calling mode

@ RestController@RequestMapping ("/ client/user") public class UserApi {@ Autowired private RestTemplate restTemplate; @ RequestMapping ("/ {id}") public String getUser (@ PathVariable ("id") String id) {String forObject = restTemplate.getForObject ("http://localhost:8081/user/1", String.class); return forObject;}}"

Start service and client respectively, call to access http://localhost:8080/client/user/1 and return server data. Of course, there are many other methods available in restTemplate, which you can explore when you actually use it.

2. The communication between the two microservices is completed. The client calls the interface of the server, and the function is complete, but this is not the posture we want. On the client, we write the address of the server using restTemplate. In the actual production environment, our server can not have only one machine, nor can our server have only one machine. We will face the following problems:

Scale-out servic

Server and client machines go online and offline

In order to solve the problem that we do not want to write the address of the server, and can dynamically switch calls to the server machine, the concept of registry is introduced. Speaking of registry, we usually think of zookeeper, but zookeeper is not used in spring cloud, but Eureka,zookeeper and Eureka are used to ensure that the way in the CAP theorem is different. Zookeeper pays more attention to CP (consistency, partition tolerance). For Eureka, the focus is on ensuring AP (consistency and partition tolerance). With the registry, how do client requests be distributed to the corresponding server? maybe we have used nginx,nginx to allocate servers by configuring server weights, ip hash or random and rotational training strategies. For spring cloud, it is Ribbon who does this job. Next, we will transform the first step of the project to make it support these requirements. First of all, we need to build an EurekaServer, that is, a registry. Unlike zookeeper, which is a software that needs to be installed, Eureka is an application built into spring boot start, we just need to introduce spring-boot-start-eureka and start the project. Before that, let's talk about the relationship between EurekaServer and our application.

As a registry, EurekaServer receives requests from the first-level registration client of the server for the list of services on the server. Eureka Client is embedded in the application to register or obtain services.

Set up EurekaServer

Pom.xml

4.0.0 com.wtf.cloud eureka-server 0.0.1-SNAPSHOT jar eureka-server Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 2.0.0.RELEASE UTF-8 UTF-8 1.8 Finchley.M8 org.springframework.cloud spring-cloud-starter-eureka-server 1.4.3.RELEASE org.springframework.cloud spring-cloud-starter-parent ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin Spring-milestones Spring Milestones https://repo.spring.io/milestone false

Main file

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

Profile application.yml

Server: port: 8888spring: application: name: eureka-servereureka: client: register-with-eureka: false fetch-registry: false serviceUrl: defaultZone: http://localhost:8888/eureka/

The applied port is 8888

Application name: eureka-server and then things related to configuring eureka. By default, the service registry will also organize its own client to try to register itself, so we need to prohibit its client behavior.

Eureka.client.register-with-eureka=false

Since the application belongs to the registry, it is set to false, which means it does not register itself like the registry.

Eureka.client.fetch-registry=false

Since the registry is responsible for maintaining service instances, it does not need to retrieve services, so it is also set to false. For more configuration, please refer to EurekaInstanceConfigBean and EurekaClientConfigBean

Start the service to access http://localhost:8888/ and you can see the following interface:

This is the end of "how to build spring cloud applications". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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