Fork me on GitHub

利用SpringCloud搭建一个最简单的微服务框架

1.微服务

微服务主要包含服务注册,服务发现,服务路由,服务配置,服务熔断,服务降级等一系列的服务,而Spring Cloud为我们提供了个一整套的服务;

 

本例子为你提供了最简单的一个服务发现例子,包含服务注册发现spingCloudEurekaServer、服务配置中心spingCloudConfServer、以及一个app应用springCloudApp

2.服务注册与发现

spingCloudEurekaServer

pom.xml

 

  1.  
    <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">
  2.  
    <modelVersion>4.0.0</modelVersion>
  3.  
    <groupId>com.caicongyang</groupId>
  4.  
    <artifactId>spingCloudEurekaServer</artifactId>
  5.  
    <version>0.0.1-SNAPSHOT</version>
  6.  
    <parent>
  7.  
    <groupId>org.springframework.cloud</groupId>
  8.  
    <artifactId>spring-cloud-starter-parent</artifactId>
  9.  
    <version>Angel.SR6</version>
  10.  
    </parent>
  11.  
    <dependencies>
  12.  
    <dependency>
  13.  
    <groupId>org.springframework.cloud</groupId>
  14.  
    <artifactId>spring-cloud-starter-eureka-server</artifactId>
  15.  
    </dependency>
  16.  
    </dependencies>
  17.  
    <build>
  18.  
    <plugins>
  19.  
    <plugin>
  20.  
    <groupId>org.springframework.boot</groupId>
  21.  
    <artifactId>spring-boot-maven-plugin</artifactId>
  22.  
    </plugin>
  23.  
    </plugins>
  24.  
    </build>
  25.  
    </project>

Application.java

 

 

  1.  
    package com.caicongyang.eureka;
  2.  
     
  3.  
    import org.springframework.boot.SpringApplication;
  4.  
    import org.springframework.boot.autoconfigure.SpringBootApplication;
  5.  
    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
  6.  
     
  7.  
    /**
  8.  
    * Spring could EurekaServer程序主入口
  9.  
    *
  10.  
    * @author Administrator
  11.  
    *
  12.  
    */
  13.  
    @SpringBootApplication
  14.  
    @EnableEurekaServer
  15.  
    public class Application {
  16.  
    public static void main(String[] args) {
  17.  
    SpringApplication.run(Application.class, args);
  18.  
    }
  19.  
    }

 

application.yml  (可用properties替代)

 

  1.  
    server:
  2.  
    port: 9999
  3.  
    eureka:
  4.  
    instance:
  5.  
    hostname: 127.0.0.1
  6.  
    client:
  7.  
    registerWithEureka: false
  8.  
    fetchRegistry: false
  9.  
    serviceUrl:
  10.  
    defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/



 

3.服务配置(全局配置中心)

pom.xml

 

  1.  
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2.  
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3.  
    <modelVersion>4.0.0</modelVersion>
  4.  
    <groupId>com.caicongyang</groupId>
  5.  
    <artifactId>spingCloudConfServer</artifactId>
  6.  
    <version>0.0.1-SNAPSHOT</version>
  7.  
     
  8.  
    <parent>
  9.  
    <groupId>org.springframework.cloud</groupId>
  10.  
    <artifactId>spring-cloud-starter-parent</artifactId>
  11.  
    <version>Angel.SR6</version>
  12.  
    </parent>
  13.  
    <dependencies>
  14.  
    <dependency>
  15.  
    <groupId>org.springframework.cloud</groupId>
  16.  
    <artifactId>spring-cloud-config-server</artifactId>
  17.  
    </dependency>
  18.  
    <!-- sping cloud 注册服务 -->
  19.  
    <dependency>
  20.  
    <groupId>org.springframework.cloud</groupId>
  21.  
    <artifactId>spring-cloud-starter-eureka</artifactId>
  22.  
    </dependency>
  23.  
    <dependency>
  24.  
    <groupId>org.springframework.boot</groupId>
  25.  
    <artifactId>spring-boot-starter-test</artifactId>
  26.  
    <scope>test</scope>
  27.  
    </dependency>
  28.  
    </dependencies>
  29.  
     
  30.  
    <build>
  31.  
    <plugins>
  32.  
    <plugin>
  33.  
    <groupId>org.springframework.boot</groupId>
  34.  
    <artifactId>spring-boot-maven-plugin</artifactId>
  35.  
    </plugin>
  36.  
    </plugins>
  37.  
    <defaultGoal>compile</defaultGoal>
  38.  
    </build>
  39.  
    </project>

