1. 首先创建一个空Maven父工程
GroupId : com.cloud
ArtifactId: spring-cloud
2. 创建成功后删除 src 目录
3.修改pom文件
添加 spring-boot-starter-parent 父依赖
4.0.0
com.cloud
spring-cloud
1.0-SNAPSHOT
UTF-8
UTF-8
1.8
cloud-client
cloud-provider
cloud-discovery
org.springframework.boot
spring-boot-starter-parent
2.0.8.RELEASE
1. 创建Module工程
file -> New -> Module
选择Spring Initializr
Group:com.cloud
Artifact:cloud-discovery
Package:com.cloud.discovery
Dependencies选择 Spring Cloud Discovery -> Eureka Server
完成工程的创建
2. 修改application.yml文件
server:
port: 8080 #指定该Eureka实例的端口
spring:
application:
name: eureka-server #服务名称,自定义
security:
user:
name: user #配置BASIC认证登录的账号
password: 123456 #配置BASIC认证登录的密码
eureka:
client:
registerWithEureka: false #禁止注册自身
fetchRegistry: false #因为该服务没有注册到其他注册中心,所以关闭从注册中心拉取服务列表。
#如果是服务注册中心集群,需要开启,开启就是去掉该配置,采用默认配置即可
serviceUrl:
defaultZone: http://user:123456@localhost:8080/eureka/ #服务注册中心地址,http://账号:密码@主机名称:端口/Eureka/
server:
renewalPercentThreshold: 0.49 #设置心跳正常的阀值,默认为1,测试时可以调低
3. 修改pom文件
添加Tomcat依赖与Web支持模块
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.3.3.RELEASE
com.cloud
cloud-discovery
0.0.1-SNAPSHOT
cloud-discovery
Demo project for Spring Boot
1.8
Hoxton.SR7
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-tomcat
provided
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
4. 启动类添加 @EnableEurekaServer 注解
package com.cloud.discovery;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class CloudDiscoveryApplication {
public static void main(String[] args) {
SpringApplication.run(CloudDiscoveryApplication.class, args);
}
}
1. 创建Module工程
file -> New -> Module
选择Spring Initializr
Group:com.cloud
Artifact:cloud-provider
Package:com.cloud.provider
Dependencies选择 Spring Cloud Discovery -> Eureka Discovery Client
完成工程创建
2. 修改application.yml文件
server:
port: 8081
spring:
application:
name: cloud-provider
eureka:
client:
serviceUrl:
defaultZone: http://user:123456@localhost:8080/eureka/ #服务注册中信地址,含有BASIC认证的用户名和密码
instance:
prefer-ip-address: true #将IP注册到服务注册中心
#放开所有节点
management:
endpoints:
web:
exposure:
include: '*'
3. 修改pom文件
添加Tomcat依赖与Web支持模块
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.3.3.RELEASE
com.cloud
cloud-provider
0.0.1-SNAPSHOT
cloud-provider
Demo project for Spring Boot
1.8
Hoxton.SR7
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-tomcat
provided
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
4. 启动类添加 @EnableDiscoveryClient 注解
package com.cloud.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class CloudProviderApplication {
public static void main(String[] args) {
SpringApplication.run(CloudProviderApplication.class, args);
}
}
5. 创建Controller与entity, 提供服务
package com.cloud.provider.controller;
import com.cloud.provider.entity.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
/**
* UserController
*
* @author zjm
* @date 2020/8/22
*/
@RestController
public class UserController {
@GetMapping("/{id}")
public User findById(@PathVariable Long id) {
User findOne = new User();
if (id == 1) {
findOne.setAge(20);
findOne.setName("zhangsan");
findOne.setUsername("zhangsan");
findOne.setId(1L);
findOne.setBalance(800D);
} else {
findOne.setAge(18);
findOne.setName("lisi");
findOne.setUsername("lisi");
findOne.setId(2L);
findOne.setBalance(2000D);
}
return findOne;
}
}
package com.cloud.provider.entity;
/**
* User
*
* @author zjm
* @date 2020/8/22
*/
public class User {
private Long id;
private String username;
private String name;
private Integer age;
private Double balance;
// GET and SET
}
1. 创建Module工程
file -> New -> Module
选择Spring Initializr
Group:com.cloud
Artifact:cloud-client
Package:com.cloud.client
Dependencies选择 Spring Cloud Discovery -> Eureka Discovery Client ; Spring Cloud Routing -> Eureka Discovery Client 。
完成工程的创建
2. 修改application.yml文件
server:
port: 8082
spring:
application:
name: cloud-client
eureka:
client:
serviceUrl:
defaultZone: http://user:123456@localhost:8080/eureka/ #服务注册中信地址,含有BASIC认证的用户名和密码
instance:
prefer-ip-address: true #将IP注册到服务注册中心
#放开所有节点
management:
endpoints:
web:
exposure:
include: '*'
3. 修改pom文件
添加Tomcat依赖与Web支持模块
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.3.3.RELEASE
com.cloud
cloud-client
0.0.1-SNAPSHOT
cloud-client
Demo project for Spring Boot
1.8
Hoxton.SR7
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-tomcat
provided
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
4. 启动类添加 @EnableDiscoveryClient 、@EnableFeignClients注解
package com.cloud.client;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication
public class CloudClientApplication {
public static void main(String[] args) {
SpringApplication.run(CloudClientApplication.class, args);
}
}
5. 创建Controller、entity、feign
package com.cloud.client.controller;
import com.cloud.client.entity.User;
import com.cloud.client.feign.UserFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
/**
* UserController
*
* @author zjm
* @date 2020/8/22
*/
@RestController
public class UserController {
@Autowired
private UserFeignClient userFeignClient;
@GetMapping("/user/{id}")
public User findById(@PathVariable Long id) {
User user = this.userFeignClient.findById(id);
return user;
}
}
package com.cloud.client.entity;
/**
* User
*
* @author zjm
* @date 2020/8/22
*/
public class User {
private Long id;
private String username;
private String name;
private Integer age;
private Double balance;
private String requestId;
}
package com.cloud.client.feign;
import com.cloud.client.entity.User;
import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* UserFeignClient
*
* @author zjm
* @date 2020/8/22
*/
@FeignClient(name = "cloud-provider", fallbackFactory = FeignClientFallbackFactory.class)
public interface UserFeignClient {
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public User findById(@PathVariable("id") Long id);
}
@Component
class FeignClientFallbackFactory implements FallbackFactory {
private static final Logger LOGGER = LoggerFactory.getLogger(FeignClientFallbackFactory.class);
@Override
public UserFeignClient create(Throwable cause) {
return new UserFeignClient() {
@Override
public User findById(Long id) {
FeignClientFallbackFactory.LOGGER.info("fallback; reason was:", cause);
User user = new User();
user.setId(-1L);
user.setUsername("默认用户");
user.setAge(0);
user.setBalance((double) 0);
return user;
}
};
}
}
依次启动 cloud-discovery 、 cloud-provider 、 cloud-client
三个项目启动成功会访问 http://127.0.0.1:8080/ 进入Eureka页面, 在Instances currently registered with Eureka 下可以看到两个注册的服务 cloud-client、cloud-provider 。
访问 http://127.0.0.1:8082/user/1 返回如下数据