我也是一个初学者,如果有建议请留言哦!

下一个博客解释最近我学习Spring cloud 的感触以及理解。

这个博客主要是通过一个简单的demo来加深对Spring cloud 的理解。

1.首先要建一个父工程,用来收集子项目中要使用到的共有的jar

对于一个Spring cloud架构的项目来说,肯定是需要将Spring cloud和Spring boot引入进来。

而对于项目来说一般要引入测试用的jar和日志jar

<parent> 
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.5.2.RELEASE</version> 
     <relativePath/>
     <!-- lookup parent from repository --> 
 </parent> 
 <!-- 使用spring cloud必须引入 --> 
 <dependencyManagement> 
     <dependencies>
         <dependency>
             <groupId>org.springframework.cloud</groupId> 
             <artifactId>spring-cloud-dependencies</artifactId>
             <version>Dalston.SR2</version> 
             <type>pom</type> 
             <scope>import</scope>
         </dependency>
     </dependencies> 
 </dependencyManagement> 
 <dependencies>
     <dependency> 
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter</artifactId> 
     </dependency>
     <dependency>
         <groupId>org.springframework.boot</groupId> 
         <artifactId>spring-boot-starter-web</artifactId> 
     </dependency>
     <!-- logback + slf4j -->
     <dependency>
         <groupId>ch.qos.logback</groupId>
         <artifactId>logback-classic</artifactId> 
     </dependency>
     <dependency>
         <groupId>org.slf4j</groupId>
         <artifactId>jcl-over-slf4j</artifactId> 
     </dependency>
     <!-- 测试模块,包括JUnit、Hamcrest、Mockito --> 
     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope> 
     </dependency>
     <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> <dependency>
         <groupId>com.alibaba</groupId>
         <artifactId>fastjson</artifactId> 
         <version>1.2.37</version> 
     </dependency> 
     <dependency>
         <groupId>org.springframework.boot</groupId> 
         <artifactId> spring-boot-configuration-processor </artifactId>
     </dependency>
 </dependencies>

2.接下来需要一个工程用来保存项目的公共配置文件(从git/SVN获取到的),并通过rest方式供其他服务获取配置信息。

两种方式:

(1)git

application.properties

server.port=8888

application-gitsimple.properties

# 指定配置文件所在的git工程路径

spring.cloud.config.server.git.uri=https://github.com/hryou0922/spring_cloud.git# 表示将搜索该文件夹下的配置文件

spring.cloud.config.server.git.searchPaths=cloud-config-git/simple

serverapplication:

@SpringBootApplication

@EnableConfigServer // 激活该应用为配置文件服务器:读取远程配置文件,转换为rest接口服务public class

public static void main(String[] args) {

args = new String[1];

args[0] = "--spring.profiles.active=gitsimple";

SpringApplication.run(CloudGitConfigServerApplication.class, args);

Git的配置文件放置在这个工程中:cloud-config-git

在simple有两个文件,名称和内容如下:

cloud-config-dev.properties:

simple.config.name=git-dev

simple.config.age=112

#注册服务的zone

registercenter.eureka.defaultzone=http://localhost:8761/eureka/

cloud-config-test.properties:

simple.config.name=git-test

simple.config.age=1

#注册服务的zone

registercenter.eureka.defaultzone=http://localhost:8761/eureka/

  • simple.config.name和simple.config.age做为测试数据,用于后续服务和客户读取配置。
  • registercenter.eureka.defaultzone:服务注册到服务注册的zone

(2)native

工程名称: cloud-config-center

在resoucres的目录下config/simple的创建配置文件

cloud-config-dev.properties

simple.config.name=native_dev

simple.config.age=113

# 注册服务的zone

registercenter.eureka.defaultzone=http://localhost:8761/eureka/

cloud-config-test.properties

simple.config.name=native_test

simple.config.age=1

# 注册服务的zone

registercenter.eureka.defaultzone=http://localhost:8761/eureka/

application-nativesimple.properties

server.port=8888# native:启动从本地读取配置文件,必须指定active的值,才可以使用本场配置模式

spring.profiles.active=native

# 自定义配置文件路径

spring.cloud.config.server.native.searchLocations=classpath:/config/simple/

CloudNativeConfigServerApplication

@SpringBootApplication@EnableConfigServer // 激活该应用为配置文件服务器:读取远程配置文件,转换为rest接口服务public class CloudNativeConfigServerApplication {

public static void main(String[] args) {

args = new String[1];

// 使用native不可以使用spring.profiles.active的方式使用native模式

// args[0] = "--spring.profiles.active=nativesimple";

args[0] = "--spring.config.name=application-nativesimple";

SpringApplication.run(CloudNativeConfigServerApplication.class, args);

}

}

(3)接下的就简单了,开始创建注册中心的工程eureka,这个我就不再赘述了,网上一大堆。

(4)server和client

道理简单就是将server将服务注册到eureka,而client从eureka中进行调用。

但是需要注意一下两个地芳:

1.feign客户端传值

  • simple:无参请求
  • simpleWithOneParam:带单个参数请求
  • simpleWithQry:带多个参数请求
  • @Configuration
  • @ConfigurationProperties(prefix = "simple.config" ,ignoreUnknownFields = false)
  • public class SimpleConfig {

    private String name;

    private Integer age;

    public String getName() {

    return name;

    }

    public void setName(String name) {

    this.name = name;

    }

    public Integer getAge() {

    return age;

    }

    public void setAge(Integer age) {

    this.age = age;

    }

    @Override

    public String toString(){

    return "name="+name+" | age=" + age;

    }

    }

  • controller中

@Autowired

private Service Service;

/**

* 无参服务

* @return

*/

@RequestMapping(value = "/simple")

public SimpleDto simple() {

return Service.simple();

}

service中:

@Servicepublic class SimpleService {

@Autowired

private SimpleConfig simpleConfig; //

public SimpleDto simple(){

SimpleDto simpleDto = new SimpleDto();

simpleDto.setAge(simpleConfig.getAge());

simpleDto.setName(simpleConfig.getName());

simpleDto.setRandomNum(new Random().nextInt(1000));

return simpleDto;

}

2.熔断hytrix的处理,简单,其实就是重写客户端类,将里面的方法进行重写,有退路

整个流程差不多就是这样,这个我也是看网上资料学习到的,很有用。

先启动配置中心,再启动注册中心,然后启动服务端,最后启动客户端。

嗯,我对这部分内容又加深了,你呢,如果你觉得对你有帮助的话给我赞吧。