application.java

 

 

  1.  
    package com.caiconyang.conf;
  2.  
     
  3.  
    import org.springframework.boot.SpringApplication;
  4.  
    import org.springframework.boot.autoconfigure.SpringBootApplication;
  5.  
    import org.springframework.cloud.config.server.EnableConfigServer;
  6.  
     
  7.  
    /**
  8.  
    * Spring could conf程序主入口
  9.  
    * @author Administrator
  10.  
    *
  11.  
    */
  12.  
    @SpringBootApplication
  13.  
    @EnableConfigServer
  14.  
    public class Application {
  15.  
    public static void main(String[] args) {
  16.  
    SpringApplication.run(Application.class,args);
  17.  
    }
  18.  
    }


application.properties

 

 

  1.  
    server.port=8888
  2.  
    ## App配置文件所在git地址
  3.  
    spring.cloud.config.server.git.uri=https://git.oschina.net/caicongyang/springCloudConfigRepo.git
  4.  
    spring.cloud.config.server.git.searchPaths=repo
  5.  
    spring.application.name=spingCloudConfServer

 

4.App

pom.xml

 

  1.  
    <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">
  2.  
    <modelVersion>4.0.0</modelVersion>
  3.  
    <groupId>com.caicongyang</groupId>
  4.  
    <artifactId>springCloudApp</artifactId>
  5.  
    <version>0.0.1-SNAPSHOT</version>
  6.  
     
  7.  
    <parent>
  8.  
    <groupId>org.springframework.cloud</groupId>
  9.  
    <artifactId>spring-cloud-starter-parent</artifactId>
  10.  
    <version>Angel.SR6</version>
  11.  
    </parent>
  12.  
    <properties>
  13.  
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  14.  
    <java.version>1.7</java.version>
  15.  
    <java.encoding>UTF-8</java.encoding>
  16.  
    <springfox.swagger.version>2.2.2</springfox.swagger.version>
  17.  
    </properties>
  18.  
    <dependencies>
  19.  
    <dependency>
  20.  
    <groupId>org.springframework.boot</groupId>
  21.  
    <artifactId>spring-boot-starter-web</artifactId>
  22.  
    </dependency>
  23.  
    <!-- sping cloud 监控 http://localhost:8080/health -->
  24.  
    <dependency>
  25.  
    <groupId>org.springframework.boot</groupId>
  26.  
    <artifactId>spring-boot-starter-actuator</artifactId>
  27.  
    </dependency>
  28.  
    <dependency>
  29.  
    <groupId>org.springframework.cloud</groupId>
  30.  
    <artifactId>spring-cloud-starter-config</artifactId>
  31.  
    </dependency>
  32.  
    <!-- sping cloud 注册服务 -->
  33.  
    <dependency>
  34.  
    <groupId>org.springframework.cloud</groupId>
  35.  
    <artifactId>spring-cloud-starter-eureka</artifactId>
  36.  
    </dependency>
  37.  
    <!-- sping cloud 路由 -->
  38.  
    <dependency>
  39.  
    <groupId>org.springframework.cloud</groupId>
  40.  
    <artifactId>spring-cloud-starter-hystrix</artifactId>
  41.  
    </dependency>
  42.  
     
  43.  
    <dependency>
  44.  
    <groupId>org.springframework.boot</groupId>
  45.  
    <artifactId>spring-boot-starter-test</artifactId>
  46.  
    <scope>test</scope>
  47.  
    </dependency>
  48.  
    <dependency>
  49.  
    <groupId>io.springfox</groupId>
  50.  
    <artifactId>springfox-swagger2</artifactId>
  51.  
    <version>${springfox.swagger.version}</version>
  52.  
    </dependency>
  53.  
    <dependency>
  54.  
    <groupId>io.springfox</groupId>
  55.  
    <artifactId>springfox-swagger-ui</artifactId>
  56.  
    <version>${springfox.swagger.version}</version>
  57.  
    </dependency>
  58.  
    </dependencies>
  59.  
     
  60.  
     
  61.  
    <build>
  62.  
    <finalName>spingcould</finalName>
  63.  
    <plugins>
  64.  
    <plugin>
  65.  
    <groupId>org.apache.maven.plugins</groupId>
  66.  
    <artifactId>maven-compiler-plugin</artifactId>
  67.  
    <configuration>
  68.  
    <source>${java.version}</source>
  69.  
    <target>${java.version}</target>
  70.  
    <encoding>${java.encoding}</encoding>
  71.  
    <showWarnings>true</showWarnings>
  72.  
    </configuration>
  73.  
    </plugin>
  74.  
    </plugins>
  75.  
    </build>
  76.  
     
  77.  
     
  78.  
    </project>


