一 Spring Cloud初学手记服务注册( 二 )

org.springframework.bootspring-boot-starter-weborg.mybatis.spring.bootmybatis-spring-boot-starter2.0.0org.springframework.bootspring-boot-devtoolsruntimeorg.springframework.bootspring-boot-starter-testtestorg.springframework.cloudspring-cloud-starter-eurekaorg.springframework.bootspring-boot-maven-pluginorg.springframework.cloudspring-cloud-dependenciesDalston.RC1pomimportspring-milestonesSpring Milestoneshttps://repo.spring.io/milestonefalse
同样需要在.yml中加入一些配置
#注册中心的地址eureka:client:serviceUrl:defaultZone: http://localhost:8761/eureka/server:#指定服务端口port: 8762spring:application:#注册至Eureka的服务名称name: service-helloworld
在该工程的主入口类上加上@t注解,表示这是一个服务应用,运行后会发布至服务注册中心(顺便我在这里写了一个请求)
@RestController@EnableDiscoveryClient@SpringBootApplication@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})public class SatsukiClients01Application {public static void main(String[] args) {SpringApplication.run(SatsukiClients01Application.class, args);}@Value("${server.port}")String port;@RequestMapping(value = "http://www.kingceram.com/hello")public String hello() {return "hello world, port : " + port;}}
启动服务后,对应的服务已经注册上去啦~
访问:8762/hello可查看发布的服务
三、(非必要)前面搭建的工程中的配置信息均是以硬编码的形式写入配置文件中,如果多台服务器集群,每台服务器都需要写入同样的配置,更改及同步配置信息会比较麻烦 。Cloud推荐使用版本管理服务器(缺省使用git)去统一维护配置文件,搭建一个 工程,其他Web服务器只需要去 上获取配置文件信息即可
新建一个 Boot工程作为 ,添加如下依赖:
org.springframework.cloudspring-cloud-config-server
在工程的主入口类上加上@注解,表示该工程为一个 ,同时加上@t注解,发布该服务至
@EnableDiscoveryClient@EnableConfigServer@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})@SpringBootApplicationpublic class ConfigServerApplication {public static void main(String[] args) {SpringApplication.run(ConfigServerApplication.class, args);}}
配置 的.yml
eureka:client:serviceUrl:defaultZone: http://localhost:8761/eureka/server:port: 8888spring:cloud:config:server:git:#git服务器的仓库地址uri: https://xxxx#配置文件所存放的仓库地址下的路径(不需要指定文件名)searchPaths: abcFloder/xyzFloder/xxxConfigapplication:name: config-server
新建一个工程,加上依赖:
【一Spring Cloud初学手记服务注册】org.springframework.cloudspring-cloud-starter-config
在新建的工程中的.yml中写入如下信息:
server:port: 8881spring:application:name: config-clientcloud:config:label: masterprofile: dev#Config Server的uriuri: http://localhost:8888/
PS:
① 应用需要在加载属性之前读取 上的配置文件信息,.yml加载的优先级高于.yml;