因为我们同时还学习多模块,所以先新建一个项目,然后把其他的东西都删除,值留下一个pom.xml和.md文件。其他的都删除。
:https://start.aliyun.com/
要记得选SpringCloud,此处新建后吧其他的都删除,只留这两个
<properties>
<java.version>1.8java.version>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<spring-boot.version>2.3.7.RELEASEspring-boot.version>
<spring-cloud.version>Hoxton.SR9spring-cloud.version>
properties>
<modules>
<module>eureka-servicemodule>
modules>
# 应用名称
spring:
application:
name: eureka-service
#端口号
server:
port: 8100
eureka:
instance:
# 服务注册中心实例的主机名
hostname: 127.0.0.1
client:
# 是否向服务注册中心注册自己
registerWithEureka: false
# 是否检索服务
fetchRegistry: false
# 服务注册中心的配置内容,指定服务注册中心的位置
serviceUrl:
# 如果配置了密码
# defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:${server.port}/eureka/
#没有密码的配置
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS
package com.example.eurekaservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* @author zhang
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaServiceApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServiceApplication.class, args);
}
}
<module>eureka-configmodule>
server:
port: 8101
# 应用名称
spring:
application:
name: eureka-config
profiles:
active: dev
cloud:
config:
# git 上的仓库分支
label: master
server:
git:
uri: https://gitee.com/zhang_jun_fa/spring-cloud-config.git
#本地路径
basedir: D:\home
# git 的用户名和密码
# password:
# username:
#指定注册中心地址
eureka:
client:
serviceUrl:
defaultZone: http://127.0.0.1:8100/eureka/
instance:
prefer-ip-address: true
package com.example.eurekaconfig;
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
@EnableConfigServer //加入注解声明为配置中心
@EnableEurekaClient
public class EurekaConfigApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaConfigApplication.class, args);
}
}
<module>eureka-adminmodule>
本包的pom中需要引入,用来动态刷新
<!--Spring Boot Actuator,感应服务端变化-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
server:
port: 8102
#指定注册中心地址
eureka:
client:
serviceUrl:
defaultZone: http://127.0.0.1:8100/eureka/
instance:
prefer-ip-address: true
# 应用名称
spring:
application:
name: eureka-admin
profiles:
active: dev
cloud:
config:
profile: dev
label: master #git仓库的分支
uri: http://localhost:8101/
name: eurekaconfig #要读取的Git仓库中的文件名称
#开启刷新端点
management:
endpoints:
web:
exposure:
include:
- refresh # 开启热刷新服务, 也可以在Gitee中的配置文件内定义。
- info
- health
package com.example.eurekaadmin;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @author zhang
*/
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class EurekaAdminApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaAdminApplication.class, args);
}
}
package com.example.eurekaadmin.controller;
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;
/**
* @version 1.0.0
* @className: TestController
* @description:
* @author: zhangjunfa
* @date: 2022/8/22 12:48
*/
@RestController
@RequestMapping
@RefreshScope //热刷新
public class TestController {
@Value("${version}")
private String value;
@GetMapping("/test")
public String getEv() {
return value;
}
}
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'version' in value "${version}"
刚学习的时候,不知道为啥,www.baidu了好久。有说是yml中要加配置的。也有说是版本。但是都不满足我这个地方的BUG。还有说是application.yml文件要命名为bootstrap.yml,然后就没有然后了。启动成功》》》》
还有一个错误,也贴出来:就是我启动后。都注册成功了。然后给我注销了
Saw local status change event StatusChangeEvent [timestamp=1661144320316, current=DOWN, previous=UP]
一百度:说是需要
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
引入这个就可以了。
启动正常:
我们用Postman测试
测试1: 测试读取Eureka-Config配置中心中的数据
测试二:热刷新
我们在 版本后面修改为test
至此,我们的SpringCloud的Eureka发现服务和注册服务都完美运行了。还有SpringCloud的Config配置中心也完美运行。后续可以加入SpringCloud的openfeign来实现远程调用。
Config配置中心配置文件Gitte: https://gitee.com/zhang_jun_fa/spring-cloud-config.git
本项目Gitte:https://gitee.com/zhang_jun_fa/ross-eureka.git