zoukankan      html  css  js  c++  java
  • 企业分布式微服务云SpringCloud SpringBoot mybatis (一)服务的注册与发现(Eureka)

    一、spring cloud简介

    spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理、服务发现、断路器、路由、微代理、事件总线、全局锁、决策竞选、分布式会话等等。它运行环境简单,可以在开发人员的电脑上跑。另外说明spring cloud是基于springboot的,所以需要开发中对springboot有一定的了解,如果不了解的话可以看这篇文章:2小时学会springboot。另外对于“微服务架构” 不了解的话,可以通过搜索引擎搜索“微服务架构”了解下。

    二、创建一个服务提供者 (eureka client)

    当client向server注册时,它会提供一些元数据,例如主机和端口,URL,主页等。Eureka server 从每个client实例接收心跳消息。 如果心跳超时,则通常将该实例从注册server中删除。

    创建过程同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.forezp</groupId>
        <artifactId>service-hi</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>service-hi</name>
        <description>Demo project for Spring Boot</description>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.2.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>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </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>Dalston.RC1</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>
    

      通过注解@EnableEurekaClient 表明自己是一个eurekaclient.

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

      仅仅@EnableEurekaClient是不够的,还需要在配置文件中注明自己的服务注册中心的地址,application.yml配置文件如下:

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

      

    需要指明spring.application.name,这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个name 。
    启动工程,打开http://localhost:8761 ,即eureka server 的网址:

    Paste_Image.png

    你会发现一个服务已经注册在服务中了,服务名为SERVICE-HI ,端口为7862

    这时打开 http://localhost:8762/hi?name=forezp ,你会在浏览器上看到 :

    hi forezp,i am from port:8762
    

      架构代码如下:

    "分布式b2b <wbr

    资料和源码来源地址

  • 相关阅读:
    视觉SLAM十四讲课后习题—ch13
    视觉SLAM中涉及的各种坐标系转换总结
    《视觉SLAM十四讲》笔记(ch13)
    《视觉SLAM十四讲》笔记(ch12)
    《视觉SLAM十四讲》课后习题—ch7(更新中……)
    安装opencv_contrib(ubuntu16.0)
    《视觉SLAM十四讲》笔记(ch8)
    如何将“您没有打开此文件的权限”的文件更改为可读写的文件
    《视觉SLAM十四讲》笔记(ch7)
    ubuntu16.04下跑通LSD-SLAM的过程记录
  • 原文地址:https://www.cnblogs.com/xiamudaren/p/8421013.html
Copyright © 2011-2022 走看看