引用官方文档解释:
简单来说,微服务架构风格[1]是一种将一个单一应用程序开发为一组小型服务的方法,每个服务运行在自己的进程中,服务间通信采用轻量级通信机制(通常用HTTP资源API)。这些服务围绕业务能力构建并且可通过全自动部署机制独立部署。这些服务共用一个最小型的集中式的管理,服务可用不同的语言开发,使用不同的数据存储技术。
Spring Cloud Eureka 是 Spring Cloud Netflix 微服务套件的一部分,基于 Netflix Eureka 做了二次封装,主要负责实现微服务架构中的服务治理功能。
Spring Cloud Eureka 是一个基于 REST 的服务,并且提供了基于 Java 的客户端组件,能够非常方便地将服务注册到 Spring Cloud Eureka 中进行统一管理。
Eureka server:创建服务注册中心
环境准备:
创建一个SpringBoot Initialize项目,详情可以参考我之前博客:SpringBoot系列之快速创建项目教程
pom,加上spring-cloud-starter-netflix-eureka-server
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
@EnableEurekaServer配置Eureka服务端:
package com.example.springcloud.server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class SpringcloudEurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudEurekaServerApplication.class, args);
}
}
eureka服务端配置:
spring:
application:
name: eurka-server
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://localhost:8761/eureka/
启动项目,访问:http://localhost:8761
在Eureka中,服务提供者和服务消费者是Eureka client提供的,使用注解@EnableEurekaClient
标明
新建SpringBoot Initializer项目
server:
port: 8081
spring:
application:
name: eureka-service-provider
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
register-with-eureka: true
fetch-registry: true
healthcheck:
enabled: false
instance:
status-page-url-path: http://localhost:8761/actuator/info
health-check-url-path: http://localhost:8761/actuator//health
prefer-ip-address: true
instance-id: eureka-service-provider8081
写个例子,以github用户为例:
package com.example.springcloud.provider.bean;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import java.io.Serializable;
/**
*
* Github User
*
*
*
* @author mazq
* 修改记录
* 修改后版本: 修改人: 修改日期: 2020/07/27 17:38 修改内容:
*
*/
@JsonIgnoreProperties(ignoreUnknown = true)
@Data
public class User implements Serializable {
private String name;
private String blog;
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", blog='" + blog + '\'' +
'}';
}
}
读取github用户信息:
package com.example.springcloud.provider.service;
import www.lanboyulezc.cn com.example.springcloud.provider.bean.User;
import www.fengminpt.cn lombok.extern.slf4j.Slf4j;
import www.shentuylgw.cn org.springframework.boot.web.client.RestTemplateBuilder;
import www.xingyunylpt.com org.springframework.stereotype.Service;
import www.lecaixuanzc.cn org.springframework.web.client.RestTemplate;
/**
*
* UserService
*
*
*
* @author mazq
* 修改记录
* 修改后版本: 修改人: 修改日期: 2020/07/27 17:42 修改内容:
*
*/
@Service
@Slf4j
public class UserService {
private final RestTemplate restTemplate;
public UserService(RestTemplateBuilder restTemplateBuilder) {
this.restTemplate = restTemplateBuilder.build(www.jucaiyle.cn);
}
public User findUser(String user) throws InterruptedException {
log.info("username[{www.tyyleapp.com}]" , user);
String url =www.shangdu2zc.cn String.format("https://api.github.com/users/%s", user);
User results = restTemplate.getForObject(url, User.class);
return results;
}
}
@EnableEurekaClient
指定eureka client:
package com.example.springcloud.provider;
import www.yongshiyule178.com com.example.springcloud.provider.bean.User;
import com.example.springcloud.provider.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import www.chuancenpt.com org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import www.jintianxuesha.com org.springframework.web.bind.annotation.GetMapping;
import www.tengyao3zc.cn org.springframework.web.bind.annotation.PathVariable;
import www.feihongyul.cn org.springframework.web.bind.annotation.ResponseBody;
import www.jujinyule.com org.springframework.web.bind.annotation.RestController;