项目结构
实例实现功能:实现通过id查询用户的订单信息
OrderCommon:公共的一些模块类型,此处为一个user对象
Eureka-Service:配置Eureka的启动类,服务端
Order-Service:提供查询功能的服务端
Order-Client:查询的客户端
OrderCommon代码
package org.example.domain;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;/*** @ClassName Order* @Author 23* @Date 2024/7/10 18:17* @Version 1.0* @Description TODO**/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Order {//定义一个信息类private Integer id;private String username;private Integer userid;private String orderinformation;
}
Eureka-Service代码
package org.example;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;/*** @ClassName EurekaStart* @Author 23* @Date 2024/7/10 19:12* @Version 1.0* @Description TODO**/
@SpringBootApplication
@EnableEurekaServer
public class EurekaStart {public static void main(String[] args) {SpringApplication.run(EurekaStart.class,args);}
}
yml配置:
server:port: 10077
eureka:instance:hostname: localhostclient:registerWithEureka: falsefetchRegistry: falseserviceUrl:defaultZone: yihttp://${eureka.instance.hostname}:${server.port}/eureka/
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>org.example</groupId><artifactId>Springcloud-Netflix</artifactId><version>1.0-SNAPSHOT</version></parent><artifactId>Eureka-Service</artifactId><packaging>jar</packaging><name>Eureka-Service</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency><!-- Eureka服务端支持 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency></dependencies>
</project>
Order-Service代码
controller
package org.example.Controller;import org.example.domain.Order;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;/*** @ClassName OderController* @Author 23* @Date 2024/7/10 18:33* @Version 1.0* @Description TODO**/
@RestController
@RequestMapping("/OrderService")
public class OderController {@GetMapping("/user/{id}")public List<Order> getOrderByUserid(@PathVariable("id") Integer id){return Arrays.asList(new Order(1,"Jack",071021,"尊贵的vip用户"));}}
启动类
package org.example;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;/*** @ClassName OrderStart* @Author 23* @Date 2024/7/10 18:34* @Version 1.0* @Description TODO**/
@SpringBootApplication
@EnableDiscoveryClient
public class OrderStart {public static void main(String[] args) {SpringApplication.run(OrderStart.class,args);}
}
yml配置
server:port: 10010
spring:application:name: order-service
eureka:client:service-url:defaultZone: http://localhost:10077/eureka #告诉服务提供者要把服务注册到哪儿instance:prefer-ip-address: true # 当调用getHostname获取实例的hostname时,返回ip而不是host名称
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>org.example</groupId><artifactId>Springcloud-Netflix</artifactId><version>1.0-SNAPSHOT</version></parent><artifactId>Order-Service</artifactId><packaging>jar</packaging><name>Order-Service</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><!--变成一个springboot项目--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><!--继承一下common--><dependency><groupId>org.example</groupId><artifactId>Order-Common</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency></dependencies>
</project>
Order-Client代码
BeanConfig
package org.example.Config;/*** @ClassName BeanConfig* @Author 23* @Date 2024/7/10 18:56* @Version 1.0* @Description TODO**/
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;//<beans>
@Configuration //<beans><bean></bean</beans>
public class BeanConfig {@Bean //<bean></bean 把new出来的对象放入spring管理public RestTemplate restTemplate(){return new RestTemplate();}
}
UserController
package org.example.Controller;import org.example.domain.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;import java.util.Arrays;
import java.util.List;/*** @ClassName UserController* @Author 23* @Date 2024/7/10 18:52* @Version 1.0* @Description TODOj**/
@RestController
@RequestMapping("/User")
public class UserController {@Autowiredprivate RestTemplate restTemplate;@Autowiredprivate DiscoveryClient discoveryClient;@GetMapping("/getOrder/{id}")public List<Order> getOrderById(@PathVariable("id") Integer id) {
// Order[] order = restTemplate.getForObject("http://localhost:10010/OrderService/user/" + id, Order[].class);
// return Arrays.asList(order);List<ServiceInstance> instances=discoveryClient.getInstances("order-service");//通过服务的名字去获取服务实例ServiceInstance serviceInstance=instances.get(0);//定死只获取第一个服务实例对象String ip=serviceInstance.getHost();//获取服务对象ipint port=serviceInstance.getPort();//获取获取的实例对象的服务端口号Order[] orders=restTemplate.getForObject("http://"+ip+":"+port+"/OrderService/user/"+id,Order[].class);return Arrays.asList(orders);}
}
User启动类
package org.example;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;/*** @ClassName ClientStart* @Author 23* @Date 2024/7/10 18:52* @Version 1.0* @Description TODO**/
@SpringBootApplication
@EnableDiscoveryClient
public class UserStart {public static void main(String[] args) {SpringApplication.run(UserStart.class,args);}
}
yml配置
server:port: 10020
spring:application:name: user-service
eureka:client:service-url:defaultZone: http://localhost:10077/eurekainstance:prefer-ip-address: true
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>org.example</groupId><artifactId>Springcloud-Netflix</artifactId><version>1.0-SNAPSHOT</version></parent><artifactId>Order-Client</artifactId><packaging>jar</packaging><name>Order-Client</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency><dependency><groupId>org.example</groupId><artifactId>Order-Common</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency></dependencies>
</project>