欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-04-01 22:19:40
spring cloud是一个基于spring boot实现的云应用开发工具,它为基于jvm的云应用开发中的配置管理、服务发现、断路器、智能路由、微代理、控制总线、全局锁、...

spring cloud是一个基于spring boot实现的云应用开发工具,它为基于jvm的云应用开发中的配置管理、服务发现、断路器、智能路由、微代理、控制总线、全局锁、决策竞选、分布式会话和集群状态管理等操作提供了一种简单的开发方式。

spring cloud包含了多个子项目(针对分布式系统中涉及的多个不同开源产品),比如:spring cloud config、spring cloud netflix、spring cloud cloudfoundry、spring cloud aws、spring cloud security、spring cloud commons、spring cloud zookeeper、spring cloud cli等项目。

1.微服务

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

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

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

2.服务注册与发现

spingcloudeurekaserver

pom.xml

<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.caicongyang</groupid> 
 <artifactid>spingcloudeurekaserver</artifactid> 
 <version>0.0.1-snapshot</version> 
 <parent> 
  <groupid>org.springframework.cloud</groupid> 
  <artifactid>spring-cloud-starter-parent</artifactid> 
  <version>angel.sr6</version> 
 </parent> 
 <dependencies>   
  <dependency> 
   <groupid>org.springframework.cloud</groupid> 
   <artifactid>spring-cloud-starter-eureka-server</artifactid> 
  </dependency> 
 </dependencies> 
 <build> 
  <plugins> 
   <plugin> 
    <groupid>org.springframework.boot</groupid> 
    <artifactid>spring-boot-maven-plugin</artifactid> 
   </plugin> 
  </plugins> 
 </build> 
</project> 

application.java

package com.caicongyang.eureka; 
 
import org.springframework.boot.springapplication; 
import org.springframework.boot.autoconfigure.springbootapplication; 
import org.springframework.cloud.netflix.eureka.server.enableeurekaserver; 
 
/** 
 * spring could eurekaserver程序主入口 
 * 
 * @author administrator 
 * 
 */ 
@springbootapplication 
@enableeurekaserver 
public class application { 
 public static void main(string[] args) { 
  springapplication.run(application.class, args); 
 } 
} 

application.yml  (可用properties替代)

server: 
  port: 9999 
eureka: 
  instance: 
   hostname: 127.0.0.1 
  client: 
   registerwitheureka: false 
   fetchregistry: false 
   serviceurl: 
      defaultzone: http://${eureka.instance.hostname}:${server.port}/eureka/ 

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

pom.xml

<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.caicongyang</groupid> 
 <artifactid>spingcloudconfserver</artifactid> 
 <version>0.0.1-snapshot</version> 
 
 <parent> 
  <groupid>org.springframework.cloud</groupid> 
  <artifactid>spring-cloud-starter-parent</artifactid> 
  <version>angel.sr6</version> 
 </parent> 
 <dependencies> 
  <dependency> 
   <groupid>org.springframework.cloud</groupid> 
   <artifactid>spring-cloud-config-server</artifactid> 
  </dependency> 
  <!-- sping cloud 注册服务 --> 
  <dependency> 
   <groupid>org.springframework.cloud</groupid> 
   <artifactid>spring-cloud-starter-eureka</artifactid> 
  </dependency> 
  <dependency> 
   <groupid>org.springframework.boot</groupid> 
   <artifactid>spring-boot-starter-test</artifactid> 
   <scope>test</scope> 
  </dependency> 
 </dependencies> 
 
 <build> 
  <plugins> 
   <plugin> 
    <groupid>org.springframework.boot</groupid> 
    <artifactid>spring-boot-maven-plugin</artifactid> 
   </plugin> 
  </plugins> 
  <defaultgoal>compile</defaultgoal> 
 </build> 
</project> 

application.java

package com.caiconyang.conf; 
 
import org.springframework.boot.springapplication; 
import org.springframework.boot.autoconfigure.springbootapplication; 
import org.springframework.cloud.config.server.enableconfigserver; 
 
/** 
 * spring could conf程序主入口 
 * @author administrator 
 * 
 */ 
@springbootapplication 
@enableconfigserver 
public class application { 
 public static void main(string[] args) {  
  springapplication.run(application.class,args);  
 }  
} 

application.properties

server.port=8888 
## app配置文件所在git地址 
spring.cloud.config.server.git.uri=https://git.oschina.net/caicongyang/springcloudconfigrepo.git 
spring.cloud.config.server.git.searchpaths=repo 
spring.application.name=spingcloudconfserver 

4.app 

pom.xml

