Simple spring cloud application building

Posted by nightdesigns on Wed, 11 Sep 2019 12:54:50 +0200

I. Sprcloud Microsoft Service Architecture

2. Building Sprcloud application step by step

service client

Service: Application that provides services. Port is 808x Client: The application port for invoking the service is 809x The whole program uses the client end to call the server end program according to the user id to return user information as an example.

  • 1. Two single spring boot application calls This section is relatively simple. Two new 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 invokes it through RestTemplate. First, we only need to create two separate Spring boot applications. The port of the server is 8081, and the port of the client is 8071. Then we can call it with RestTemplate on the client. The code of the server is not pasted. It is a common spring boot web project created with spring initializer. This is mainly about the adjustment of the client. Use.

Configure RestTemplate

@Configuration
public 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, by calling access http://localhost:8080/client/user/1, return server-side data, of course, restTemplate has many other methods available, you can explore when you actually use it.

  • 2. The communication between the two micro-services is completed. The client calls the interface of the server and the function is finished. But this is not the desired posture. On the client side, we use restTemplate to write the address of the server. In the actual production environment, we can not only have one server, our server can not only have one machine. We will face the following problems:
  • Horizontal Extension Service
  • Server and client machines go online

In order to solve the problem that we don't want to write the address of the server and can dynamically switch the call to the server machine, we usually think of zookeeper by introducing the concept of registry. But in spring cloud, we don't use zookeeper, instead, we use Eureka, zookeeper and Eureka for security. There are different ways to prove CAP theorem. Zookeeper pays more attention to CP (consistency, partition tolerance), while Eureka focuses on AP (consistency and partition tolerance). With the registry, how can client requests be distributed to a server? Maybe we have used nginx, nginx to distribute servers by configuring the server's weight, IP hash or random and rotational training strategies. For spring cloud, it's Ribbon. The next step is to support these needs by revamping the first step of the project. First, we need to build an Eureka Server, which is the registry. Unlike zookeeper, which is a software that needs to be installed, Eureka is an application built in 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 Eureka Server and our application.

As a registry, Eureka Server receives requests from registered clients of the server for service lists. Eureka Client is embedded in the application program to register services or obtain services.
Building Eureka Server

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>

   <groupId>com.wtf.cloud</groupId>
   <artifactId>eureka-server</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>

   <name>eureka-server</name>
   <description>Demo project for Spring Boot</description>

   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.0.0.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>

   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <java.version>1.8</java.version>
      <spring-cloud.version>Finchley.M8</spring-cloud.version>
   </properties>

   <dependencies>
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-eureka-server</artifactId>
         <version>1.4.3.RELEASE</version>
      </dependency>


   </dependencies>

   <dependencyManagement>
      <dependencies>
         <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-parent</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
         </dependency>
      </dependencies>
   </dependencyManagement>

   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>

   <repositories>
      <repository>
         <id>spring-milestones</id>
         <name>Spring Milestones</name>
         <url>https://repo.spring.io/milestone</url>
         <snapshots>
            <enabled>false</enabled>
         </snapshots>
      </repository>
   </repositories>


</project>

main file

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

Configuration file application.yml

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

The application port is 8888
Application name: eureka-server Then there's something about configuring eureka By default, the service registry will also try to register itself by organizing its own client, 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's responsibility is to maintain service instances, it does not need to retrieve services, so it is also set to false. More configurations can refer to Eureka Instance ConfigBean and Eureka Client ConfigBean
Start service access http://localhost:8888/ You can see the following interface:

Topics: Programming Spring Zookeeper Maven Apache