《SpringCloud微服务实战》学习笔记之服务治理Eureka(单节点)
来源:互联网 发布:电磁场仿真软件 编辑:程序博客网 时间:2024/06/08 04:27
Euraka是NetFlix微服务套件中的一部分,它基于Netflix Eureka做了二次封装,主要负责完成微服务架构中的服务治理和服务发现功能。
示例:
一、Eureka搭建服务注册
1、创建maven项目SpringCloud-EurekaServer
2、修改pom.xml文件,pom中添加Euraka依赖jar包。
<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.spring</groupId> <artifactId>SpringCloud-EurekaServer</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>SpringCloud-EurekaServer</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Brixton.RELEASE</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></project>
3、创建application.propertises文件
server.port=8888eureka.client.register-with-eureka=falseeureka.client.fetch-registry=falseeureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
- server.port:Eureka服务注册端口。
- eureka.client.register-with-eureka:设置为false,代表不向注册中心注册自己。
eureka.client.fetch-registry:设置为false,代表不去检索服务。
通过http://localhost:8888/访问注册中心:
由于该注册中心还没注册任何服务,所以Instances currently registered with Eureka栏为“No instances available”。
二、在服务注册中心注册helloService服务
1、创建Maven项目SpringCloud-Service
2、修改pom.xml,添加Euraka和SpringBoot依赖
<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.spring</groupId> <artifactId>SpringCloud-Service</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>SpringCloud-Service</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Brixton.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement></project>
3、新增application.propertises文件
server.port=9999spring.application.name=helloServiceeureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/
- server.port:helloService服务端口。
- spring.application.name:向服务注册中心注册的服务名。
- eureka.client.serviceUrl.defaultZone:eureka服务注册中心Url。
4、实现HelloServiceController
package com.spring.springcloud.rest;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;@RestControllerpublic class HelloServiceController { @Autowired private DiscoveryClient client; @RequestMapping(value="/hello",method=RequestMethod.GET) public String greeting(){ ServiceInstance si = client.getLocalServiceInstance(); return "hello"; }}
5、最后向Application主类中添加注解@EnableDiscoveryClient,用于激活Eureka中的EnableDiscoveryClient实现。
package com.spring.springcloud;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@EnableDiscoveryClient@SpringBootApplicationpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}
运行HelloService注册服务,启动成功之后,访问注册中心:http://localhost:8888/
此时我们看见helloService已经成功注册。
阅读全文
0 0
- 《SpringCloud微服务实战》学习笔记之服务治理Eureka(单节点)
- 《SpringCloud微服务实战》学习笔记之网关服务zuul
- 《SpringCloud微服务实战》学习笔记之服务消费者Ribbon+RestTemplate
- 《SpringCloud微服务实战》学习笔记之分布式服务跟踪Sleuth
- 《SpringCloud微服务实战》学习笔记之断路器Hystrix
- 《SpringCloud微服务实战》学习笔记之Feign
- 《SpringCloud微服务实战》学习笔记之配置中心Config
- 《SpringCloud微服务实战》学习笔记之消息驱动Stream
- springcloud微服务二:Eureka服务治理之服务注册中心
- springcloud微服务二:Eureka服务治理之服务注册中心
- springcloud微服务实战:Eureka+Zuul+Ribbon+Hystrix+SpringConfig
- springcloud微服务三:Eureka服务治理之注册服务提供者及服务的发现和消费
- 《SpringCloud微服务实战》学习笔记之消息总线BUS-RabbitMQ
- SpringCloud微服务实战
- SpringCloud---单节点Eureka Server
- 【微服务】Springcloud学习笔记(一) —— Eureka
- 基于springcloud的微服务实战
- SpringCloud微服务实战-SpringCloudConfig配置中心
- C#之中的字符串替换方法以及判断是否存在特定子字符串
- Struts2的简单搭建(一)
- MyEclipse如何修改编码如“GBK”“UTF-8”
- HDU Sum It Up
- 2018第三届中国青年健康论坛——生理、心理与教育(CYHF 2018)
- 《SpringCloud微服务实战》学习笔记之服务治理Eureka(单节点)
- 机器学习
- JAVA 一 输出格式,基础知识
- Java虚拟机内存的代的划分
- 初次使用指针
- 免费框架与付费框架之争
- 外汇模拟交易软件B&G GLOBAL MARKETS受到热捧
- 解决Anaconda navigator闪退问题
- JAVA 二 规范,常量,整数表示方式