<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.caicongyang</groupid> 
 <artifactid>springcloudapp</artifactid> 
 <version>0.0.1-snapshot</version> 
 
 <parent> 
  <groupid>org.springframework.cloud</groupid> 
  <artifactid>spring-cloud-starter-parent</artifactid> 
  <version>angel.sr6</version> 
 </parent> 
 <properties> 
  <project.build.sourceencoding>utf-8</project.build.sourceencoding> 
  <java.version>1.7</java.version> 
  <java.encoding>utf-8</java.encoding> 
  <springfox.swagger.version>2.2.2</springfox.swagger.version> 
 </properties> 
 <dependencies> 
  <dependency> 
   <groupid>org.springframework.boot</groupid> 
   <artifactid>spring-boot-starter-web</artifactid> 
  </dependency> 
  <!-- sping cloud 监控 http://localhost:8080/health --> 
  <dependency> 
   <groupid>org.springframework.boot</groupid> 
   <artifactid>spring-boot-starter-actuator</artifactid> 
  </dependency> 
  <dependency> 
   <groupid>org.springframework.cloud</groupid> 
   <artifactid>spring-cloud-starter-config</artifactid> 
  </dependency> 
  <!-- sping cloud 注册服务 --> 
  <dependency> 
   <groupid>org.springframework.cloud</groupid> 
   <artifactid>spring-cloud-starter-eureka</artifactid> 
  </dependency> 
   <!-- sping cloud 路由 --> 
  <dependency> 
   <groupid>org.springframework.cloud</groupid> 
   <artifactid>spring-cloud-starter-hystrix</artifactid> 
  </dependency> 
 
  <dependency> 
   <groupid>org.springframework.boot</groupid> 
   <artifactid>spring-boot-starter-test</artifactid> 
   <scope>test</scope> 
  </dependency> 
  <dependency> 
   <groupid>io.springfox</groupid> 
   <artifactid>springfox-swagger2</artifactid> 
   <version>${springfox.swagger.version}</version> 
  </dependency> 
  <dependency> 
   <groupid>io.springfox</groupid> 
   <artifactid>springfox-swagger-ui</artifactid> 
   <version>${springfox.swagger.version}</version> 
  </dependency> 
 </dependencies> 
  
 
 <build> 
  <finalname>spingcould</finalname> 
  <plugins> 
   <plugin> 
    <groupid>org.apache.maven.plugins</groupid> 
    <artifactid>maven-compiler-plugin</artifactid> 
    <configuration> 
     <source>${java.version}</source> 
     <target>${java.version}</target> 
     <encoding>${java.encoding}</encoding> 
     <showwarnings>true</showwarnings> 
    </configuration> 
   </plugin> 
  </plugins> 
 </build> 
 
 
</project> 

application.java

package com.caicongyang.springcloudapp.main; 
 
import org.springframework.boot.springapplication; 
import org.springframework.boot.autoconfigure.enableautoconfiguration; 
import org.springframework.cloud.client.discovery.enablediscoveryclient; 
import org.springframework.context.annotation.componentscan; 
import org.springframework.context.annotation.configuration; 
 
/** 
 * spring could web程序主入口 
 * @author administrator 
 * 
 */ 
@configuration//配置控制 
@enableautoconfiguration//启用自动配置 
@componentscan(value={"com.caicongyang.springcloudapp"})//组件扫描 
@enablediscoveryclient 
public class application { 
 public static void main(string[] args) {  
  //第一个简单的应用,  
  springapplication.run(application.class,args);  
 }  
} 

swaggerconfig.java

package com.caicongyang.springcloudapp.conf; 
 
import org.springframework.beans.factory.annotation.value; 
import org.springframework.context.annotation.bean; 
import org.springframework.context.annotation.configuration; 
 
import springfox.documentation.service.apiinfo; 
import springfox.documentation.spi.documentationtype; 
import springfox.documentation.spring.web.plugins.docket; 
import springfox.documentation.swagger2.annotations.enableswagger2; 
 
/** 
 * 
 * @author caicongyang1 
 * @version id: swaggerconfig, v 0.1 16/4/22 下午4:12 caicongyang1 exp $$ 
 */ 
@configuration 
@enableswagger2 
public class swaggerconfig { 
 
 @value("${swagger.ui.enable}") //该配置项在配置中心管理 
 private boolean environmentspecificbooleanflag; 
  
 @bean 
 public docket docketfactory() { 
  return new docket(documentationtype.swagger_2).apiinfo( 
   new apiinfo("接口文档", "spingcloud web接口列表", "1.0", "", "", "", "")).enable(environmentspecificbooleanflag); 
 } 
} 

application.properties

server.port=8080 
spring.cloud.config.uri=http://127.0.0.1:8888 
spring.cloud.config.name=springcloudapp 
spring.cloud.config.profile=${config.profile:dev} 
#service discovery url 
eureka.client.serviceurl.defaultzone=http://localhost:9999/eureka/ 
#service name 
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.源码:以上所有源码:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。