Commit 56215815 authored by duanledexianxianxian's avatar duanledexianxianxian 😁

add config server

parent d8c43bc0
Pipeline #107 failed
......@@ -29,11 +29,24 @@ springboot与spring cloud兼容版本
### 问题
1.Eureka控制台出现
EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.
1. Eureka控制台出现
EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.
说明Eureka已经进入了保护模式。
2. @value 如果配置文件中没有这个配置项且没有指定默认值,则会报错:
Could not resolve placeholder 'xxx' in value "${aaa.bbb.ccc}"
@value指定默认的方法
```
@Value("${aaa.bbb.ccc:DefaultValue}")
```
3. docker 安装ELK(https://www.jianshu.com/p/f4812c21d6a8)
### 参考
1. https://windmt.com/2018/04/14/spring-cloud-1-services-governance/
2. https://blog.csdn.net/libertine1993/article/details/80765886
......@@ -17,6 +17,8 @@
<spring.cloud.version>Greenwich.RELEASE</spring.cloud.version>
<java.version>1.8</java.version>
<lombok.version>1.18.8</lombok.version>
<encoding.reporting>UTF-8</encoding.reporting>
<java.encoding>UTF-8</java.encoding>
</properties>
<dependencyManagement>
......@@ -53,6 +55,21 @@
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.version}</version>
</plugin>
<!--The Compiler Plugin is used to compile the sources of your project.-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<compilerVersion>${java.version}</compilerVersion>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>${java.encoding}</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
......@@ -17,9 +17,13 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
</project>
......@@ -13,7 +13,7 @@ import org.springframework.cloud.config.server.EnableConfigServer;
*/
@SpringBootApplication
@EnableConfigServer
public class SpringConfigApplication {
public class SpringConfigServiceApplication {
/**
* The entry point of application.
......@@ -21,6 +21,6 @@ public class SpringConfigApplication {
* @param args the input arguments
*/
public static void main(String[] args) {
SpringApplication.run(SpringConfigApplication.class, args);
SpringApplication.run(SpringConfigServiceApplication.class, args);
}
}
server:
# 服务器端口
port: 9300
eureka:
client:
service-url:
defaultZone: http://localhost:9000/eureka/
spring:
application:
......@@ -10,7 +14,7 @@ spring:
server:
git:
# 配置git仓库的地址
uri: http://platform.kuopu.net:9999/gitlab/duanledexianxianxian/spring-cloud-learn
uri: http://platform.kuopu.net:9999/gitlab/duanledexianxianxian/spring-cloud-learn.git
# git仓库地址下的相对地址,可以配置多个,用,分割。
search-paths: spring-cloud-hello-world/config-repo
skip-ssl-validation: false
<?xml version="1.0" encoding="UTF-8"?>
<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>spring-cloud-hello-world</artifactId>
<groupId>com.duanledexian.spring.cloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>order-service</artifactId>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
package com.duanledexian.spring.cloud.order;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* 服务提供方
*
* @author Administrator
*/
@SpringBootApplication
@EnableEurekaClient
public class SpringOrderServiceApplication {
public static void main(String[] args) {
SpringApplication.run(SpringOrderServiceApplication.class, args);
}
}
package com.duanledexian.spring.cloud.order.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author Administrator
* <p>需要给加载变量的类上面加载 @RefreshScope,在客户端执行 /actuator/refresh 的时候就会更新此类下面的变量值。</p>
*/
@RestController
@RequestMapping("/api/v1")
@RefreshScope
@Slf4j
public class HomeController {
@Value("${hello.name:world}")
private String name;
@GetMapping("/sayHello")
public String sayHello() {
return "order-service hello " + name;
}
}
server:
# 服务器端口
port: 9204
hello:
name: "order application default name"
spring:
application:
name: order-service
management:
endpoints:
web:
exposure:
include: refresh,info
spring:
cloud:
config:
# 配置中心的具体地址,即 config-server
# uri: http://localhost:9300
# 对应 {application} 部分
name: order-service
# 对应 {profile} 部分
profile: dev
# 对应 {label} 部分,即 Git 的分支。如果配置中心使用的是本地存储,则该参数无用
label: master
discovery:
enabled: true
service-id: config-server
eureka:
client:
service-url:
defaultZone: http://localhost:9000/eureka/
......@@ -18,6 +18,8 @@
<module>hello-consumer-ribbon</module>
<module>hello-consumer-feign</module>
<module>hello-config-server</module>
<module>user-service</module>
<module>order-service</module>
</modules>
<dependencies>
......
<?xml version="1.0" encoding="UTF-8"?>
<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>spring-cloud-hello-world</artifactId>
<groupId>com.duanledexian.spring.cloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>user-service</artifactId>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
package com.duanledexian.spring.cloud.user;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 服务提供方
*
* @author Administrator
*/
@SpringBootApplication
public class SpringUserServiceApplication {
public static void main(String[] args) {
SpringApplication.run(SpringUserServiceApplication.class, args);
}
}
package com.duanledexian.spring.cloud.user.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author Administrator
* <p>需要给加载变量的类上面加载 @RefreshScope,在客户端执行 /actuator/refresh 的时候就会更新此类下面的变量值。</p>
*/
@RestController
@RequestMapping("/api/v1")
@RefreshScope
@Slf4j
public class HomeController {
@Value("${hello.name:world}")
private String name;
@GetMapping("/sayHello")
public String sayHello() {
return "hello " + name;
}
}
server:
# 服务器端口
port: 9203
hello:
name: "application default name"
spring:
application:
name: user-service
management:
endpoints:
web:
exposure:
include: refresh
spring:
cloud:
config:
# 配置中心的具体地址,即 config-server
# uri: http://localhost:9300
# 对应 {application} 部分
name: user-service
# 对应 {profile} 部分
profile: dev
# 对应 {label} 部分,即 Git 的分支。如果配置中心使用的是本地存储,则该参数无用
label: master
discovery:
enabled: true
service-id: config-server
eureka:
client:
service-url:
defaultZone: http://localhost:9000/eureka/
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment