1.用spring腳手架搭建eureka註冊中心
然後輸入模塊名稱,以及包名
然後選擇對應的服務端,接着,選擇版本,記得要統一版本
這樣點擊下一步,然後保存就可以了。
接着需要配置pom.xml
我這裏選擇的是2.1.13版本的spring-boot ,對應配置的springcloud 的版本爲Greenwich.SR1
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
這裏是部分配置文件,直接添加到對應的pom.xml文件的位置就好了。
然後是配置application.yml配置
server:
port: 10086
spring:
application:
name: service-eureka
eureka:
client:
service-url:
defaultZone: http://localhost:10086/eureka
端口號自己對應的改一下就可以了。
最後在啓動的mian方法的類中添加標籤@EnableEurekaServer,就完成了配置
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class ServiceEureka01Application {
public static void main(String[] args) {
SpringApplication.run(ServiceEureka01Application.class, args);
}
}
這裏註冊中心就配置完成了。
接下來是配置生產者,配置自己需要的腳手架
我這裏首先配置了web還有sql,因爲我這邊生產者需要連接數據庫。
然後就是配置pom.xml文件
<properties>
<!--<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>-->
<!--<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>-->
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
然後配置application.yml配置文件
server:
port: 8080
spring:
application:
name: service-provider
datasource:
password: test
username: root
url: jdbc:mysql:///test
driverClassName: com.mysql.jdbc.Driver
main:
allow-bean-definition-overriding: true
eureka:
client:
service-url: # EurekaServer地址
defaultZone: http://127.0.0.1:10086/eureka
接着就是配置啓動類
package com.w.service;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class ServiceProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceProviderApplication.class, args);
}
}
主要是加上註解@EnableDiscoveryClient註解
這樣生產者也配置進去了
配置生產者和消費者是一樣的配置
最後在調用的時候添加上
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Controller;
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.ResponseBody;
import org.springframework.web.client.RestTemplate;
import java.util.List;
@Controller
@RequestMapping("/customer/user")
public class UserController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private DiscoveryClient discoveryClient;
@GetMapping("{id}")
@ResponseBody
public User findById(@PathVariable("id") Integer id) {
List<ServiceInstance> instances = discoveryClient.getInstances("service-provider");
ServiceInstance serviceInstance = instances.get(0);
return restTemplate.getForObject("http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/user/" + id, User.class);
}
}
消費者也就配置完成了,這裏中間的配置生產者的連接數據庫等操作自己可以做一下,很簡單,這裏不再贅述了。
當我們想配置多個註冊中心集羣的時候,可以直接copy註冊中心的啓動類即可
右鍵複製一個啓動類即可,這樣修改的時候,就可以啓動一個eureka註冊中心後,繼續啓動另一個註冊中心,只需要修改application.yml文件即可。
server:
port: 10086
spring:
application:
name: service-eureka
eureka:
client:
service-url:
defaultZone: http://localhost:10087/eureka
比如我這裏配置了一個10086端口,去註冊10087,然後再配置一個10086去訪問10086,這樣就可以搭建一個小的集羣了
最後打開註冊中心,注意不需要輸入最後的那個/eureka,就可以看到註冊中心裏的服務了。