springCloud之feign使用Denmo

部分代碼,完整代碼可以自行下載,有完整註釋,歡迎討論交流!
github下載地址:
https://github.com/liucile/myRepository/tree/master/microservicecloud

Eureak 集羣

啓動類

package com.hph.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer ////EurekaServer服務器端啓動類,接受其它微服務註冊進來

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

配置文件

啓動不同的配置文件:7001,7002,7003

server:
  port: 7001

eureka:
  instance:
    hostname: eureka7001.com #eureka服務端的實例名稱
  client:
    register-with-eureka: false     #false表示不向註冊中心註冊自己。
    fetch-registry: false     #false表示自己端就是註冊中心,我的職責就是維護服務實例,並不需要去檢索服務
    service-url:
      #單機 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/       #設置與Eureka Server交互的地址查詢服務和註冊服務都需要依賴這個地址(單機)。
      defaultZone: http://127.0.0.1:7002/eureka/,http://127.0.0.1:7003/eureka/
---
server:
  port: 7002

eureka:
  instance:
    hostname: eureka7002.com #eureka服務端的實例名稱
  client:
    register-with-eureka: false     #false表示不向註冊中心註冊自己。
    fetch-registry: false     #false表示自己端就是註冊中心,我的職責就是維護服務實例,並不需要去檢索服務
    service-url:
      #defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/       #設置與Eureka Server交互的地址查詢服務和註冊服務都需要依賴這個地址。
      defaultZone: http://127.0.0.1:7001/eureka/,http://127.0.0.1:7003/eureka/
---
server:
  port: 7003

eureka:
  instance:
    hostname: eureka7003.com #eureka服務端的實例名稱
  client:
    register-with-eureka: false     #false表示不向註冊中心註冊自己。
    fetch-registry: false     #false表示自己端就是註冊中心,我的職責就是維護服務實例,並不需要去檢索服務
    service-url:
      #defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/       #設置與Eureka Server交互的地址查詢服務和註冊服務都需要依賴這個地址。
      defaultZone: http://127.0.0.1:7001/eureka/,http://127.0.0.1:7002/eureka/



pom 依賴

<dependencies>
        <!--eureka-server服務端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
        <!-- 修改後立即生效,熱部署 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>

服務提供者

服務提供者,端口分別8001,8002,8003
在這裏插入圖片描述

配置

server:
  port: 8001

mybatis:
  config-location: classpath:mybatis/mybatis.cfg.xml        # mybatis配置文件所在路徑
  type-aliases-package: com.hph.springcloud.entities    # 所有Entity別名類所在包
  mapper-locations:
    - classpath:mybatis/mapper/**/*.xml                       # mapper映射文件

spring:
  application:
    name: microservicecloud-dept
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource            # 當前數據源操作類型
    driver-class-name: org.gjt.mm.mysql.Driver              # mysql驅動包
    url: jdbc:mysql://127.0.0.1:3306/cloudDB01         # 數據庫名稱
    username: root
    password: 123456
    dbcp2:
      min-idle: 5                                           # 數據庫連接池的最小維持連接數
      initial-size: 5                                       # 初始化連接數
      max-total: 5                                          # 最大連接數
      max-wait-millis: 200                                  # 等待連接獲取的最大超時時間

eureka:
  client: #客戶端註冊進eureka服務列表內
    service-url:
      defaultZone:  http://127.0.0.1:7001/eureka/,http://127.0.0.1:7002/eureka/,http://127.0.0.1:7003/eureka/
  instance:
    instance-id: microservicecloud-dept8001   #自定義服務名稱信息
    prefer-ip-address: true     #訪問路徑可以顯示IP地址

info:
  app.name: hph-microservicecloud
  company.name: www.hphblog.cn
  build.artifactId: ${project.artifactId}
  build.version: ${project.version}

啓動eureka,啓動服務提供者

在這裏插入圖片描述
服務已經註冊到eureka

統一的接口

package com.hph.springcloud.service;

import com.hph.springcloud.entities.Dept;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@FeignClient(value = "MICROSERVICECLOUD-DEPT")// value的值是服務提供者的名字
public interface DeptClientService {
    @RequestMapping(value = "/dept/get/{id}",method = RequestMethod.GET)
    public Dept get(@PathVariable("id") long id);

    @RequestMapping(value = "/dept/list",method = RequestMethod.GET)
    public List<Dept> list();

    @RequestMapping(value = "/dept/add",method = RequestMethod.POST)
    public boolean add(Dept dept);

}

在這裏插入圖片描述

啓動服務消費者

    <dependencies>
        <dependency><!-- 自己定義的api -->
            <groupId>com.hph.springcloud</groupId>
            <artifactId>microservicecloud-api</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
        <!-- Ribbon相關 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 修改後立即生效,熱部署 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>
</project>

在這裏插入圖片描述

實現遠程訪問和負載均衡

默認輪詢訪問服務提供者
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

github下載地址:
https://github.com/liucile/myRepository/tree/master/microservicecloud

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章