1.eureka的作用
要管理分布式环境下的各个springBoot微服务,就需要服务的注册与发现问题。
既然是注册,必定有个管理注册中心的服务器,每个需要在springcloud管理的服务或者springboot应用被称为client需要在eureka中注册。
springcloud使用eureka server作为注册的服务中心,每个需要访问配置文件的client都通过server进行注册。
因为eureka在后端没有缓存,所有每个client注册后需要像注册中心发送心跳。默认情况下,server也是一个client,必须指定一个server。
server的默认端口为8761。启动eureka server,然后访问http://localhost:8761, 界面如下, "No instances available" 表示无client注册.
pom.xml
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.chry</groupId>
<artifactId>springcloud.helloworld.eureka.server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springcloud.helloworld.Eureka.server</name>
<description>Demo Spring Eureka Server</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--eureka server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- spring boot test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
在主类中添加@EnableEurekaServer
在application.yml中配置eureka
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviveUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
访问localhost:8761, "No instances available" 表示无client注册。
3.另外创建一个maven工程 eureka client
3.1pom配置
3.2application.yml配置
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8762
spring:
application:
name: service-helloworld
3.3.启动类
package springcloud.helloworld.eureka.client;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableEurekaClient
@RestController
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
@Value("${server.port}")
String port;
@RequestMapping("/")
public String home() {
return "hello world from port " + port;26 }
}
3.5访问http://localhost:8761
可以发现 service-helloworld已经注册到server中了
idea代码格式化ctrl+alt+L
二、配置管理
1.我们要搭建一个网站,需要配置数据库连接,指定数据库服务器的IP地址,数据库名称,用户名和口令等信息。通常的方法, 我们可以在一个配置文件中定义这些信息,或者开发一个页面专门配置这些东西。只有一个web服务器的时候, 很方便。但是当需要搭建多台服务器的时候,需要对每台服务器做同样的配置,以至于维护和同步会很麻烦。
2.springcloud的解决方案是:将配置文件放在git上。所有web服务都从git上获得配置文件。
3.首先在git上存放配置文件hello=Hello World from GIT version 5
4.新建maven工程pom包引入
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
5.创建Config Server。使用@EnableConfigServer说明这是一个Config server。也需要将其作为client注册到服务中心
6.
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
server:
port: 8888
spring:
cloud:
config:
server:
git:
uri: https://gitee.com/nameisgood/springcloud/
search-paths: helloworldConfig
username: 867416119@qq.com
password:
application:
name: config-server
7.访问http://localhost:8888/config-client-dev.properties
创建config client读取配置文件
1.配置pom
2.启动类配置@RestController
@Value("${hello}")
String hello;
@RequestMapping(value = "/hello")
public String hello(){
return hello;
}
需要读取配置文件中hello的值,client server向config server提交Rest请求。config server访问git服务器,将获得的配置项返回给client
3.yml
spring:
application:
name: config-client
cloud:
config:
label: master
profile: dev
uri: http://localhost:8888/
server:
port: 8881
profile 采用dev git分支为master uri是config server的地址。
从哪个配置文件获得hello的值?--->spring cloud约定:配置文件命名如下:{application}-{profile}.properties(或者{application}-{profile}.yml)。
同时,有个问题还没解决。当git上的文件改变时,无论如何刷新client端的页面都不能反映配置的变化。
要解决这个问题,就要引入spring cloud的配置自动更新机制。
不更新的原因在于,client只会在应用启动的时候读取一次git的内容。在页面内的刷新当然不会读取到最新的配置信息值。
想要读取最新的配置文件信息只有在应用重启只会,即使是重启config server也不能做到更新client的值。
spring cloud提出的解决方式是:client使用post请求/fresh方法,以此达到刷新配置内容。
1.在client端的pom添加spring-boot-starter-actuator
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2在client的启动类配置@RefreshScope
3.通过post请求访问 /refresh刷新
遇到Unauthorized错误,在配置文件中添加关闭安全验证
刷新成功
访问到最新的值
------------------------------------------------------------------------------------
以上的第一个方法需要手动去访问/refresh方法。如果需要客户端自动触发refresh机制,可以通过git提供的githook来监听push命令来监听事件,并直接调用/refresh方法
3.当config-server搬到另外一个独立ip时候,client必须修改uri地址,这很不方便。
既然他们都通过eureka注册到服务中心,通过服务的发现就可以找到config-server。
client在配置中只需要提供config-server在注册中心注册的名字
将原本通过8888端口访问,变为通过service-id访问