springboot+springcloud微服务入门

MicroService实现技术:

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

  ##SpringCloud的五大神兽

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

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

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

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

  #5.分布式配置——Spring Cloud Config
    对微服务的配置文件做同一管理

SpringCloud入门步奏

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

    1.2从每个微服务注册微服务地址清单

    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/页面显示如下就说明微服务配置成功
 

  

猜你喜欢

转载自www.cnblogs.com/bigbigxiao/p/12109902.html