基本上和服务提供者1一样
1、新建项目,选择Create New Project
2、 下一步
3、下一步
4、下一步
5、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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.chenclass</groupId>
<artifactId>xaeurekaserviceprovider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>xaeurekaserviceprovider</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR2</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>
6、 设置application.properties
server.port=8763
spring.application.name=xaeurekaserviceprovider2
eureka.client.service-url.defaultZone: http://localhost:8761/eureka/
7、修改Application
package com.chenclass.xaeurekaserviceprovider2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan("com.chenclass")
@EnableDiscoveryClient
@SpringBootApplication
public class Xaeurekaserviceprovider2Application {
public static void main(String[] args) {
SpringApplication.run(Xaeurekaserviceprovider2Application.class, args);
}
}
8、添加controller包
9、在controller包添加类TestController
package com.chenclass.xaeurekaserviceprovider2.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Value("${server.port}")
String port;
@RequestMapping("/test")
public String test(){
return "server2 被调用了!:" +port;
}
}