Application.java

 

 

  1.  
    package com.caicongyang.springCloudApp.main;
  2.  
     
  3.  
    import org.springframework.boot.SpringApplication;
  4.  
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  5.  
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  6.  
    import org.springframework.context.annotation.ComponentScan;
  7.  
    import org.springframework.context.annotation.Configuration;
  8.  
     
  9.  
    /**
  10.  
    * Spring could web程序主入口
  11.  
    * @author Administrator
  12.  
    *
  13.  
    */
  14.  
    @Configuration//配置控制
  15.  
    @EnableAutoConfiguration//启用自动配置
  16.  
    @ComponentScan(value={"com.caicongyang.springCloudApp"})//组件扫描
  17.  
    @EnableDiscoveryClient
  18.  
    public class Application {
  19.  
    public static void main(String[] args) {
  20.  
    //第一个简单的应用,
  21.  
    SpringApplication.run(Application.class,args);
  22.  
    }
  23.  
    }

SwaggerConfig.java

 

 

  1.  
    package com.caicongyang.springCloudApp.conf;
  2.  
     
  3.  
    import org.springframework.beans.factory.annotation.Value;
  4.  
    import org.springframework.context.annotation.Bean;
  5.  
    import org.springframework.context.annotation.Configuration;
  6.  
     
  7.  
    import springfox.documentation.service.ApiInfo;
  8.  
    import springfox.documentation.spi.DocumentationType;
  9.  
    import springfox.documentation.spring.web.plugins.Docket;
  10.  
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
  11.  
     
  12.  
    /**
  13.  
    *
  14.  
    * @author caicongyang1
  15.  
    * @version id: SwaggerConfig, v 0.1 16/4/22 下午4:12 caicongyang1 Exp $$
  16.  
    */
  17.  
    @Configuration
  18.  
    @EnableSwagger2
  19.  
    public class SwaggerConfig {
  20.  
     
  21.  
    @Value("${swagger.ui.enable}") //该配置项在配置中心管理
  22.  
    private boolean environmentSpecificBooleanFlag;
  23.  
     
  24.  
    @Bean
  25.  
    public Docket docketFactory() {
  26.  
    return new Docket(DocumentationType.SWAGGER_2).apiInfo(
  27.  
    new ApiInfo("接口文档", "SpingCloud web接口列表", "1.0", "", "", "", "")).enable(environmentSpecificBooleanFlag);
  28.  
    }
  29.  
    }

application.properties

 

 

  1.  
    server.port=8080
  2.  
    spring.cloud.config.uri=http://127.0.0.1:8888
  3.  
    spring.cloud.config.name=springCloudApp
  4.  
    spring.cloud.config.profile=${config.profile:dev}
  5.  
    #service discovery url
  6.  
    eureka.client.serviceUrl.defaultZone=http://localhost:9999/eureka/
  7.  
    #service name
  8.  
    spring.application.name=springCloudApp


5.测试与验证

顺序启动服务注册发现spingCloudEurekaServer、服务配置中心spingCloudConfServer、以及一个app应用springCloudApp

测试与验证

1.访问http://localhost:9999/eureka/  app是否已经注册上来

2.访问 http://localhost:8080/swagger-ui.html 是否正常访问,如果正常访问说明争取读取到config配置中心的swagger.ui.enable配置项

 

6.源码:

 

以上所有源码:

 

https://git.oschina.net/caicongyang/springcloud.git

 

 

posted @ 2018-07-19 10:14  梳碧湖砍柴的人  阅读(230)  评论(0编辑  收藏  举报