1.創(chuàng)建Eureka服務(wù)器
創(chuàng)建springboot項(xiàng)目,在pom.xml文件中添加下面依賴。
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
在resource目錄下創(chuàng)建application.yml配置文件,并進(jìn)行server的配置,包括名稱、端口、地址等。
server:
port: 8761 #HTTP端口
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false #表明是server
fetchRegistry: false #表明是server
serviceUrl:
defaultZone: http://localhost:${server.port}/eureka/
配置完成后,在啟動(dòng)類中添加注解。
/**
* @EnableEurekaServer :服務(wù)注冊(cè)中心
*/
@EnableEurekaServer
@SpringBootApplication
public class ServerApplication {
public static void main(String[] args) {
SpringApplication.run(ServerApplication.class, args);
}
}
目錄結(jié)構(gòu)如下

image.png
啟動(dòng)工程,訪問(wèn)地址http://localhost:8761/,可以看到服務(wù)注冊(cè)中心中還沒(méi)有服務(wù)注冊(cè)。

image.png
2.服務(wù)注冊(cè)
創(chuàng)建springboot項(xiàng)目(業(yè)務(wù)工程項(xiàng)目),編寫好自己的項(xiàng)目后,在resource文件夾下的application.properties(也可使用application.yml文件,由于我是之前開(kāi)發(fā)好的項(xiàng)目,所以沿用了之前的配置文件)配置文件中配置Eureka服務(wù)器的信息。
#數(shù)據(jù)庫(kù)配置
spring.datasource.url = jdbc:mysql://127.0.0.1:3306/yc_charging?characterEncoding=utf8
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName = com.mysql.jdbc.Driver
#注冊(cè)服務(wù)器地址
eureka.client.serviceUrl.defaultZone: http://localhost:8761/eureka/
server.port: 8762
spring.application.name: charging-service
##指向mapper的xml文件位置
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.yc.charging.entity
logging.level.com.yc.charging.mapper=debug
在pom.xml文件中添加服務(wù)提供者所需依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>1.4.4.RELEASE</version>
</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>
<scope>test</scope>
</dependency>
啟動(dòng)類上添加注解
@EnableEurekaClient
@EnableAutoConfiguration
@SpringBootApplication
@MapperScan("com.yc.charging.dao") //掃描mapper接口
public class ChargingServiceApplication{
public static void main(String[] args) {
SpringApplication.run(ChargingServiceApplication.class, args);
}
}
完整的項(xiàng)目目錄如下

image.png
啟動(dòng)項(xiàng)目,輸入地址http://localhost:8761/,可看到charging-service已經(jīng)注冊(cè)到了Eureka服務(wù)器(服務(wù)注冊(cè)中心)。

image.png
3.服務(wù)網(wǎng)關(guān)
通常外部使用微服務(wù)提供的接口并不是直接調(diào)用,而是通過(guò)服務(wù)網(wǎng)關(guān)調(diào)用,這樣便于對(duì)微服務(wù)的統(tǒng)一管理。接下來(lái)創(chuàng)建一個(gè)springboot項(xiàng)目,然后在pom.xml中添加依賴。
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>1.4.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>1.4.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
在resource文件夾下創(chuàng)建application.yml配置文件
spring:
application:
name: gateway
server:
port: 8765
#配置路由
zuul:
routes:
charging:
path: /charging/**
serviceId: charging-service
coupon:
path: /coupon/**
serviceId: coupon-service
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
在啟動(dòng)類上添加注解
@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
啟動(dòng)GatewayApplication項(xiàng)目,訪問(wèn)http://localhost:8761/,可以看到網(wǎng)關(guān)已經(jīng)注冊(cè)到了服務(wù)器。

image.png
4.測(cè)試
在charging-service項(xiàng)目中有一個(gè)接口

image.png
在瀏覽器中輸入地址http://localhost:8762/business/test,可以看到返回字符串“hello world”,接口請(qǐng)求成功。

image.png
接下來(lái)使用服務(wù)網(wǎng)關(guān)來(lái)請(qǐng)求該接口。在瀏覽器中輸入地址http://localhost:8765/charging/business/test,可以看到返回字符串“
hello world“,說(shuō)明網(wǎng)關(guān)轉(zhuǎn)發(fā)請(qǐng)求成功。

image.png
說(shuō)明:地址http://localhost:8765/charging/business/test中,端口號(hào)8765說(shuō)明請(qǐng)求的是服務(wù)網(wǎng)關(guān)。charging/business/test因?yàn)榍熬Y是charging,根據(jù)網(wǎng)關(guān)中的路由配置,會(huì)轉(zhuǎn)發(fā)到charging-service中的business/test接口,然后請(qǐng)求成功返回字符串。
5.負(fù)載均衡和斷路器之后更新,敬請(qǐng)關(guān)注。