项目结构

nemo-cloud
POM
<?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>
<packaging>pom</packaging>
<modules>
<module>my-client</module>
<module>my-service</module>
<module>eureka-service</module>
<module>my-service-client</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.6</version>
<relativePath/>
</parent>
<groupId>org.example</groupId>
<artifactId>nemo-cloud</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<java.version>1.8</java.version>
<spring-cloud.version>2021.0.1</spring-cloud.version>
</properties>
<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>
</project>eureka-service

pom
<?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">
<parent>
<artifactId>nemo-cloud</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>eureka-service</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>EurekaServiceSpringBootApplication
@EnableEurekaServer
@SpringBootApplication
public class EurekaServiceSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServiceSpringBootApplication.class, args);
}
}application.properties
spring.application.name=eureka-service
#服务注册中心端口号
server.port=8761
#服务注册中心实例的主机名
eureka.instance.hostname=localhost
#指向服务注册中心地址
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
#是否向服务注册中心注册自己
#eureka.client.register-with-eureka=falsemy-service-client(公共类)
公共类,共享服务接口

POM
<?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">
<parent>
<artifactId>nemo-cloud</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>my-service-client</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
</project>MyService
@FeignClient("my-service")
public interface MyService {
@GetMapping("/getHostAddress")
String getHostAddress();
}my-service(服务提供者)

POM
<?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">
<parent>
<artifactId>nemo-cloud</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>my-service</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<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>
<dependency>
<groupId>org.example</groupId>
<artifactId>my-service-client</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>MyServiceSpringBootApplication
//启用服务注册客户端
@EnableDiscoveryClient
//spring boot启动注解
@SpringBootApplication
public class MyServiceSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(MyServiceSpringBootApplication.class, args);
}
}application.properties
spring.application.name=my-service server.port=9001 #指向服务注册中心地址 eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
MyServiceImpl
@RestController
public class MyServiceImpl implements MyService{
@RequestMapping("/getHostAddress")
@Override
public String getHostAddress() {
try {
InetAddress address = InetAddress.getLocalHost();
return address.getHostAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
}
return null;
}
}my-client(服务调用者)

POM
<?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">
<parent>
<artifactId>nemo-cloud</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>my-client</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<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>
<dependency>
<groupId>org.example</groupId>
<artifactId>my-service-client</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
</project>ClientController
@RestController
public class ClientController {
@Autowired
private DiscoveryClient discoveryClient;
@Autowired
private MyService myService;
/**
* 自己通过eureka获取服务端信息
*/
@RequestMapping("/getUrl")
public String getUrl(){
//获取注册到eureka server实例列表
List<ServiceInstance> instances = discoveryClient.getInstances("my-service");
// 获取第一个服务信息
ServiceInstance instanceInfo = instances.get(0);
//获取ip
String ip = instanceInfo.getHost();
//获取port
int port = instanceInfo.getPort();
String url ="http://"+ip+":"+port;
return url;
}
/**
* 通过openfeign调用服务端接口
*/
@RequestMapping("/getHostAddress")
public String getHostAddress(){
return myService.getHostAddress();
}
}MyClientSpringBootApplication
//扫描包路径下的@FeignClient
@EnableFeignClients(basePackages = "com.service")
@EnableDiscoveryClient
@SpringBootApplication
public class MyClientSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(MyClientSpringBootApplication.class, args);
}
}application.properties
spring.application.name=my-client server.port=8001 #指向服务注册中心地址 eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
测试
是否注册成功

测试通过eureka获取服务端信息

测试通过openfeign调用服务端接口

版权声明:本文为huijiangu4621原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。