使用Spring Cloud中的Eureka创建微服务注册中心,然后服务提供者向其中注册服务,服务消费者调用已注册的服务。即:服务注册中心(Eureka)、服务提供者(Provider)、服务消费者(Consumer)。
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.micloud</groupId>
<artifactId>miservices</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>miservices</name>
<description>miservices project for Spring Cloud</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties
在默认设置下,服务注册中心也会将"自己"作为客户端来尝试注册它本身,禁用它的客户端注册行为。
spring.application.name=spring-cloud-eureka
server.port=9000
#是否将自己注册到Eureka Server,默认为true
eureka.client.register-with-eureka=false
#是否从Eureka Server获取注册信息,默认为true
eureka.client.fetch-registry=false
#设置与Eureka Server交互的地址,用于查询服务和注册服务
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
CloudApplication.java
@SpringBootApplication
@EnableEurekaServer//启动服务注册中心
public class CloudApplication {
public static void main(String[] args) {
SpringApplication.run(CloudApplication.class, args);
}
}
pom.xml spring boot版本与注册中心保持一致。在尝试使用与注册版本不一致的情况,尚未测试通过!
....
<groupId>com.micloud</groupId>
<artifactId>cloud_provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>cloud_provider</name>
<description>Demo project for Spring cloud_provider</description>
....
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<!-- 引入web模块 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
....
application.properties
spring.application.name=spring-cloud-provider
server.port=8081
eureka.client.serviceUrl.defaultZone=http://localhost:9000/eureka/
CloudProviderApplication.java
@EnableDiscoveryClient
@SpringBootApplication
public class CloudProviderApplication {
public static void main(String[] args) {
SpringApplication.run(CloudProviderApplication.class, args);
}
}
HelloController.java 模拟业务代码
@RestController
public class HelloController {
@RequestMapping("/hello")
public Object hello(@RequestParam String name) {
Map<String,String> obj = new HashMap<String,String>();
obj.put("name", name);
obj.put("memo", "the messge come from Spring-Cloud-Provider");
return obj;
}
}
pom.xml spring boot版本与注册中心保持一致。在尝试使用与注册版本不一致的情况,尚未测试通过!
....
<groupId>com.micloud</groupId>
<artifactId>cloud_consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>cloud_consumer</name>
<description>Demo project for Spring Boot Consumer</description>
....
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
....
application.properties
spring.application.name=spring-cloud-consumer
server.port=8082
eureka.client.serviceUrl.defaultZone=http://localhost:9000/eureka/
# 服务提供者
provider.app.name=SPRING-CLOUD-PROVIDER
CloudConsumerApplication.java
@SpringBootApplication
@EnableDiscoveryClient
public class CloudConsumerApplication
{
@Bean//注册bean,也可以配置在config类中:@Configuration
@LoadBalanced//坑,不仅仅是负载均衡,还可以使用服务名称调用服务,否则报错找不到主机
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main( String[] args )
{
SpringApplication.run(CloudConsumerApplication.class, args);
}
}
ConsumerController.java 模拟调用已注册的SPRING-CLOUD-PROVIDER服务
@RestController
public class ConsumerController {
@Autowired
private RestTemplate restTemplate;
@Value("${provider.app.name}")//从配置文件获取数据
private String serviceName;
@RequestMapping(value = "/testhello")
public String testProviderHello() {
String result = restTemplate.getForObject("http://" + serviceName
+ "/hello?name=tom", String.class);
return "this is requestResult:" + result;
}
}