首页 > 其他分享 >springcloud微服务搭建demo

springcloud微服务搭建demo

时间:2023-02-14 17:22:39浏览次数:67  
标签:spring demo springcloud boot springframework import org cloud 搭建

软件 版本
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注解 ![](/i/l/?n=23&i=blog/775846/202302/775846-20230214160530784-1236719061.png) 启动eureka服务 ![](/i/l/?n=23&i=blog/775846/202302/775846-20230214160647971-997301591.png) 访问 http://localhost:8761/ 网址,可看到Eureka页面,暂无Application ![](/i/l/?n=23&i=blog/775846/202302/775846-20230214160918727-1176070306.png)

新建一个服务提供者

将这个服务的信息注册到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两个不同服务间交替调用。

标签:spring,demo,springcloud,boot,springframework,import,org,cloud,搭建
From: https://www.cnblogs.com/vinphy/p/17120256.html

相关文章

  • springcloud sidecar 实现C语言调用语言模块
    以前对springcloud的印象停留在大项目功能模块的独立、负载均衡、熔断等功能。这次项目接触了另一个用法,多语言异构。以前Java调C都是用的JNA或者JNI,这次C调Java用了spring......
  • react豆瓣API获取电影数据小demo
    entd使用使用entd的layout布局快速搭建页面​​entd官网​​页面刷新,路由不刷新,设置sider和路由同步,在componentWillMount还是componentDidMount都获取不到路由信息,但是能......
  • 【前端】microApp微前端搭建简单Demo
    创建项目第一步,创建项目,分别创建base_app(主基座)、a_app(子项目1)配置主基座项目Main.js中引入@Micro-zoe/micro-app//main.jsimportmicroappfrom'@micro-zoe/mic......
  • 案例分析&环境搭建
    案例需求:1.提供index.html页面,页面中有一个省份下拉列表2.当页面加载完成后,发送Ajax请求,加载所有省份*注意:使用redis缓存一些不经常发生变化的数据......
  • window下gogs搭建
    前置条件:安装git并可使用且环境变量中有如下配置:    接下来是gogs搭建步骤:1下载nssmhttps://nssm.cc/release/nssm-2.24.zip2下载gogshttps://dl.gogs.io......
  • 微服务学习计划——SpringCloud
    微服务学习计划——SpringCloud在学习并掌握了众多基础框架之后,我们的项目繁杂且难以掌握,那么我们就需要开启一门新的课程,也就是我们常说的微服务架构随着互联网行业的发......
  • 只需 3 步,人人都能搭建自己的 chatgpt 微信机器人
    大家好,我是徐公,大厂6年经验,CSDN博客专家。最近,ChatGpt很火,身边的人都在讨论,会不会成为下一个风口,像前几年互联网一样,迎来井喷式的发展。徐公我最近也是在密切关注,最近......
  • Android 之 环境搭建
    1.Gradlegradle跟maven一样是一个包管理工具,Android项目默认的包管理工具,这两天使用下来,感觉比maven更加简洁,其他暂时没啥赶脚。1.1配置Javagradle需要java8+1.2......
  • springboot多模块项目搭建
    最近在负责的是一个比较复杂项目,模块很多,代码中的二级模块就有9个,部分二级模块下面还分了多个模块。代码中的多模块是用maven管理的,每个模块都使用springboot框架。之前有......
  • 【PyQt】PyQt学习(一)框架介绍、环境搭建
    简介大家好,我们的gzh是朝阳三只大明白,满满全是干货,分享近期的学习知识以及个人总结(包括读研和IT),跪求一波关注,希望和大家一起努力、进步!!写在最前面的话在决定学习、使用一个......