使用STS建立EurekaServer工程。java
點擊File —— New —— Spring Starter Project後,會彈出建立SpringBoot工程的窗口,輸入工程信息後點擊下一步。git
選擇SpringBoot的版本,並搜索eureka,勾選Eureka Server後,點擊Finish建立工程。github
建立好的工程目錄結構以下:web
工程會自動爲咱們建立maven結構的工程,而且建立了OwlBookstoreEurekaServerApplication類和對應的測試類,在resources文件夾下還有一個空白的application.properties屬性文件。spring
爲了將工程做爲Eureka註冊中心,咱們在建立工程時引入了Eureka Server包,在pom文件中能夠看到:apache
<?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> <groupId>cn.net.bysoft</groupId> <artifactId>owl-bookstore-eureka-server</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>owl-bookstore-eureka-server</name> <description>owl-bookstore-eureka-server</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <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>Dalston.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- 熱部署 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <scope>true</scope> </dependency> </dependencies> <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> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <!-- 熱部署插件 --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> </configuration> </plugin> </plugins> </build> </project>
接下來,咱們修改配置信息,啓動Eureka Server。tomcat
SpringBoot支持yml格式的配置文件,在resources中建立一個application.yml文件,並編寫以下內容:springboot
spring: application: name: eureka-server server: port: 8761 eureka: instance: hostname: localhost client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ debug: true logging: file: wol-bookstore-eureka-server.log level: root: info
最後用@EnableEurekaServer修飾主類OwlBookstoreEurekaServerApplication。bash
package cn.net.bysoft.owl.bootstore; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; /** * <ul> * <li>使用<em>@EnableEurekaServer</em>來修飾主類;</li> * </ul> * * @author xuepeng */ @SpringBootApplication @EnableEurekaServer public class OwlBookstoreEurekaServerApplication { public static void main(String[] args) { SpringApplication.run(OwlBookstoreEurekaServerApplication.class, args); } }
使用SpringBootApp運行程序,程序運行後訪問地址http://localhost:8761來進入註冊中心。這裏須要注意,雖然配置的是http://${eureka.instance.hostname}:${server.port}/eureka/,可是註冊中心的地址不帶有/eureka。app
到此,Eureka註冊中心已經運行成功了。
建立一個服務,命名爲owl-bookstore-service-user。
在pom文件中,加入spring-cloud-starter-eureka依賴。
<?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> <groupId>cn.net.bysoft</groupId> <artifactId>owl-bookstore-service-user</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>owl-bookstore-service-user</name> <description>owl-bookstore-service-user</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <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>Dalston.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <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> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ server: port: 7001 spring: application: name: SERVICE-USER
cn.net.bysoft.owl.bookstore.service.user包下存放着主類,使用@EnableDiscoveryClient修飾主類:
package cn.net.bysoft.owl.bookstore.service.user; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class OwlBookstoreServiceUserApplication { public static void main(String[] args) { SpringApplication.run(OwlBookstoreServiceUserApplication.class, args); } }
cn.net.bysoft.owl.bookstore.service.user.entity包下存放user服務的實體類:
package cn.net.bysoft.owl.bookstore.service.user.entity; public class User { private final long id; private final String name; public User(long id, String name) { this.id = id; this.name = name; } public long getId() { return id; } public String getName() { return name; } }
cn.net.bysoft.owl.bookstore.service.user.facade包下存放着具體的user服務:
package cn.net.bysoft.owl.bookstore.service.user.facade; import java.util.List; import cn.net.bysoft.owl.bookstore.service.user.entity.User; public interface UserService { List<User> getAll(); }
cn.net.bysoft.owl.bookstore.service.user.facade.impl包下存放着服務的實現,用@RestController修飾類,用@RequestMapping修飾方式,@Value用來讀取application.yml文件中的server.port的值:
package cn.net.bysoft.owl.bookstore.service.user.facade.impl; import java.util.ArrayList; import java.util.List; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import cn.net.bysoft.owl.bookstore.service.user.entity.User; import cn.net.bysoft.owl.bookstore.service.user.facade.UserService; @RestController public class UserServiceImpl implements UserService { @Value("${server.port}") String port; @RequestMapping(value = "users", method = RequestMethod.GET) @Override public List<User> getAll() { List<User> users = new ArrayList<>(); users.add(new User(1L, port)); return users; } }
啓動服務,並訪問服務地址:http://localhost:7001/users,測試服務是否啓動成功:
經過上圖能夠看到服務已經啓動成功,接下來刷新註冊中心,會發現服務已經註冊到Eureka Server中了:
至此,服務的註冊已經完成。
這裏建立一個bookstore後臺管理的springboot工程,選擇war包方式:
引入如下依賴:
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> <groupId>cn.net.bysoft</groupId> <artifactId>owl-bookstore-web-console</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>owl-bookstore-web-console</name> <description>owl-bookstore-service-user</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <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>Dalston.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <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> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
spring: application: name: owl-bookstore-web-console eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ server: port: 8000
鏈接到數據中心,並將應用端口設置爲8000。
cn.net.bysoft.owl.bookstore.web.console包下存放着主類,使用@EnableDiscoveryClient來修飾它,在類中定義了一個RestTemplate,使用它以rest風格來消費(使用)服務,這個類還被@LoadBalanced所修飾,意爲啓動負載均衡。
package cn.net.bysoft.owl.bookstore.web.console; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication @EnableDiscoveryClient public class OwlBookstoreWebConsoleApplication { @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(OwlBookstoreWebConsoleApplication.class, args); } }
cn.net.bysoft.owl.bookstore.web.console.service包內存放的是業務實現類,自動裝配RestTemplate,並以rest風格,經過http協議,向服務發送get請求。
此處,uri的地址爲服務的[註冊名]/[requestmapping]:
package cn.net.bysoft.owl.bookstore.web.console.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; @Service public class UserService { @Autowired RestTemplate restTemplate; public String getAll() { return restTemplate.getForEntity("http://SERVICE-USER/users", String.class).getBody(); } }
cn.net.bysoft.owl.bookstore.web.console.controller包內存放着控制器:
package cn.net.bysoft.owl.bookstore.web.console.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import cn.net.bysoft.owl.bookstore.web.console.service.UserService; @RestController(value = "/") public class UserController { @Autowired UserService userService; @RequestMapping(value = "users") public String index() { return userService.getAll() + " ==> console"; } }
爲了測試負載均衡,咱們經過命令行的方式啓動兩個service-user:
如今刷新註冊中心就會發現SERVICE-USER有兩個:
隨後,啓動owl-bookstore-web-console應用,刷新註冊中心,會看到該應用也註冊成功:
如今,訪問http://localhost:8000/users,並刷新頁面,會看到應用經過輪詢的方式消費7001端口與7002端口的服務了。
以上,就是服務註冊與消費的簡單demo。