<?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>
<?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>
@EnableEurekaServer @SpringBootApplication public class EurekaServiceSpringBootApplication { public static void main(String[] args) { SpringApplication.run(EurekaServiceSpringBootApplication.class, args); } }
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=false
公共类,共享服务接口
<?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>
@FeignClient("my-service") public interface MyService { @GetMapping("/getHostAddress") String getHostAddress(); }
<?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>
//启用服务注册客户端 @EnableDiscoveryClient //spring boot启动注解 @SpringBootApplication public class MyServiceSpringBootApplication { public static void main(String[] args) { SpringApplication.run(MyServiceSpringBootApplication.class, args); } }
spring.application.name=my-service server.port=9001 #指向服务注册中心地址 eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
@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; } }
<?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>
@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(); } }
//扫描包路径下的@FeignClient @EnableFeignClients(basePackages = "com.service") @EnableDiscoveryClient @SpringBootApplication public class MyClientSpringBootApplication { public static void main(String[] args) { SpringApplication.run(MyClientSpringBootApplication.class, args); } }
spring.application.name=my-client server.port=8001 #指向服务注册中心地址 eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
因篇幅问题不能全部显示,请点此查看更多更全内容