(一)Spring Cloud Finchley.SR4 基础框架介绍
版权声明:本文为博主原创文章,版权归烟台华东数据科技有限公司与博主本人共同所有。烟台华东数据科技有限公司及其下属机构毋须博主授权即可在任意用途下转载、使用!目录前言Spring Cloud基础框架搭建本项目Spring Cloud基本框架介绍各模块基本介绍一、项目root二、HDD_EUREKA_SERVER三、HDD_EUREKA_CONFIG四、H...
版权声明:本文为博主原创文章,版权归烟台华东数据科技有限公司与博主本人共同所有。烟台华东数据科技有限公司及其下属机构毋须博主授权即可在任意用途下转载、使用!
目录
前言
本系列是基于有Spring Cloud的搭建基础的人员学习使用。在查看本系列文章前,要求已学习的技术前提如下:
- 了解并学会使用SpringMVC进行开发
- 了解并学会使用Spring Boot应用开发
- 了解Spring Cloud基本框架和常用组件
- 了解并学会使用Redis数据库进行开发
- 有编写过使用RESTful API接口规范实现的接口开发经验
本文主要用于框架基础介绍学习所用。基于自主学习思考的目标,源码不会开放。
本文主要是介绍项目的基本框架,为后续的其他组件和微服务学习打下基础。
本文写与2019年6月,使用的是当前的稳定版本,后续版本可能存在差异,本文不为后续版本的差异负责。数据公司内部后续进行升级时,请相应升级更新本系列说明博客。
Environment
- Intellij Idea version : 2019.1.2
- JavaSDK version : 1.8
- Maven version : 3.6.1
- Spring Cloud version : Finchley.SR4
- SpringBoot version : 2.0.9.RELEASE
Spring Cloud基础框架搭建
Spring Cloud基础框架搭建在网上有很多教程,此处不再赘述。如果有疑问,可以移步下方的地址进行学习和自行搭建。
《Spring Cloud 从入门到精通》作者博客 https://segmentfault.com/u/coderdd/articles?page=5
Spring Cloud 中文文档 https://springcloud.cc/spring-cloud-dalston.html
Spring Cloud 官方 https://spring.io/projects/spring-cloud
Spring Cloud 中国社区 http://springcloud.cn/
本项目Spring Cloud基本框架介绍
HDD_EUREKA_SERVER eureka注册中心
HDD_EUREKA_CONFIG 配置服务模块。此模块主要是把所有微服务的相关数据库配置集中化管理。也可以放在Git上面管理,从而可以实现动态刷新功能。
HDD_REDIS_SERVICE Redis数据库的微服务模块。实际运行的模块,可以配置试用多IP实现多实例
HDD_REDIS_SERVICE_INTERFACE Redis微服务模块的接口模块,主要用于提供给其他微服务或者其他项目调用使用。
此处需要强调一个问题,当前网络上很多示例并没有做服务和接口的区分,基本都是做在一个服务模块中,这样其实非常不利于多项目的接入和开发。因此我们使用服务和接口分开的模式。
服务使用多实例运行注册到eureka中心,然后对外提供相应的接口jar包,其他各个项目只需要引用jar包则可以直接调用接口,而不必自行实现RestTemplate调用。原理是基于Feign实现,部分老项目不能使用Feign则在后面会有专门的文章介绍解决此问题。
HDD_SPRING_ADMIN 监控模块
HDD_BREAK_MONITOR 熔断集中监控模块
Spring Cloud 版本:Finchley SR4
使用maven管理
各模块基本介绍
一、项目root
pom文件:
<groupId>net.huadong</groupId>
<artifactId>HDD_SHJJHY_PLATFORM</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>HDD_SHJJHY_PLATFORM</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Finchley.SR4</version>
</parent>
<modules>
<module>HDD_EUREKA_SERVER</module>
<module>HDD_EUREKA_CONFIG</module>
<module>HDD_BREAK_MONITOR</module>
<module>HDD_SPRING_ADMIN</module>
<module>HDD_REDIS_SERVICE</module>
<module>HDD_REDIS_SERVICE_INTERFACE</module>
</modules>
<!-- 此处的jar包引用在所有的子模块中都会引用 -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
二、HDD_EUREKA_SERVER
Server只需要配置一个启动类,然后配置application.yml即可
package net.huadong;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* Spring Cloud 的主服务
* Eureka 服务启动类
*/
@EnableEurekaServer //Eureka服务注解
@SpringBootApplication
public class HddEurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(HddEurekaServerApplication.class, args);
}
}
application.yml
server:
port: 8101
eureka:
datacenter: Spring Cloud
enviroment: prod
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
server:
enable-self-preservation: false
eviction-interval-timer-in-ms: 4000
其中一定要关闭自己的注册,然后关闭了保护模式
三、HDD_EUREKA_CONFIG
在配置中心,弄了两套配置文件,主要用于本地测试和正式系统发布。切换在application.yml文件中切换
spring:
cloud:
config:
server:
native:
search-locations: classpath:/config/test
server:
port: 8888
如果要配置在Git上,在这里修改配置即可,感兴趣的请自行搜索实现。
bootstrap.yml
spring:
application:
name: config #1
profiles:
active: native #2
eureka:
instance:
non-secure-port: ${server.port:8888} #3
metadata-map:
instanceId: ${spring.application.name}:${random.value} #4
client:
service-url:
defaultZone: http://${eureka.host:localhost}:${eureka.port:8101}/eureka/ #5
四、HDD_REDIS_SERVICE
启动类:
package net.huadong;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient
@SpringBootApplication
public class HddRedisServiceApplication {
public static void main(String[] args) {
SpringApplication.run(HddRedisServiceApplication.class, args);
}
}
另,本项目中使用了swagger2来进行接口API的管理和测试,有关swagger2如何配置的请自行学习
Redis配置类:
package net.huadong;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import redis.clients.jedis.JedisPoolConfig;
@Configuration
public class RedisConfig implements EnvironmentAware {
private Environment env;
@Override
public void setEnvironment(Environment env) {
this.env = env;
}
/**
* 配置JedisPoolConfig
* @return JedisPoolConfig实体
*/
@Bean(name = "jedisPoolConfig")
public JedisPoolConfig jedisPoolConfig() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
// 连接池最大连接数(使用负值表示没有限制)
jedisPoolConfig.setMaxTotal(Integer.valueOf(env.getProperty("spring.redis.pool.max-active")));
// 连接池最大阻塞等待时间(使用负值表示没有限制)
jedisPoolConfig.setMaxWaitMillis(Long.valueOf(env.getProperty("spring.redis.pool.max-wait")));
// 连接池中的最大空闲连接
jedisPoolConfig.setMaxIdle(Integer.valueOf(env.getProperty("spring.redis.pool.max-idle")));
// 连接池中的最小空闲连接
jedisPoolConfig.setMinIdle(Integer.valueOf(env.getProperty("spring.redis.pool.min-idle")));
return jedisPoolConfig;
}
/**
* 实例化 RedisConnectionFactory 对象
* @param poolConfig
* @return
*/
@Bean(name = "jedisConnectionFactory")
public RedisConnectionFactory jedisConnectionFactory(@Qualifier(value = "jedisPoolConfig") JedisPoolConfig poolConfig) {
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(poolConfig);
jedisConnectionFactory.setHostName(env.getProperty("spring.redis.host"));
jedisConnectionFactory.setPort(Integer.parseInt(env.getProperty("spring.redis.port")));
jedisConnectionFactory.setDatabase(Integer.parseInt(env.getProperty("spring.redis.database")));
jedisConnectionFactory.setPassword(env.getProperty("spring.redis.password"));
return jedisConnectionFactory;
}
}
此处使用的是jedispool作为连接池,现在官方推荐使用LetterPool,后续会另外介绍。
然后到这一步,很多人会疑惑,到底是在哪里实现,微服务会去配置中心调取相应的配置文件呢,下面我来介绍。
主要是在bootstrap.yml文件中:
spring: application: name: redisService #1 cloud: config: enabled: true discovery: enabled: true service-id: config #1 eureka: instance: non-secure-port: ${server.port:8889} #3 client: service-url: defaultZone: http://${eureka.host:localhost}:${eureka.port:8101}/eureka/ #5
- 设定注册的eureka服务的地址
- 设定当前微服务的配置中心启用,配置中心在eureka中的id为config(HDD_EUREKA_CONFIG——bootstrap.yml——spring.application.name)
- 然后在配置中心中,文件名为redisService.yml的即为自动读取的当前微服务的配置文件
application.yml
server: port: 8889 logging: config: classpath:logback.xml level: org.springframework: error debug: false eureka: client: healthcheck: enabled: true
logback.xml为日志配置文件,有关slf4j的相关配置请自行学习。
然后是SpringMVC相关的代码了,有关SpringMVC的知识请自行学习,三层结构如下,相关代码不再贴出来了。
五、HDD_REDIS_SERVICE_INTERFACE
此模块是仅用来作为Feign桥梁,给其他微服务模块或者其他项目调用使用的。其他项目需要调用微服务方法,只需要把此模块打成jar包,然后导入其他需要使用的项目中即可正常使用了。同时程序不需要做很大的调整和变动。
示例:
package net.huadong.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
@FeignClient(name = "redisService")
@Component
public interface RedisService {
/**
* 根据传入的map(key,value)值设置redis
*
* @param map
* @return
*/
@RequestMapping(value = "/Redis/setRedis", method = RequestMethod.POST)
void set(@RequestBody Map<String, Object> map);
/**
* 传入key获取对应的redis 值
*
* @return obj
*/
@RequestMapping(value = "/Redis/getRedis", method = RequestMethod.POST)
@ResponseBody
Object get(@RequestParam("key") String key);
}
此处只暴露了Redis微服务中的两个基本方法,其中
- FeignClient中配置的name即为在eureka中注册的微服务的name
- RequestMapping中的地址,即为/controller的name/方法的ReqMapping地址
六、其他
由于当前使用的是Finchley版本,Spring Boot 版本为2.0,因此部分组件的引用与之前的版本有所差异,相关引用如下:
<!-- starter组件 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<!-- 需要使用配置中心需要的组件 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<!-- 与之前的版本引用有所差异 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- SpringMVC -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Swagger2组件 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
演示效果:
启动后,访问http://localhost:8101
然后访问redisService的Swagger:
可以看到,实际我的微服务的Controller中提供了很多方法,但是我的interface模块中,我只对外暴露了两个方法。
至此,基本框架已经说明完成。下一篇主要介绍Spring Admin监控和Hystrix熔断和监控。
更多推荐
所有评论(0)