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实例,消费方跟 ...
随机推荐
- charles 手机证书下载安装
本文参考:charles 手机证书下载安装 本文的Charles,适应windows/MAC/IOS/Android,避免抓包HTTPS失败和乱码: 用的版本是V4.1.2,其它版本原理类似: cha ...
- PYTHON WEB开发学习路线
两年大数据广告项目测试:项目覆盖几千万用户前景还不错:只是在工作中感觉测试太无力,最近准备辞职转web开发:同时会离开成都,(/(ㄒoㄒ)/~~待了6年,要离开喜欢的城市,喜欢的女孩很难受) ps:列 ...
- Visual Studio 快捷键、常见问题
前言 由于平时自己在使用 VS 过程中遇到过一些很常见的问题,比如基本的设置.工具等.VS 十分的强大(宇宙第一 IDE),所以有些功能藏的深也不好找,就在这里记录下. 正文 一.快捷操作 1.调试. ...
- CORS通信
CORS 是一个 W3C 标准,全称是"跨域资源共享"(Cross-origin resource sharing).它允许浏览器向跨域的服务器,发出XMLHttpRequest请 ...
- P2P system: Chord
DHT= Distributed Hash Table store the objects(files) at nodes (hosts, machines) in a cluster. The cl ...
- fsLayuiPlugin入门使用
简介 源码下载后,不能直接打开,必须运行在容器下,例如:nginx.tomcat.jetty等容器. 源码中默认配置了nginx容器,可以直接启动nginx访问. 本文主要介绍下载源码后的使用,避免在 ...
- go语言-数据类型及类型之间转换
数据类型分类 一.数据类型-基本数据类型 1.整数型(int.有符号(int8/1字节.int16/2字节.int32/4字节.int64/8字节).无符号(uint.uint8.uint16.uin ...
- Codeforces Round #336 (Div. 2) D. Zuma(区间DP)
题目链接:https://codeforces.com/contest/608/problem/D 题意:给出n个宝石的颜色ci,现在有一个操作,就是子串的颜色是回文串的区间可以通过一次操作消去,问最 ...
- mysql - 所有笔记
# 编码: 1. 查看创建数据库时设置的编码格式: shwo create database 数据库名; 2. 查看创建表时所设置的 编码格式 和 字段详细 show create table 表名; ...
- jquery显示隐藏密码跟显示密码
今天讲述的是html5中input的password密码的加密与显示 都知道input标签加上password输入密码显示的都是原点.......怎么点一个按钮让他显示回来明文数字1234567 上代 ...