springboot+springcloud微服务入门

MicroService实现技术:java

  用springBoot来建立单个服务,用SpringCloud来管理这些微服务。web

  ##SpringCloud的五大神兽spring

  #1.注册/服务发现——Netflix Eureka
    管理服务器地址和ip的springboot

  #2.客服端负载均衡——Netflix Ribbon\Feign
    服务请求的分配服务器

  #3.断路器——Netflix Hystrix
    对有故障的服务进行处理app

  #4.服务网关——Netflix Zuul
    微服务的统一入口。

  #5.分布式配置——Spring Cloud Config
    对微服务的配置文件作同一管理负载均衡

SpringCloud入门步奏maven

  1.注册中心
    用于管理微服务的地址
    1.1当微服务能够解决注册的注册表(IP、端口、服务名称)分布式

    1.2从每一个微服务注册微服务地址清单spring-boot

    1.3使用心跳机制微服务合同:每N秒发送一个请求到注册表,告诉注册表,

      我还活着,若是微服务挂,有心跳合同失败,

      注册表微服务地址列表,其余服务将更新地址列表

  2.用户管理微服务
  3.订单管理微服务

 

1.注册中心配置

  1.1先建立个普通的maven项目父级模块,

    在pom.xml里配置公用的jar包管理

    

<properties>
        <!--编码-->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <!-- 1.管理SpringBoot的jar包 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
    </parent>

    <!-- 2.管理spring-cloud的jar包 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <!-- 测试 -->
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>

 1.2.建立注册中心 --项目名:eureka-server-1000

 在父级模块中建立个spring-boot项目,也是maven项目,

  可是要选中quickstart

  

 

 

 

  1.3在eureka-server-1000模块中的pom.xml添加依赖

  

<parent>
        <artifactId>spring-cloud-parent</artifactId>
        <groupId>spring-cloud-parent</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>eureka-server-1000</artifactId>

    <name>eureka-server-1000</name>



    <dependencies>
        <!-- 注册/服务发现的依赖 有了它,才能获取到全部微服务的ip,端口,服务名 有了它,其余的微服务才能获取到全部微服务的ip,端口,服务名清单 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <!-- springboot-web的环境依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

 

  1.4再建个resources配置文件夹,

  在配置文件夹中建个application.yml文件

  在yml文件中添加

  

server: port: 1000 #端口号 eureka: instance: hostname: localhost #主机 client: registerWithEureka: false #禁止注册中心向本身注册 fetchRegistry: false #禁止注册中心拉取注册地址清单 serviceUrl: #微服务向注册中心注册的地址 defaultZone: http://localhost:1000/eureka/

 

  1.5在java中建个配置类添加

/** * @EnableEurekaServer * 启动注册中心,默认关闭 */ @SpringBootApplication @EnableEurekaServer public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class,args); } }

  启动main方法能够看见页面

  在网页上输入http://localhost:1000/显示以下页面说明配置成功

 

 

 

2..用户管理微服务

  模块名:order-server-2000

  和建立注册模块同样但配置不同

 

  2.1在pom.xml中添加依赖

  

<parent>
        <artifactId>spring-cloud-parent</artifactId>
        <groupId>spring-cloud-parent</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>order-server-2000</artifactId>

    <name>order-server-2000</name>



    <dependencies>
        <!-- 注册的微服务控制器 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!-- springboot的web环境配置 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>

  2.2在resources配置文件夹中新建个application.yml

  在yml中添加配置

  

eureka: client: serviceUrl: #指定注册中心的地址 defaultZone: http://localhost:1000/eureka/ instance: #是否显示ip地址 prefer-ip-address: true server: #端口号 port: 2000 spring: application: #服务名 name: orders-server cloud: discovery: client: simple: local: service-id: orders-server:2000 #显示的id

 2.3在java文件夹中建个

OrdersServerApplication.java
添加以下代码
@SpringBootApplication @RestController public class OrdersServerApplication { @RequestMapping("/") public String home() { return "Hello world"; } public static void main(String[] args) { SpringApplication.run(OrdersServerApplication.class); } }

启动main方法能够看见页面

  在网页上输入http://localhost:2000/页面显示

  Hello world

  说明配置成功

  再重启动注册模块的main方法
  在网页上输入http://localhost:1000/页面显示以下就说明微服务配置成功
  

 

  3.订单管理微服务 

项目名:user-server-3000

和建立注册模块同样但配置不同

 

3.1在pom.xml中添加依赖

<parent>
        <artifactId>spring-cloud-parent</artifactId>
        <groupId>spring-cloud-parent</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>user-server-3000</artifactId>

    <name>user-server-3000</name>



    <dependencies>
        <!-- 注册的微服务控制器 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <!-- springboot的web环境配置 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>

3.2在resources配置文件夹中新建个application.yml

  在yml中添加配置

eureka: client: serviceUrl: defaultZone: http://localhost:1000/eureka/ instance: prefer-ip-address: true server: port: 3000 spring: application: name: user-server cloud: discovery: client: simple: local: service-id: user-server:3000

 3.3在java文件夹中建个

OrdersServerApplication.java
添加以下代码
 
/** * @EnableEurekaClient * 启动微服务; * 默认开启 */ @SpringBootApplication @RestController @EnableEurekaClient public class UserServerApplication { @RequestMapping("/") public String home() { return "Hello world2"; } public static void main(String[] args) { SpringApplication.run(UserServerApplication.class); } }
 

启动main方法能够看见页面

  在网页上输入http://localhost:3000/页面显示

  Hello world2

  说明配置成功

  再重启动注册模块的main方法
  在网页上输入http://localhost:1000/页面显示以下就说明微服务配置成功
相关文章
相关标签/搜索