美文网首页
SpringCloud微服务架构-代码篇

SpringCloud微服务架构-代码篇

作者: Tomthy | 来源:发表于2018-04-24 17:58 被阅读0次

1.创建Eureka服务器
创建springboot项目,在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目录下创建application.yml配置文件,并进行server的配置,包括名称、端口、地址等。

server:
  port: 8761   #HTTP端口
eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false  #表明是server
    fetchRegistry: false        #表明是server
    serviceUrl:
      defaultZone: http://localhost:${server.port}/eureka/

配置完成后,在启动类中添加注解。

/**
 * @EnableEurekaServer  :服务注册中心
 */
@EnableEurekaServer
@SpringBootApplication
public class ServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServerApplication.class, args);
    }
}

目录结构如下

image.png
启动工程,访问地址http://localhost:8761/,可以看到服务注册中心中还没有服务注册。
image.png
2.服务注册
创建springboot项目(业务工程项目),编写好自己的项目后,在resource文件夹下的application.properties(也可使用application.yml文件,由于我是之前开发好的项目,所以沿用了之前的配置文件)配置文件中配置Eureka服务器的信息。
#数据库配置
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
#注册服务器地址
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文件中添加服务提供者所需依赖

        <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>

启动类上添加注解

@EnableEurekaClient
@EnableAutoConfiguration
@SpringBootApplication
@MapperScan("com.yc.charging.dao")  //扫描mapper接口
public class ChargingServiceApplication{
    public static void main(String[] args) {
        SpringApplication.run(ChargingServiceApplication.class, args);
    }
}

完整的项目目录如下

image.png
启动项目,输入地址http://localhost:8761/,可看到charging-service已经注册到了Eureka服务器(服务注册中心)。
image.png
3.服务网关
通常外部使用微服务提供的接口并不是直接调用,而是通过服务网关调用,这样便于对微服务的统一管理。接下来创建一个springboot项目,然后在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文件夹下创建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/

在启动类上添加注解

@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
public class GatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }
}

启动GatewayApplication项目,访问http://localhost:8761/,可以看到网关已经注册到了服务器。

image.png
4.测试
在charging-service项目中有一个接口
image.png
在浏览器中输入地址http://localhost:8762/business/test,可以看到返回字符串“hello world”,接口请求成功。
image.png
接下来使用服务网关来请求该接口。在浏览器中输入地址http://localhost:8765/charging/business/test,可以看到返回字符串“
hello world“,说明网关转发请求成功。
image.png
说明:地址http://localhost:8765/charging/business/test中,端口号8765说明请求的是服务网关。charging/business/test因为前缀是charging,根据网关中的路由配置,会转发到charging-service中的business/test接口,然后请求成功返回字符串。
5.负载均衡和断路器之后更新,敬请关注。

相关文章

网友评论

      本文标题:SpringCloud微服务架构-代码篇

      本文链接:https://www.haomeiwen.com/subject/yeijlftx.html