springcloud微服务搭建demo
软件 | 版本 |
---|---|
IDEA | 2022.3.1 <兼容maven 3.8.1及之前的所用版本> |
JDK | 1.8_64 |
Maven | 3.8.2 |
本demo只使用了服务发现与注册、Feign调用及负载均衡。不涉及熔断与网关等模块。demo可通过百度云盘下载:链接:https://pan.baidu.com/s/1kfrBffhhQJhkpsJ8rbeQ7Q | |
提取码:yics | |
![]() |
新建工程
新建eureka服务发现与注册模块
EurekaServer注册中心,里面有一个注册表,保存了各个服务所在的机器和端口号
新建一个模块,选择Eureka Server依赖
pom.xml配置如下:
`
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.7.8
com.vinphy
eureka-service
0.0.1-SNAPSHOT
eureka-service
eureka-service
<java.version>1.8</java.version>
<spring-cloud.version>2021.0.5</spring-cloud.version>
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
<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>
`
application.yml配置如下:
`server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka`
启动类添加@EnableEurekaServer注解

启动eureka服务

访问 http://localhost:8761/ 网址,可看到Eureka页面,暂无Application

新建一个服务提供者
将这个服务的信息注册到EurekaServer中,别的服务可以在不知道IP和端口的情况下调用该服务。
新建一个模块,引入Eureka Discovery Client 依赖
pom.xml配置文件如下:
`
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.7.8
com.vinphy
eureka-client
0.0.1-SNAPSHOT
eureka-client
eureka-client
<java.version>1.8</java.version>
<spring-cloud.version>2021.0.5</spring-cloud.version>
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-web
<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>
`
该服务可部署多个,建立一个公共配置文件,然后在子配置文件中配置不同端口,如本示例中8762、8763两个端口。
application.yml配置文件如下:
`spring:
application:
name: service-support
profiles:
active: server1`
application-server1.yml配置文件如下:
`server:
port: 8762
eureka:
instance:
hostname: server1
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/`
application-server2.yml配置文件如下:
`server:
port: 8763
eureka:
instance:
hostname: server2
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
`
启动类加上@EnableEurekaClient注解
`package com.vinphy.eurekaclient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
}
新建一个TestController类,定义/support方法
package com.vinphy.eurekaclient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Value("${server.port}")
String port;
@RequestMapping(value = "/support",method = RequestMethod.GET)
public String home(@RequestParam String name)
{
return "hi " + name + ",i am from port:" + port;
}
}
`
启动服务,可以在Eureka网站上看到SERVICE-SUPPORT服务端口为8762
访问网址 http://localhost:8762/support?name=nwh 可得到support方法打印内容
再启动一次service-support服务
修改application.yml配置文件中active配置为server2
spring: application: name: service-support profiles: active: server2
添加一个启动配置,并启动
访问Eureka网页,可以看到service-support启了两个实例,端口分别为8762、8763
新建feign动态代理
Feign使用了动态代理,用注解定义一个FeignClient接口,然后调用这个接口就可以了。FeignClient会在底层根据注解,与指定的服务建立连接、构建请求地址、发起请求、获取响应、解析响应等等。
新建一个模块,引入OpenFeign依赖
pom.xml配置文件如下:
`
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.7.8
com.vinphy
serice-feign
0.0.1-SNAPSHOT
serice-feign
serice-feign
<java.version>1.8</java.version>
<spring-cloud.version>2021.0.5</spring-cloud.version>
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-openfeign
<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>
`
application.yml配置文件如下:
`server:
port: 8765
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: service-feign`
启动类加上@EnableDiscoveryClient、@EnableFeignClients注解
`package com.vinphy.sericefeign;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
动态代理进行调用
*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class SericeFeignApplication {public static void main(String[] args) {
SpringApplication.run(SericeFeignApplication.class, args);
}
}
定义一个HiController类,和hi方法
package com.vinphy.sericefeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HiController {
@Autowired
SchedualServiceHi schedualServiceHi;
@RequestMapping(value = "/hi",method = RequestMethod.GET)
public String sayHi(@RequestParam String name){
return schedualServiceHi.sayHiFromClientOne(name);
}
}
定义一个SchedualServiceHi接口,通过@FeignClient注解绑定service-support服务。
package com.vinphy.sericefeign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
/**绑定service-support服务,调用该服务的support方法*/
@FeignClient(value = "service-support")
public interface SchedualServiceHi {
@RequestMapping(value = "/support", method = RequestMethod.GET)
String sayHiFromClientOne(@RequestParam(value = "name") String name);
}
`
启动服务,此时可以通过访问8765端口实际调用service-support服务的support方法,因为service-support服务有两个端口,不断访问 http://localhost:8765/hi?name=vinphy ,会交替打印862、8763端口
新建Ribbon负载均衡服务
Ribbon会帮你在每次请求时选择一台机器均匀的把你的请求分发到各个机器上
新建service-ribbon模块
eureka包含了ribbon,所以不需要单独引入ribbon依赖
pom.xml配置文件如下:
`
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.7.8
com.vinphy
service-ribbon
0.0.1-SNAPSHOT
service-ribbon
service-ribbon
<java.version>1.8</java.version>
<spring-cloud.version>2021.0.5</spring-cloud.version>
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.1.0.RELEASE</version>
</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>
`
application.xml配置文件如下:
`server:
port: 8764
spring:
application:
name: service-ribbon
eureka:
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://localhost:8761/eureka/`
启动类加入@EnableDiscoveryClient、@EnableHystrix注解。并添加restTemplate方法
`package com.vinphy.serviceribbon;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/**
负载均衡
*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
public class ServiceRibbonApplication {public static void main(String[] args) {
SpringApplication.run(ServiceRibbonApplication.class, args);
}@Bean
@LoadBalanced
RestTemplate restTemplate()
{
return new RestTemplate();
}
}
新建HelloControler类,定义hi方法
package com.vinphy.serviceribbon;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloControler {
@Autowired
HelloService helloService;
@RequestMapping(value = "/hi",method = RequestMethod.GET)
public String hi(@RequestParam String name)
{
return helloService.hiService(name);
}
}
新建HelloService类,自动注入restTemplate。通过restTemplate的getForObject方法去用http方式访问其他服务。不需要知道被访问服务的IP和端口,只需要知道被访问服务的注册服务名。
package com.vinphy.serviceribbon;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class HelloService {
@Autowired
RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "hiError")
public String hiService(String name)
{
return restTemplate.getForObject("http://service-support/"+"support?name=" + name, String.class);
}
public String hiError(String name) {
return "hi," + name + ",sorry,error!";
}
}
`
启动服务,访问网址 http://localhost:8765/hi?name=vinphy ,会在876、8763两个不同服务间交替调用。
springcloud微服务搭建demo的更多相关文章
- 十一、Docker搭建部署SpringCloud微服务项目Demo
环境介绍 技术选型:SpringCloud&SpringCloud Alibaba&Docker 微服务模块划分: 员工模块:ems-employees 部门模块:ems-depart ...
- springcloud微服务架构搭建
SpringCloud微服务框架搭建 一.微服务架构 1.1什么是分布式 不同模块部署在不同服务器上 作用:分布式解决网站高并发带来问题 1.2什么是集群 多台服务器部署相同应用构成一个集群 作用:通 ...
- 【微服务】之二:从零开始,轻松搞定SpringCloud微服务系列--注册中心(一)
微服务体系,有效解决项目庞大.互相依赖的问题.目前SpringCloud体系有强大的一整套针对微服务的解决方案.本文中,重点对微服务体系中的服务发现注册中心进行详细说明.本篇中的注册中心,采用Netf ...
- SpringCloud微服务(基于Eureka+Feign+Hystrix+Zuul)
一.搭建注册中心 1.1.创建一个cloud-service项目 1.2:POM文件依赖 1 <?xml version="1.0" encoding="UTF-8 ...
- 从零开始,轻松搞定SpringCloud微服务系列
本系列博文目录 [微服务]之一:从零开始,轻松搞定SpringCloud微服务系列–开山篇(spring boot 小demo) [微服务]之二:从零开始,轻松搞定SpringCloud微服务系列–注 ...
- 【微服务】之六:轻松搞定SpringCloud微服务-API网关zuul
通过前面几篇文章的介绍,我们可以轻松搭建起来微服务体系中比较重要的几个基础构建服务.那么,在本篇博文中,我们重点讲解一下,如何将所有微服务的API同意对外暴露,这个就设计API网关的概念. 本系列教程 ...
- java~springcloud微服务目录索引
回到占占推荐博客索引 最近写了不过关于java,spring,微服务的相关文章,今天把它整理一下,方便大家学习与参考. java~springcloud微服务~目录索引 springcloud~服务注 ...
- SpringCloud微服务负载均衡与网关
1.使用ribbon实现负载均衡ribbon是一个负载均衡客户端 类似nginx反向代理,可以很好的控制htt和tcp的一些行为.Feign默认集成了ribbon. 启动两个会员服务工程,端口号分别为 ...
- SpringCloud微服务之跨服务调用后端接口
SpringCloud微服务系列博客: SpringCloud微服务之快速搭建EurekaServer:https://blog.csdn.net/egg1996911/article/details ...
- springcloud微服务实战:Eureka+Zuul+Feign/Ribbon+Hystrix Turbine+SpringConfig+sleuth+zipkin
相信现在已经有很多小伙伴已经或者准备使用springcloud微服务了,接下来为大家搭建一个微服务框架,后期可以自己进行扩展.会提供一个小案例: 服务提供者和服务消费者 ,消费者会调用提供者的服务,新 ...
随机推荐
- 论文解读丨【CVPR 2022】不使用人工标注提升文字识别器性能
摘要:本文提出了一种针对文字识别的半监督方法.区别于常见的半监督方法,本文的针对文字识别这类序列识别问题做出了特定的设计. 本文分享自华为云社区<[CVPR 2022] 不使用人工标注提升文字识 ...
- easygui的简单使用——实现猜字谜小游戏
游戏:随机生成个谜底,每轮有3次机会,猜对了结束本轮游戏,猜错了提示猜大了还是猜小了,并显示剩余次数,3次用完后本轮字谜游戏结束,可重新开始猜字谜,也可结束游戏 # 使用 easygui 实现猜字谜游 ...
- Jmeter 逻辑控制器之吞吐量控制器(Throughput Controller)
吞吐量控制器(Throughput Controller)用来控制其下元件的执行次数,并无控制吞吐量的功能,想要控制吞吐量可以用Constant Throughput Timer,吞吐量控制器有两种模 ...
- Blazor组件自做十四 : Blazor FileViewer 文件预览 组件
Blazor FileViewer 文件预览 组件 目前支支持 Excel(.docx) 和 Word(.xlsx) 格式 示例: https://www.blazor.zone/fileViewer ...
- 【转载】SQL SERVER 中各种存储过程创建及执行方式
一. 什么是存储过程系统存储过程是系统创建的存储过程,目的在于能够方便的从系统表中查询信息或完成与更新数据库表相关的管理任务或其他的系统管理任务.系统存储过程主要存储在master数据库中,以&quo ...
- 本机无法配置远程服务器上的MySQL ODBC连接驱动
1.问题描述 我想要访问远程windows服务器上的MySQL数据库,因此需要在本地ODBC驱动上配好远程服务器的MySQL.但配置好基本信息后,测试的时候出现如下问题: 2.解决方法 之所以产生这种 ...
- kali开启ssh并开机自启
安装和启用SSH Kali默认是没有安装ssh和启用ssh的 我们需要先安装:apt install ssh 然后vim /etc/ssh/sshd_config (如果不需要启用Root登陆可以跳过 ...
- dfs学习笔记
题目链接 可以通过参考一道例题来加深对dfs的认知和学习 题意描述 按照字典序输出自然数 1 到 n 所有不重复的排列,即 n 的全排列,要求所产生的任一数 字序列中不允许出现重复的数字. 输出格式 ...
- Redis缓存何以一枝独秀?——从百变应用场景与热门面试题中感受下Redis的核心特性与使用注意点
大家好,又见面了. 本文是笔者作为掘金技术社区签约作者的身份输出的缓存专栏系列内容,将会通过系列专题,讲清楚缓存的方方面面.如果感兴趣,欢迎关注以获取后续更新. 作为<深入理解缓存原理与实战设计 ...
- DVWA靶场——靶场搭建
DVWA靶场搭建 一.phpstudy环境准备: 第一步:下载PHPstudy: 找到PHPstudy官网,下载windows版小皮面板,然后安装.官网下载地址:https://www.xp.cn/ ...