基於Eureka的springcloud註冊中心及服務提供者
阿新 • • 發佈:2020-09-07
一、註冊中心
1、在父工程中新建模組
2、配置pom,xml
<dependencies> <!--eureka server--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>View Code<!--引入自己定義的api通用包,可以使用payment支付Entity--> <dependency> <groupId>com.atguigu.springcloud</groupId> <artifactId>cloud-api-commons</artifactId> <version>${project.version}</version> </dependency><!--boot web actuator--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--圖形監控--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!--一般通用配置--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> </dependencies>
3、配置application.yml
server:
port: 7001
eureka:
instance:
hostname: localhost #eureka服務端的例項名稱
client:
#false表示不向註冊中心註冊自己
register-with-eureka: false
#false表示自己端就是註冊中心,我的職責就是維護服務例項,並不需要去檢索服務
fetch-registry: false
service-url:
#設定與Eureka Server互動的地址查詢服務和註冊服務都需要依賴這個地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
View Code
4、啟動類
@SpringBootApplication @EnableEurekaServer public class EurekaMain7001 { public static void main(String[] args){ SpringApplication.run(EurekaMain7001.class,args); } }
二、服務提供者
1、新建模組
2、配置pom.xml
<dependencies> <!--引入自己定義的api通用包,可以使用Payment支付Entity--> <dependency> <!--工程、名字、版本號--> <groupId>com.atguigu.springcloud</groupId> <artifactId>cloud-api-commons</artifactId> <version>${project.version}</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>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> <!--mysql-connector-java--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>View Code
3、配置application.yml
server: port: 8001 spring: application: name: cloud-payment-service datasource: # 當前資料來源操作型別 type: com.alibaba.druid.pool.DruidDataSource # mysql驅動類 driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8 username: root password: 123456 mybatis: mapper-locations: classpath*:mapper/*.xml type-aliases-package: com.atguigu.springcloud.entities #entity別名類所在包 # eureka配置 eureka: client: #表示是否將自己註冊進EurekaServer預設為true register-with-eureka: true #是否從EurekaServer抓取已有的註冊資訊,預設為true。單節點無所謂,叢集必須設定為true才能配合ribbon使用負載均衡 fetch-registry: true service-url: defaultZone: http://localhost:7001/eurekaView Code
4、啟動類
@SpringBootApplication @EnableEurekaClient public class PatmentMain8001 { public static void main(String[] args){ SpringApplication.run(PatmentMain8001.class,args); } }