Holy Null 's Blog
Holy Null!
Toggle navigation
Holy Null 's Blog
主页
机器学习
Flume
Nginx
Hadoop
Apache Shiro
架构理论
Docker
Spring Cloud
关于我
归档
标签
Spring Cloud 教程|第三篇 开发部署一个服务
2017-12-25 14:08:57
299
0
0
holynull
# 第三篇 开发部署一个服务 本篇中我们将开发和部署一个基于Spring Cloud Eureka Server的服务。服务主要实现对一个Oracle数据库中的表的增删查改功能。 ## 一、在Docker中启动一个Oracle数据库 在`docker-compose.yml`中创建一个命名为`oracle12c`的服务。在`services`下增加如下服务配置: ``` oracle12c: image: sath89/oracle-12c ports: - 1521:1521 - 8090:8080 - 6800:6800 environment: MANUAL_DBCA: 'true' VNC_PASSWORD: oracle12c DBCA_TOTAL_MEMORY: 512 volumes: - ./tmp/oracle12c/data:/u01/app/oracle networks: mynet: ipv4_address: 172.19.0.14 ``` 在命令行中执行如下命令启动Oracle 12c数据库: ``` docker-compose up -d oracle12c ``` 第一次启动时会比较慢,因为Docker daemon会去dockerhub上下载镜像,并初始化数据库。 [镜像地址](https://store.docker.com/community/images/sath89/oracle-12c) 我们可以通过如下设置使用数据库客户端测试链接: ``` hostname: localhost port: 1521 sid: xe service name: xe username: system password: oracle ``` ## 二、创建服务 我们在`spring-cloud-poc`项目下创建一个模块,我们在这个模块中需要用到`spring data jpa`来进行数据库访问,实现数据库的增删查改功能。 ### 2.1 安装Oracle 数据库驱动 需要访问Oracle官网,找到Oracle 12c 数据库的驱动jar包,下载并保存。项目中已经准备好了,并且提供了Maven环境下的安装脚本`ojdbc7-install.sh`。 ``` mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc7 -Dversion=12.1.0.2.0 -Dpackaging=jar -Dfile=ojdbc7.jar ``` ### 2.2 创建模块`eurekaClient` 在根项目下创建一个名叫`eurekaClient`的模块。我们之所以命名为`eurekaClient`是因为本模块具有Eureka Server注册中心客户端的特性的模块,通过集成Eureka Server客户端的特性,使我们的开发的应用服务在Eureka Server注册中心中进行注册,从而能够被其他服务消费者发现。 我们的pom.xml文件如下: ``` <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>cloud</artifactId> <groupId>com.ultimatech</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>eurekaClient</artifactId> <packaging>jar</packaging> <name>eurekaClient</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.ultimatech</groupId> <artifactId>poc-base</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc7</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </build> </project> ``` 引入如下依赖,来实现Eureka Client功能,即在代码中可以使用Eureka的相关注解: ``` <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> ``` 引入如下依赖,来实现基于Spring Boot风格的Restful服务: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> ``` 引入如下依赖,帮助使用Spring data JPA来实现数据库操作: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> ``` pom.xml文件中还定义了依赖`poc-base`这样一个模块,在这个模块中定义了一些数据模型,主要用来在服务消费者和服务提供者之间数据通信时封装消息。 ### 2.3 创建服务启动类 创建服务启动类`com.ultimatech.eurekaclient.ServiceHiApp`,在启动类中加入注解`@EnableEurekaClient`,使模块成为一个Eureka Server的client: ``` @SpringBootApplication @EnableEurekaClient @RestController public class ServiceHiApp { public static void main(String[] args) { SpringApplication.run(ServiceHiApp.class, args); } ...... } ``` ### 2.4 创建配置文件 在模块的`resouces`目录下创建一个基础配置文件,内容如下: ``` eureka: instance: leaseRenewalIntervalInSeconds: 1 leaseExpirationDurationInSeconds: 2 statusPageUrlPath: /info healthCheckUrlPath: /health client: healthcheck: enabled: true server: port: 8080 spring: application: name: service-hi datasource: url: jdbc:mysql://mysql-1:3306/poc username: root password: root driver-class-name: com.mysql.jdbc.Driver max-idle: 10 max-wait: 10000 min-idle: 5 initial-size: 5 #=====================jpa config================================ #实体类维护数据库表结构的具体行为:update/create/create-drop/validate/none jpa: hibernate: ddl-auto: update #打印sql语句 show-sql: true #格式化输出的json字符串 jackson: serialization: indent_output: true management: security: enabled: false ``` 在模块的`config`目录下创建一个部署到docker环境下的配置文件`eclient-1-bootstrap.yml`: ``` # 定义注册中的注册和发现服务的端点 eureka: client: serviceUrl: defaultZone: http://eureka-1:8761/eureka/,http://eureka-2:8761/eureka/ ``` 在部署环境中,我们要求服务实例启动时要加载Config Server提供的配置文件,所以我们使用如下配置: ``` # 定义从配置服务中获取配置数据 spring: cloud: config: name: eclient-1 label: master profile: prd ``` 表示该实例启动时,会去请求Config Server上`eclient-1-prd`开头的配置文件。我们通过访问`http://localhost:8082/eclient-1-prd.yml`可以看到该配置文件内容。 ``` eureka: instance: hostname: eclient-1 serviceUrl: defaultZone: http://eureka-1:8761/eureka/,http://eureka-2:8761/eureka/ spring: datasource: # url: jdbc:oracle:thin:@oracle12c:1521:xe url: jdbc:oracle:thin:@10.177.193.116:1521:workflow username: workflow # password: workflow password: workflow1 driver-class-name: oracle.jdbc.OracleDriver max-idle: 10 max-wait: 10000 min-idle: 5 initial-size: 5 #=====================jpa config================================ #实体类维护数据库表结构的具体行为:update/create/create-drop/validate/none jpa: hibernate: ddl-auto: update #打印sql语句 show-sql: true #格式化输出的json字符串 jackson: serialization: indent_output: true ``` ### 2.5 编写功能代码 - `com.ultimatech.eurekaclient.model`持久化类 - `com.ultimatech.eurekaclient.dao`数据库操作接口 - `com.ultimatech.eurekaclient.dao.impl`数据库操作实现 - `com.ultimatech.eurekaclient.service`业务逻辑接口 - `com.ultimatech.eurekaclient.service.impl`业务逻辑实现 在`com.ultimatech.eurekaclient.ServiceHiApp`中使用了`@RestController`。所以web访问的端点可以定义在`ServiceHiApp`中。 其他请参考项目源代码。 ## 三、部署 在项目`spring-cloud-poc`根目录下的`docker-compose.yml`文件中的`services`下增加: ``` eclient-1: build: ./docker/webnode container_name: eclient-1 volumes: - "/tmp" - "./eurekaClient/target/eurekaClient-1.0-SNAPSHOT.jar:/app.jar" - "/config" - "./eurekaClient/config/eclient-1-bootstrap.yml:/config/bootstrap-prd.yml" depends_on: - eureka-1 - eureka-2 - config-server-1 environment: HOSTNAME: eclient-1 JAVA_OPTS: -XX:MetaspaceSize=64m -XX:MaxMetaspaceSize=128m networks: mynet: ipv4_address: 172.19.0.5 extra_hosts: - "eureka-1:172.19.0.2" - "eureka-2:172.19.0.3" - "eclient-1:172.19.0.5" - "eclient-2:172.19.0.6" - "feign-1:172.19.0.7" - "config-server-1:172.19.0.8" - "service-2-1:172.19.0.9" - "apigateway-1:172.19.0.10" - "feign-2:172.19.0.11" - "mysql-1:172.19.0.12" # links: # - oracle12c ports: - 8081:8080 ``` ## 四、启动 在模块目录运行如下命令进行安装编译: ``` mvn clean install ``` 在项目的根目录下(docker-compose.yml文件同级目录下),执行如下命令启动: ``` docker-compose up -d eclient-1 ``` ## 五、验证 我们可以查看代码`com.ultimatech.eurekaclient.ServiceHiApp`,定义了如下几个端点: ``` @RequestMapping(path = "/createUser") public Message<UserVo> createUser(@RequestBody UserVo user){ ...... } @RequestMapping(path = "/delUser") public Message<Integer> delUser(Integer userId){ ...... } @RequestMapping(path = "/updateUser") public Message<UserVo> updateUser(@RequestBody UserVo user){ ...... } @RequestMapping(path = "/getuser") public Message<UserVo> getUserById(Integer id){ ...... } @RequestMapping(path = "/findAllUser") public Message<List<UserVo>> findAllUser(){ ...... } ``` 通过浏览器访问如下端点来验证数据库中user表的数据变化: ``` http://localhost:8081/createUser?name=&number= http://localhost:8081/delUser?userId= http://localhost:8081/updateUser?id=&name=&number= http://localhost:8081/findAllUser ```
上一篇:
Spring Cloud 教程|第四篇 Feign客户端
下一篇:
Spring Cloud 教程|第二篇 统一配置服务
0
赞
299 人读过
新浪微博
微信
腾讯微博
QQ空间
人人网
提交评论
立即登录
, 发表评论.
没有帐号?
立即注册
0
条评论
More...
文档导航
没有帐号? 立即注册