出识springcloud我们这里需要建立两个项目 来感受下微服务

一、配置服务

1. Spring Initializr. 用idea自带的 Spring Initializr. 建立第一个项目

2.加入依赖

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.3.1.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.example</groupId>
  12. <artifactId>demo</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>demo</name>
  15. <description>Demo project for Spring Boot</description>
  16.  
  17. <properties>
  18. <java.version>1.8</java.version>
  19. <spring-cloud.version>Hoxton.SR5</spring-cloud.version>
  20. </properties>
  21.  
  22. <dependencies>
  23. <dependency>
  24. <groupId>org.springframework.cloud</groupId>
  25. <artifactId>spring-cloud-config-server</artifactId>
  26. </dependency>
  27.  
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-test</artifactId>
  31. <scope>test</scope>
  32. <exclusions>
  33. <exclusion>
  34. <groupId>org.junit.vintage</groupId>
  35. <artifactId>junit-vintage-engine</artifactId>
  36. </exclusion>
  37. </exclusions>
  38. </dependency>
  39. </dependencies>
  40.  
  41. <dependencyManagement>
  42. <dependencies>
  43. <dependency>
  44. <groupId>org.springframework.cloud</groupId>
  45. <artifactId>spring-cloud-dependencies</artifactId>
  46. <version>${spring-cloud.version}</version>
  47. <type>pom</type>
  48. <scope>import</scope>
  49. </dependency>
  50. </dependencies>
  51. </dependencyManagement>
  52.  
  53. <build>
  54. <plugins>
  55. <plugin>
  56. <groupId>org.springframework.boot</groupId>
  57. <artifactId>spring-boot-maven-plugin</artifactId>
  58. </plugin>
  59. </plugins>
  60. </build>
  61.  
  62. </project>

3.修改application.properties文件  改个端口 不然后面启动两个项目端口冲突

  1. server.port=8888

4.在你的git仓库中提交一个文件a-bootiful-client.properties  用以感受我们的springcloud项目与Git的结合使用

  1. message = Hello world

文件中写上这个就行 便于等下测试

5。再次修改application.properties

  1. spring.cloud.config.server.git.uri=https://gitee.com/你的仓库地址.git
  2. #spring.cloud.config.server.git.searchPaths=config
  3. #
  4. spring.cloud.config.server.git.username=你的用户名
  5.  
  6. spring.cloud.config.server.git.password=你的密码
  7.  
  8. spring.application.name=a-bootiful-client#刚才上传的文件名

6.修改DemoApplication 加上@EnableConfigServer注解

  1. package com.example.demo;
  2.  
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.cloud.config.server.EnableConfigServer;
  6.  
  7. @EnableConfigServer
  8. @SpringBootApplication
  9. public class DemoApplication {
  10.  
  11. public static void main(String[] args) {
  12. SpringApplication.run(DemoApplication.class, args);
  13. }
  14.  
  15. }

7.这个时候我们可以来建立第二项目啦

8.pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.3.1.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.example</groupId>
  12. <artifactId>demo</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>demo</name>
  15. <description>Demo project for Spring Boot</description>
  16.  
  17. <properties>
  18. <java.version>1.8</java.version>
  19. <spring-cloud.version>Hoxton.SR5</spring-cloud.version>
  20. </properties>
  21.  
  22. <dependencies>
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-actuator</artifactId>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.springframework.boot</groupId>
  29. <artifactId>spring-boot-starter-web</artifactId>
  30. </dependency>
  31. <dependency>
  32. <groupId>org.springframework.cloud</groupId>
  33. <artifactId>spring-cloud-starter-config</artifactId>
  34. </dependency>
  35.  
  36. <dependency>
  37. <groupId>org.springframework.boot</groupId>
  38. <artifactId>spring-boot-starter-test</artifactId>
  39. <scope>test</scope>
  40. <exclusions>
  41. <exclusion>
  42. <groupId>org.junit.vintage</groupId>
  43. <artifactId>junit-vintage-engine</artifactId>
  44. </exclusion>
  45. </exclusions>
  46. </dependency>
  47. </dependencies>
  48.  
  49. <dependencyManagement>
  50. <dependencies>
  51. <dependency>
  52. <groupId>org.springframework.cloud</groupId>
  53. <artifactId>spring-cloud-dependencies</artifactId>
  54. <version>${spring-cloud.version}</version>
  55. <type>pom</type>
  56. <scope>import</scope>
  57. </dependency>
  58. </dependencies>
  59. </dependencyManagement>
  60.  
  61. <build>
  62. <plugins>
  63. <plugin>
  64. <groupId>org.springframework.boot</groupId>
  65. <artifactId>spring-boot-maven-plugin</artifactId>
  66. </plugin>
  67. </plugins>
  68. </build>
  69.  
  70. </project>

