整体框架
由于整个微服务使用springcloud进行管理,所以采用maven聚合工程
导包
最好使用父项目进行版本控制
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR4</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.2.5.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies></dependencyManagement>
注意点:maven中和Java类似没有多继承这个概念,间接实现多继承就可以使用import对包进行导入,这样子包就能继承版本控制
注册中心
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency></dependencies>
客户端(服务提供者也是客户端)
<dependencies> <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.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!--实现负载均衡--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> <!--显示info信息--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency></dependencies>
application配置
注册中心
server: port: 8000eureka: client: register-with-eureka: false # 不讲自己注册进注册中心 fetch-registry: false # 不拉取服务
在springboot应用中开启EurekaServer
@SpringBootApplication@EnableEurekaServerpublic class RegistryApplication { public static void main(String[] args) { SpringApplication.run(RegistryApplication.class); }}
服务提供者或消费者
server: port: 8081eureka: client: service-url: defaultZone: http://localhost:8000/eureka # 服务中心的地址,','隔开可以写多个实现集群 instance: instance-id: consumer-port:8081 # status的名称spring: application: name: consumer # 服务的名称debug: true# 配置自己的信息info: app.name: roderick
在springboot应用中开启EurekaServer
@SpringBootApplication@EnableEurekaClientpublic class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class); }}
消费者调用提供者Rest接口
这里使用RestTemplate连接客户端
@RestControllerpublic class MainController { RestTemplate restTemplate; DiscoveryClient discoveryClient; private static final String URL = "http://localhost:8100"; private static final String ID_URL = "http://CONSUMER"; //通过id直接获取(内部玩法) @Autowired public void setRestTemplate(RestTemplate restTemplate) { this.restTemplate = restTemplate; } @Autowired public void setDiscoveryClient(DiscoveryClient discoveryClient) { this.discoveryClient = discoveryClient; } @GetMapping("/get/{id}") public String getUser(@PathVariable("id") String id) { return restTemplate.getForObject(ID_URL + "/test/" + id, String.class); } @GetMapping("/services") public String getService() { //获取服务列表 Map<String, List<ServiceInstance>> map = new HashMap<>(); List<String> services = discoveryClient.getServices(); for (String service : services) { List<ServiceInstance> instances = discoveryClient.getInstances(service); map.put(service, instances); } return map.toString(); }}
代码中将URL写死是不行的,我们可以使用DiscoveryClient获取或者直接将服务名作为url,通过解析可以获取到服务的真实地址