JAVA语言之spring cloud demo解释
小标 2018-07-24 来源 : 阅读 786 评论 0

摘要:本文主要向大家介绍了JAVA语言之spring cloud demo解释,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。

本文主要向大家介绍了JAVA语言之spring cloud demo解释,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。

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

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

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

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

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

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

 

 1 <parent>

 2     <groupId>org.springframework.boot</groupId>

 3     <artifactId>spring-boot-starter-parent</artifactId>

 4     <version>1.5.2.RELEASE</version>

 5     <relativePath/>

 6     <!-- lookup parent from repository -->

 7 </parent>

 8 <!-- 使用spring cloud必须引入 -->

 9 <dependencyManagement>

10     <dependencies>

11         <dependency>

12             <groupId>org.springframework.cloud</groupId>

13             <artifactId>spring-cloud-dependencies</artifactId>

14             <version>Dalston.SR2</version>

15             <type>pom</type>

16             <scope>import</scope>

17         </dependency>

18     </dependencies>

19 </dependencyManagement>

20 <dependencies>

21     <dependency>

22         <groupId>org.springframework.boot</groupId>

23         <artifactId>spring-boot-starter</artifactId>

24     </dependency>

25     <dependency>

26         <groupId>org.springframework.boot</groupId>

27         <artifactId>spring-boot-starter-web</artifactId>

28     </dependency>

29     <!-- logback + slf4j -->

30     <dependency>

31         <groupId>ch.qos.logback</groupId>

32         <artifactId>logback-classic</artifactId>

33     </dependency>

34     <dependency>

35         <groupId>org.slf4j</groupId>

36         <artifactId>jcl-over-slf4j</artifactId>

37     </dependency>

38     <!-- 测试模块,包括JUnit、Hamcrest、Mockito -->

39     <dependency>

40         <groupId>org.springframework.boot</groupId>

41         <artifactId>spring-boot-starter-test</artifactId>

42         <scope>test</scope>

43     </dependency>

44     <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> <dependency>

45         <groupId>com.alibaba</groupId>

46         <artifactId>fastjson</artifactId>

47         <version>1.2.37</version>

48     </dependency>

49     <dependency>

50         <groupId>org.springframework.boot</groupId>

51         <artifactId> spring-boot-configuration-processor </artifactId>

52     </dependency>

53 </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=//localhost:8761/eureka/

 

cloud-config-test.properties:

simple.config.name=git-test

simple.config.age=1

#注册服务的zone

registercenter.eureka.defaultzone=//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=//localhost:8761/eureka/

 

cloud-config-test.properties

simple.config.name=native_test

simple.config.age=1

# 注册服务的zone

registercenter.eureka.defaultzone=//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的处理,简单,其实就是重写客户端类,将里面的方法进行重写,有退路

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

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

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注编程语言JAVA频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved