【教程】IDEA创建SpringCloud ( 一 ) Eureka Server服务端项目
【教程】IDEA创建SpringCloud+Eureka+Zuul ( 二 ) Eureka Client 服务提供者 客户端项目
代码参考:https://gitee.com/guanweiCode/SpringCloudGw
客户端Client提供真正服务的角色的配置,它提供服务在服务注册方Server(注册中心)进行注册
1. 同Eureka Server创建步骤, 新建Module, 选择 quickstart点下一步
2.
3.
4 成功后并列 如果不并列请重新创建
5 创建pom文件 跟server的pom大体一致
<?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.gw</groupId>
<artifactId>springcloud-eureka-servicesupport</artifactId>
<version>1.0-SNAPSHOT</version>
<name>springcloud-eureka-servicesupport</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<!-- 引入SpringBoot-parent父项目 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<!-- 引入SpringCloud的euekea server依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- web服务 -->
<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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<!-- 指定下载源和使用SpringCloud的版本 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR8</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>
6. 同server 创建 application.yml
server:
port: 8701 #服务提供方
eureka:
instance:
hostname: localhost
client:
service-url:
# 指定当前eureka客户端的注册地址 端口8700对应server的端口
defaultZone: http://${eureka.instance.hostname}:8700/eureka
spring:
application:
name: eureka-client #指定应用名称
7.创建 启动类
EurekaServerApplication.java
package com.gw;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @Title: Eureka服务提供方
* @Description: 描述
* @Version: v1.0
* @Author: Mr.Guan
* @Mail GuanWeiMail@163.com
* @DateTime: 2021-02
* @Project SpringCloudDemo
* @Package com.gw
*/
/**
* //当前使用Eureka的Server
*/
@SpringBootApplication
/**
* 代表自己是一个服务提供方
*/
@EnableEurekaClient
public class EurekaServiceApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServiceApplication.class, args);
}
}
8. 创建所提供的服务 DemoController
package com.gw.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Title: 所提供的具体服务
* @Description: 描述
* @Version: v1.0
* @Author: Mr.Guan
* @Mail GuanWeiMail@163.com
* @DateTime: 2021-02
* @Project springcloud-eureka-servicesupport
* @Package com.gw.controller
*/
@RestController
@RequestMapping("/api")
public class DemoController {
@RequestMapping("helloWorld")
public String helloWorld(String s){
System.out.println(String.format("传入的值为 %s", s));
return String.format("传入的值为 %s", s);
}
}
UserController.java
package com.gw.eureka.servicesupport.user.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Title: 用户服务
* @Description: user为服务者(提供服务的)
* @Version: v1.0
* @Author: Mr.Guan
* @Mail GuanWeiMail@163.com
* @DateTime: 2021-02-23
* @Project springcloud-gw-parent
* @Package com.gw.eureka.servicesupport.user
*/
@Api(tags = "用户模块")
@RestController
@RequestMapping("user")
public class UserController {
@Value("${server.port}")
private String port;
/**
* 通过用户ID获取用户
* @param userId
* @return
*/
@ApiOperation(value = "根据用户ID查询用户")
@GetMapping("/getUser/{userId}")
public String getUser(@PathVariable("userId") String userId){
return String.format("【%s-服务端】:%s", port, userId);
}
}
9 启动 Eureka Server( 一 ) 服务注册者
10 启动 Eureka Client( 二 ) 服务提供者
11 访问http://127.0.0.1:8700/ 可以看到 服务提供者 已经被注册进 服务注册者
12 测试服务提供者的服务 http://127.0.0.1:8701/api/helloWorld?s=测试
可以访问, 证明此微服务可用
一 Eureka Server 注册中心
二 Eureka Client 服务提供者
一般不直接调用所需的微服务,而是经过 提供注册服务 的 服务器server, 获取所需的服务提供者列表(为一个列表, 此列表包含了能提供相应服务的服务器),他们也许是个集群,因此server会返回一个 ip + 端口号的表, 这种在服务消费者一方挑选服务器为自己服务器的方式是一种客户端的负载均衡(参考nginx的负载均衡)
第一种调用方式为:restTemplate+ribbon
第二种调用方式为:feign