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.启动工程,我们可以在服务注册中心看到我们注册的服务

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

 

小舟从此逝,江海寄余生。 --狐狸
优秀的个人博客,低调大师

微信关注我们

原文链接:https://yq.aliyun.com/articles/651185

转载内容版权归作者及来源网站所有!

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

相关文章

发表评论

资源下载

更多资源
Oracle Database,又名Oracle RDBMS

Oracle Database,又名Oracle RDBMS

Oracle Database,又名Oracle RDBMS,或简称Oracle。是甲骨文公司的一款关系数据库管理系统。它是在数据库领域一直处于领先地位的产品。可以说Oracle数据库系统是目前世界上流行的关系数据库管理系统,系统可移植性好、使用方便、功能强,适用于各类大、中、小、微机环境。它是一种高效率、可靠性好的、适应高吞吐量的数据库方案。

Apache Tomcat7、8、9(Java Web服务器)

Apache Tomcat7、8、9(Java Web服务器)

Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache、Sun 和其他一些公司及个人共同开发而成。因为Tomcat 技术先进、性能稳定,而且免费,因而深受Java 爱好者的喜爱并得到了部分软件开发商的认可,成为目前比较流行的Web 应用服务器。

Eclipse(集成开发环境)

Eclipse(集成开发环境)

Eclipse 是一个开放源代码的、基于Java的可扩展开发平台。就其本身而言,它只是一个框架和一组服务,用于通过插件组件构建开发环境。幸运的是,Eclipse 附带了一个标准的插件集,包括Java开发工具(Java Development Kit,JDK)。

Java Development Kit(Java开发工具)

Java Development Kit(Java开发工具)

JDK是 Java 语言的软件开发工具包,主要用于移动设备、嵌入式设备上的java应用程序。JDK是整个java开发的核心,它包含了JAVA的运行环境(JVM+Java系统类库)和JAVA工具。