9.8.Config整合Eureka
Config 整合Eureka,是需要将Config Server和Config Client都整合到Eureka,然后Config Client通过服务发现,找到Config Server,从而连接Config Server,获取配置信息。
1、创建两个项目:
futurecloud-config-client-eureka
futurecloud-config-server-eureka
分别是Config Client和Config Server。两个项目都引入eureka的客户端依赖
futurecloud-config-client-eureka项目的依赖如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--Spring Cloud Config 客户端依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<!--Spring Boot Actuator ,监控服务端变化-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--添加eureka 客户端依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
futurecloud-config-server-eureka项目的依赖如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--引入spring cloud config server依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<!--添加安全认证-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-security</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<!--添加eureka 客户端依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<!--Spring Boot Actuator ,监控服务端变化-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2、配置文件
(1)Config Client客户端配置
futurecloud-config-client-eureka项目的配置文件如下,
注意,注册到eureka的配置信息必须配置在bootstrap.yml中,
否则futurecloud-config-client-eureka项目在启动时,找不到服务futurecloud-config-server-eureka而报错:
java.lang.IllegalStateException: No instances found of configserver (futurecloud-config-server-eureka)
bootstrap.yml的配置如下:
spring:
cloud:
config:
name: futurecloud-config
profile: dev #指定的环境
label: master #指定分支,当使用git的时候,默认是master
discovery: #服务发现,原本是在application.yml中配置,但由于项目启动过程中,需要访问Config Server的配置(Controller配置有加载变量),所以就配置在这里
enabled: true
service-id: futurecloud-config-server-eureka
#如果service-id 配置来看安全认证,这里要配置认证的用户名、密码后才能访问
username: user
password: 123
#将此服务注册到eureka
eureka:
client:
serviceUrl:
defaultZone: http://user:123@localhost:10000/eureka
instance:
prefer-ip-address: true
application.yml的配置如下,也可以配置到bootstrap.yml中
server:
port: 7004 #程序启动端口,也就是tomcat的端口
spring:
application:
name: futurecloud-config-client-eureka #应用名称,别名
(1)Config Server服务端配置
futurecloud-config-server-eureka项目的配置文件如下:
server:
port: 7003 #程序启动端口,也就是tomcat的端口
spring:
application:
name: futurecloud-config-server-eureka #应用名称,别名
cloud:
config:
server:
git:
uri: https://gitee.com/makyan/futurecloud-config # 默认访问路径
security:
basic:
enabled: true # 开启安全认证
user:
name: user #登录用户名
password: 123 #登录密码
#将此服务注册到eureka
eureka:
client:
serviceUrl:
defaultZone: http://user:123@localhost:10000/eureka
instance:
prefer-ip-address: true
3、Spirng boot main 启动类
futurecloud-config-client-eureka 主类增加配置@EnableEurekaClient,具体如下:
package com.futurecloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class FutruecloudConfigClientEurekaApplication
{
public static void main( String[] args )
{
SpringApplication.run(FutruecloudConfigClientEurekaApplication.class);
}
}
futurecloud-config-server-eureka 主类需要增加两个配置:
@EnableConfigServer
@EnableEurekaClient
具体如下:
package com.futurecloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class FutruecloudConfigServerEurekaApplication
{
public static void main( String[] args )
{
SpringApplication.run(FutruecloudConfigServerEurekaApplication.class);
}
}
futurecloud-config-client-eureka 的controller如下:
package com.futurecloud.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.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConfigClientEurekaController {
/**
* from 这个属性值,是配置在gitee上的配置文件中的属性
*/
@Value("${from}")
private String fromValue;
/**
* 返回配置文件中的值
* @return
*/
@GetMapping("/api/from")
@ResponseBody
public String getFromValue(){
return fromValue;
}
}
依次启动项目:
futurecloud-service
futurecloud-config-server-eureka
futurecloud-config-client-eureka
访问:http://localhost:7004/api/from ,返回如下图: