Spring Cloud服务框架版本升级--JDK10+Gradle4.9+Spring Boot 2.0+Finchley.SR1

目标:原有版本升级为Spring Boot 2.0与Spring Cloud Finchley.SR1,使用gradle管理工程,搭建注册、配置、网关与追踪框架,加入k8s api微服务

环境:IntelliJ IDEA

步骤:版本升级及其说明->注册中心框架->配置中心框架->服务网关框架->服务追踪框架->k8s api微服务改造->运行测试

1.版本升级及其说明

版本升级的主要对象:

jdk:1.8 -> 10

maven 3.5.0 -> gradle 4.9

spring boot:1.5.3 -> 2.0.4

spring cloud:Edgware.SR3 -> Finchley.SR1  

2.注册中心框架

注:服务注册中心eureka在jdk10版本下,需要新增配置,才能启动tomcat应用。其原因为jdk10版本默认没有加载JAXB-API。

解决方案:Run -> Edit Configurations 在VM选项增加 --add-modules java.se.ee

注册中心主要工程结构:

build.gradle为项目依赖:以下几个组件不再重复,仅展示增加的部分依赖,compile的部分为依赖

buildscript {
    ext {
        springBootVersion = '2.0.4.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}
 
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
 
group = 'com.boe.cloud'
version = '1.0.0'
sourceCompatibility = 10
 
repositories {
    mavenCentral()
}
 
 
ext {
    springCloudVersion = 'Finchley.SR1'
}
 
dependencies {
 
    compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-server')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}
 
dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

*Application为主程序:使用@EnableEurekaServer注解

package com.boe.cloud.springcloud.eureka;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
 
@SpringBootApplication
@EnableEurekaServer
public class SpringcloudEurekaApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(SpringcloudEurekaApplication.class, args);
    }
}

application.yml为配置文件:设置为本地

server:
  port: 8761
 
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

3.配置中心框架

配置中心工程结构:与注册中心类似

build.gradle为项目配置文件:此处仅展示依赖部分,其他参考注册中心

dependencies {
    compile('org.springframework.cloud:spring-cloud-config-server')
    compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

*Application.java为主程序:使用@EnableEurekaClient启用注册客户端,将自身注册至注册中心,使用@EnableConfigServer启用配置中心服务端

package com.boe.cloud.springcloud.config;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
 
@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class SpringcloudConfigApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(SpringcloudConfigApplication.class, args);
    }
}

application.yml为配置文件:其中git配置为配置文件访问的仓库地址,可设置用户名密码登录

spring:
  application:
    name: Config-Server
  cloud:
    config:
      server:
        git:
          uri: git_url
          search-paths: path
          username: xxxxx
          password: xxxxx
      label: master
server:
  port: 8769
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

4.服务网关框架

网关工程结构:将配置文件application.yml改为bootstrap.yml,该配置在git仓库配置前生效

build.gradle为项目配置文件:此处仅展示依赖部分,其他参考注册中心

dependencies {
    compile('org.springframework.cloud:spring-cloud-starter-config')
    compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')
    compile('org.springframework.cloud:spring-cloud-starter-netflix-zuul')
    compile('org.springframework.cloud:spring-cloud-starter-zipkin')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

*Application.java:主程序,使用@EnableEurekaClient启用注册客户端,将自身注册至注册中心,使用@EnableZuulProxy注解启用网关

package com.boe.cloud.springcloud.zuul;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
 
@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class SpringcloudZuulApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(SpringcloudZuulApplication.class, args);
    }
}

bootstrap.yml配置文件:启动配置,应用相关配置在git仓库

spring:
  application:
    name: Zuul-Gateway
  cloud:
    config:
      profile: dev
      label: master
      discovery:
        enabled: true
        service-id: Config-Server
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

application.yml:git仓库配置文件,此处使用routes功能,对接k8s api微服务,zipkin为追踪服务端

server:
  port: 5000
spring:
  sleuth:
    sampler:
      percentage: 1.0
  zipkin:
    base-url: http://localhost:9411
zuul:
  routes:
    k8sapi:
      path: /cloud/**
      serviceId: Service-Kubernetes

5.服务追踪框架

注:spring boot 2.0版本之后不再支持zipkin开发,改为成熟组件,可以通过jar包或者docker镜像的方式运行

参考:https://zipkin.io/pages/quickstart

在开发主机上以镜像方式运行zipkin server

docker run -d -p 9411:9411 openzipkin/zipkin

6.运行测试

改造k8s api微服务:加入相关依赖,主程序使用@EnableEurekaClient注解与,配置zipkin选项与地址

访问注册中心:http://ip:8761

访问kubernetes的服务,通过服务网关:http://ip:5000/cloud/**

此处使用swagger-ui直接访问:

查看追踪链路:

调用关系:

以上,版本升级与基本框架搭建完成。

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值