springboot 构建 spring cloud 微服务项目 搭建ARTHUR框架分享

在此分享下本人最近搭建的一个springcloud微服务项目,包括注册中心、配置中心、熔断器、turbine熔断器集中监控中心、fegin、前后端负载均衡、swagger2、网关、bus动态修改配置等等。项目只是初步搭建,之后还会加入各种好玩的技术,会不定期更新,感兴趣的可下载进行研究交流,微服务的明天会更好。

github

arthur https://github.com/ArthurFamily/arthur.git
config https://github.com/ArthurFamily/config-center.git

码云
https://git.oschina.net/ArthurFamily/arthur.git
https://git.oschina.net/ArthurFamily/config-center.git

(注:配置文件通过git项目读取,放到一个项目中读取超过5分钟才会有响应,经测试不是网络原因,配置文件单独放置一个项目没有问题。)

<modules>
    <!-- 注册中心与配置中心使用jhipster2.6.0 -->
    <module>arthur-eureka</module>
    <!-- 配置中心 -->
    <module>arthur-config-center</module>
    <!-- 集中监控断路器仪表盘 -->
    <module>arthur-monitor-turbine</module>
    <!-- zuul网关 -->
    <module>arthur-gateway</module>
    <!-- 后台管理之服务网管理 -->
    <module>arthur-manage-serviceWeb</module>
    <!-- 注册流程 -->
    <module>arthur-manage-registrationProcess</module>
</modules>


最后两个模块主要做模块间通讯测试,没有页面,只是简单的连接了数据库,页面之后会增加。

注册中心eureka主要使用的Jhipster的jhipster-registry-2.6.0版本,最新的版本3.1.0启动异常,貌似是zuul的问题,故采用稳定版,页面比原生的eureka页面要美观一些Jhipster抽空还会介绍,可以生成微服务项目,但是前端采用的angularjs,还在入门,故想集成bootstrap框架,所以子项目自己搭建,只用它的注册中心。如果需要搭建高可用集群注册中心,可部署多台eureka进行以来注册,也就是通常说的服务端的负载均衡。



1.首先介绍下配置中心 arthur-config-center,需要引入spring-cloud-config-server

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
        <version>${springcloudconfig-version}</version>
        <exclusions>
            <exclusion>
                <artifactId>slf4j-api</artifactId>
                <groupId>org.slf4j</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-logging</artifactId>
    </dependency>
</dependencies>
加入EnableConfigServer注解

@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigCenter {

    private static final Logger LOG = LoggerFactory.getLogger(ConfigCenter.class.getName());

    public static void main(String[] args) throws UnknownHostException {
        SpringApplication app = new SpringApplication(ConfigCenter.class);
        //DefaultProfileUtil.addDefaultProfile(app);
        Environment env = app.run(args).getEnvironment();
        LOG.info("\n----------------------------------------------------------\n\t" +
                        "Application '{}' is running! Access URLs:\n\t" +
                        "Local: \t\thttp://127.0.0.1:{} \n\t" +
                        "External: \thttp://{}:{} \n\t" +
                        "Profile(s): \t{}\n----------------------------------------------------------",
                env.getProperty("spring.application.name")+"("+env.getProperty("version")+")",
                env.getProperty("server.port"),
                InetAddress.getLocalHost().getHostAddress(),
                env.getProperty("server.port"),
                env.getProperty("spring.profiles.active"));
    }
}
进行注册,这里采用的高可用配置中心,可部署多台应用,然后依赖的项目跟进注册中心的服务名去读取各自的配置文件

# 注册中心
eureka:
  client:
    enabled: true
    #注册中心开启健康检查会刷新上下文
    healthcheck:
        enabled: false
    fetch-registry: true
    register-with-eureka: true
    instance-info-replication-interval-seconds: 10
    registry-fetch-interval-seconds: 10
    serviceUrl:
        defaultZone: http://${
 eurekaCenter.username}:${
 eurekaCenter.pasword}@localhost:8761/eureka/
配置Git

#配置中心 GitHub配置
cloud:
  config:
    server:
      git:
        uri: https://github.com/ArthurFamily/config-center.git
        search-paths: /config/dev
        strict-host-key-checking: false
启动后访问http://127.0.0.1:10002/arthur-manage-serviceWeb/dev,返回配置文件json, 这里有一个对应规则,对应配置文件中的{服务名-dev(profiles->active的名)},配置文件中放置数据库、redis等配置


2.微服务项目 arthur-manage-serviceWeb 包括mybatis分页插件,自动配置数据源,与arthur-manage-registration 通过fegin进行接口调用,swagger2,ribbon的使用等。

<dependencies>
    <!-- 自动配置,引入后,配置文件里必须配置,用到再引入故不在父级放置 -->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>${springboot-mybatis}</version>
        <exclusions>
            <exclusion>
         
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值