1 在根项目下创建服务生产项目springcloud-demo-a
pom.xml如下:
<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>
<parent>
<groupId>com.mxx.springcloud</groupId>
<artifactId>springcloud-root</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>springcloud-demo-a</artifactId>
<name>springcloud-demo-a</name>
<description>springcloud-demo-a</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>
spring-cloud-starter-netflix-eureka-client
</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- spring-admin-actuator相关 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
</project>
启动类:DemoAServiceApplication
package com.mxx.springcloud.eureka.client;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableEurekaClient
@RestController
@RequestMapping(value = "/actuator")
public class DemoAServiceApplication {
public static void main(String[] args) {
SpringApplication.run(DemoAServiceApplication.class, args);
}
@RequestMapping(value = "/info")
public String info() {
return "ok";
}
}
配置文件application.yml:
#端口
server:
port: 8001
#应用名称
spring:
application:
name: euraka-client-a
#eureka
eureka:
instance:
instance-id: euraka-client-a-local:8001
prefer-ip-address: true
client:
service-url:
defaultZone: http://192.168.125.66:8888/eureka/
#管理端点的配置
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS
2 服务DemoAController实例:
package com.mxx.springcloud.eureka.client;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value="/a")
public class DemoAController {
@Value("${server.port}")
private String port;
@RequestMapping(value="/hello")
public String hello(){
return "hello"+this.port+","+System.currentTimeMillis();
}
}
本文档介绍了如何在SpringCloud环境中创建服务生产者项目。首先,在根项目下创建名为springcloud-demo-a的服务生产项目,配置pom.xml并提供启动类DemoAServiceApplication。接着,详细展示了服务DemoAController的实例化过程。
168万+

被折叠的 条评论
为什么被折叠?