9.org.springframework.cloud:spring-cloud-starter-config依赖  然后在properties里配置 就能保证 在不需要重启jvm的情况下 (当然还需要别的依赖和操作请看到最后)刷新配置

这里 我写在了bootstrap.properties文件里 你也可以写在application.properties或者application.yml里

  1. spring.application.name=a-bootiful-client
  2. # N.B. this is the default:
  3. spring.cloud.config.uri=http://localhost:8888

启用/refresh 修改application.properties

  1. management.endpoints.web.exposure.include=*

10.客户端可以通过使用传统的机制(如访问所述配置服务器的任何值@ConfigurationProperties@Value("${…​}")或通过Environment

  1. package com.example.demo;
  2.  
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.boot.SpringApplication;
  5. import org.springframework.boot.autoconfigure.SpringBootApplication;
  6. import org.springframework.cloud.context.config.annotation.RefreshScope;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RestController;
  9.  
  10. @SpringBootApplication
  11. public class DemoApplication {
  12.  
  13. public static void main(String[] args) {
  14. SpringApplication.run(DemoApplication.class, args);
  15. }
  16.  
  17. }
  18. @RefreshScope
  19. @RestController
  20. class MessageRestController {
  21.  
  22. @Value("${message:Hello default}")
  23. private String message;
  24.  
  25. @RequestMapping("/message")
  26. String getMessage() {
  27. return this.message;
  28. }
  29. }

11.千辛万苦终于来到测试了

① http://localhost:8080/message 你会看见Hello world

②修改a-bootiful-client.properties(记得commit)的message 改为Hello spring  访问http://localhost:8888/a-bootiful-client/default你可以查看是否已经更改

③再点击http://localhost:8080/message这个你会发现依然是Hello world 字样what????不是说是无刷新获取的么????? 这个时候不要苦恼 继续进行第四步

④以post方式请求http://localhost:8080/actuator/refresh 然后再http://localhost:8080/message看看 是不是已经变成了Hello spring???? 那就对了

(这里是用的org.springframework.boot:spring-boot-starter-actuator 使用refresh以强制客户端刷新自身并提取新值)

从Spring Initializr开始的更多相关文章

  1. Springboot(一):使用Intellij中的Spring Initializr来快速构建Spring Boot工程

    使用Intellij中的Spring Initializr来快速构建Spring Boot工程 New---Project 可以看到图所示的创建功能窗口.其中Initial Service Url指向 ...

  2. 使用Spring Initializr创建项目

    Spring initializr 是Spring 官方提供的一个很好的工具,可以用来用来创建一个Spring boot 的项目.可以选择使用Maven管理或者使用Gradle管理,还可以选择使用的编 ...

  3. IDEA新建项目时,没有Spring Initializr选项

    换了台新电脑,然后重新安装了Intellij IDEA,创建spring boot项目的时候找不到Spring Initializr选项了. 然后百度了下,发现有前辈做出了回答,就复制存到了自己随笔里 ...

  4. Spring initializr使用

    Spring initializr 是Spring 官方提供的一个很好的工具,用来初始化一个Spring boot 的项目. 有两种方式可以使用Spring initializr来创建一个项目: ht ...

  5. 使用Intellij中的Spring Initializr来快速构建Spring Boot/Cloud工程(十五)

    在之前的所有Spring Boot和Spring Cloud相关博文中,都会涉及Spring Boot工程的创建.而创建的方式多种多样,我们可以通过Maven来手工构建或是通过脚手架等方式快速搭建,也 ...

  6. (转载)IDEA新建项目时,没有Spring Initializr选项

    最近开始使用IDEA作为开发工具,然后也是打算开始学习使用spring boot. 看着博客来进行操作上手spring boot,很多都是说 创建一个新项目(Create New Project) 选 ...

  7. 使用Intellij中的Spring Initializr来快速构建Spring Boot/Cloud工程

    在之前的所有Spring Boot和Spring Cloud相关博文中,都会涉及Spring Boot工程的创建.而创建的方式多种多样,我们可以通过Maven来手工构建或是通过脚手架等方式快速搭建,也 ...

  8. 在线官网Spring Initializr 或 IntelliJ IDEA 快速搭建springboot项目

    Spring Boot是由Pivotal团队提供的全新框架,设计目的是用来简化新Spring应用的初始搭建以及开发过程.它主要推崇的是'消灭配置’,实现零配置. 那么,如何快速新建一个一个spring ...

  9. 如何搭建自己的SPRING INITIALIZR server

    这两天在慕课学Spring boot ,用idea通过spring initializr新建项目 即使用代理连不上.无奈. 参考了 GitHub - spring-io/initializr: A w ...

  10. SpringCloud核心教程 | 第一篇: 使用Intellij中的Spring Initializr来快速构建Spring Cloud工程

    spring cloud简介 spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选.分布式会话等等.它运行环 ...

随机推荐

  1. 枚举&注解

    枚举:自定义枚举类 使用Enum关键字定义的枚举类 注解:jdk内置的基本注解类型(3个) 自定义注解类型 对注解进行注解(元注解4个) 利用反射获取注解信息(反射部分涉及) 自定义枚举类: Test ...

  2. matlab自学笔记

    1.字符串格式化,用sprintf如a=sprintf('%.2f_除以%d等于%.3f',1.5,2,0.75)%则a=1.50除以2等于0.750 2.for循环只能针对整数,不能遍历字符串或其他 ...

  3. 自己的win7第一次使用RabbitMQ

    使用的过程中参考了https://www.cnblogs.com/longlongogo/p/6489574.html所写的内容 一.环境搭建 1.由于RabbitMQ使用Erlang语言编写,所以先 ...

  4. 设计MyTime类 代码参考

    #include <iostream> #include <cstdio> using namespace std; class MyTime { private: int h ...

  5. UPX的使用

    UPX是一个通用可执行文件压缩器,由于其具有: 压缩率高:压缩效果优于zip/gzip: 解压速度快:在奔腾133上即可达到大约10MB/秒: 压缩的可执行文件没有额外的内存开销: 安全:可以列表,检 ...

  6. Rocket - decode - 解码单个信号

    https://mp.weixin.qq.com/s/0D_NaeBEZX5LBQRdCz2seQ     介绍解码单个信号逻辑的实现.    1. 单个信号   每个指令对应了一组信号,每个信号对应 ...

  7. Chisel3-Intellij IDEA中使用sbt构建Chisel项目

    https://mp.weixin.qq.com/s/gssjiiPW6zUzKwCFZdNduw   1. 使用Intellij IDEA创建Scala项目   Chisel项目,就是构建Scala ...

  8. java方法句柄-----4.你所不知道的MethodHandle【翻译】

    Method Handles in Java 1.介绍 在本文中,我们将探讨一个重要的API,它是在Java 7中引入的,并在Java 7版本之后更加完善:全限定名是:Java.lang.invoke ...

  9. Java实现蓝桥杯十六进制转八进制

    基础练习 十六进制转八进制 时间限制:1.0s 内存限制:512.0MB 提交此题 锦囊1 锦囊2 问题描述 给定n个十六进制正整数,输出它们对应的八进制数. 输入格式 输入的第一行为一个正整数n ( ...

  10. Java实现 LeetCode 301 删除无效的括号

    301. 删除无效的括号 删除最小数量的无效括号,使得输入的字符串有效,返回所有可能的结果. 说明: 输入可能包含了除 ( 和 ) 以外的字符. 示例 1: 输入: "()())()&quo ...