SpringCloud+Boot簡單例子筆記

 

1、spring cloud簡介

spring cloud 爲開發人員提供了快速構建分佈式系統的一些工具,包括配置管理、服務發現、斷路器、路由、微代理、事件總線、全局鎖、決策競選、分佈式會話等等。它運行環境簡單,能夠在開發人員的電腦上跑。另外說明spring cloud是基於springboot的,因此須要開發中對springboot有必定的瞭解,若是不瞭解的話能夠看這篇文章:簡單入門SpringBoot。另外對於「微服務架構」 不瞭解的話,能夠經過搜索引擎搜索「微服務架構」瞭解下。html

2、建立服務註冊中心

在這裏,咱們須要用的的組件上Spring Cloud Netflix的Eureka ,eureka是一個服務註冊和發現模塊。java

2.1 首先建立一個maven主工程。spring

2.2 而後建立2個model工程:一個model工程做爲服務註冊中心,即Eureka Server,另外一個做爲Eureka Client。apache

下面以建立server爲例子,詳細說明建立過程:後端

右鍵工程->建立model-> 選擇spring initialir 以下圖:瀏覽器

-->Next緩存

下一步->選擇cloud discovery->eureka server ,而後一直下一步就好了。springboot

 

建立完後的工程的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.ww</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</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.M9</spring-cloud.version>
    </properties>

    <dependencies>
        <!--eureka server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <!-- spring boot test-->
        <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.3 啓動一個服務註冊中心,只須要一個註解@EnableEurekaServer,這個註解須要在springboot工程的啓動application類上加:app

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

2.4 **eureka是一個高可用的組件,它沒有後端緩存,每個實例註冊以後須要向註冊中心發送心跳(所以能夠在內存中完成)

  ,在默認狀況下erureka server也是一個eureka client ,必需要指定一個 server。eureka server的配置文件appication.yml:

server: port: 8761 eureka: instance: hostname: localhost client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

經過eureka.client.registerWithEureka:false和fetchRegistry:false來代表本身是一個eureka server.

2.5 eureka server 是有界面的,啓動工程,打開瀏覽器訪問: 
http://localhost:8761 ,界面以下:

No application available 沒有服務被發現 ……^_^ 由於沒有註冊服務固然不可能有服務被發現了。

3、建立一個服務提供者 (eureka client)

當client向server註冊時,它會提供一些元數據,例如主機和端口,URL,主頁等。Eureka server 從每一個client實例接收心跳消息。 若是心跳超時,則一般將該實例從註冊server中刪除。

建立過程同server相似

經過註解@EnableEurekaClient 代表本身是一個eurekaclient.

@SpringBootApplication @EnableEurekaClient @RestController public class ClientApplication { public static void main(String[] args) { SpringApplication.run(ClientApplication.class, args); } @Value("${server.port}") String port; @RequestMapping("hi") public String home(@RequestParam String name) { return "name: "+ name + " port:" + port;
 } }

僅僅@EnableEurekaClient是不夠的,還須要在配置文件中註明本身的服務註冊中心的地址,application.yml配置文件以下:

server: port: 8762 spring: application: name: service-hi eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/

須要指明spring.application.name,這個很重要,這在之後的服務與服務之間相互調用通常都是根據這個name 。 
啓動工程,打開http://localhost:8761 ,即eureka server 的網址:

你會發現一個服務已經註冊在服務中了,服務名爲SERVICE-HI ,端口爲7862

這時打開 http://localhost:8762/hi?name=zww ,你會在瀏覽器上看到 :

name: zww port:8762

 

轉載地址: https://blog.csdn.net/forezp/article/details/69696915

相關文章
相關標籤/搜索