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

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

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

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

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

 <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency> <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>${spring-cloud.version}</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>

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

 server.port=9000

 spring.application.name=eureka-server
spring.profiles.active=dev eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.server.enable-self-preservation=false

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

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

 package com.example.enrekaserver;

 import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer
@SpringBootApplication
public class EnrekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EnrekaServerApplication.class, args);
}
}

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

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

 package com.example.demoOne;

 import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient
@SpringBootApplication
public class DemoOneApplication {
public static void main(String[] args) {
SpringApplication.run(DemoOneApplication.class, args);
}
}

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

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

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

 package com.example.demoOne.didispace;

 import org.springframework.beans.factory.annotation.Autowired;

 import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import java.util.logging.Logger; @RestController
public class HelloController {
private final Logger logger=Logger.getLogger(String.valueOf(getClass())); @Autowired
private DiscoveryClient client; @RequestMapping(value = "/hello",method = RequestMethod.POST)
public String index(){
ServiceInstance instance = (ServiceInstance) client.getServices();
logger.info("/hello,host:"+instance.getHost()+",service_id:"+instance.getServiceId());
return "hello world";
}
}

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

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

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

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

spring cloud(学习笔记) Enreka服务治理的更多相关文章

  1. Spring Cloud 学习笔记(一)——入门、特征、配置

    [TOC] 0 放在前面 0.1 参考文档 http://cloud.spring.io/spring-cloud-static/Brixton.SR7/ https://springcloud.cc ...

  2. Spring Cloud学习笔记-005

    服务消费者 之前已经搭建好了微服务中的核心组件——服务注册中心(包括单节点模式和高可用模式).也有了服务提供者,接下来搭建一个服务消费者,它主要完成两个目标,发现服务以及消费服务.其中,服务发现的任务 ...

  3. Spring Cloud学习笔记【一】Eureka服务注册与发现

    Spring Cloud Eureka 是 Spring Cloud Netflix 微服务套件的一部分,基于 Netflix Eureka 做了二次封装,主要负责完成微服务架构中的服务治理功能,服务 ...

  4. Spring Cloud学习笔记之微服务架构

    目录 什么是微服务 架构优点 架构的挑战 设计原则 什么是微服务     微服务构架方法是以开发一种小型服务的方式,来开发一个独立的应用系统的.     其中每个小型服务都运行在自己的进程中,并经常采 ...

  5. Spring Cloud学习笔记【八】服务网关 Zuul(过滤器)

    在上篇文章中我们了解了 Spring Cloud Zuul 作为网关所具备的最基本功能:路由(Router),下面我们将关注 Spring Cloud Zuul 的另一核心功能:过滤器(Filter) ...

  6. Spring Cloud学习笔记【七】服务网关 Zuul(路由)

    Spring Cloud Zuul 路由是微服务架构的不可或缺的一部分,提供动态路由.监控.弹性.安全等的边缘服务.Zuul 是 Netflix 出品的一个基于 JVM 路由和服务端的负载均衡器. 准 ...

  7. spring cloud学习笔记五 网关服务zuul

    网关服务是指,客户端发送的请求不用直接访问特定的微服务接口,而且是经过网关服务的接口进行交互,网关服务再去到特定的微服务中进行调用.   网关服务的路由功能和Nginx的反向代理一样,所有的服务都先会 ...

  8. Spring Cloud学习笔记--Spring Boot初次搭建

    1. Spring Boot简介 初次接触Spring的时候,我感觉这是一个很难接触的框架,因为其庞杂的配置文件,我最不喜欢的就是xml文件,这种文件的可读性很不好.所以很久以来我的Spring学习都 ...

  9. Spring Cloud系列之Eureka服务治理

    写在前面 Spring Cloud Eureka是基于Netflix Eureka做的二次封装.主要包含两部分: 服务注册中心 eureka server 服务提供者 eureka client ps ...

  10. Spring Cloud学习笔记-002

    搭建Spring Cloud注册中心:Eureka 服务注册:在服务治理框架中,通常都会构建一个注册中心,每个服务单元向注册中心登记自己提供的服务,将主机与端口号.版本号.通信协议等一些附加信息告诉注 ...

随机推荐

  1. 使用bcftools提取指定样本的vcf文件(extract specified samples in vcf format)

    1.下载安装bcftools. 2.准备样本ID文件,这里命名为samplelistname.txt,一个样本一行,如下所示: sample1 sample2 sample3 3.输入命令: bcft ...

  2. python之OpenCv

    1.安装 pip install opencv-python 2.初步简绍 创建窗口显示图像 cv2.namedWindow(窗口名称[,窗口标识]) 例如:cv2.namedWindow(" ...

  3. python类继承的重写和super

    给已经存在的类添加新的行为,继承是非常好的实现方式.但是如果要改变行为呢?比如在Python继承扩展内置类,我们的contact类只允许一个名字和一个邮箱,但是如果要对某些人增加电话号码呢?这里可以通 ...

  4. jmeter sampler maven项目排错记

    eclipse 创建的maven项目,引入jar包之后出现红色叹号,一直找不到原因,连main方法都无法运行,提示找不到类: 错误: 找不到或无法加载主类 soapsampler.SoapSample ...

  5. django 跨域解决方案

    使用django-cors-headers模块 github:https://github.com/ottoyiu/django-cors-headers 官方文档中有详细说明 简要配置 1.安装 p ...

  6. java io系列02之 ByteArrayInputStream的简介,源码分析和示例(包括InputStream)

    我们以ByteArrayInputStream,拉开对字节类型的“输入流”的学习序幕.本章,我们会先对ByteArrayInputStream进行介绍,然后深入了解一下它的源码,最后通过示例来掌握它的 ...

  7. JAVA-集合类型List(ArrayList、LinkedList)常用操作例子(基础必备)

    package com.net.xinfang.reflect; import java.util.ArrayList; import java.util.Arrays; import java.ut ...

  8. 使用List和Map遇到得空指针异常

    如题,经常遇到同类得问题,因为集合在使用之前必须先new一个对象. private List<Orderdatil> orderdatilList=new ArrayList<Ord ...

  9. css 绝对定位实现水平垂直居中

    负margin实现水平垂直居中 width: 500px; height: 500px; position: absolute; left: 50%; top :50%; margin-left: - ...

  10. go结构体方法

    Golang中的方法是作用在特定类型的变量上,因此自定义类型,都可以有方法,而不仅仅是struct. 定义格式 func (var *Struct_Name) FuncName( var0, var1 ...