演示项目源码下载:(访问密码:9987)
Spring-Cloud-discovery-server.zip
了解如何创建微服务的基础上,Spring Cloud,对Netflix的Eureka注册表服务器以及如何等微服务(Eureka客户端)使用它来注册和发现服务来调用其API。
我们将使用基于 Spring Boot 的 Spring Cloud API。我们将使用 Netflix Eureka 服务器来构建服务注册服务器和 Eureka 客户端,这些客户端将自行注册并发现其他服务以调用 REST API。
Eureka server
和Actuator
。给出其他maven GAV坐标并下载项目。SpringEurekaServerApplication
在下载的项目中打开spring已经生成的类,并@EnableEurekaServer
在类上添加注释。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class SpringEurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringEurekaServerApplication.class, args);
}
}
再次构建项目。有了这个注解,这个工件就会像微服务注册中心和发现服务器一样工作。
application.yml
在src\main\resources
目录中调用的文件。添加这些属性 -
server:
port: ${PORT:8761} # Indicate the default PORT where this service will be started
eureka:
client:
registerWithEureka: false #telling the server not to register himself in the service registry
fetchRegistry: false
server:
waitTimeInMsWhenSyncEmpty: 0 #wait time for subsequent sync
创建另一个bootstrap.yml
在src\main\resources
目录中调用的文件。添加这些属性 -
spring:
application:
name: eureka
cloud:
config:
uri: ${CONFIG_SERVER_URL:http://localhost:8888}
http://localhost:8761/
,您应该会看到如下所示的 eureka 服务器主页Actuator
、Web
、Rest Repositories
、Eureka Discovery
。给出其他maven GAV坐标并下载项目。@EnableEurekaClient
在src
文件夹中存在的 Spring boot 应用程序类上添加注释。有了这个注解,这个工件将像一个 spring 发现客户端一样,并将在附加到这个服务的Eureka服务器中注册自己。import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class SpringEurekaClientStudentServiceApplication {
public static void main(String[] args) {
SpringApplication.run(SpringEurekaClientStudentServiceApplication.class, args);
}
}
application.yml
在src\main\resources
目录中调用的文件并添加以下行。
server:
port: 8098 #default port where the service will be started
eureka: #tells about the Eureka server details and its refresh time
instance:
leaseRenewalIntervalInSeconds: 1
leaseExpirationDurationInSeconds: 2
client:
serviceUrl:
defaultZone: http://127.0.0.1:8761/eureka/
healthcheck:
enabled: true
lease:
duration: 5
spring:
application:
name: student-service #current service name to be used by the eureka server
management:
security:
enabled: false #disable the spring security on the management endpoints like /env, /refresh etc.
logging:
level:
com.example.howtodoinjava: DEBUG
RestController
并公开一个 rest 端点,用于获取特定学校的所有学生详细信息。在这里,我们公开/getStudentDetailsForSchool/{schoolname}
端点以服务于业务目的。为简单起见,我们对学生的详细信息进行了硬编码。
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.example.howtodoinjava.springeurekaclientstudentservice.domain.Student;
@RestController
public class StudentServiceController {
private static Map<String, List<Student>> schooDB = new HashMap<String, List<Student>>();
static {
schooDB = new HashMap<String, List<Student>>();
List<Student> lst = new ArrayList<Student>();
Student std = new Student("Sajal", "Class IV");
lst.add(std);
std = new Student("Lokesh", "Class V");
lst.add(std);
schooDB.put("abcschool", lst);
lst = new ArrayList<Student>();
std = new Student("Kajal", "Class III");
lst.add(std);
std = new Student("Sukesh", "Class VI");
lst.add(std);
schooDB.put("xyzschool", lst);
}
@RequestMapping(value = "/getStudentDetailsForSchool/{schoolname}", method = RequestMethod.GET)
public List<Student> getStudents(@PathVariable String schoolname) {
System.out.println("Getting Student details for " + schoolname);
List<Student> studentList = schooDB.get(schoolname);
if (studentList == null) {
studentList = new ArrayList<Student>();
Student std = new Student("Not Found", "N/A");
studentList.add(std);
}
return studentList;
}
}
Student
class 是一个简单的 POJO。
public class Student
{
private String name;
private String className;
public Student(String name, String className) {
super();
this.name = name;
this.className = className;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
}
/getStudentDetailsForSchool/{schoolname}
端点是否已启动并正在运行。转到浏览器并转到http://localhost:8098/getStudentDetailsForSchool/abcschool
,它将提供特定学校的学生详细信息abcschool
。Actuator
、Web
、Rest Repositories
、Eureka Discovery
。给出其他maven GAV坐标并下载项目。@EnableEurekaClient
在src
文件夹中存在的 Spring boot 应用程序类上添加注释。有了这个注解,这个工件将像一个 spring 发现客户端一样,并将在附加到这个服务的Eureka服务器中注册自己。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class SpringEurekaClientSchoolServiceApplication {
public static void main(String[] args) {
SpringApplication.run(SpringEurekaClientSchoolServiceApplication.class, args);
}
}
application.yml
在src\main\resources
目录中调用的文件并添加以下行。除了端口号和服务名称外,这些配置与学生服务非常相似。
server:
port: 9098 #port number
eureka:
instance:
leaseRenewalIntervalInSeconds: 1
leaseExpirationDurationInSeconds: 2
client:
serviceUrl:
defaultZone: http://127.0.0.1:8761/eureka/
healthcheck:
enabled: true
lease:
duration: 5
spring:
application:
name: school-service #service name
logging:
level:
com.example.howtodoinjava: DEBUG
RestController
并公开一个休息端点以获取学校详细信息。此端点将使用使用应用程序名称的服务发现样式 URL,而不是带有 host:port 的完整 URL。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class SchoolServiceController {
@Autowired
RestTemplate restTemplate;
@RequestMapping(value = "/getSchoolDetails/{schoolname}", method = RequestMethod.GET)
public String getStudents(@PathVariable String schoolname)
{
System.out.println("Getting School details for " + schoolname);
String response = restTemplate.exchange("http://student-service/getStudentDetailsForSchool/{schoolname}",
HttpMethod.GET, null, new ParameterizedTypeReference<String>() {}, schoolname).getBody();
System.out.println("Response Received as " + response);
return "School Name - " + schoolname + " \n Student Details " + response;
}
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
这样我们就可以摆脱特定的服务配置,我们可以将服务查找责任交给Eureka服务器和这里提供的休息模板。@LoadBalanced
如果多个实例正在为同一服务运行,我们也可以在此处应用负载平衡(请参阅注释)。http://student-service/getStudentDetailsForSchool/{schoolname}
. 显然,我们只使用服务名称student-service
来代替host:port
. 这将由 spring 框架、eureka 服务器和 rest 模板一起在内部处理。@EnableEurekaServer
和@EnableEurekaClient
是应用生态系统的心脏。没有这两件事就根本行不通。演示项目源码下载:(访问密码:9987)
Spring-Cloud-discovery-server.zip
地址:https://www.leftso.com/article/861.html