SpringCloud之Eureka注册中心搭建
Spring Cloud Eureka 是Spring Cloud Netfix 微服务套件的一部分,基于 Netfix Eureka做了二次封装,主要负责实现微服务架构中的服务治理功能 。
一、服务治理
1. 服务注册:
在服务治理框架中,通常都会构建一个注册中心,每个服务但愿向注册中心登记自己提供的服务,将主机与端口号、版本号、通信协议等一些附加信息告知注册中心,注册中心按服务名分类组织服务清单。另外,服务注册中心还需要以心跳的方式去检测清单的服务是否可用,若不可用需要从服务清单中剔除,达到排除故障服务的效果。
2. 服务发现:
由于在服务治理框架下运作,服务间的调用不在通过指定具体的实例地址来实现, 而是通过向服务名发起请求调用实现。所以,服务调用房在调用提供方的借口的时候,并不知道具体的服务实例的位置。因此,调用方需要向服务注册中心咨询服务,冰获取所有服务的实例清单,以实现对具体服务实例的访问。
二、编写注册中心服务
1. 添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
注意:spring-cloud-dependencies 的版本一定要和spring-boot的版本一致,我使用的spring-boot的版本是 2.3.0.RELEASE
详细的版本对应在这个链接中 https://start.spring.io/actuator/info 。
2. 在启动类上添加 @EnableEurekaServer 注解
3. 在配置文件中添加如下配置:
#设置端口号
server.port=8761
#由于该应用为注册中心,所以设置为false,代表该不向注册中心注册自己
eureka.client.register-with-eureka=false
#由于注册中心的职责是就是维护实例,它并不需要去检索服务,所以也要是这只为false
eureka.client.fetch-registry=false
注意:eureka.client.register-with-eureka 一定要设置为false,不然启动的时候会 把自己当做客户端向自己注册,会报错。
4. 启动服务会看到下面的界面:
看到上面的界面就代表了成功了。
如果看到下面这个界面,并报“org.springframework.web.servlet.NoHandlerFoundException: No handler found for GET /eureka/css/wro.css”这个错误,需要进行下面的排查:
a. 是否在启动类中加了 @EnableConfigServer 注解,如果加了,在配置文件中添加这个配置 spring.cloud.config.server.prefix=/config。
b. 如果没有添加 @EnableConfigServer 注解,请在配置文件中检查是否配置了这个 spring.resources.add-mappings=false,如果有配置注释掉就好了。
二、编写服务提供者
1. 添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2. 在启动类中添加 @EnableDiscoveryClient 注解。
3. 在配置文件中添加下面的配置
spring.application.name=eureka-client-user-service
server.port=8081
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
eureka.client.serviceUrl.defaultZone 的地址就是我们之前启动的 Eureka 服务的地址,在程序启动的时候需要将自身的信息注册到 Eureka 中。
4. 编写提供接口
@GetMapping("/user/hello")
public String sayHello(){
return "hello everyOne";
}
5. 启动应用,在 Eureka 的控制台中看到下面的信息就证明成功啦。
注意:一定要引入下面的这个依赖 不然程序启动后会自动停止。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
三、编写服务消费者
1.引入的依赖和配置
我们需要编写一个服务消费者,消费我们刚刚服务提供者的"/user/hello"的接口,其中引入的依赖和启动类添加的注解都和服务提供者的一样。唯一不同的就是配置文件中的配置:
spring.application.name=eureka-client-article-service
server.port=8082
eureka.client.serviceUrl.defaultZone=http://ocalhost:8761/eureka/
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
2. 编写消费的接口:
RestTemplate 是Spring提供的用于访问Rest服务的客户端。RestTemplate 提供了多种便捷访问远程Http 服务的方法,能够大大的提高客户端的编写效率。我们可以通过配置 RestTemplate 来调用接口。
a. RestTemplate 配置
@Configuration
public class BeanConfiguration {
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
b. 调用接口
@GetMapping("/article/callhello")
public String callHello(){
return restTemplate.getForObject("http://localhost:8081/user/hello",String.class);
}
启动应用后通过访问 即可 http://localhost:8082/article/callhello 即可
通过 Eureka 来消费接口
a. RestTemplate 配置
@Configuration
public class BeanConfiguration {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
b. 调用接口
@GetMapping("/article/callhello2")
public String callHello2(){
return restTemplate.getForObject("http://eureka-client-user-service/user/hello",String.class);
}
启动应用后通过访问 即可 http://localhost:8082/article/callhello2 即可
到此为止服务消费也搭建成功了
四、开启 Eureka 认证
1. 添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
2. 配置文件中加入如下的配置
spring.security.user.name=用户名
spring.security.user.password=密码
3. 添加Security配置类
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
http.authorizeRequests().
anyRequest().authenticated().and().httpBasic();
}
}
重新启动后,访问localhost:8761 ,看到下图中的页面代表成功了
同时客户端的注册也要加上用户名和密码的信息例如:
eureka.client.serviceUrl.defaultZone=http://用户名:密码@localhost:8761/eureka/
五、关闭自我保护:
在配置文件中添加下面的配置即可:
eureka.server.enable-self-preservation=false
六、自定义实例id
在客户端注册的配置文件中加入下面的配置:
eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
七、自定义实例跳转链接
在客户端注册的配置文件中加入下面的配置:
eureka.instance.status-page-url=http://localhost:8081/user/hello
在下图所示的地方可以点击进入:
下一篇: 关于java的数组