1 2 3 4

spring cloud(学习笔记) Enreka服务治理

 

服务治理是微服务架构最为核心和基础的模块,主要用来实现各个微服务实例的自动化注册和发现。

记录一下服务注册中心的搭建以及高可用注册中心的实现

1.首先创建两个基础 的spring boot工程,spring boot创建工程的网站:http://start.spring.io/,创建界面如下

2.解压工程,用Maven的形式导入工程(File->new->project from Existing Soures,选择解压工程导入)

3.两个工程中都添加依赖(eureka)

复制代码
 1 <properties>
 2         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 3         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
 4         <java.version>1.8</java.version>
 5         <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
 6     </properties>
 7 
 8     <dependencies>
 9         <dependency>
10             <groupId>org.springframework.boot</groupId>
11             <artifactId>spring-boot-starter-web</artifactId>
12         </dependency>
13         <dependency>
14             <groupId>org.springframework.cloud</groupId>
15             <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
16         </dependency>
17 
18         <dependency>
19             <groupId>org.springframework.boot</groupId>
20             <artifactId>spring-boot-starter-test</artifactId>
21             <scope>test</scope>
22         </dependency>
23     </dependencies>
24 
25     <dependencyManagement>
26         <dependencies>
27             <dependency>
28                 <groupId>org.springframework.cloud</groupId>
29                 <artifactId>spring-cloud-dependencies</artifactId>
30                 <version>${spring-cloud.version}</version>
31                 <type>pom</type>
32                 <scope>import</scope>
33             </dependency>
34         </dependencies>
35     </dependencyManagement>
36 
37     <build>
38         <plugins>
39             <plugin>
40                 <groupId>org.springframework.boot</groupId>
41                 <artifactId>spring-boot-maven-plugin</artifactId>
42             </plugin>
43         </plugins>
44     </build>
复制代码

4.在注册中心工程配置文件中添加服务

复制代码
1 server.port=9000
2 
3 spring.application.name=eureka-server
4 spring.profiles.active=dev
5 
6 eureka.instance.hostname=localhost
7 eureka.client.register-with-eureka=false
8 eureka.client.fetch-registry=false
9 eureka.server.enable-self-preservation=false
复制代码

不知道这些参数的自己百度

5.在注册中心的入口类中加入注解@EnableEurekaServer

复制代码
 1 package com.example.enrekaserver;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
 6 
 7 @EnableEurekaServer
 8 @SpringBootApplication
 9 public class EnrekaServerApplication {
10     public static void main(String[] args) {
11         SpringApplication.run(EnrekaServerApplication.class, args);
12     }
13 }
复制代码

6.启动注册中心,在浏览器中访问,界面如下

 7.在另一个工程中,加入注解 @EnableDiscoveryClient

复制代码
 1 package com.example.demoOne;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
 6 
 7 @EnableDiscoveryClient
 8 @SpringBootApplication
 9 public class DemoOneApplication {
10     public static void main(String[] args) {
11         SpringApplication.run(DemoOneApplication.class, args);
12     }
13 }
复制代码

8.配置文件中加入配置,指定服务注册中心

1 spring.application.name=demoOne-service
2 spring.profiles.active=dev
3 eureka.client.service-url.defaultZone=http://localhost:9000/eureka/

9.我们可以在需要注册是工程里添加一个类作为测试,并打印日志

复制代码
 1 package com.example.demoOne.didispace;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 
 5 import org.springframework.cloud.client.ServiceInstance;
 6 import org.springframework.cloud.client.discovery.DiscoveryClient;
 7 import org.springframework.web.bind.annotation.RequestMapping;
 8 import org.springframework.web.bind.annotation.RequestMethod;
 9 import org.springframework.web.bind.annotation.RestController;
10 
11 import java.util.logging.Logger;
12 
13 @RestController
14 public class HelloController {
15     private  final Logger logger=Logger.getLogger(String.valueOf(getClass()));
16 
17     @Autowired
18     private DiscoveryClient client;
19 
20     @RequestMapping(value = "/hello",method = RequestMethod.POST)
21     public String index(){
22         ServiceInstance instance = (ServiceInstance) client.getServices();
23         logger.info("/hello,host:"+instance.getHost()+",service_id:"+instance.getServiceId());
24         return "hello world";
25     }
26 }
复制代码

10.启动工程,我们可以在服务注册中心看到我们注册的服务

注册中心搭建成功。可以测试一下试试

 

 

 

欢迎大家关注公众号,不定时干货,只做有价值的输出

 

作者:Dawnzhang 
出处:https://www.cnblogs.com/clwydjgs/ 
版权:本文版权归作者
转载:欢迎转载,但未经作者同意,必须保留此段声明;必须在文章中给出原文连接;否则必究法律责任

posted @   Dawnzhang  阅读(710)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述
PV
点击右上角即可分享
微信分享提示