Greenwich.SR2版本的Spring Cloud Eureka实例
作为微服务架构中最为核心和基础的服务治理,注册中心提供了微服务实例的自动化注册与发现。而作为一个服务注册中心,eureka的作用与传统的zk、etcd的作用是一样的,同样也支持高可用(集群)。不同之处在于它不是作为一个独立的服务器或者服务器集群存在,而是作为一个组件(或者说服务)存在,它分为服务端组件、客户端组件。可以把服务端组件看成是zk或者etcd(注册中心),其他注册到服务端的组件都可以看成是把客户端组件,比如服务提供方、消费方的微服务等。另外一个明显的特征是eureka是由java代码实现的,因此主要使用对象是JVM兼容的分布式系统。
搭建单例eureka服务端很简单,三板斧即可:
1、pom.xml引入最新版本Greenwich.SR2的spring-cloud-starter-netflix-eureka-server:
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId>
<artifactId>eureka-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<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>Greenwich.SR2</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>
2、application.properties配置端口、单机的话无需让注册中心注册自己、也无需去注册中心查询服务(集群时需要):
#本机端口
server.port=8888 #是否向注册中心注册自己
eureka.client.register-with-eureka=false #是否从注册中心查询服务
eureka.client.fetch-registry=false #注册中心地址
eureka.client.service-url.defaultZone=http://localhost:8888/eureka/
3、最后写一个主类,只要加上@EnableEurekaServer证明它是注册中心:
package hello; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer
@SpringBootApplication
public class EurekaServiceApplication { public static void main(String[] args) {
SpringApplication.run(EurekaServiceApplication.class, args);
}
}
客户端分服务提供方、服务消费方,先看服务提供方,还是三板斧:
1、pom需要把eureka客户端jar引入spring-cloud-starter-netflix-eureka-client:
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId>
<artifactId>eureka-client-service-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<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-netflix-eureka-server</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR2</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>
2、application配置:
#本机端口
server.port=8762
#本机服务名
spring.application.name=a-bootiful-client
#注册中心地址
eureka.client.service-url.defaultZone=http://localhost:8888/eureka/
3、主类加上注解@EnableDiscoveryClient用于服务发现:
package hello; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.*; import java.util.List; @EnableDiscoveryClient
@SpringBootApplication
public class EurekaClientServiceProviderApplication { public static void main(String[] args) {
SpringApplication.run(EurekaClientServiceProviderApplication.class, args);
}
} @RestController
class ServiceInstanceRestController { @Value("${server.port}")
private String port; @Autowired
private DiscoveryClient discoveryClient; @RequestMapping("/service-instances/{applicationName}")
public List<ServiceInstance> serviceInstancesByApplicationName(
@PathVariable String applicationName) {
return this.discoveryClient.getInstances(applicationName);
} @RequestMapping("/hello")
public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
return "Hello, " + name + "---------port: " + port;
}
}
服务消费方还是老套路:
1、pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId>
<artifactId>eureka-client-service-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<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-netflix-eureka-server</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR2</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>
2、application,这里配置了app.service.url是服务方实例名,方便调用时指定,当然你也可以在调用服务提供方的代码里硬编码指定:
#本机端口
server.port=8763
#本机服务名
spring.application.name=a-beautiful-client
#注册中心地址
eureka.client.service-url.defaultZone=http://localhost:8888/eureka/ #服务提供者实例名称
app.service.url=http://A-BOOTIFUL-CLIENT/
3、主类同样需要加上:
package hello;
import hello.service.ConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate; import java.util.List; @EnableDiscoveryClient
@SpringBootApplication
public class EurekaClientServiceClientApplication { @LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
} public static void main(String[] args) {
SpringApplication.run(EurekaClientServiceClientApplication.class, args);
}
} @RestController
class ServiceInstanceRestController { @Autowired
private ConsumerService consumerService; @Autowired
private DiscoveryClient discoveryClient; @RequestMapping("/sayHello")
public String sayHello(@RequestParam(value = "name", defaultValue = "Spring Cloud") String name) { return consumerService.call(name);
} @RequestMapping("/service-instances/{applicationName}")
public List<ServiceInstance> serviceInstancesByApplicationName(
@PathVariable String applicationName) {
return this.discoveryClient.getInstances(applicationName);
}
}
ConsumerService类:
package hello.service; public interface ConsumerService {
String call(String name); }
ConusmerServiceImpl类:
package hello.service.impl; import hello.service.ConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate; @Service
public class ConusmerServiceImpl implements ConsumerService { @Value("${app.service.url}")
private String appServiceUrl; @Autowired
private RestTemplate restTemplate; @Override
public String call(String name) {
ResponseEntity resultResponseEntity = restTemplate.postForEntity(appServiceUrl + "hello?name=" + name, null, String.class);
if (resultResponseEntity != null && resultResponseEntity.getBody() != null) {
return name + " says: " + resultResponseEntity.getBody().toString();
}
return null;
}
}
好了,接下来依次启动注册中心、服务提供方、服务消费方,浏览器上输入http://localhost:8763/sayHello?name=world,可以看到消费方通过注册中心发现了提供方,然后消费方调用了提供方的/hello:
直接调提供方:
这个是注册中心:
如图,服务端(a-bootiful-client)可以看到有两个实例,为何起两个呢?用来测试ribbon的负载均衡,这块内容详见Greenwich.SR2版本的Spring Cloud Ribbon实例。
Greenwich.SR2版本的Spring Cloud Eureka实例的更多相关文章
- Greenwich.SR2版本的Spring Cloud Feign实例
前面我们了解了Spring Cloud Ribbon和Hystrix,在使用上它们基本上会成队出现,那么是不是可以把它们组合起来使用?而且我们发现,在服务消费方a-beautiful-client里通 ...
- Greenwich.SR2版本的Spring Cloud Hystrix实例
之前我们在eureka(参见Greenwich.SR2版本的Spring Cloud Eureka实例)中,服务消费方a-beautiful-client调用服务提供方a-bootiful-clien ...
- Greenwich.SR2版本的Spring Cloud Ribbon实例
上次我们了解了eureka(参见Greenwich.SR2版本的Spring Cloud Eureka实例),里面的服务消费方(服务实例a-beautiful-client)我们其实已经用到了ribb ...
- Greenwich.SR2版本的Spring Cloud Zuul实例
网关作为对外服务,在微服务架构中是一个很重要的组件,主要体现在动态路由和接入鉴权这两个功能上.现在我们通过Spring Cloud Zuul来实现对之前a-feign-client(参见Greenwi ...
- Greenwich.SR2版本的Spring Cloud Zipkin实例
调用链跟踪是微服务架构中的基础能力,Spring Cloud Zipkin+Sleuth为我们提供了该能力.首先我们先建立Zipkin服务端,它需要集成Eureka,用于发现服务提供方和消费方,进行数 ...
- Greenwich.SR2版本的Spring Cloud Config+BUS实例
Spring Cloud Config统一的配置中心同注册中心Eureka一样,也分服务端和客户端.服务端用来保存配置信息,客户端用来读取.它的优势是基于Git仓库,支持多环境.多分支配置.动态刷新. ...
- Spring Cloud Eureka集群部署到Linux环境
还是三板斧:先改配置文件,支持集群,然后出包,上传到linux环境(3个节点),最后启动jar包跑起来. 1.在原eureka服务端代码(参见Greenwich.SR2版本的Spring Cloud ...
- Spring Cloud Eureka集群配置及注意事项(Greenwich版本)
Spring Cloud Eureka集群配置及注意事项(Greenwich版本) 一·概述 Spring Cloud Netflix Eureka 是一个提供服务注册与发现的套件.服务提供者只需要将 ...
- 0.9.0.RELEASE版本的spring cloud alibaba nacos+feign实例
这里的feign依然是原来的feign,只不过将注册中心由eureka换成了nacos.服务提供方参见0.9.0.RELEASE版本的spring cloud alibaba nacos实例,消费方跟 ...
随机推荐
- RedisTemplate的key默认序列化器问题
原文:https://blog.csdn.net/skymouse2002/article/details/80736577 redis的客户端换成了spring-boot-starter-data- ...
- 模板引擎-vue中的模板如何被解析,指令如何处理
模板是什么 <div id='app'> <div> <input v-model="title"/> <button v-on:clic ...
- Java并发包--ConcurrentHashMap原理解析
ConcurrentHashMap实现原理及源码分析 ConcurrentHashMap是Java并发包中提供的一个线程安全且高效的HashMap实现(若对HashMap的实现原理还不甚了解,可参 ...
- Composer的安装以及替换为国内镜像
Composer的安装以及使用国内镜像 Composer 学习网址 Composer官网 https://getcomposer.org/ Composer中文网 http://www.phpcomp ...
- JVM 平台上的 Scheme 语言实现 JSchemeMin
JSchemeMin 是一个JVM平台上的Scheme语言实现. 作为R7RS的实现,JSchemeMin支持Scheme的所有标准特性,包括头等公民地位的过程.尾递归优化.继续.用户定义记录.库(包 ...
- JSP中9大内置对象类型
JSP中九大内置对象为: request 请求对象 类型 javax.servlet.ServletRequest 作用域 Requ ...
- javascript权威指南第11章 DOM扩展
//javascript 权威指南 第三版 第11章 DOM扩展 //取得body元素 var body = document.querySelector("body"); //取 ...
- 008——转载——c#获取当前日期时间
(一)转载——c#获取当前日期时间 我们可以通过使用DataTime这个类来获取当前的时间.通过调用类中的各种方法我们可以获取不同的时间:如:日期(2008-09-04).时间(12:12:12).日 ...
- ashx 图片上传
为了方便多出调用图片上传方法 首先我们将图片上传方法抽离出来 创建ashx 一个新的方法 using System; using System.Collections.Generic; u ...
- Centos7 安装php7.3 并扩展 MySQL、postgresql
首先是安装需要的扩展文件 yum -y install freetype-devel yum -y install libpng-devel yum -y install libjpeg-devel ...