Springcloud学习笔记(二)

来源:互联网 发布:面板数据分析什么 编辑:程序博客网 时间:2024/09/21 08:48

上回在springcloud学习笔记(一)中我们演示了如何搭建eureka注册中心和网关,今天我们借着老A的项目接着演示用户微服务和配置微服务。用户微服务就是用来模拟真实开发环境中的具体业务服务,配置微服务则是用来统一管理每个微服务的启动文件的。

一:用户微服务搭建

第一步是在ag-parent父工程中新建一个子工程取名ag-uc,接下来还是之前的套路,搭建一个微服务的三个步骤:导入依赖文件,修改配置文件以及编写启动类。

用户微服务(亦即普通微服务)的依赖文件如下:

<?xml version="1.0" encoding="UTF-8"?><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">    <parent>        <artifactId>ag-parent</artifactId>        <groupId>com.github.wxiaoqi.learning</groupId>        <version>1.0-SNAPSHOT</version>    </parent>    <modelVersion>4.0.0</modelVersion>    <artifactId>ag-uc</artifactId>    <dependencies>        <dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-starter-eureka</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-actuator</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-config-client</artifactId>        </dependency>    </dependencies></project>
修改配置文件:application.yml

spring:  application:    name: user-centereureka:  client:    service-url:      defaultZone: http://localhost:8761/eurekaserver:  port: 7771

配置文件中只需要填写以上基本的属性配置就能启动成功。

最后一步是编写启动类UserCenterBootstrap启动类:

package com.github.wxiaoqi.learning.uc;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;/** * <pre> *    author  : lpf *    time    : 2017/10/2922:21 *    desc    : 输入描述 * </pre> */@SpringBootApplication@EnableEurekaClientpublic class UseCenterBootstrap {    public static void main(String[] args) {        SpringApplication.run(UseCenterBootstrap.class,args);    }}
到这里,用户微服务就搭建完成并且可以正常启动了,下面我们再写一个测试接口来模拟请求用户微服务的数据。

新建一个Controller取名UserRest,内容如下:

package com.github.wxiaoqi.learning.uc.rest;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * <pre> *    author  : lpf *    time    : 2017/10/3020:29 *    desc    : 输入描述 * </pre> */@RestController@RequestMapping("user")public class UserRest {        @RequestMapping("/{id}")    public String getUserInfo(@PathVariable String id){        return "接口访问成功";    }}
接下来我们就可以通过网关访问用户微服务的接口了,如下图:



如果有不明白请求的链接为什么是http://localhost:8765/api/uc/user/1,可以看我上一篇关于网关的路由分发配置,相信看完之后就会明白为什么是/api/uc打头的路径了。

二:配置中心微服务搭建

到这里就该演示如何搭建一个配置服务了,搭建的基本思路不变,只是部分配置和之前有些不同。

依赖文件:

<?xml version="1.0" encoding="UTF-8"?><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">    <parent>        <artifactId>ag-parent</artifactId>        <groupId>com.github.wxiaoqi.learning</groupId>        <version>1.0-SNAPSHOT</version>    </parent>    <modelVersion>4.0.0</modelVersion>    <artifactId>ag-config</artifactId>    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-actuator</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-config</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-starter-eureka</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-config-server</artifactId>        </dependency>    </dependencies></project>
配置文件:

spring:  application:    name: ag-config  cloud:    config:      server:        git:          uri: https://git.oschina.net/geek_qi/AG-Config.giteureka:  client:    service-url:      defaultZone: http://localhost:8761/eurekaserver:  port: 8750
在配置文件中除了要定义端口,注册到eureka上,给服务起名外,还要配置configserver的git地址,该地址表示配置文件存放的网址。

配置微服务启动类:

package com.github.wxiaoqi.learning.config;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.config.server.EnableConfigServer;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;/** * <pre> *    author  : lpf *    time    : 2017/10/2922:21 *    desc    : 输入描述 * </pre> */@SpringBootApplication@EnableEurekaClient@EnableConfigServer@EnableAutoConfigurationpublic class ConfigServerBootstrap {    public static void main(String[] args) {        SpringApplication.run(ConfigServerBootstrap.class,args);    }}
接下来我们来演示如何获取config微服务中的配置文件数据,我们还是用user微服务来演示,首先要在user微服务中新建一个bootstrap.yml文件,在该文件中填写需要从config微服务获取内容的具体信息。bootstrap文件内容如下:

spring:  cloud:    config:        discovery:           service-id: ag-config           enabled: true        label: master  # git 分支        name: uc        profile: dev
上面这段文字说明表示user微服务将从ag-config这个微服务中读取配置文件的内容,即从master分支上读取名称是uc且后缀为dev的配置文件。
至于这里为什么要新建bootstrap文件填写配置信息而不在application配置文件中填写,这里没有明显的区别。只是大家需要知道bootstrap.yml要比application.yml文件先加载,并且bootstrap.yml文件会覆盖application.yml中相同属性的内容(如果有的话)。所以bootstrap.yml文件中通常都会配置一些固定的数据,比如配置文件的读取方式以及加解密信息。

最后我们再改造一下用户微服务的接口打印出配置文件中的内容,先看一下配置文件中的内容:



改造用户微服务的接口:

package com.github.wxiaoqi.learning.uc.rest;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * <pre> *    author  : lpf *    time    : 2017/10/3020:29 *    desc    : 输入描述 * </pre> */@RestController@RequestMapping("user")public class UserRest {    @Value("${language.en}")    private String hello;    @RequestMapping("/{id}")    public String getUserInfo(@PathVariable String id){        return hello;    }}
使用postman来请求接口:

到这里我们已经把配置微服务和用户微服务搭建完成,并演示了接口调用以及从配置服务中读取内容的过程。整个springcloud的脚手架基本搭建完成,后续任务就是一步一步填充这个框架,使其更加丰满。





原创粉丝点击