Early spring cloud practice

  Recent better time, decided to learn about spring cloud. 

  We must first understand what is spring cloud, spring cloud is a set of tools to build a distributed system based on spring boot, it can help you quickly build a distributed system. Relatively simple and approachable, learning to have some knowledge of the spring boot before spring cloud and master.

  Next we set up to build a simple spring cloud environment,

1. We can first build a spring boot of the project, and the introduction of dependency.

<?xml version="1.0" encoding="UTF-8"?>
    <name>springCloudDemo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</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>
 

2. After the introduction of dependence began to write the configuration file:

 Server:
  Port: 8761 
  
eureka:
  instance:
    hostname: localhost # Your Address
  Client:
    the Register-with-eureka: false # Indicates whether to register itself to eureka server,
    FETCH-Registry: false # FETCH-Registry indicates whether obtained from the eureka server registration information
    service-url:
      defaultzone: HTTP: // $ {eureka.instance.hostname}: $ {server.port} / eureka /
        # defaultzone is more important, is to set the address eureka server is located, tracking and registration services We need to rely on this address.

3. On the Startup annotation classes with the @EnableEurekaServer

Then you can start the eureka served,

 

4. The following spring boot to build a project, import dependence. As long as the spring-cloud-starter-netflix-eureka-server revised to spring-cloud-starter-netflix-eureka-client can, dependent on the other have not changed.

5. Client Profile

6. On the Startup type client, plus @EnableEurekaClient notes, and then you can start the client. After starting the refresh complete eureka server,

 

As shown, indicating the client to the server successfully registered.

 

     接下来要使用restTemplate 时间服务器之前的调用。因为每个客户端都在服务器端注册了,所以我们在访问其他客户端时,可以使用restTemplate 来进行调用,也十分的方便。

   首先建一个restTemplate的工具类。

注意:当你调用地址想用你注册时候的name时,必须加@LoadBalanced注解。这样然后就可以使用restTemplate了。

如:

 

当访问 /getString时 就会跳转到 CLIENTDEMO的 /getName 

 

 

 这样就可以随意跳转到 注册到服务器端的任何一个客户端了。

 

 

发布了20 篇原创文章 · 获赞 7 · 访问量 1万+

Guess you like

Origin blog.csdn.net/qrnhhhh/article/details/97141261