添加注册eureka
服务端 ConfigServer
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
spring-cloud-starter-eureka与spring-cloud-starter-eureka-server的区别?
添加EnableDiscoveryClient
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
@EnableDiscoveryClient与@EnableEurekaClient的区别?
application.yml
server:
port: ${PORT:8888} #配置工程端口号
spring:
application:
name: cloud-config-server #设置该服务应用名称
profiles:
active: native #设置读取为本地工程文件
config:
server:
native:
searchLocations: classpath:/config #配置文件根目录,也就是XXX-dev.properties等的目录
#注册到eureka服务中心进行监控
eureka:
client:
serviceUrl:
defaultZone: http://eureka:eureka@localhost:8761/eureka # 可以逗号分隔,配置多个
官网也用发发发发这个端口。
配置文件
新建配置文件:
XXX-dev.properties
XXX-test.properties
客户端AppClient
pom.xml也需要增加eureka
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
bootstrap.properties配置
spring.cloud.config.name=XXX
spring.cloud.config.profile=dev
#spring.cloud.config.profile=test
spring.cloud.config.uri=http://localhost:8888/ # 只能配置一个,不能逗号分隔配置多个config
application.yml配置,和config一样增加eureka
server:
port: 8080 #设置当前服务端口
context-path: /abc #设置服务上下文路径
spring:
application:
name: app-client #service name 设置当前服务名称
eureka:
client:
serviceUrl:
defaultZone: http://eureka:eureka@localhost:8761/eureka # 可以逗号分隔,配置多个
application类增加@EnableEurekaClient
@SpringBootApplication
@EnableEurekaClient
public class ClientApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(ClientApplication.class, args);
}
}