ubuntu14.04 spring cloud config server + gradle搭建


Server端:
在eclipse上,创建Java Project项目。自带的src包删掉手动建文件夹。基础的目录文件都创建上

|--ZSpringCloud
|--build.gradle
|----src
|------main
|--------java
|--------resources

配置build.gradle文件:

buildscript {
  repositories {
    mavenLocal()
    mavenCentral()
  }
  dependencies {
    classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.7.RELEASE")
    classpath("org.springframework:springloaded:1.2.0.RELEASE")
    classpath("nz.org.geonet:gradle-build-version-plugin:1.0.4")
    classpath("io.spring.gradle:dependency-management-plugin:0.5.6.RELEASE")
  }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'io.spring.dependency-management'

jar {
  baseName = 'spring-cloud-service'
  version =  '0.0.1'
}

repositories {
  maven { url "https://repo.spring.io/libs-milestone" }
mavenLocal() mavenCentral() } dependencyManagement { imports { mavenBom
'org.springframework.cloud:spring-cloud-netflix:1.2.0.M1' mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Camden.SR2' } } sourceCompatibility = 1.8 targetCompatibility = 1.8 dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile("org.springframework.cloud:spring-cloud-starter-parent:Brixton.SR4") compile("org.springframework.boot:spring-boot-starter-security") compile("org.springframework.cloud:spring-cloud-config-server") compile("org.springframework.boot:spring-boot-starter-web") compile("org.springframework.boot:spring-boot-starter-actuator") }

当时报了很多错,从晚上找了很多东西,不记得哪个是解决问题的了,所有的依赖都粘上了应该有没用的。
现在已经有了空文件夹,和一个写好的build.gradle文件,打开一个终端进入ZSpringCloud目录。

gradle eclipse

eclipse调成Spring的视图看的更舒服写。执行完成后这个样子(现在还没有build文件夹),确保项目没有一个大感叹号。

在src/main/java里创建main类(Application名字随便起),先建个空的。

public class Application {
  public static void main(String[] args) {
  }
}

现在有程序入口,在终端执行

gradle build
gradle eclipse

确保执行成功,中间应该不会出错了,出错搜一下。
修改Application类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableAutoConfiguration
@EnableConfigServer
public class Application {
  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
}

在src/main/resources里创建两个文件,application.properties和bootstarp.properties

#<application.properties>
server.port=8888             #Server跑的端口 security.basic.enabled=false      #这两个spring验证的,不加这个两个会报一个
management.security.enabled=false   #Spring的安全的什么错误,应该能解决,但是现在先不管。如果不加访问的时候需要用户名和密码,这个用户和密码不知道在哪
#<bootstrap.properties>
spring.cloud.config.server.git.uri=git@192.168.1.6:fzk/SpringCloudTest.git    #是从哪个地方获取配置文件,ssh路径的跑通了,http待研究 #spring.cloud.config.server.git.uri=http://192.168.1.6/root/SpringCloudTest.git spring.cloud.config.server.git.searchPaths=repo                    #这个是进入上面的路径后,从哪个文件夹搜索文件 #spring.application.name=configserver                           #spring.application.label=master                             #label名,分支名,如果写了,以后在获取的时候需要加上{label}

现在就已经能启动项目了,先启动一下看看哪里报错了。没报错先放着,还有一大堆服务器的东西需要配置。

我自己在另外一个虚拟机上搭了一个gitlab,这个可以不用自己搭,随便找个gitlab库能放代码就可以了。如果自己搭了最好设成静态ip。(虚拟机ip:192.168.1.6)

在gitlab上建一个存配置文件的项目<SpringCloudTest>,在本地建一个SpringCloudTest的文件夹,在里面建一个repo文件夹,repo里建一个fzk-beta.properties文件
|SpringCloudTest
|--repo
|----fzk-beta.properties

把这个项目推上gitlab建的项目上,从本地文件夹push的方法在gitlab剪完项目后最下面的那块告诉怎么弄了。
gitlab剪完项目后点击项目名就看见两个地址,ssh和http,就是在Server端 bootstrap.properties的 spring.cloud.config.server.git.uri 配置的路径。
现在项目已经建完了,开始服务器的配置。现在有两个服务器,一个是跑Server的A,一个gitlab的B。
在A上,用什么用户跑这个程序就用什么用户生成一对秘钥,我全是用普通用户跑的。在A上执行

ssh-keygen -t rsa

在用户的~/.ssh/ 目录下会生成一对秘钥,id_rsa    id_rsa.pub。把id_rsa.pub的内容复制到gitlab项目的Deploy Keys中
Deploy Keys的位置在:在浏览器上找到你的那个项目,右上角有个一看就是设置的按钮,点开就能找到Deploy Keys ,点击进入title就是随便起的名,下面大的文本框里粘贴上刚才从公钥中复制的内容。确保最下面秘钥列表中刚才弄的那个公钥是enable的。默认就是。

在A上通过ssh 链接一下gitlab的服务器,保证A上能识别这个ip。问是不是永久储存在knowhosts中,yes。

ssh root@192.168.1.6

如果现在就开始访问,还是不成功,说什么不识别B的ip或者秘钥不对之类的错误。
在A上进入 ~/.ssh/  目录,vim config。把下面内容复制上,改一下。

 

Host 192.168.1.6                #B的域名或ip
RSAAuthentication yes              #是否公钥验证
StrictHostKeyChecking no            #看这个意思应该是什么严格的验证之类的
IdentityFile /home/fzk/.ssh/id_rsa      #通过公钥验证的私钥的位置
User fzk                     #在gitlab上注册的用户名,登陆的时候的那个

 

好,现在就已经全部完事了。
在浏览器上输入:
http://localhost:8888/fzk/beta

{"name":"fzk","profiles":["beta"],"label":"master","version":"ebcf17eac0699644200aca3b8b0d1a23563ce242","state":null,"propertySources":[{"name":"git@192.168.1.6:fzk/SpringCloudTest.git/repo/fzk-beta.properties","source":{"server.port":"8013","fzk.nick":"badboy"}}]}

http://localhost:8888/fzk-beta.properties

fzk.nick: badboy
server.port: 8013
访问格式:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM