
这里直接全部next就行了。
1.2选择是否删掉maven项目下的src 二.创建注册中心eureka-service
2.2配置注册中心yml文件4.0.0 org.springframework.boot spring-boot-starter-parent 2.3.3.RELEASE com.eureka eureka_server 0.0.1-SNAPSHOT eureka_server 注册中心 1.8 Hoxton.SR8 org.springframework.cloud spring-cloud-starter-netflix-eureka-server org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin
#注册中心的名字
spring:
application:
name: my-eureka
#服务端口
server:
port: 10211
#注册中心相关配置
eureka:
server:
# 配置关闭自我保护,并按需配置Eureka Server清理无效节点的时间间隔(5000ms)。
enable-self-preservation: false
eviction-interval-timer-in-ms: 5000
client:
# 不将自己注册到注册中心
register-with-eureka: false #fasle:
# 因为自己是注册中心,因此不用检索服务信息
fetch-registry: false #true: Cannot execute request on any know server
# 注册中心的地址
service-url:
defaultZone: http://localhost:10211/eureka/
instance:
prefer-ip-address: true
2.3启动类加入@EnableEurekaService注解
package com.eureka.eureka_server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
2.4检验eureka是否搭建成功
三.创建web-service
构建模型时要将web组件和spring cloud discovery组件给选上,否则服务注册不到注册中心
3.2配置web-service yml文件4.0.0 org.springframework.boot spring-boot-starter-parent 2.3.3.RELEASE com.web web_server 0.0.1-SNAPSHOT web_server web应用 1.8 Hoxton.SR8 org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin
spring:
application:
name: client-test
#服务端口
server:
port: 10212
# 注册中心地址
eureka:
client:
service-url:
defaultZone: http://localhost:10211/eureka/
instance:
prefer-ip-address: true
3.3启动类加入启动类加入EnableEurekaClient注解
package com.web.web_server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class WebServerApplication {
public static void main(String[] args) {
SpringApplication.run(WebServerApplication.class, args);
}
}
3.4开启启动类将服务注册到注册中心
四.使用Feign实现微服务之间的调用
Feign介绍
Feign是一种负载均衡的HTTP客户端, 使用Feign调用API就像调用本地方法一样,从避免了 调用目标微服务时,需要不断的解析/封装json 数据的繁琐。
Fegin是一个声明似的web服务客户端,它使得编写web服务客户端变得更加容易。使用Fegin创建一个接口并对它进行注解。它具有可插拔的注解支持包括Feign注解与JAX-RS注解,Feign还支持可插拔的编码器与解码器,Spring Cloud 增加了对 Spring MVC的注解,Spring Web 默认使用了HttpMessageConverters, Spring Cloud 集成 Ribbon 和 Eureka 提供的负载均衡的HTTP客户端 Feign。
简单来说通过feign来调用各个服务之间的通信
Fegin使用Fegin是基于接口的注解,使用在Interface上面
服务注册者 4.1查看feign_server_caller配置pom文件4.2配置feigin_server_caller yml文件4.0.0 org.springframework.boot spring-boot-starter-parent 2.3.3.RELEASE com.feign feign_server_caller 0.0.1-SNAPSHOT feign_server_caller feign_server_caller 服务注册者 1.8 Hoxton.SR8 org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.cloud spring-cloud-starter-openfeign org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin
spring:
application:
name: feign-server-caller
#服务端口
server:
port: 10213
# 注册中心地址
eureka:
client:
service-url:
defaultZone: http://localhost:10211/eureka/
instance:
prefer-ip-address: true
4.3配置启动类注解
package com.feign.feign_server_caller;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableFeignClients //允许服务调用
@EnableEurekaClient //允许注册到注册中心
@SpringBootApplication
public class FeignServerCallerApplication {
public static void main(String[] args) {
SpringApplication.run(FeignServerCallerApplication.class, args);
}
}
服务提供者
4.4配置feigin_server_surport yml
服务提供者与服务注册者配置文件除了端口不一样 其他都是一致的
spring:
application:
name: feign-server-surport
#服务端口
server:
port: 10214
# 注册中心地址
eureka:
client:
service-url:
defaultZone: http://localhost:10211/eureka/
instance:
prefer-ip-address: true
4.5在server-surport(服务提供者)中定义被调用的方法
在controller下定义一个sayHello类,该类中有一个sayHello方法,返回一个hello
package com.feign.feign_server_surport.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class SayHello {
@RequestMapping("/hello")
@ResponseBody
public String sayHello(){
return "hello";
}
}
4.6在server-caller()(服务调用者)中调用server-surport(服务提供者)的sayHello方法
实现:
1).首先,需要提供一个接口,接口中的方法与server-surport(服务提供者)的方法 一样,但只声明不实现。
package com.feign.feign_server_caller.feignclient;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
@FeignClient(value = "feign-server-surport") //调用的服务名称
public interface SayHelloCaller {
@RequestMapping("/hello") //对应引用服务映射的controller
public String sayHello();
}
2).其次,在接口上加入注解@FeignClient(“引用的服务名称”),在这里我给我的引用服务起名为feign-server-surport,那么就是@FeignClient(value = “feign-server-surport”)
服务调用者接口声明的特点是:
1).方法声明和server-surport的方法声明相同
2)请求方式也相同,例如:post、get、put…
3).对应的请求映射路径也相同
4).加入@FeignClient(value = “被调用的服务”),声明被调用的服务
4.6定义PersonController来测试调用
package com.feign.feign_server_caller.controller;
import com.feign.feign_server_caller.feignclient.SayHelloCaller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class PersonController {
@Autowired
private SayHelloCaller sayHelloCaller;
@RequestMapping("/sayHello")
@ResponseBody
public String sayHello(){
return sayHelloCaller.sayHello();
}
}
4.7测试Controller
项目配置的server-surport的端口号是:10214,server-caller配置的端口号是10213,由下图可见,我们在server-caller服务中成功的访问了server-surport提供的sayHello服务。
localhost:10213/sayHello五.创建Zuul API网关服务 Zuul 概念
zuul是微服务架构的重要组成部分,是API网关,是用来映射客户端的请求与服务之间的关系,提供了路由、服务器端负载均衡、安全等功能 。
微服务网关的作用:1.映射客户端的请求与服务之间的关系
2.提供路由
3.服务器端负载均衡负载均衡
4.安全功能等(过滤,校验等)
将所有的微服务链接起来形成一个完整的系统,通常一个微服务类型的项目,有多个网关。
5.1配置网关服务 pom文件5.2配置gateway-service yml 路由4.0.0 org.springframework.boot spring-boot-starter-parent 2.3.3.RELEASE com.gateway gateway_server 0.0.1-SNAPSHOT gateway_server 网关服务 1.8 Hoxton.SR8 org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.cloud spring-cloud-starter-netflix-zuul org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin
pring:
application:
name: gateway
server:
port: 10215
zuul:
routes:
# 网关使用两种转发方式,一种是转发服务给注册中心的名称,另一种为服务端口转发
# 面向服务的路由
api-a:
path: /client/**
serviceId: feign-server-caller #feign-server-caller 对应的是注册中心的服务的名称
#服务分发的方式是这样的,我们通过localhost:10215/client访问,实际是将地址映射到了feign-server-caller这个服务的地址上
# 传统的路由
api-b-url:
path: /caller/**
#由传统的路由模式,我们可以知道用户访问的路由路径为localhost:10215/caller,将服务映射到了http://localhost:10214
url: http://localhost:10214/
eureka:
client:
service-url:
defaultZone: http://localhost:10211/eureka/
5.3启动类配置注解
package com.gateway.gateway_server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@EnableEurekaClient //允许注册到注册中心
@EnableZuulProxy //能够进行网关服务
@SpringBootApplication
public class GatewayServerApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayServerApplication.class, args);
}
}
5.4配置zuul控制服务的访问
测试,通过gateway(网关服务)来访问,上文的client(测试将服务注册到注册中心),server-caller(服务调用者)这两个服务。
1).client服务中定义一个controller类,类名为ClientServer,类中定义一个sayHello()方法
package com.gateway.gateway_server.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class ClientServerController {
@RequestMapping("/sayhello")
@ResponseBody
public String sayHello(){
return "我是来自client服务端,你好呀!";
}
}
2).server-caller服务也定义controller类,类名为CallerServerController,类中也定义衣蛾sayHello()方法
@RequestMapping("/hello")
@ResponseBody
public String hello(){
return "我是来自caller的服务端,你好呀!";
}
5.5测试由zull来分发服务
三个服务:
gateway(网关服务): 端口号10215
client(测试注册中心服务): 端口号10216
server-caller(服务调用):端口号10213
由gateway分发对client、 server-caller服务的访问
测试:访问client的sayHello()方法
通过网关地址locallhost:10215/client 进行访问, client将地址映射到serviceId上对应的client服务对应的地址。