发布时间:2022-12-12 08:30
前言:
开发工具:IntelliJ IDEA 2020版 (Ultimate Edition)
框架:spring boot 、spring cloud
搭建一套spring cloud微服务系统,实现服务之间的调用。
需要搭建一个父工程springcloud-test,一个服务注册中心eureka-server,两个微服务cloud-client,cloud-provider。
两个微服务均注册到服务注册中心。
一.搭建父项目
2.
3.
(1)删掉src目录
(2)定义pom.xml文件
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.4.1
com.chen.test
springcloud-test
1.0-SNAPSHOT
pom
UTF-8
UTF-8
1.8
二.搭建eureka-server注册中心
1.
2.
3.
4.
5.
6.
(1)定义pom.xml文件
4.0.0
com.chen.test
springcloud-test
1.0-SNAPSHOT
com.chen.test
eureka-server
0.0.1-SNAPSHOT
jar
eureka-server
Demo project for Spring Boot
UTF-8
UTF-8
1.8
2020.0.0
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
(2)删掉test文件夹(自己设置,可有可无)
(3)启动加注解@EnableEurekaServer(开启eureka服务)
package com.chen.test.eurekaserver;
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);
}
}
(4)定义配置文件
配置文件改为application.yml(可有可无,看自己喜好)
server:
port: 8080
spring:
application:
#应用名称(在注册中显示的)
name: eureka-server
eureka:
client:
#此客户端是否获取eureka服务器注册表上的注册信息,默认为true
fetch-registry: false
#实例是否在eureka服务器上注册自己的信息以供其他服务发现,默认为true,即自己注册自己。
register-with-eureka: true
#与Eureka注册服务中心的通信zone和url地址
serviceUrl:
#http://localhost:8080/eureka/eureka
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
#服务注册中心实例的主机名
instance:
hostname: 127.0.0.1
prefer-ip-address: true
instance-id: 127.0.0.1:8080
server:
#设为false,关闭自我保护,即Eureka server在云心光器件会去统计心跳失败比例在15分钟之内是否低于85%,如果低于85%,EurekaServer
#会将这些事例保护起来,让这些事例不会过期,但是在保护器内如果刚哈这个服务提供者非正常下线了,此时服务消费者会拿到一个无效的服务
#实例,此时调用会失败,对于这个问题需要服务消费者端有一些容错机制,如重试、断路器等;
enable-self-preservation: false
#扫描失效服务的间隔时间(单位是毫秒,摩恩是60*1000),即60s
eviction-interval-timer-in-ms: 10000
(5)测试
三.搭建提供者服务
1.
2.
3.
4.
5.
6.
(1)定义pom.xml文件
4.0.0
com.chen.test
springcloud-test
1.0-SNAPSHOT
com.chen.test
cloud-provider
0.0.1-SNAPSHOT
jar
cloud-provider
Demo project for Spring Boot
UTF-8
UTF-8
1.8
2020.0.0
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.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
(2)删除test文件加(可有可无)
(3)修改配置文件为application.yml(可有可无)
spring:
application:
name: cloud-provider
server:
port: 8081
eureka:
client:
#此客户端是否获取eureka服务器注册表上的注册信息,默认为true
fetch-registry: false
#实例是否在eureka服务器上注册自己的信息以供其他服务发现,默认为true,即自己注册自己。
register-with-eureka: true
service-url:
#defaultZone 这个是不会提示的,此处需要自己写
#实际上属性应该是service-url,这个属性是个map(key-value)格式;当key是defaultZone的时候才能被解析;所以这里没有提示,
#但是自己还需要写一个defaultZone;
defaultZone: http://localhost:8080/eureka
#服务注册中心实例的主机名
instance:
hostname: 127.0.0.1
prefer-ip-address: true
instance-id: 127.0.0.1:8081
(4)启动类加注解@EnableEurekaClient
package com.chen.test.cloudprovider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class CloudProviderApplication {
public static void main(String[] args) {
SpringApplication.run(CloudProviderApplication.class, args);
}
}
(5)测试
四.搭建消费者服务
1.
2.
3.
4.
5.
(1)定义pom.xml文件
4.0.0
com.chen.test
springcloud-test
1.0-SNAPSHOT
com.chen.demo
cloud-client
0.0.1-SNAPSHOT
jar
cloud-client
Demo project for Spring Boot
UTF-8
UTF-8
1.8
2020.0.0
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.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
(2)删除test文件夹(可有可无)
(3)修改配置文件为application.yml(可有可无)
server:
#定义端口号
port: 8082
spring:
application:
#定义应用名称,即服务名称
name: cloud-client
eureka:
client:
service-url:
defaultZone: http://localhost:8080/eureka
#服务注册中心实例的主机名
instance:
hostname: 127.0.0.1
prefer-ip-address: true
instance-id: 127.0.0.1:8082
(4)启动类加注解@EnableEurekaClient
package com.chen.demo.cloudclient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class CloudClientApplication {
public static void main(String[] args) {
SpringApplication.run(CloudClientApplication.class, args);
}
}
(5)测试
(6)在父pom中加上
eureka-server
cloud-provider
cloud-client
五.实现服务之间的调用
1.在cloud-provider中创建controller包和service包
package com.chen.test.cloudprovider.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.chen.test.cloudprovider.service.HelloService;
@RestController
@RequestMapping("/hello")
public class HelloController {
@Autowired
private HelloService helloService;
@GetMapping("/getHello")
public String getHello(){
return helloService.getHello();
}
}
package com.chen.test.cloudprovider.service;
public interface HelloService {
String getHello();
}
package com.chen.test.cloudprovider.service.impl;
import com.chen.test.cloudprovider.service.HelloService;
import org.springframework.stereotype.Service;
@Service
public class HelloServiceImpl implements HelloService {
@Override
public String getHello() {
return "你好兄弟";
}
}
(2)测试
(3)在cloud-client中创建controller包和service包
package com.chen.demo.cloudclient.controller;
import com.chen.demo.cloudclient.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
@RequestMapping("/Hello")
public class HelloClient {
@Autowired
private HelloService helloService;
@GetMapping("/getClient")
public String getClient(){
return helloService.getProduct();
}
}
package com.chen.demo.cloudclient.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
//name 为product项目中application.yml配置文件中的application.name;
//path 为product项目中application.yml配置文件中的context.path;
@FeignClient(name = "cloud-provider",path ="/hello" )
//@Componet注解最好加上,不加idea会显示有错误,但是不影响系统运行;
@Component
public interface HelloService {
@RequestMapping(value = "getHello")
String getProduct();
}
特别注意
在启动类加上注解@EnableFeignClients
package com.chen.demo.cloudclient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class CloudClientApplication {
public static void main(String[] args) {
SpringApplication.run(CloudClientApplication.class, args);
}
}
(4)测试